Proyectos de Subversion Moodle

Rev

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