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_text;
18
 
19
use core_customfield_generator;
20
use core_customfield_test_instance_form;
21
 
22
/**
23
 * Functional test for customfield_text
24
 *
25
 * @package    customfield_text
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' => 'text',
51
                'configdata' => ['maxlength' => 30, 'displaysize' => 50], 'description' => null]);
52
        $this->cfields[2] = $this->get_generator()->create_field(
53
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield2', 'type' => 'text',
54
                'configdata' => ['required' => 1, 'maxlength' => 30, 'displaysize' => 50]]);
55
        $this->cfields[3] = $this->get_generator()->create_field(
56
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield3', 'type' => 'text',
57
                'configdata' => ['defaultvalue' => 'Defvalue', 'maxlength' => 30, 'displaysize' => 50]]);
58
        $this->cfields[4] = $this->get_generator()->create_field(
59
            ['categoryid' => $this->cfcat->get('id'), 'shortname' => 'myfield4', 'type' => 'text',
60
                'configdata' => ['link' => 'https://twitter.com/$$', 'maxlength' => 30, 'displaysize' => 50]]);
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,
67
            'Value1');
68
        $this->cfdata[2] = $this->get_generator()->add_instance_data($this->cfields[1], $this->courses[2]->id,
69
            'Value2');
70
 
71
        $this->setUser($this->getDataGenerator()->create_user());
72
    }
73
 
74
    /**
75
     * Get generator
76
     * @return core_customfield_generator
77
     */
78
    protected function get_generator(): core_customfield_generator {
79
        return $this->getDataGenerator()->get_plugin_generator('core_customfield');
80
    }
81
 
82
    /**
83
     * Test for initialising field and data controllers
84
     */
11 efrain 85
    public function test_initialise(): void {
1 efrain 86
        $f = \core_customfield\field_controller::create($this->cfields[1]->get('id'));
87
        $this->assertTrue($f instanceof field_controller);
88
 
89
        $f = \core_customfield\field_controller::create(0, (object)['type' => 'text'], $this->cfcat);
90
        $this->assertTrue($f instanceof field_controller);
91
 
92
        $d = \core_customfield\data_controller::create($this->cfdata[1]->get('id'));
93
        $this->assertTrue($d instanceof data_controller);
94
 
95
        $d = \core_customfield\data_controller::create(0, null, $this->cfields[1]);
96
        $this->assertTrue($d instanceof data_controller);
97
    }
98
 
99
    /**
100
     * Test for configuration form functions
101
     *
102
     * Create a configuration form and submit it with the same values as in the field
103
     */
11 efrain 104
    public function test_config_form(): void {
1 efrain 105
        $this->setAdminUser();
106
        $submitdata = (array)$this->cfields[1]->to_record();
107
        $submitdata['configdata'] = $this->cfields[1]->get('configdata');
108
 
109
        $submitdata = \core_customfield\field_config_form::mock_ajax_submit($submitdata);
110
        $form = new \core_customfield\field_config_form(null, null, 'post', '', null, true,
111
            $submitdata, true);
112
        $form->set_data_for_dynamic_submission();
113
        $this->assertTrue($form->is_validated());
114
        $form->process_dynamic_submission();
115
    }
116
 
117
    /**
118
     * Test for instance form functions
119
     */
11 efrain 120
    public function test_instance_form(): void {
1 efrain 121
        global $CFG;
122
        require_once($CFG->dirroot . '/customfield/tests/fixtures/test_instance_form.php');
123
        $this->setAdminUser();
124
        $handler = $this->cfcat->get_handler();
125
 
126
        // First try to submit without required field.
127
        $submitdata = (array)$this->courses[1];
128
        core_customfield_test_instance_form::mock_submit($submitdata, []);
129
        $form = new core_customfield_test_instance_form('POST',
130
            ['handler' => $handler, 'instance' => $this->courses[1]]);
131
        $this->assertFalse($form->is_validated());
132
 
133
        // Now with required field.
134
        $submitdata['customfield_myfield2'] = 'Some text';
135
        core_customfield_test_instance_form::mock_submit($submitdata, []);
136
        $form = new core_customfield_test_instance_form('POST',
137
            ['handler' => $handler, 'instance' => $this->courses[1]]);
138
        $this->assertTrue($form->is_validated());
139
 
140
        $data = $form->get_data();
141
        $this->assertNotEmpty($data->customfield_myfield1);
142
        $this->assertNotEmpty($data->customfield_myfield2);
143
        $handler->instance_form_save($data);
144
    }
145
 
146
    /**
147
     * Test for data_controller::get_value and export_value
148
     */
11 efrain 149
    public function test_get_export_value(): void {
1 efrain 150
        $this->assertEquals('Value1', $this->cfdata[1]->get_value());
151
        $this->assertEquals('Value1', $this->cfdata[1]->export_value());
152
 
153
        // Field without data but with a default value.
154
        $d = \core_customfield\data_controller::create(0, null, $this->cfields[3]);
155
        $this->assertEquals('Defvalue', $d->get_value());
156
        $this->assertEquals('Defvalue', $d->export_value());
157
 
158
        // Field with a link.
159
        $d = $this->get_generator()->add_instance_data($this->cfields[4], $this->courses[1]->id, 'mynickname');
160
        $this->assertEquals('mynickname', $d->get_value());
161
        $this->assertEquals('<a href="https://twitter.com/mynickname">mynickname</a>', $d->export_value());
162
    }
163
 
164
    /**
165
     * Deleting fields and data
166
     */
11 efrain 167
    public function test_delete(): void {
1 efrain 168
        $this->cfcat->get_handler()->delete_all();
169
    }
170
}