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_checkbox;
18
 
19
use core_customfield_generator;
20
use core_customfield_test_instance_form;
21
 
22
/**
23
 * Functional test for customfield_checkbox
24
 *
25
 * @package    customfield_checkbox
26
 * @copyright  2019 Marina Glancy
27
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28
 */
1441 ariadna 29
final class plugin_test extends \advanced_testcase {
1 efrain 30
 
31
    /** @var \stdClass[]  */
32
    private $courses = [];
33
    /** @var \core_customfield\category_controller */
34
    private $cfcat;
35
    /** @var \core_customfield\field_controller[] */
36
    private $cfields;
37
    /** @var \core_customfield\data_controller[] */
38
    private $cfdata;
39
 
40
    /**
41
     * Tests set up.
42
     */
43
    public function setUp(): void {
1441 ariadna 44
        parent::setUp();
1 efrain 45
        $this->resetAfterTest();
46
 
47
        $this->cfcat = $this->get_generator()->create_category();
48
 
49
        $this->cfields[1] = $this->get_generator()->create_field(
50
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield1', 'type' => 'checkbox']);
51
        $this->cfields[2] = $this->get_generator()->create_field(
52
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'checkbox',
53
                'configdata' => ['required' => 1]]);
54
        $this->cfields[3] = $this->get_generator()->create_field(
55
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'checkbox',
56
                'configdata' => ['checkbydefault' => 1]]);
57
 
58
        $this->courses[1] = $this->getDataGenerator()->create_course();
59
        $this->courses[2] = $this->getDataGenerator()->create_course();
60
        $this->courses[3] = $this->getDataGenerator()->create_course();
61
 
62
        $this->cfdata[1] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[1]->id, 1);
63
        $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id, 1);
64
 
65
        $this->setUser($this->getDataGenerator()->create_user());
66
    }
67
 
68
    /**
69
     * Get generator
70
     * @return core_customfield_generator
71
     */
72
    protected function get_generator(): core_customfield_generator {
73
        return $this->getDataGenerator()->get_plugin_generator('core_customfield');
74
    }
75
 
76
    /**
77
     * Test for initialising field and data controllers
78
     */
11 efrain 79
    public function test_initialise(): void {
1 efrain 80
        $f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
81
        $this->assertTrue($f instanceof field_controller);
82
 
83
        $f = \core_customfield\field_controller::create(0, (object)['type' => 'checkbox'], $this->cfcat);
84
        $this->assertTrue($f instanceof field_controller);
85
 
86
        $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
87
        $this->assertTrue($d instanceof data_controller);
88
 
89
        $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
90
        $this->assertTrue($d instanceof data_controller);
91
    }
92
 
93
    /**
94
     * Test for configuration form functions
95
     *
96
     * Create a configuration form and submit it with the same values as in the field
97
     */
11 efrain 98
    public function test_config_form(): void {
1 efrain 99
        $this->setAdminUser();
100
        $submitdata = (array)$this->cfields[1]->to_record();
101
        $submitdata['configdata'] = $this->cfields[1]->get('configdata');
102
 
103
        $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
104
        $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
105
            $submitdata, true);
106
        $form->set_data_for_dynamic_submission();
107
        $this->assertTrue($form->is_validated());
108
        $form->process_dynamic_submission();
109
 
110
        // Try submitting with 'unique values' checked.
111
        $submitdata['configdata']['uniquevalues'] = 1;
112
 
113
        $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
114
        $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
115
            $submitdata, true);
116
        $form->set_data_for_dynamic_submission();
117
        $this->assertFalse($form->is_validated());
118
    }
119
 
120
    /**
121
     * Test for instance form functions
122
     */
11 efrain 123
    public function test_instance_form(): void {
1 efrain 124
        global $CFG;
125
        require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
126
        $this->setAdminUser();
127
        $handler = $this->cfcat->get_handler();
128
 
129
        // First try to submit without required field.
130
        $submitdata = (array)$this->courses[1];
131
        core_customfield_test_instance_form::mock_submit($submitdata, []);
132
        $form = new core_customfield_test_instance_form('POST',
133
            ['handler' => $handler, 'instance' => $this->courses[1]]);
134
        $this->assertFalse($form->is_validated());
135
 
136
        // Now with required field.
137
        $submitdata['customfield_myfield2'] = 1;
138
        core_customfield_test_instance_form::mock_submit($submitdata, []);
139
        $form = new core_customfield_test_instance_form('POST',
140
            ['handler' => $handler, 'instance' => $this->courses[1]]);
141
        $this->assertTrue($form->is_validated());
142
 
143
        $data = $form->get_data();
144
        $this->assertNotEmpty($data->customfield_myfield1);
145
        $this->assertNotEmpty($data->customfield_myfield2);
146
        $handler->instance_form_save($data);
147
    }
148
 
149
    /**
150
     * Test for data_controller::get_value and export_value
151
     */
11 efrain 152
    public function test_get_export_value(): void {
1 efrain 153
        $this->assertEquals(1, $this->cfdata[1]->get_value());
154
        $this->assertEquals('Yes', $this->cfdata[1]->export_value());
155
 
156
        // Field without data.
157
        $d = \core_customfield\data_controller::create(0, null, $this->cfields[2]);
158
        $this->assertEquals(0, $d->get_value());
159
        $this->assertEquals('No', $d->export_value());
160
 
161
        // Field without data that is checked by default.
162
        $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]);
163
        $this->assertEquals(1, $d->get_value());
164
        $this->assertEquals('Yes', $d->export_value());
165
    }
166
 
167
    /**
168
     * Deleting fields and data
169
     */
11 efrain 170
    public function test_delete(): void {
1 efrain 171
        $this->cfcat->get_handler()->delete_all();
172
    }
173
}