Merge branch 'MDL-65081-master' of git://github.com/junpataleta/moodle
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 26 Mar 2019 21:38:06 +0000 (22:38 +0100)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 26 Mar 2019 21:38:06 +0000 (22:38 +0100)
lib/outputcomponents.php
lib/templates/single_select.mustache
lib/templates/url_select.mustache
lib/tests/outputcomponents_test.php

index cac9e61..cb79c92 100644 (file)
@@ -1020,7 +1020,7 @@ class single_select implements renderable, templatable {
     var $formid = null;
 
     /**
-     * @var array List of attached actions
+     * @var help_icon The help icon for this element.
      */
     var $helpicon = null;
 
@@ -1111,7 +1111,19 @@ class single_select implements renderable, templatable {
         $data->title = $this->tooltip;
         $data->formid = !empty($this->formid) ? $this->formid : html_writer::random_id('single_select_f');
         $data->id = !empty($attributes['id']) ? $attributes['id'] : html_writer::random_id('single_select');
+
+        // Select element attributes.
+        // Unset attributes that are already predefined in the template.
         unset($attributes['id']);
+        unset($attributes['class']);
+        unset($attributes['name']);
+        unset($attributes['title']);
+        unset($attributes['disabled']);
+
+        // Map the attributes.
+        $data->attributes = array_map(function($key) use ($attributes) {
+            return ['name' => $key, 'value' => $attributes[$key]];
+        }, array_keys($attributes));
 
         // Form parameters.
         $params = $this->url->params();
@@ -1185,8 +1197,11 @@ class single_select implements renderable, templatable {
 
         // Label attributes.
         $data->labelattributes = [];
+        // Unset label attributes that are already in the template.
+        unset($this->labelattributes['for']);
+        // Map the label attributes.
         foreach ($this->labelattributes as $key => $value) {
-            $data->labelattributes = ['name' => $key, 'value' => $value];
+            $data->labelattributes[] = ['name' => $key, 'value' => $value];
         }
 
         // Help icon.
@@ -1260,7 +1275,7 @@ class url_select implements renderable, templatable {
     var $formid = null;
 
     /**
-     * @var array List of attached actions
+     * @var help_icon The help icon for this element.
      */
     var $helpicon = null;
 
@@ -1423,6 +1438,7 @@ class url_select implements renderable, templatable {
         unset($attributes['id']);
         unset($attributes['name']);
         unset($attributes['title']);
+        unset($attributes['disabled']);
 
         $data->showbutton = $this->showbutton;
 
@@ -1442,6 +1458,9 @@ class url_select implements renderable, templatable {
 
         // Label attributes.
         $data->labelattributes = [];
+        // Unset label attributes that are already in the template.
+        unset($this->labelattributes['for']);
+        // Map the label attributes.
         foreach ($this->labelattributes as $key => $value) {
             $data->labelattributes[] = ['name' => $key, 'value' => $value];
         }
@@ -1451,8 +1470,8 @@ class url_select implements renderable, templatable {
 
         // Finally all the remaining attributes.
         $data->attributes = [];
-        foreach ($this->attributes as $key => $value) {
-            $data->attributes = ['name' => $key, 'value' => $value];
+        foreach ($attributes as $key => $value) {
+            $data->attributes[] = ['name' => $key, 'value' => $value];
         }
 
         return $data;
index 3994c78..071d38c 100644 (file)
@@ -95,7 +95,8 @@
         {{#helpicon}}
             {{>core/help_icon}}
         {{/helpicon}}
-        <select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="{{name}}">
+        <select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="{{name}}"
+                {{#title}}title="{{.}}"{{/title}} {{#disabled}}disabled{{/disabled}}>
             {{#options}}
                 {{#optgroup}}
                     <optgroup label="{{name}}">
index 2015076..e7d2293 100644 (file)
@@ -32,7 +32,9 @@
             [
                 {"name": "Item 3", "isgroup": false, "value": "3"},
                 {"name": "Item 4", "isgroup": false, "value": "4"}
-            ]}]
+            ]}],
+        "disabled": false,
+        "title": "Some cool title"
     }
 }}
 <div class="{{classes}}">
@@ -46,7 +48,8 @@
         {{#helpicon}}
             {{>core/help_icon}}
         {{/helpicon}}
-        <select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="jump">
+        <select {{#attributes}}{{name}}="{{value}}" {{/attributes}} id="{{id}}" class="custom-select {{classes}}" name="jump"
+                {{#title}}title="{{.}}"{{/title}} {{#disabled}}disabled{{/disabled}}>
             {{#options}}
                 {{#isgroup}}
                     <optgroup label="{{name}}">
index 67e2c0c..705b04c 100644 (file)
@@ -465,4 +465,168 @@ EOF;
         $reason = 'An icon with alt text is not hidden from screenreaders.';
         $this->assertNotContains('aria-hidden="true"', $renderer->pix_icon('t/print', 'Print'), $reason);
     }
+
+    /**
+     * Test for checking the template context data for the single_select element.
+     */
+    public function test_single_select() {
+        global $PAGE;
+
+        $fakename = 'fakename';
+        $fakeclass = 'fakeclass';
+        $faketitle = 'faketitle';
+        $fakedisabled = true;
+        $fakefor = 'fakefor';
+
+        $someid = 'someid';
+        $realname = 'realname';
+        $realclass = 'realclass';
+        $realtitle = 'realtitle';
+        $realdisabled = false;
+        $reallabel = 'Some cool label';
+        $labelclass = 'somelabelclass';
+        $labelstyle = 'font-weight: bold';
+
+        $dataaction = 'actiondata';
+        $dataother = 'otherdata';
+
+        $attributes = [
+            'id' => $someid,
+            'class' => $fakeclass,
+            'title' => $faketitle,
+            'disabled' => $fakedisabled,
+            'name' => $fakename,
+            'data-action' => $dataaction,
+            'data-other' => $dataother,
+        ];
+        $labelattributes = [
+            'for' => $fakefor,
+            'class' => $labelclass,
+            'style' => $labelstyle
+        ];
+
+        $options = [ "Option A", "Option B", "Option C" ];
+        $nothing = ['' => 'choosedots'];
+
+        $url = new moodle_url('/');
+
+        $singleselect = new single_select($url, $realname, $options, null, $nothing, 'someformid');
+        $singleselect->class = $realclass;
+        $singleselect->tooltip = $realtitle;
+        $singleselect->disabled = $realdisabled;
+        $singleselect->attributes = $attributes;
+        $singleselect->label = $reallabel;
+        $singleselect->labelattributes = $labelattributes;
+
+        $renderer = $PAGE->get_renderer('core');
+        $data = $singleselect->export_for_template($renderer);
+
+        $this->assertEquals($realtitle, $data->title);
+        $this->assertEquals($singleselect->class, $data->classes);
+        $this->assertEquals($realname, $data->name);
+        $this->assertEquals($reallabel, $data->label);
+        $this->assertEquals($realdisabled, $data->disabled);
+        $this->assertEquals($someid, $data->id);
+
+        // Validate attributes array.
+        // The following should not be included: id, class, name, disabled.
+        $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
+        $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
+        $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
+        $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
+        // The rest should be fine.
+        $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
+        $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
+
+        // Validate label attributes.
+        // The for attribute should not be included.
+        $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
+        // The rest should be fine.
+        $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
+        $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
+    }
+
+    /**
+     * Test for checking the template context data for the url_select element.
+     */
+    public function test_url_select() {
+        global $PAGE;
+
+        $fakename = 'fakename';
+        $fakeclass = 'fakeclass';
+        $faketitle = 'faketitle';
+        $fakedisabled = true;
+        $fakefor = 'fakefor';
+
+        $someid = 'someid';
+        $realclass = 'realclass';
+        $realtitle = 'realtitle';
+        $realdisabled = false;
+        $reallabel = 'Some cool label';
+        $labelclass = 'somelabelclass';
+        $labelstyle = 'font-weight: bold';
+
+        $dataaction = 'actiondata';
+        $dataother = 'otherdata';
+
+        $attributes = [
+            'id' => $someid,
+            'class' => $fakeclass,
+            'title' => $faketitle,
+            'disabled' => $fakedisabled,
+            'name' => $fakename,
+            'data-action' => $dataaction,
+            'data-other' => $dataother,
+        ];
+        $labelattributes = [
+            'for' => $fakefor,
+            'class' => $labelclass,
+            'style' => $labelstyle
+        ];
+
+        $url1 = new moodle_url("/#a");
+        $url2 = new moodle_url("/#b");
+        $url3 = new moodle_url("/#c");
+
+        $urls = [
+            $url1->out() => 'A',
+            $url2->out() => 'B',
+            $url3->out() => 'C',
+        ];
+        $nothing = ['' => 'choosedots'];
+
+        $urlselect = new url_select($urls, null, $nothing, 'someformid');
+        $urlselect->class = $realclass;
+        $urlselect->tooltip = $realtitle;
+        $urlselect->disabled = $realdisabled;
+        $urlselect->attributes = $attributes;
+        $urlselect->label = $reallabel;
+        $urlselect->labelattributes = $labelattributes;
+
+        $renderer = $PAGE->get_renderer('core');
+        $data = $urlselect->export_for_template($renderer);
+
+        $this->assertEquals($realtitle, $data->title);
+        $this->assertEquals($urlselect->class, $data->classes);
+        $this->assertEquals($reallabel, $data->label);
+        $this->assertEquals($realdisabled, $data->disabled);
+        $this->assertEquals($someid, $data->id);
+
+        // Validate attributes array.
+        // The following should not be included: id, class, name, disabled.
+        $this->assertFalse(in_array(['name' => 'id', 'value' => $someid], $data->attributes));
+        $this->assertFalse(in_array(['name' => 'class', 'value' => $fakeclass], $data->attributes));
+        $this->assertFalse(in_array(['name' => 'name', 'value' => $fakeclass], $data->attributes));
+        $this->assertFalse(in_array(['name' => 'disabled', 'value' => $fakedisabled], $data->attributes));
+        // The rest should be fine.
+        $this->assertTrue(in_array(['name' => 'data-action', 'value' => $dataaction], $data->attributes));
+        $this->assertTrue(in_array(['name' => 'data-other', 'value' => $dataother], $data->attributes));
+
+        // Validate label attributes.
+        // The for attribute should not be included.
+        $this->assertFalse(in_array(['name' => 'for', 'value' => $someid], $data->labelattributes));
+        // The rest should be fine.
+        $this->assertTrue(in_array(['name' => 'class', 'value' => $labelclass], $data->labelattributes));
+        $this->assertTrue(in_array(['name' => 'style', 'value' => $labelstyle], $data->labelattributes));
+    }
 }