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
/**
18
 * Class provider_test
19
 *
20
 * @package     core_customfield
21
 * @copyright   2019 Marina Glancy
22
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
23
 */
24
namespace core_customfield\privacy;
25
 
26
defined('MOODLE_INTERNAL') || die();
27
 
28
use core_privacy\tests\provider_testcase;
29
use core_privacy\local\request\approved_contextlist;
30
use core_privacy\local\request\writer;
31
use core_customfield\privacy\provider;
32
 
33
/**
34
 * Class provider_test
35
 *
36
 * @package     core_customfield
37
 * @copyright   2019 Marina Glancy
38
 * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
1441 ariadna 40
final class provider_test extends provider_testcase {
1 efrain 41
 
42
    /**
43
     * Generate data.
44
     *
45
     * @return array
46
     */
47
    protected function generate_test_data(): array {
48
        $this->resetAfterTest();
49
 
50
        $generator = $this->getDataGenerator()->get_plugin_generator('core_customfield');
51
        $cfcats[1] = $generator->create_category();
52
        $cfcats[2] = $generator->create_category();
53
        $cffields[11] = $generator->create_field(
54
            ['categoryid' => $cfcats[1]->get('id'), 'type' => 'checkbox']);
55
        $cffields[12] = $generator->create_field(
56
            ['categoryid' => $cfcats[1]->get('id'), 'type' => 'date']);
57
        $cffields[13] = $generator->create_field(
58
            ['categoryid' => $cfcats[1]->get('id'),
59
            'type' => 'select', 'configdata' => ['options' => "a\nb\nc"]]);
60
        $cffields[14] = $generator->create_field(
61
            ['categoryid' => $cfcats[1]->get('id'), 'type' => 'text']);
62
        $cffields[15] = $generator->create_field(
63
            ['categoryid' => $cfcats[1]->get('id'), 'type' => 'textarea']);
64
        $cffields[21] = $generator->create_field(
65
            ['categoryid' => $cfcats[2]->get('id')]);
66
        $cffields[22] = $generator->create_field(
67
            ['categoryid' => $cfcats[2]->get('id')]);
68
 
69
        $courses[1] = $this->getDataGenerator()->create_course();
70
        $courses[2] = $this->getDataGenerator()->create_course();
71
        $courses[3] = $this->getDataGenerator()->create_course();
72
 
73
        $generator->add_instance_data($cffields[11], $courses[1]->id, 1);
74
        $generator->add_instance_data($cffields[12], $courses[1]->id, 1546300800);
75
        $generator->add_instance_data($cffields[13], $courses[1]->id, 2);
76
        $generator->add_instance_data($cffields[14], $courses[1]->id, 'Hello1');
77
        $generator->add_instance_data($cffields[15], $courses[1]->id,
78
            ['text' => '<p>Hi there</p>', 'format' => FORMAT_HTML]);
79
 
80
        $generator->add_instance_data($cffields[21], $courses[1]->id, 'hihi1');
81
 
82
        $generator->add_instance_data($cffields[14], $courses[2]->id, 'Hello2');
83
 
84
        $generator->add_instance_data($cffields[21], $courses[2]->id, 'hihi2');
85
 
86
        $user = $this->getDataGenerator()->create_user();
87
        $this->setUser($user);
88
 
89
        return [
90
            'user' => $user,
91
            'cfcats' => $cfcats,
92
            'cffields' => $cffields,
93
            'courses' => $courses,
94
        ];
95
    }
96
 
97
    /**
98
     * Test for provider::get_metadata()
99
     */
11 efrain 100
    public function test_get_metadata(): void {
1 efrain 101
        $collection = new \core_privacy\local\metadata\collection('core_customfield');
102
        $collection = provider::get_metadata($collection);
103
        $this->assertNotEmpty($collection);
104
    }
105
 
106
    /**
107
     * Test for provider::get_customfields_data_contexts
108
     */
11 efrain 109
    public function test_get_customfields_data_contexts(): void {
1 efrain 110
        global $DB;
111
        [
112
            'cffields' => $cffields,
113
            'cfcats' => $cfcats,
114
            'courses' => $courses,
115
        ] = $this->generate_test_data();
116
 
117
        list($sql, $params) = $DB->get_in_or_equal([$courses[1]->id, $courses[2]->id], SQL_PARAMS_NAMED);
118
        $r = provider::get_customfields_data_contexts('core_course', 'course', '=0',
119
            $sql, $params);
1441 ariadna 120
        $this->assertEqualsCanonicalizing(
121
            [
122
                \context_course::instance($courses[1]->id)->id,
123
                \context_course::instance($courses[2]->id)->id,
124
            ],
125
            array_values($r->get_contextids()),
126
        );
1 efrain 127
    }
128
 
129
    /**
130
     * Test for provider::get_customfields_configuration_contexts()
131
     */
11 efrain 132
    public function test_get_customfields_configuration_contexts(): void {
1 efrain 133
        $this->generate_test_data();
134
 
135
        $r = provider::get_customfields_configuration_contexts('core_course', 'course');
136
        $this->assertEquals([\context_system::instance()->id], $r->get_contextids());
137
    }
138
 
139
    /**
140
     * Test for provider::export_customfields_data()
141
     */
11 efrain 142
    public function test_export_customfields_data(): void {
1 efrain 143
        global $USER, $DB;
144
        $this->resetAfterTest();
145
        [
146
            'cffields' => $cffields,
147
            'cfcats' => $cfcats,
148
            'courses' => $courses,
149
        ] = $this->generate_test_data();
150
 
151
        // Hack one of the fields so it has an invalid field type.
152
        $invalidfieldid = $cffields[21]->get('id');
153
        $DB->update_record('customfield_field', ['id' => $invalidfieldid, 'type' => 'invalid']);
154
 
155
        $context = \context_course::instance($courses[1]->id);
156
        $contextlist = new approved_contextlist($USER, 'core_customfield', [$context->id]);
157
        provider::export_customfields_data($contextlist, 'core_course', 'course', '=0', '=:i', ['i' => $courses[1]->id]);
158
        /** @var core_privacy\tests\request\content_writer $writer */
159
        $writer = writer::with_context($context);
160
 
161
        // Make sure that all and only data for the course1 was exported.
162
        // There is no way to fetch all data from writer as array so we need to fetch one-by-one for each data id.
163
        $invaldfieldischecked = false;
164
        foreach ($DB->get_records('customfield_data', []) as $dbrecord) {
165
            $data = $writer->get_data(['Custom fields data', $dbrecord->id]);
166
            if ($dbrecord->instanceid == $courses[1]->id) {
167
                $this->assertEquals($dbrecord->fieldid, $data->fieldid);
168
                $this->assertNotEmpty($data->fieldtype);
169
                $this->assertNotEmpty($data->fieldshortname);
170
                $this->assertNotEmpty($data->fieldname);
171
                $invaldfieldischecked = $invaldfieldischecked ?: ($data->fieldid == $invalidfieldid);
172
            } else {
173
                $this->assertEmpty($data);
174
            }
175
        }
176
 
177
        // Make sure field with was checked in this test.
178
        $this->assertTrue($invaldfieldischecked);
179
    }
180
 
181
    /**
182
     * Test for provider::delete_customfields_data()
183
     */
11 efrain 184
    public function test_delete_customfields_data(): void {
1 efrain 185
        global $USER, $DB;
186
        $this->resetAfterTest();
187
        [
188
            'cffields' => $cffields,
189
            'cfcats' => $cfcats,
190
            'courses' => $courses,
191
        ] = $this->generate_test_data();
192
 
193
        $approvedcontexts = new approved_contextlist($USER, 'core_course', [\context_course::instance($courses[1]->id)->id]);
194
        provider::delete_customfields_data($approvedcontexts, 'core_course', 'course');
195
        $this->assertEmpty($DB->get_records('customfield_data', ['instanceid' => $courses[1]->id]));
196
        $this->assertNotEmpty($DB->get_records('customfield_data', ['instanceid' => $courses[2]->id]));
197
    }
198
 
199
    /**
200
     * Test for provider::delete_customfields_configuration()
201
     */
11 efrain 202
    public function test_delete_customfields_configuration(): void {
1 efrain 203
        global $USER, $DB;
204
        $this->resetAfterTest();
205
        [
206
            'cffields' => $cffields,
207
            'cfcats' => $cfcats,
208
            'courses' => $courses,
209
        ] = $this->generate_test_data();
210
 
211
        // Remember the list of fields in the category 2 before we delete it.
212
        $catid1 = $cfcats[1]->get('id');
213
        $catid2 = $cfcats[2]->get('id');
214
        $fids2 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid2]);
215
        $this->assertNotEmpty($fids2);
216
        list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
217
        $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
218
 
219
        // A little hack here, modify customfields configuration so they have different itemids.
220
        $DB->update_record('customfield_category', ['id' => $catid2, 'itemid' => 1]);
221
        $contextlist = new approved_contextlist($USER, 'core_course', [\context_system::instance()->id]);
222
        provider::delete_customfields_configuration($contextlist, 'core_course', 'course', '=:i', ['i' => 1]);
223
 
224
        // Make sure everything for category $catid2 is gone but present for $catid1.
225
        $this->assertEmpty($DB->get_records('customfield_category', ['id' => $catid2]));
226
        $this->assertEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql, $fparams));
227
        $this->assertEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
228
 
229
        $this->assertNotEmpty($DB->get_records('customfield_category', ['id' => $catid1]));
230
        $fids1 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid1]);
231
        list($fsql1, $fparams1) = $DB->get_in_or_equal($fids1, SQL_PARAMS_NAMED);
232
        $this->assertNotEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql1, $fparams1));
233
        $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql1, $fparams1));
234
    }
235
 
236
    /**
237
     * Test for provider::delete_customfields_configuration_for_context()
238
     */
11 efrain 239
    public function test_delete_customfields_configuration_for_context(): void {
1 efrain 240
        global $USER, $DB;
241
        $this->resetAfterTest();
242
        [
243
            'cffields' => $cffields,
244
            'cfcats' => $cfcats,
245
            'courses' => $courses,
246
        ] = $this->generate_test_data();
247
 
248
        // Remember the list of fields in the category 2 before we delete it.
249
        $catid1 = $cfcats[1]->get('id');
250
        $catid2 = $cfcats[2]->get('id');
251
        $fids2 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid2]);
252
        $this->assertNotEmpty($fids2);
253
        list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
254
        $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
255
 
256
        // A little hack here, modify customfields configuration so they have different contexts.
257
        $context = \context_user::instance($USER->id);
258
        $DB->update_record('customfield_category', ['id' => $catid2, 'contextid' => $context->id]);
259
        provider::delete_customfields_configuration_for_context('core_course', 'course', $context);
260
 
261
        // Make sure everything for category $catid2 is gone but present for $catid1.
262
        $this->assertEmpty($DB->get_records('customfield_category', ['id' => $catid2]));
263
        $this->assertEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql, $fparams));
264
        $this->assertEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql, $fparams));
265
 
266
        $this->assertNotEmpty($DB->get_records('customfield_category', ['id' => $catid1]));
267
        $fids1 = $DB->get_fieldset_select('customfield_field', 'id', 'categoryid=?', [$catid1]);
268
        list($fsql1, $fparams1) = $DB->get_in_or_equal($fids1, SQL_PARAMS_NAMED);
269
        $this->assertNotEmpty($DB->get_records_select('customfield_field', 'id ' . $fsql1, $fparams1));
270
        $this->assertNotEmpty($DB->get_records_select('customfield_data', 'fieldid ' . $fsql1, $fparams1));
271
    }
272
 
273
    /**
274
     * Test for provider::delete_customfields_data_for_context()
275
     */
11 efrain 276
    public function test_delete_customfields_data_for_context(): void {
1 efrain 277
        global $DB;
278
        $this->resetAfterTest();
279
        [
280
            'cffields' => $cffields,
281
            'cfcats' => $cfcats,
282
            'courses' => $courses,
283
        ] = $this->generate_test_data();
284
 
285
        provider::delete_customfields_data_for_context('core_course', 'course',
286
            \context_course::instance($courses[1]->id));
287
        $fids2 = $DB->get_fieldset_select('customfield_field', 'id', '1=1', []);
288
        list($fsql, $fparams) = $DB->get_in_or_equal($fids2, SQL_PARAMS_NAMED);
289
        $fparams['course1'] = $courses[1]->id;
290
        $fparams['course2'] = $courses[2]->id;
291
        $this->assertEmpty($DB->get_records_select('customfield_data', 'instanceid = :course1 AND fieldid ' . $fsql, $fparams));
292
        $this->assertNotEmpty($DB->get_records_select('customfield_data', 'instanceid = :course2 AND fieldid ' . $fsql, $fparams));
293
    }
294
}