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
 * Data provider tests.
19
 *
20
 * @package    mod_survey
21
 * @category   test
22
 * @copyright  2018 Frédéric Massart
23
 * @author     Frédéric Massart <fred@branchup.tech>
24
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
 */
26
namespace mod_survey\privacy;
27
 
28
defined('MOODLE_INTERNAL') || die();
29
global $CFG;
30
 
31
use core_privacy\tests\provider_testcase;
32
use core_privacy\local\request\approved_contextlist;
33
use core_privacy\local\request\approved_userlist;
34
use core_privacy\local\request\transform;
35
use core_privacy\local\request\writer;
36
use mod_survey\privacy\provider;
37
 
38
require_once($CFG->dirroot . '/mod/survey/lib.php');
39
 
40
/**
41
 * Data provider testcase class.
42
 *
43
 * @package    mod_survey
44
 * @category   test
45
 * @copyright  2018 Frédéric Massart
46
 * @author     Frédéric Massart <fred@branchup.tech>
47
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
48
 */
49
class provider_test extends provider_testcase {
50
 
51
    public function setUp(): void {
52
        global $PAGE;
53
        $this->resetAfterTest();
54
        $PAGE->get_renderer('core');
55
 
56
        // Survey module is disabled by default, enable it for testing.
57
        $manager = \core_plugin_manager::resolve_plugininfo_class('mod');
58
        $manager::enable_plugin('survey', 1);
59
    }
60
 
11 efrain 61
    public function test_get_contexts_for_userid(): void {
1 efrain 62
        $dg = $this->getDataGenerator();
63
 
64
        $c1 = $dg->create_course();
65
        $c2 = $dg->create_course();
66
        $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
67
        $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
68
        $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
69
        $cm2a = $dg->create_module('survey', ['template' => 1, 'course' => $c2]);
70
        $cm2b = $dg->create_module('survey', ['template' => 1, 'course' => $c2]);
71
        $u1 = $dg->create_user();
72
        $u2 = $dg->create_user();
73
 
74
        $this->create_answer($cm1a->id, 1, $u1->id);
75
        $this->create_answer($cm1a->id, 1, $u2->id);
76
        $this->create_answer($cm1b->id, 1, $u2->id);
77
        $this->create_answer($cm2a->id, 1, $u1->id);
78
        $this->create_analysis($cm2b->id, $u1->id);
79
        $this->create_analysis($cm1c->id, $u2->id);
80
 
81
        $contextids = provider::get_contexts_for_userid($u1->id)->get_contextids();
82
        $this->assertCount(3, $contextids);
83
        $this->assertTrue(in_array(\context_module::instance($cm1a->cmid)->id, $contextids));
84
        $this->assertTrue(in_array(\context_module::instance($cm2a->cmid)->id, $contextids));
85
        $this->assertTrue(in_array(\context_module::instance($cm2b->cmid)->id, $contextids));
86
 
87
        $contextids = provider::get_contexts_for_userid($u2->id)->get_contextids();
88
        $this->assertCount(3, $contextids);
89
        $this->assertTrue(in_array(\context_module::instance($cm1a->cmid)->id, $contextids));
90
        $this->assertTrue(in_array(\context_module::instance($cm1b->cmid)->id, $contextids));
91
        $this->assertTrue(in_array(\context_module::instance($cm1c->cmid)->id, $contextids));
92
    }
93
 
94
    /**
95
     * Test for provider::test_get_users_in_context().
96
     */
11 efrain 97
    public function test_get_users_in_context(): void {
1 efrain 98
        $dg = $this->getDataGenerator();
99
        $component = 'mod_survey';
100
 
101
        $c1 = $dg->create_course();
102
        $c2 = $dg->create_course();
103
        $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
104
        $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
105
        $cm2 = $dg->create_module('survey', ['template' => 1, 'course' => $c2]);
106
        $cm1acontext = \context_module::instance($cm1a->cmid);
107
        $cm1bcontext = \context_module::instance($cm1b->cmid);
108
        $cm2context = \context_module::instance($cm2->cmid);
109
 
110
        $u1 = $dg->create_user();
111
        $u2 = $dg->create_user();
112
        $bothusers = [$u1->id, $u2->id];
113
        sort($bothusers);
114
 
115
        $this->create_answer($cm1a->id, 1, $u1->id);
116
        $this->create_answer($cm1b->id, 1, $u1->id);
117
        $this->create_answer($cm1b->id, 1, $u2->id);
118
        $this->create_answer($cm2->id, 1, $u2->id);
119
        $this->create_analysis($cm2->id, $u1->id);
120
 
121
        // Cm1a should only contain u1.
122
        $userlist = new \core_privacy\local\request\userlist($cm1acontext, $component);
123
        provider::get_users_in_context($userlist);
124
 
125
        $this->assertCount(1, $userlist);
126
        $this->assertEquals([$u1->id], $userlist->get_userids());
127
 
128
        // Cm1b should contain u1 and u2 (both have answers).
129
        $userlist = new \core_privacy\local\request\userlist($cm1bcontext, $component);
130
        provider::get_users_in_context($userlist);
131
 
132
        $this->assertCount(2, $userlist);
133
        $actual = $userlist->get_userids();
134
        sort($actual);
135
        $this->assertEquals($bothusers, $actual);
136
 
137
        // Cm2 should contain u1 (analysis) and u2 (answer).
138
        $userlist = new \core_privacy\local\request\userlist($cm2context, $component);
139
        provider::get_users_in_context($userlist);
140
 
141
        $this->assertCount(2, $userlist);
142
        $actual = $userlist->get_userids();
143
        sort($actual);
144
        $this->assertEquals($bothusers, $actual);
145
    }
146
 
11 efrain 147
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 148
        global $DB;
149
        $dg = $this->getDataGenerator();
150
 
151
        $c1 = $dg->create_course();
152
        $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
153
        $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
154
        $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
155
        $u1 = $dg->create_user();
156
        $u2 = $dg->create_user();
157
 
158
        $this->create_answer($cm1a->id, 1, $u1->id);
159
        $this->create_answer($cm1a->id, 1, $u2->id);
160
        $this->create_answer($cm1b->id, 1, $u2->id);
161
        $this->create_answer($cm1c->id, 1, $u1->id);
162
        $this->create_analysis($cm1a->id, $u1->id);
163
        $this->create_analysis($cm1b->id, $u1->id);
164
        $this->create_analysis($cm1a->id, $u2->id);
165
        $this->create_analysis($cm1c->id, $u2->id);
166
 
167
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
168
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
169
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
170
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
171
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
172
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
173
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
174
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
175
 
176
        // Deleting the course does nothing.
177
        provider::delete_data_for_all_users_in_context(\context_course::instance($c1->id));
178
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
179
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
180
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
181
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
182
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
183
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
184
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
185
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
186
 
187
        provider::delete_data_for_all_users_in_context(\context_module::instance($cm1c->cmid));
188
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
189
        $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
190
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
191
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
192
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
193
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
194
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
195
        $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
196
 
197
        provider::delete_data_for_all_users_in_context(\context_module::instance($cm1a->cmid));
198
        $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
199
        $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
200
        $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
201
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
202
        $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
203
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
204
        $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
205
        $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
206
    }
207
 
11 efrain 208
    public function test_delete_data_for_user(): void {
1 efrain 209
        global $DB;
210
        $dg = $this->getDataGenerator();
211
 
212
        $c1 = $dg->create_course();
213
        $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
214
        $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
215
        $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
216
        $u1 = $dg->create_user();
217
        $u2 = $dg->create_user();
218
 
219
        $this->create_answer($cm1a->id, 1, $u1->id);
220
        $this->create_answer($cm1a->id, 1, $u2->id);
221
        $this->create_answer($cm1b->id, 1, $u2->id);
222
        $this->create_answer($cm1c->id, 1, $u1->id);
223
        $this->create_analysis($cm1a->id, $u1->id);
224
        $this->create_analysis($cm1b->id, $u1->id);
225
        $this->create_analysis($cm1a->id, $u2->id);
226
        $this->create_analysis($cm1c->id, $u2->id);
227
 
228
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
229
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
230
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
231
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
232
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
233
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
234
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
235
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
236
 
237
        provider::delete_data_for_user(new approved_contextlist($u1, 'mod_survey', [
238
            \context_course::instance($c1->id)->id,
239
            \context_module::instance($cm1a->cmid)->id,
240
            \context_module::instance($cm1b->cmid)->id,
241
        ]));
242
        $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
243
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
244
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
245
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
246
        $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
247
        $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
248
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
249
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
250
    }
251
 
252
    /**
253
     * Test for provider::delete_data_for_users().
254
     */
11 efrain 255
    public function test_delete_data_for_users(): void {
1 efrain 256
        global $DB;
257
        $dg = $this->getDataGenerator();
258
        $component = 'mod_survey';
259
 
260
        $c1 = $dg->create_course();
261
        $cm1a = $dg->create_module('survey', ['template' => 1, 'course' => $c1]);
262
        $cm1b = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
263
        $cm1c = $dg->create_module('survey', ['template' => 2, 'course' => $c1]);
264
        $cm1acontext = \context_module::instance($cm1a->cmid);
265
        $cm1bcontext = \context_module::instance($cm1b->cmid);
266
 
267
        $u1 = $dg->create_user();
268
        $u2 = $dg->create_user();
269
 
270
        $this->create_answer($cm1a->id, 1, $u1->id);
271
        $this->create_answer($cm1a->id, 1, $u2->id);
272
        $this->create_analysis($cm1a->id, $u1->id);
273
        $this->create_analysis($cm1a->id, $u2->id);
274
        $this->create_answer($cm1b->id, 1, $u2->id);
275
        $this->create_analysis($cm1b->id, $u1->id);
276
        $this->create_answer($cm1c->id, 1, $u1->id);
277
        $this->create_analysis($cm1c->id, $u2->id);
278
 
279
        // Confirm data exists before deletion.
280
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
281
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
282
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
283
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1b->id]));
284
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
285
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1b->id]));
286
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
287
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
288
 
289
        // Ensure only approved user data is deleted.
290
        $approveduserids = [$u1->id];
291
        $approvedlist = new approved_userlist($cm1acontext, $component, $approveduserids);
292
        provider::delete_data_for_users($approvedlist);
293
 
294
        $this->assertFalse($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1a->id]));
295
        $this->assertFalse($DB->record_exists('survey_analysis', ['userid' => $u1->id, 'survey' => $cm1a->id]));
296
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u2->id, 'survey' => $cm1a->id]));
297
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1a->id]));
298
 
299
        $approveduserids = [$u1->id, $u2->id];
300
        $approvedlist = new approved_userlist($cm1bcontext, $component, $approveduserids);
301
        provider::delete_data_for_users($approvedlist);
302
 
303
        $this->assertFalse($DB->record_exists('survey_answers', ['survey' => $cm1b->id]));
304
        $this->assertFalse($DB->record_exists('survey_analysis', ['survey' => $cm1b->id]));
305
 
306
        $this->assertTrue($DB->record_exists('survey_answers', ['userid' => $u1->id, 'survey' => $cm1c->id]));
307
        $this->assertTrue($DB->record_exists('survey_analysis', ['userid' => $u2->id, 'survey' => $cm1c->id]));
308
    }
309
 
11 efrain 310
    public function test_export_data_for_user(): void {
1 efrain 311
        global $DB;
312
        $dg = $this->getDataGenerator();
313
 
314
        $templates = $DB->get_records_menu('survey', array('template' => 0), 'name', 'name, id');
315
 
316
        $c1 = $dg->create_course();
317
        $s1a = $dg->create_module('survey', ['template' => $templates['attlsname'], 'course' => $c1]);
318
        $s1b = $dg->create_module('survey', ['template' => $templates['ciqname'], 'course' => $c1]);
319
        $s1c = $dg->create_module('survey', ['template' => $templates['collesapname'], 'course' => $c1]);
320
        $u1 = $dg->create_user();
321
        $u2 = $dg->create_user();
322
 
323
        $s1actx = \context_module::instance($s1a->cmid);
324
        $s1bctx = \context_module::instance($s1b->cmid);
325
        $s1cctx = \context_module::instance($s1c->cmid);
326
 
327
        $this->answer_survey($s1a, $u1, $c1, $s1actx);
328
        $this->answer_survey($s1b, $u1, $c1, $s1bctx);
329
        $this->create_analysis($s1a->id, $u1->id, 'Hello,');
330
 
331
        $this->answer_survey($s1a, $u2, $c1, $s1actx);
332
        $this->answer_survey($s1c, $u2, $c1, $s1cctx);
333
        $this->create_analysis($s1b->id, $u2->id, 'World!');
334
 
335
        provider::export_user_data(new approved_contextlist($u1, 'mod_survey', [$s1actx->id, $s1bctx->id, $s1cctx->id]));
336
 
337
        $data = writer::with_context($s1actx)->get_data([]);
338
        $this->assertNotEmpty($data);
339
        $this->assert_exported_answers($data->answers, $u1, $s1a);
340
        $data = writer::with_context($s1actx)->get_related_data([], 'survey_analysis');
341
        $this->assertEquals('Hello,', $data->notes);
342
 
343
        $data = writer::with_context($s1bctx)->get_data([]);
344
        $this->assertNotEmpty($data);
345
        $this->assert_exported_answers($data->answers, $u1, $s1b);
346
        $data = writer::with_context($s1bctx)->get_related_data([], 'survey_analysis');
347
        $this->assertEmpty($data);
348
 
349
        $data = writer::with_context($s1cctx)->get_data([]);
350
        $this->assertEmpty($data);
351
        $data = writer::with_context($s1cctx)->get_related_data([], 'survey_analysis');
352
        $this->assertEmpty($data);
353
 
354
        writer::reset();
355
        provider::export_user_data(new approved_contextlist($u2, 'mod_survey', [$s1actx->id, $s1bctx->id, $s1cctx->id]));
356
 
357
        $data = writer::with_context($s1actx)->get_data([]);
358
        $this->assertNotEmpty($data);
359
        $this->assert_exported_answers($data->answers, $u2, $s1a);
360
        $data = writer::with_context($s1actx)->get_related_data([], 'survey_analysis');
361
        $this->assertEmpty($data);
362
 
363
        $data = writer::with_context($s1bctx)->get_data([]);
364
        $this->assertEmpty($data);
365
        $data = writer::with_context($s1bctx)->get_related_data([], 'survey_analysis');
366
        $this->assertEquals('World!', $data->notes);
367
 
368
        $data = writer::with_context($s1cctx)->get_data([]);
369
        $this->assertNotEmpty($data);
370
        $this->assert_exported_answers($data->answers, $u2, $s1c);
371
        $data = writer::with_context($s1cctx)->get_related_data([], 'survey_analysis');
372
        $this->assertEmpty($data);
373
    }
374
 
375
    /**
376
     * Answer a survey in a predictable manner.
377
     *
378
     * @param stdClass $survey The survey.
379
     * @param stdClass $user The user.
380
     * @param stdClass $course The course.
381
     * @param context_module $context The module context.
382
     * @return void
383
     */
384
    protected function answer_survey($survey, $user, $course, \context_module $context) {
385
        global $USER;
386
 
387
        $userid = $user->id;
388
        $questions = survey_get_questions($survey);
389
        $answer = function(&$answers, $q) use ($userid) {
390
            $key = 'q' . ($q->type == 2 ? 'P' : '') . $q->id;
391
 
392
            if ($q->type < 1) {
393
                $a = "A:{$q->id}:{$userid}";
394
                $answers[$key] = $a;
395
 
396
            } else if ($q->type < 3) {
397
                $options = explode(',', get_string($q->options, 'mod_survey'));
398
                $answers[$key] = ($q->id + $userid) % count($options) + 1;
399
 
400
            } else {
401
                $options = explode(',', get_string($q->options, 'mod_survey'));
402
                $answers["q{$q->id}"] = ($q->id + $userid) % count($options) + 1;
403
                $answers["qP{$q->id}"] = ($q->id + $userid + 1) % count($options) + 1;
404
            }
405
 
406
        };
407
 
408
        foreach ($questions as $q) {
409
            if ($q->type < 0) {
410
                continue;
411
            } else if ($q->type > 0 && $q->multi) {
412
                $subquestions = survey_get_subquestions($q);
413
                foreach ($subquestions as $sq) {
414
                    $answer($answers, $sq);
415
                }
416
            } else {
417
                $answer($answers, $q);
418
            }
419
        }
420
 
421
        $origuser = $USER;
422
        $this->setUser($user);
423
        survey_save_answers($survey, $answers, $course, $context);
424
        $this->setUser($origuser);
425
    }
426
 
427
    /**
428
     * Assert the answers provided to a survey.
429
     *
430
     * @param array $answers The answers.
431
     * @param object $user The user.
432
     * @param object $survey The survey.
433
     * @return void
434
     */
435
    protected function assert_exported_answers($answers, $user, $survey) {
436
        global $DB;
437
 
438
        $userid = $user->id;
439
        $questionids = explode(',', $survey->questions);
440
        $topquestions = $DB->get_records_list('survey_questions', 'id', $questionids, 'id');
441
        $questions = [];
442
 
443
        foreach ($topquestions as $q) {
444
            if ($q->type < 0) {
445
                continue;
446
            } else if ($q->type > 0 && $q->multi) {
447
                $questionids = explode(',', $q->multi);
448
                $subqs = $DB->get_records_list('survey_questions', 'id', $questionids, 'id');
449
            } else {
450
                $subqs = [$q];
451
            }
452
            foreach ($subqs as $sq) {
453
                $questions[] = $sq;
454
            }
455
        }
456
 
457
        $this->assertCount(count($questions), $answers);
458
 
459
        $answer = reset($answers);
460
        foreach ($questions as $question) {
461
            $qtype = $question->type;
462
            $question = survey_translate_question($question);
463
            $options = $qtype > 0 ? explode(',', $question->options) : '-';
464
            $this->assertEquals($question->text, $answer['question']['text']);
465
            $this->assertEquals($question->shorttext, $answer['question']['shorttext']);
466
            $this->assertEquals($question->intro, $answer['question']['intro']);
467
            $this->assertEquals($options, $answer['question']['options']);
468
 
469
            if ($qtype < 1) {
470
                $this->assertEquals("A:{$question->id}:{$userid}", $answer['answer']['actual']);
471
 
472
            } else if ($qtype == 1 || $qtype == 2) {
473
                $chosen = ($question->id + $userid) % count($options);
474
                $key = $qtype == 1 ? 'actual' : 'preferred';
475
                $this->assertEquals($options[$chosen], $answer['answer'][$key]);
476
 
477
            } else {
478
                $chosen = ($question->id + $userid) % count($options);
479
                $this->assertEquals($options[$chosen], $answer['answer']['actual']);
480
                $chosen = ($question->id + $userid + 1) % count($options);
481
                $this->assertEquals($options[$chosen], $answer['answer']['preferred']);
482
            }
483
 
484
            // Grab next answer, if any.
485
            $answer = next($answers);
486
        }
487
 
488
    }
489
 
490
    /**
491
     * Create analysis.
492
     *
493
     * @param int $surveyid The survey ID.
494
     * @param int $userid The user ID.
495
     * @param string $notes The nodes.
496
     * @return stdClass
497
     */
498
    protected function create_analysis($surveyid, $userid, $notes = '') {
499
        global $DB;
500
        $record = (object) [
501
            'survey' => $surveyid,
502
            'userid' => $userid,
503
            'notes' => $notes
504
        ];
505
        $record->id = $DB->insert_record('survey_analysis', $record);
506
        return $record;
507
    }
508
 
509
    /**
510
     * Create answer.
511
     *
512
     * @param int $surveyid The survey ID.
513
     * @param int $questionid The question ID.
514
     * @param int $userid The user ID.
515
     * @param string $answer1 The first answer field.
516
     * @param string $answer2 The second answer field.
517
     * @return stdClass
518
     */
519
    protected function create_answer($surveyid, $questionid, $userid, $answer1 = '', $answer2 = '') {
520
        global $DB;
521
        $record = (object) [
522
            'survey' => $surveyid,
523
            'question' => $questionid,
524
            'userid' => $userid,
525
            'answer1' => $answer1,
526
            'answer2' => $answer2,
527
            'time' => time()
528
        ];
529
        $record->id = $DB->insert_record('survey_answers', $record);
530
        return $record;
531
    }
532
 
533
}