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
/**
18
 * Privacy tests for gradingform_guide.
19
 *
20
 * @package    gradingform_guide
21
 * @category   test
22
 * @copyright  2018 Sara Arjona <sara@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace gradingform_guide\privacy;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
global $CFG;
30
 
31
use core_privacy\tests\provider_testcase;
32
use core_privacy\local\request\writer;
33
use gradingform_guide\privacy\provider;
34
 
35
/**
36
 * Privacy tests for gradingform_guide.
37
 *
38
 * @package    gradingform_guide
39
 * @copyright  2018 Sara Arjona <sara@moodle.com>
40
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41
 */
42
class provider_test extends provider_testcase {
43
 
44
    /**
45
     * Ensure that export_user_preferences returns no data if the user has no data.
46
     */
11 efrain 47
    public function test_export_user_preferences_not_defined(): void {
1 efrain 48
        $user = \core_user::get_user_by_username('admin');
49
        provider::export_user_preferences($user->id);
50
 
51
        $writer = writer::with_context(\context_system::instance());
52
        $this->assertFalse($writer->has_any_data());
53
    }
54
 
55
    /**
56
     * Ensure that export_user_preferences returns single preferences.
57
     */
11 efrain 58
    public function test_export_user_preferences(): void {
1 efrain 59
        $this->resetAfterTest();
60
 
61
        // Define a user preference.
62
        $user = $this->getDataGenerator()->create_user();
63
        $this->setUser($user);
64
        set_user_preference('gradingform_guide-showmarkerdesc', 0, $user);
65
        set_user_preference('gradingform_guide-showstudentdesc', 1, $user);
66
 
67
        // Validate exported data.
68
        provider::export_user_preferences($user->id);
69
        $context = \context_user::instance($user->id);
70
        /** @var \core_privacy\tests\request\content_writer $writer */
71
        $writer = writer::with_context($context);
72
        $this->assertTrue($writer->has_any_data());
73
        $prefs = $writer->get_user_preferences('gradingform_guide');
74
        $this->assertCount(2, (array) $prefs);
75
        $this->assertEquals(
76
            get_string('privacy:metadata:preference:showstudentdesc', 'gradingform_guide'),
77
            $prefs->{'gradingform_guide-showstudentdesc'}->description
78
        );
79
        $this->assertEquals(get_string('no'), $prefs->{'gradingform_guide-showmarkerdesc'}->value);
80
        $this->assertEquals(get_string('yes'), $prefs->{'gradingform_guide-showstudentdesc'}->value);
81
    }
82
 
83
    /**
84
     * Test the export of guide data.
85
     */
11 efrain 86
    public function test_get_gradingform_export_data(): void {
1 efrain 87
        global $DB;
88
        $this->resetAfterTest();
89
        $course = $this->getDataGenerator()->create_course();
90
        $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
91
        $user = $this->getDataGenerator()->create_user();
92
 
93
        $this->setUser($user);
94
 
95
        $modulecontext = \context_module::instance($module->cmid);
96
        $controller = $this->get_test_guide($modulecontext);
97
 
98
        // In the situation of mod_assign this would be the id from assign_grades.
99
        $itemid = 1;
100
        $instance = $controller->create_instance($user->id, $itemid);
101
        $data = $this->get_test_form_data(
102
            $controller,
103
            $itemid,
104
            5, 'This user made several mistakes.',
105
            10, 'This user has two pictures.'
106
        );
107
 
108
        $instance->update($data);
109
        $instanceid = $instance->get_data('id');
110
 
111
        // Let's try the method we are testing.
112
        provider::export_gradingform_instance_data($modulecontext, $instance->get_id(), ['Test']);
113
        $data = (array) writer::with_context($modulecontext)->get_data(['Test', 'Marking guide', $instanceid]);
114
        $this->assertCount(2, $data);
115
        $this->assertEquals('Spelling mistakes', $data['Spelling mistakes']->shortname);
116
        $this->assertEquals('This user made several mistakes.', $data['Spelling mistakes']->remark);
117
        $this->assertEquals('Pictures', $data['Pictures']->shortname);
118
        $this->assertEquals('This user has two pictures.', $data['Pictures']->remark);
119
    }
120
 
121
    /**
122
     * Test the deletion of guide user information via the instance ID.
123
     */
11 efrain 124
    public function test_delete_gradingform_for_instances(): void {
1 efrain 125
        global $DB;
126
        $this->resetAfterTest();
127
        $course = $this->getDataGenerator()->create_course();
128
        $module = $this->getDataGenerator()->create_module('assign', ['course' => $course]);
129
        $user = $this->getDataGenerator()->create_user();
130
 
131
        $this->setUser($user);
132
 
133
        $modulecontext = \context_module::instance($module->cmid);
134
        $controller = $this->get_test_guide($modulecontext);
135
 
136
        // In the situation of mod_assign this would be the id from assign_grades.
137
        $itemid = 1;
138
        $instance = $controller->create_instance($user->id, $itemid);
139
        $data = $this->get_test_form_data(
140
            $controller,
141
            $itemid,
142
            5, 'This user made several mistakes.',
143
            10, 'This user has two pictures.'
144
        );
145
 
146
        $instance->update($data);
147
        $instanceid = $instance->get_data('id');
148
 
149
        $itemid = 2;
150
        $instance = $controller->create_instance($user->id, $itemid);
151
        $data = $this->get_test_form_data(
152
            $controller,
153
            $itemid,
154
            25, 'This user made no mistakes.',
155
            5, 'This user has one pictures.'
156
        );
157
 
158
        $instance->update($data);
159
        $instanceid = $instance->get_data('id');
160
 
161
        // Check how many records we have in the fillings table.
162
        $records = $DB->get_records('gradingform_guide_fillings');
163
        $this->assertCount(4, $records);
164
        // Let's delete one of the instances (the last one would be the easiest).
165
        provider::delete_gradingform_for_instances([$instance->get_id()]);
166
        $records = $DB->get_records('gradingform_guide_fillings');
167
        $this->assertCount(2, $records);
168
        foreach ($records as $record) {
169
            $this->assertNotEquals($instance->get_id(), $record->instanceid);
170
        }
171
    }
172
 
173
    /**
174
     * Generate a guide controller with sample data required for testing of this class.
175
     *
176
     * @param \context_module $context
177
     * @return \gradingform_guide_controller
178
     */
179
    protected function get_test_guide(\context_module $context): \gradingform_guide_controller {
180
        $generator = \testing_util::get_data_generator();
181
        $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
182
 
183
        return $guidegenerator->get_test_guide($context);
184
    }
185
 
186
    /**
187
     * Fetch a set of sample data.
188
     *
189
     * @param \gradingform_guide_controller $controller
190
     * @param int $itemid
191
     * @param float $spellingscore
192
     * @param string $spellingremark
193
     * @param float $picturescore
194
     * @param string $pictureremark
195
     * @return array
196
     */
197
    protected function get_test_form_data(
198
        \gradingform_guide_controller $controller,
199
        int $itemid,
200
        float $spellingscore,
201
        string $spellingremark,
202
        float $picturescore,
203
        string $pictureremark
204
    ): array {
205
        $generator = \testing_util::get_data_generator();
206
        $guidegenerator = $generator->get_plugin_generator('gradingform_guide');
207
 
208
        return $guidegenerator->get_test_form_data(
209
            $controller,
210
            $itemid,
211
            $spellingscore,
212
            $spellingremark,
213
            $picturescore,
214
            $pictureremark
215
        );
216
    }
217
}