Proyectos de Subversion Moodle

Rev

Rev 1 | | Comparar con el anterior | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 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
namespace customfield_select;
18
 
19
use core_customfield_generator;
20
use core_customfield_test_instance_form;
11 efrain 21
use stdClass;
1 efrain 22
 
23
/**
24
 * Functional test for customfield_select
25
 *
26
 * @package    customfield_select
11 efrain 27
 * @covers     \customfield_select\data_controller
28
 * @covers     \customfield_select\field_controller
1 efrain 29
 * @copyright  2019 Marina Glancy
30
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
31
 */
11 efrain 32
final class plugin_test extends \advanced_testcase {
1 efrain 33
 
34
    /** @var stdClass[]  */
35
    private $courses = [];
36
    /** @var \core_customfield\category_controller */
37
    private $cfcat;
38
    /** @var \core_customfield\field_controller[] */
39
    private $cfields;
40
    /** @var \core_customfield\data_controller[] */
41
    private $cfdata;
42
 
43
    /**
44
     * Tests set up.
45
     */
46
    public function setUp(): void {
47
        $this->resetAfterTest();
48
 
49
        $this->cfcat = $this->get_generator()->create_category();
50
 
51
        $this->cfields[1] = $this->get_generator()->create_field(
52
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'select',
53
                'configdata' => ['options' => "a\nb\nc"]]);
54
        $this->cfields[2] = $this->get_generator()->create_field(
55
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'select',
56
                'configdata' => ['required' => 1, 'options' => "a\nb\nc"]]);
57
        $this->cfields[3] = $this->get_generator()->create_field(
58
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'select',
59
                'configdata' => ['defaultvalue' => 'b', 'options' => "a\nb\nc"]]);
60
 
61
        $this->courses[1] = $this->getDataGenerator()->create_course();
62
        $this->courses[2] = $this->getDataGenerator()->create_course();
63
        $this->courses[3] = $this->getDataGenerator()->create_course();
64
 
65
        $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1);
66
        $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1);
67
 
68
        $this->setUser($this->getDataGenerator()->create_user());
69
    }
70
 
71
    /**
72
     * Get generator
73
     * @return core_customfield_generator
74
     */
75
    protected function get_generator(): core_customfield_generator {
76
        return $this->getDataGenerator()->get_plugin_generator('core_customfield');
77
    }
78
 
79
    /**
80
     * Test for initialising field and data controllers
81
     */
11 efrain 82
    public function test_initialise(): void {
1 efrain 83
        $f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
84
        $this->assertTrue($f instanceof field_controller);
85
 
86
        $f = \core_customfield\field_controller::create(0, (object)['type' => 'select'], $this->cfcat);
87
        $this->assertTrue($f instanceof field_controller);
88
 
89
        $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
90
        $this->assertTrue($d instanceof data_controller);
91
 
92
        $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
93
        $this->assertTrue($d instanceof data_controller);
94
    }
95
 
96
    /**
97
     * Test for configuration form functions
98
     *
99
     * Create a configuration form and submit it with the same values as in the field
100
     */
11 efrain 101
    public function test_config_form(): void {
1 efrain 102
        $this->setAdminUser();
103
        $submitdata = (array)$this->cfields[1]->to_record();
104
        $submitdata['configdata'] = $this->cfields[1]->get('configdata');
105
 
106
        $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
107
        $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
108
            $submitdata, true);
109
        $form->set_data_for_dynamic_submission();
110
        $this->assertTrue($form->is_validated());
111
        $form->process_dynamic_submission();
112
    }
113
 
114
    /**
115
     * Test for instance form functions
116
     */
11 efrain 117
    public function test_instance_form(): void {
1 efrain 118
        global $CFG;
119
        require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
120
        $this->setAdminUser();
121
        $handler = $this->cfcat->get_handler();
122
 
123
        // First try to submit without required field.
124
        $submitdata = (array)$this->courses[1];
125
        core_customfield_test_instance_form::mock_submit($submitdata, []);
126
        $form = new core_customfield_test_instance_form('POST',
127
            ['handler' => $handler, 'instance' => $this->courses[1]]);
128
        $this->assertFalse($form->is_validated());
129
 
130
        // Now with required field.
131
        $submitdata['customfield_myfield2'] = 1;
132
        core_customfield_test_instance_form::mock_submit($submitdata, []);
133
        $form = new core_customfield_test_instance_form('POST',
134
            ['handler' => $handler, 'instance' => $this->courses[1]]);
135
        $this->assertTrue($form->is_validated());
136
 
137
        $data = $form->get_data();
138
        $this->assertNotEmpty($data->customfield_myfield1);
139
        $this->assertNotEmpty($data->customfield_myfield2);
140
        $handler->instance_form_save($data);
141
    }
142
 
143
    /**
144
     * Test for data_controller::get_value and export_value
145
     */
11 efrain 146
    public function test_get_export_value(): void {
1 efrain 147
        $this->assertEquals(1, $this->cfdata[1]->get_value());
148
        $this->assertEquals('a', $this->cfdata[1]->export_value());
149
 
150
        // Field without data but with a default value.
151
        $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]);
152
        $this->assertEquals(2, $d->get_value());
153
        $this->assertEquals('b', $d->export_value());
154
    }
155
 
156
    /**
11 efrain 157
     * Test getting field options, formatted
158
     */
159
    public function test_get_options(): void {
160
        filter_set_global_state('multilang', TEXTFILTER_ON);
161
        filter_set_applies_to_strings('multilang', true);
162
 
163
        $field = $this->get_generator()->create_field([
164
            'categoryid' => $this->cfcat->get('id'),
165
            'type' => 'select',
166
            'shortname' => 'myselect',
167
            'configdata' => [
168
                'options' => <<<EOF
169
                    <span lang="en" class="multilang">Beginner</span><span lang="es" class="multilang">Novato</span>
170
                    <span lang="en" class="multilang">Intermediate</span><span lang="es" class="multilang">Intermedio</span>
171
                    <span lang="en" class="multilang">Advanced</span><span lang="es" class="multilang">Avanzado</span>
172
                EOF,
173
            ],
174
        ]);
175
 
176
        $this->assertEquals([
177
            '',
178
            'Beginner',
179
            'Intermediate',
180
            'Advanced',
181
        ], $field->get_options());
182
    }
183
 
184
    /**
1 efrain 185
     * Data provider for {@see test_parse_value}
186
     *
187
     * @return array
188
     */
11 efrain 189
    public static function parse_value_provider(): array {
1 efrain 190
        return [
191
            ['Red', 1],
192
            ['Blue', 2],
193
            ['Green', 3],
194
            ['Mauve', 0],
195
        ];
196
    }
197
 
198
    /**
199
     * Test field parse_value method
200
     *
201
     * @param string $value
202
     * @param int $expected
203
     *
204
     * @dataProvider parse_value_provider
205
     */
11 efrain 206
    public function test_parse_value(string $value, int $expected): void {
1 efrain 207
        $field = $this->get_generator()->create_field([
208
            'categoryid' => $this->cfcat->get('id'),
209
            'type' => 'select',
210
            'shortname' => 'myselect',
211
            'configdata' => [
212
                'options' => "Red\nBlue\nGreen",
213
            ],
214
        ]);
215
 
216
        $this->assertSame($expected, $field->parse_value($value));
217
    }
218
 
219
    /**
220
     * Deleting fields and data
221
     */
11 efrain 222
    public function test_delete(): void {
1 efrain 223
        $this->cfcat->get_handler()->delete_all();
224
    }
225
}