weekly release 5.0dev
[moodle.git] / lib / form / tests / select_test.php
blobc12df378fcee0fd441830a08f7a33dc4af32b91e
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.
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/>.
17 namespace core_form;
19 use MoodleQuickForm_select;
21 defined('MOODLE_INTERNAL') || die();
23 global $CFG;
24 require_once($CFG->libdir . '/form/select.php');
26 /**
27 * Unit tests for MoodleQuickForm_select
29 * @package core_form
30 * @category test
31 * @copyright 2024 the Open University
32 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
33 * @covers \MoodleQuickForm_select
35 final class select_test extends \advanced_testcase {
37 /**
38 * Testcase to check generated timestamp
40 public function test_multi_select_uses_sensible_default_size(): void {
41 global $OUTPUT;
43 // With fewer than 10 choices, default the size to that number (3 here).
44 $element = new MoodleQuickForm_select('testel', 'Label',
45 ['Choice 1', 'Choice 2', 'Choice 3'], ['id' => 'testel_id', 'multiple' => true]);
47 $html = $OUTPUT->mform_element($element, false, false, '', false);
48 $this->assertStringContainsString(' size="3"', $html);
49 $this->assertEquals(3, $element->_attributes['size']);
51 // With more than 10 choices, set to size 10.
52 $element = new MoodleQuickForm_select('testel', 'Label', [
53 'Choice 1', 'Choice 2', 'Choice 3',
54 'Choice 4', 'Choice 5', 'Choice 6',
55 'Choice 7', 'Choice 8', 'Choice 9',
56 'Choice 10', 'Choice 11', 'Choice 12',
57 ], ['id' => 'testel_id', 'multiple' => true]);
59 $html = $OUTPUT->mform_element($element, false, false, '', false);
60 $this->assertStringContainsString(' size="10"', $html);
61 $this->assertEquals(10, $element->_attributes['size']);
63 // If a size is already set, don't change in.
64 $element = new MoodleQuickForm_select('testel', 'Label',
65 ['Choice 1', 'Choice 2', 'Choice 3'], ['id' => 'testel_id', 'multiple' => true, 'size' => 7]);
67 $html = $OUTPUT->mform_element($element, false, false, '', false);
68 $this->assertStringContainsString(' size="7"', $html);
69 $this->assertEquals(7, $element->_attributes['size']);
71 // Don't set a size for single selects.
72 $element = new MoodleQuickForm_select('testel', 'Label',
73 ['Choice 1', 'Choice 2', 'Choice 3'], ['id' => 'testel_id']);
75 $html = $OUTPUT->mform_element($element, false, false, '', false);
76 $this->assertStringNotContainsString('size', $html);
77 $this->assertArrayNotHasKey('size', $element->_attributes);