1 <?php 2 // This file is part of Moodle - http://moodle.org/ 3 // 4 // Moodle is free software: you can redistribute it and/or modify 5 // it under the terms of the GNU General Public License as published by 6 // the Free Software Foundation, either version 3 of the License, or 7 // (at your option) any later version. 8 // 9 // Moodle is distributed in the hope that it will be useful, 10 // but WITHOUT ANY WARRANTY; without even the implied warranty of 11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12 // GNU General Public License for more details. 13 // 14 // You should have received a copy of the GNU General Public License 15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>. 16 17 /** 18 * Unit tests for core\output\choice class. 19 * 20 * @package core 21 * @category test 22 * @copyright 2023 Ferran Recio <ferran@moodle.com> 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 namespace core\output; 27 28 use advanced_testcase; 29 30 /** 31 * Unit tests for the `icon_system` class. 32 * 33 * @coversDefaultClass \core\output\choicelist 34 */ 35 class choicelist_test extends advanced_testcase { 36 /** 37 * Test for a choice without options. 38 * 39 * @covers ::_construct 40 * @covers ::add_option 41 * @covers ::export_for_template 42 */ 43 public function test_empty_export(): void { 44 $page = new \moodle_page(); 45 $page->set_url('/user/profile.php'); 46 $page->set_context(\context_system::instance()); 47 $renderer = $page->get_renderer('core'); 48 49 $choice = new choicelist('Choose an option'); 50 $export = $choice->export_for_template($renderer); 51 52 $this->assertEquals('Choose an option', $export['description']); 53 $this->assertEquals(false, $export['hasoptions']); 54 $this->assertEquals([], $export['options']); 55 } 56 57 /** 58 * Test for a choice with basic options. 59 * 60 * @covers ::_construct 61 * @covers ::add_option 62 * @covers ::export_for_template 63 */ 64 public function test_basic_export(): void { 65 $page = new \moodle_page(); 66 $page->set_url('/user/profile.php'); 67 $page->set_context(\context_system::instance()); 68 $renderer = $page->get_renderer('core'); 69 70 $choice = new choicelist('Choose an option'); 71 $choice->add_option('option1', 'Option 1'); 72 $choice->add_option('option2', 'Option 2'); 73 74 $export = $choice->export_for_template($renderer); 75 76 $this->assertEquals('Choose an option', $export['description']); 77 $this->assertEquals(true, $export['hasoptions']); 78 $this->assertCount(2, $export['options']); 79 $this->validate_option($export['options'][0], 'option1', 'Option 1', []); 80 $this->validate_option($export['options'][1], 'option2', 'Option 2', []); 81 } 82 /** 83 * Test for a choice with extras options definition. 84 * 85 * @covers ::_construct 86 * @covers ::add_option 87 * @covers ::set_option_extras 88 * @covers ::export_for_template 89 */ 90 public function test_option_defintion_export(): void { 91 $page = new \moodle_page(); 92 $page->set_url('/user/profile.php'); 93 $page->set_context(\context_system::instance()); 94 $renderer = $page->get_renderer('core'); 95 96 $choice = new choicelist('Choose an option'); 97 $definition1 = [ 98 'disabled' => true, 99 'description' => 'Description', 100 'url' => new \moodle_url('/user/profile.php'), 101 'icon' => new \pix_icon('i/grade', 'Grade'), 102 'extras' => [ 103 'data-attribute' => 'value', 104 ], 105 ]; 106 $choice->add_option('option1', 'Option 1', $definition1); 107 $definition2 = [ 108 'disabled' => false, 109 'description' => null, 110 'url' => null, 111 'icon' => null, 112 'extras' => null, 113 ]; 114 $choice->add_option('option2', 'Option 2', $definition2); 115 116 $export = $choice->export_for_template($renderer); 117 118 $this->assertEquals('Choose an option', $export['description']); 119 $this->assertEquals(true, $export['hasoptions']); 120 $this->assertCount(2, $export['options']); 121 $definition1['iconexport'] = $definition1['icon']->export_for_pix($renderer); 122 $this->validate_option($export['options'][0], 'option1', 'Option 1', $definition1); 123 $this->validate_option($export['options'][1], 'option2', 'Option 2', $definition2); 124 } 125 126 /** 127 * Test for a choice with option selected. 128 * 129 * @covers ::_construct 130 * @covers ::add_option 131 * @covers ::set_selected_value 132 * @covers ::get_selected_value 133 * @covers ::export_for_template 134 */ 135 public function test_option_selected_export(): void { 136 $page = new \moodle_page(); 137 $page->set_url('/user/profile.php'); 138 $page->set_context(\context_system::instance()); 139 $renderer = $page->get_renderer('core'); 140 141 $choice = new choicelist('Choose an option'); 142 $choice->add_option('option1', 'Option 1'); 143 $choice->add_option('option2', 'Option 2'); 144 $choice->set_selected_value('option1'); 145 146 $export = $choice->export_for_template($renderer); 147 148 $this->assertEquals('Choose an option', $export['description']); 149 $this->assertEquals(true, $export['hasoptions']); 150 $this->assertCount(2, $export['options']); 151 $this->assertEquals('option1', $choice->get_selected_value()); 152 $this->validate_option($export['options'][0], 'option1', 'Option 1', [], true); 153 $this->validate_option($export['options'][1], 'option2', 'Option 2', []); 154 } 155 156 /** 157 * Validate a choice option export. 158 * @param array $option the option export 159 * @param string $value the option value 160 * @param string $name the option name 161 * @param array|null $definition the option definition 162 * @param bool $selected if the option is selected 163 */ 164 private function validate_option( 165 array $option, 166 string $value, 167 string $name, 168 ?array $definition, 169 bool $selected = false 170 ): void { 171 $this->assertEquals($value, $option['value']); 172 $this->assertEquals($name, $option['name']); 173 $this->assertEquals($definition['disabled'] ?? false, $option['disabled']); 174 $this->assertEquals($definition['description'] ?? null, $option['description']); 175 if (isset($definition['url'])) { 176 $this->assertEquals($definition['url']->out(true), $option['url']); 177 $this->assertTrue($option['hasurl']); 178 } 179 if (isset($definition['icon'])) { 180 $this->assertEquals($definition['iconexport'], $option['icon']); 181 $this->assertTrue($option['hasicon']); 182 } 183 if ($selected) { 184 $this->assertTrue($option['selected']); 185 } 186 if (isset($definition['extras'])) { 187 foreach ($option['extras'] as $extra) { 188 $attribute = $extra['attribute']; 189 $this->assertEquals($definition['extras'][$attribute], $extra['value']); 190 } 191 } else { 192 $this->assertFalse(isset($option['extras'])); 193 } 194 } 195 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body