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
 * Privacy provider tests.
19
 *
20
 * @package    enrol_paypal
21
 * @category   test
22
 * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
23
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24
 */
25
namespace enrol_paypal\privacy;
26
 
27
defined('MOODLE_INTERNAL') || die();
28
 
29
use core_privacy\local\metadata\collection;
30
use enrol_paypal\privacy\provider;
31
use core_privacy\local\request\writer;
32
use stdClass;
33
 
34
/**
35
 * Privacy provider test for enrol_paypal.
36
 *
37
 * @copyright  2018 Shamim Rezaie <shamim@moodle.com>
38
 * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
39
 */
1441 ariadna 40
final class provider_test extends \core_privacy\tests\provider_testcase {
1 efrain 41
 
42
    /** @var stdClass A user whose email address matches the business field in some of the PayPal transactions. */
43
    protected $businessuser1;
44
 
45
    /** @var stdClass A user whose email address matches the business field in some of the PayPal transactions. */
46
    protected $businessuser2;
47
 
48
    /** @var stdClass A user whose email address matches the business field in some of the PayPal transactions. */
49
    protected $businessuser3;
50
 
51
    /** @var stdClass A user whose email address matches the receiver_email field in some of the PayPal transactions. */
52
    protected $receiveruser1;
53
 
54
    /** @var stdClass A user whose email address matches the receiver_email field in some of the PayPal transactions. */
55
    protected $receiveruser2;
56
 
57
    /** @var stdClass A user whose email address matches the receiver_email field in some of the PayPal transactions. */
58
    protected $receiveruser3;
59
 
60
    /** @var stdClass A user who is not enrolled in any course. */
61
    protected $student0;
62
 
63
    /** @var stdClass A student who is only enrolled in course1. */
64
    protected $student1;
65
 
66
    /** @var stdClass A student who is only enrolled in course2 with 2 transaction histories in the course. */
67
    protected $student2;
68
 
69
    /** @var stdClass A student who is only enrolled in course3 with 1 transaction histories in the course. */
70
    protected $student3;
71
 
72
    /** @var stdClass A student who is enrolled in both course1 and course2. */
73
    protected $student12;
74
 
75
    /** @var stdClass A test course with 2 enrolments for student1 and student12. */
76
    protected $course1;
77
 
78
    /** @var stdClass A test course with 2 enrolments for student2 and student12. */
79
    protected $course2;
80
 
81
    /** @var stdClass A test course with 2 enrolments for student2 and student12. */
82
    protected $course3;
83
 
84
    protected function setUp(): void {
85
        global $DB;
1441 ariadna 86
        parent::setUp();
1 efrain 87
 
88
        $this->resetAfterTest();
89
 
90
        $studentrole = $DB->get_record('role', array('shortname' => 'student'));
91
 
92
        $generator = $this->getDataGenerator();
93
 
94
        // Create seller accounts.
95
        $this->businessuser1 = $generator->create_user(['email' => 'business1@domain.invalid']);
96
        $this->businessuser2 = $generator->create_user(['email' => 'business2@domain.invalid']);
97
        $this->businessuser3 = $generator->create_user(['email' => 'business3@domain.invalid']);
98
        $this->receiveruser1 = $generator->create_user(['email' => 'receiver1@domain.invalid']);
99
        $this->receiveruser2 = $generator->create_user(['email' => 'receiver2@domain.invalid']);
100
        $this->receiveruser3 = $generator->create_user(['email' => 'receiver3@domain.invalid']);
101
 
102
        // Create courses.
103
        $this->course1 = $generator->create_course();
104
        $this->course2 = $generator->create_course();
105
        $this->course3 = $generator->create_course();
106
 
107
        // Create enrolment instances.
108
        $paypalplugin = enrol_get_plugin('paypal');
109
 
110
        $enrolinstanceid = $paypalplugin->add_instance($this->course1,
111
                ['roleid'   => $studentrole->id, 'courseid' => $this->course1->id]);
112
        $enrolinstance1  = $DB->get_record('enrol', array('id' => $enrolinstanceid));
113
 
114
        $enrolinstanceid = $paypalplugin->add_instance($this->course2,
115
                ['roleid'   => $studentrole->id, 'courseid' => $this->course2->id]);
116
        $enrolinstance2 = $DB->get_record('enrol', array('id' => $enrolinstanceid));
117
 
118
        $enrolinstanceid = $paypalplugin->add_instance($this->course3,
119
                ['roleid'   => $studentrole->id, 'courseid' => $this->course3->id]);
120
        $enrolinstance3 = $DB->get_record('enrol', array('id' => $enrolinstanceid));
121
 
122
        // Create students.
123
        $this->student0 = $generator->create_user();    // This user will not be enrolled in any course.
124
        $this->student1 = $generator->create_user();
125
        $this->student2 = $generator->create_user();
126
        $this->student3 = $generator->create_user();
127
        $this->student12 = $generator->create_user();
128
 
129
        // Enrol student1 in course1.
130
        $paypalplugin->enrol_user($enrolinstance1, $this->student1->id, $studentrole->id);
131
        $this->create_enrol_paypal_record(
132
            $this->businessuser1,
133
            $this->receiveruser1,
134
            $this->course1,
135
            $this->student1,
136
            $enrolinstance1,
137
            'STUDENT1-IN-COURSE1-00',
138
            time()
139
        );
140
 
141
        // Enrol student2 in course2.
142
        $paypalplugin->enrol_user($enrolinstance2, $this->student2->id, $studentrole->id);
143
        // This user has 2 transaction histories.
144
        // Here is the first one.
145
        $this->create_enrol_paypal_record(
146
            $this->businessuser1,
147
            $this->receiveruser2,
148
            $this->course2,
149
            $this->student2,
150
            $enrolinstance2,
151
            'STUDENT2-IN-COURSE2-00',
152
            // Yesterday.
153
            time() - DAYSECS
154
        );
155
        // And now, the second one.
156
        $this->create_enrol_paypal_record(
157
            $this->businessuser1,
158
            $this->receiveruser2,
159
            $this->course2,
160
            $this->student2,
161
            $enrolinstance2,
162
            'STUDENT2-IN-COURSE2-01',
163
            time()
164
        );
165
 
166
        // Enrol student12 in course1 and course2.
167
        // First in course1.
168
        $paypalplugin->enrol_user($enrolinstance1, $this->student12->id, $studentrole->id);
169
        $this->create_enrol_paypal_record(
170
            $this->businessuser2,
171
            $this->receiveruser1,
172
            $this->course1,
173
            $this->student12,
174
            $enrolinstance1,
175
            'STUDENT12-IN-COURSE1-00',
176
            time()
177
        );
178
        // Then in course2.
179
        $paypalplugin->enrol_user($enrolinstance2, $this->student12->id, $studentrole->id);
180
        $this->create_enrol_paypal_record(
181
            $this->businessuser2,
182
            $this->receiveruser2,
183
            $this->course2,
184
            $this->student12,
185
            $enrolinstance2,
186
            'STUDENT12-IN-COURSE2-00',
187
            time()
188
        );
189
 
190
        // Enrol student3 in course3 with businessuser3 as the receiver.
191
        $paypalplugin->enrol_user($enrolinstance1, $this->student3->id, $studentrole->id);
192
        $this->create_enrol_paypal_record(
193
            $this->businessuser3,
194
            $this->receiveruser3,
195
            $this->course3,
196
            $this->student3,
197
            $enrolinstance3,
198
            'STUDENT3-IN-COURSE3-00',
199
            time()
200
        );
201
    }
202
 
203
    /**
204
     * Test for provider::get_metadata().
205
     */
11 efrain 206
    public function test_get_metadata(): void {
1 efrain 207
        $collection = new collection('enrol_paypal');
208
        $newcollection = provider::get_metadata($collection);
209
        $itemcollection = $newcollection->get_collection();
210
        $this->assertCount(2, $itemcollection);
211
 
212
        $location = reset($itemcollection);
213
        $this->assertEquals('paypal.com', $location->get_name());
214
        $this->assertEquals('privacy:metadata:enrol_paypal:paypal_com', $location->get_summary());
215
 
216
        $privacyfields = $location->get_privacy_fields();
217
        $this->assertArrayHasKey('os0', $privacyfields);
218
        $this->assertArrayHasKey('custom', $privacyfields);
219
        $this->assertArrayHasKey('first_name', $privacyfields);
220
        $this->assertArrayHasKey('last_name', $privacyfields);
221
        $this->assertArrayHasKey('address', $privacyfields);
222
        $this->assertArrayHasKey('city', $privacyfields);
223
        $this->assertArrayHasKey('email', $privacyfields);
224
        $this->assertArrayHasKey('country', $privacyfields);
225
 
226
        $table = next($itemcollection);
227
        $this->assertEquals('enrol_paypal', $table->get_name());
228
        $this->assertEquals('privacy:metadata:enrol_paypal:enrol_paypal', $table->get_summary());
229
 
230
        $privacyfields = $table->get_privacy_fields();
231
        $this->assertArrayHasKey('business', $privacyfields);
232
        $this->assertArrayHasKey('receiver_email', $privacyfields);
233
        $this->assertArrayHasKey('receiver_id', $privacyfields);
234
        $this->assertArrayHasKey('item_name', $privacyfields);
235
        $this->assertArrayHasKey('courseid', $privacyfields);
236
        $this->assertArrayHasKey('userid', $privacyfields);
237
        $this->assertArrayHasKey('instanceid', $privacyfields);
238
        $this->assertArrayHasKey('memo', $privacyfields);
239
        $this->assertArrayHasKey('tax', $privacyfields);
240
        $this->assertArrayHasKey('option_selection1_x', $privacyfields);
241
        $this->assertArrayHasKey('payment_status', $privacyfields);
242
        $this->assertArrayHasKey('pending_reason', $privacyfields);
243
        $this->assertArrayHasKey('reason_code', $privacyfields);
244
        $this->assertArrayHasKey('txn_id', $privacyfields);
245
        $this->assertArrayHasKey('parent_txn_id', $privacyfields);
246
        $this->assertArrayHasKey('payment_type', $privacyfields);
247
        $this->assertArrayHasKey('timeupdated', $privacyfields);
248
    }
249
 
250
    /**
251
     * Test for provider::get_contexts_for_userid().
252
     */
11 efrain 253
    public function test_get_contexts_for_userid(): void {
1 efrain 254
        $coursecontext1 = \context_course::instance($this->course1->id);
255
        $coursecontext2 = \context_course::instance($this->course2->id);
256
 
257
        // Student1 is only enrolled in 1 course.
258
        $contextlist = provider::get_contexts_for_userid($this->student1->id);
259
        $this->assertCount(1, $contextlist);
260
 
261
        $contextids = $contextlist->get_contextids();
262
        $this->assertEquals([$coursecontext1->id], $contextids);
263
 
264
        // Student12 is enrolled in 2 course.
265
        $contextlist = provider::get_contexts_for_userid($this->student12->id);
266
        $this->assertCount(2, $contextlist);
267
 
268
        $contextids = $contextlist->get_contextids();
269
        $this->assertContainsEquals($coursecontext1->id, $contextids);
270
        $this->assertContainsEquals($coursecontext2->id, $contextids);
271
    }
272
 
273
    /**
274
     * Test for provider::get_contexts_for_userid with a user who is a receiver.
275
     */
11 efrain 276
    public function test_get_contexts_for_userid_receiver(): void {
1 efrain 277
        $coursecontext1 = \context_course::instance($this->course1->id);
278
        $coursecontext2 = \context_course::instance($this->course2->id);
279
 
280
        // Receiver User 1 is the Receiver of one course.
281
        $contextlist = provider::get_contexts_for_userid($this->receiveruser1->id);
282
        $this->assertCount(1, $contextlist);
283
 
284
        $contextids = $contextlist->get_contextids();
285
        $this->assertEquals([$coursecontext1->id], $contextids);
286
 
287
        // Receiver User 2 is the Receiver of course.
288
        $contextlist = provider::get_contexts_for_userid($this->receiveruser2->id);
289
        $this->assertCount(1, $contextlist);
290
 
291
        $contextids = $contextlist->get_contextids();
292
        $this->assertEquals([$coursecontext2->id], $contextids);
293
    }
294
 
295
    /**
296
     * Test for provider::get_contexts_for_userid with a user who is a business.
297
     */
11 efrain 298
    public function test_get_contexts_for_userid_business(): void {
1 efrain 299
        $coursecontext1 = \context_course::instance($this->course1->id);
300
        $coursecontext2 = \context_course::instance($this->course2->id);
301
        $coursecontext3 = \context_course::instance($this->course3->id);
302
 
303
        // Business User 1 is the Receiver of course 1 and course 2.
304
        $contextlist = provider::get_contexts_for_userid($this->businessuser1->id);
305
        $this->assertCount(2, $contextlist);
306
 
307
        $contextids = $contextlist->get_contextids();
308
        $this->assertEqualsCanonicalizing([$coursecontext1->id, $coursecontext2->id], $contextids);
309
 
310
        // Business User 3 is the Receiver of course 3 only.
311
        $contextlist = provider::get_contexts_for_userid($this->businessuser3->id);
312
        $this->assertCount(1, $contextlist);
313
 
314
        $contextids = $contextlist->get_contextids();
315
        $this->assertEquals([$coursecontext3->id], $contextids);
316
    }
317
 
318
    /**
319
     * Test for provider::export_user_data().
320
     */
11 efrain 321
    public function test_export_user_data(): void {
1 efrain 322
        $coursecontext1 = \context_course::instance($this->course1->id);
323
 
324
        $this->setUser($this->student1);
325
 
326
        // Export all of the data for the context.
327
        $this->export_context_data_for_user($this->student1->id, $coursecontext1, 'enrol_paypal');
328
        $writer = writer::with_context($coursecontext1);
329
        $this->assertTrue($writer->has_any_data());
330
 
331
        $data = $writer->get_data([get_string('transactions', 'enrol_paypal')]);
332
 
333
    }
334
 
335
    /**
336
     * Test for provider::export_user_data() when user is not enrolled.
337
     */
11 efrain 338
    public function test_export_user_data_not_enrolled(): void {
1 efrain 339
        $coursecontext1 = \context_course::instance($this->course1->id);
340
 
341
        $this->setUser($this->student2);
342
 
343
        // Export all of the data for the context.
344
        $this->export_context_data_for_user($this->student2->id, $coursecontext1, 'enrol_paypal');
345
        $writer = writer::with_context($coursecontext1);
346
        $this->assertFalse($writer->has_any_data());
347
    }
348
 
349
    /**
350
     * Test for provider::export_user_data() when user has no enrolment.
351
     */
11 efrain 352
    public function test_export_user_data_no_enrolment(): void {
1 efrain 353
        $coursecontext1 = \context_course::instance($this->course1->id);
354
 
355
        $this->setUser($this->student0);
356
 
357
        // Export all of the data for the context.
358
        $this->export_context_data_for_user($this->student0->id, $coursecontext1, 'enrol_paypal');
359
        $writer = writer::with_context($coursecontext1);
360
        $this->assertFalse($writer->has_any_data());
361
    }
362
 
11 efrain 363
    public function test_export_user_data_multiple_paypal_history(): void {
1 efrain 364
        $coursecontext2 = \context_course::instance($this->course2->id);
365
 
366
        $this->setUser($this->student2);
367
        // Export all of the data for the context.
368
        $this->export_context_data_for_user($this->student2->id, $coursecontext2, 'enrol_paypal');
369
        $writer = writer::with_context($coursecontext2);
370
        $this->assertTrue($writer->has_any_data());
371
 
372
        $data = $writer->get_data([get_string('transactions', 'enrol_paypal')]);
373
        $this->assertCount(2, $data->transactions);
374
        $this->assertEqualsCanonicalizing(
375
                ['STUDENT2-IN-COURSE2-00', 'STUDENT2-IN-COURSE2-01'],
376
                array_column($data->transactions, 'txn_id'));
377
    }
378
 
379
    /**
380
     * Test for provider::delete_data_for_all_users_in_context().
381
     */
11 efrain 382
    public function test_delete_data_for_all_users_in_context(): void {
1 efrain 383
        global $DB;
384
 
385
        $coursecontext1 = \context_course::instance($this->course1->id);
386
        $this->setUser($this->student1);
387
 
388
        // Before deletion, we should have 2 PayPal transactions in course1 and 3 PayPal transactions in course2.
389
        $this->assertEquals(
390
                2,
391
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
392
        );
393
        $this->assertEquals(
394
                3,
395
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
396
        );
397
 
398
        // Delete data based on context.
399
        provider::delete_data_for_all_users_in_context($coursecontext1);
400
 
401
        // After deletion, PayPal transactions in course1 should have been deleted.
402
        $this->assertEquals(
403
                0,
404
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
405
        );
406
        $this->assertEquals(
407
                3,
408
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
409
        );
410
    }
411
 
412
    /**
413
     * Test for provider::delete_data_for_all_users_in_context() when there is multiple transaction histories for a user.
414
     */
11 efrain 415
    public function test_delete_data_for_all_users_in_context_multiple_transactions(): void {
1 efrain 416
        global $DB;
417
 
418
        $coursecontext2 = \context_course::instance($this->course2->id);
419
        $this->setUser($this->student2);
420
 
421
        // Before deletion, we should have 2 PayPal transactions in course1 and 3 PayPal transactions in course2.
422
        $this->assertEquals(
423
                2,
424
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
425
        );
426
        $this->assertEquals(
427
                3,
428
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
429
        );
430
 
431
        // Delete data based on context.
432
        provider::delete_data_for_all_users_in_context($coursecontext2);
433
 
434
        // After deletion, PayPal transactions in course2 should have been deleted.
435
        $this->assertEquals(
436
                2,
437
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
438
        );
439
        $this->assertEquals(
440
                0,
441
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
442
        );
443
    }
444
 
445
    /**
446
     * Test for provider::delete_data_for_user() when student is enrolled in multiple courses and deleting from one of them.
447
     */
11 efrain 448
    public function test_delete_data_for_user_from_single_context(): void {
1 efrain 449
        global $DB;
450
 
451
        $coursecontext1 = \context_course::instance($this->course1->id);
452
 
453
        $this->setUser($this->student12);
454
 
455
        // Before deletion, we should have 2 PayPal transactions (1 of them for student12) in course1
456
        // and 3 PayPal transactions (1 of them for student12) in course2.
457
        $this->assertEquals(
458
                2,
459
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
460
        );
461
        $this->assertEquals(
462
                1,
463
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id, 'userid' => $this->student12->id])
464
        );
465
        $this->assertEquals(
466
                3,
467
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
468
        );
469
        $this->assertEquals(
470
                1,
471
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id, 'userid' => $this->student12->id])
472
        );
473
 
474
        // Delete data for user.
475
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->student12, 'enrol_paypal',
476
                [$coursecontext1->id]);
477
        provider::delete_data_for_user($contextlist);
478
 
479
        // After deletion, PayPal transactions for student12 in course1 should have been deleted.
480
        $this->assertEquals(
481
                1,
482
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
483
        );
484
        $this->assertEquals(
485
                0,
486
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id, 'userid' => $this->student12->id])
487
        );
488
        $this->assertEquals(
489
                3,
490
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
491
        );
492
        $this->assertEquals(
493
                1,
494
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id, 'userid' => $this->student12->id])
495
        );
496
    }
497
 
498
    /**
499
     * Test for provider::delete_data_for_user() when student is enrolled in multiple courses and deleting from all of them.
500
     */
11 efrain 501
    public function test_delete_data_for_user_from_multiple_context(): void {
1 efrain 502
        global $DB;
503
 
504
        $coursecontext1 = \context_course::instance($this->course1->id);
505
        $coursecontext2 = \context_course::instance($this->course2->id);
506
 
507
        $this->setUser($this->student12);
508
 
509
        // Before deletion, we should have 2 PayPal transactions (1 of them for student12) in course1
510
        // and 3 PayPal transactions (1 of them for student12) in course2.
511
        $this->assertEquals(
512
                2,
513
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
514
        );
515
        $this->assertEquals(
516
                1,
517
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id, 'userid' => $this->student12->id])
518
        );
519
        $this->assertEquals(
520
                3,
521
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
522
        );
523
        $this->assertEquals(
524
                1,
525
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id, 'userid' => $this->student12->id])
526
        );
527
 
528
        // Delete data for user.
529
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->student12, 'enrol_paypal',
530
                [$coursecontext1->id, $coursecontext2->id]);
531
        provider::delete_data_for_user($contextlist);
532
 
533
        // After deletion, PayPal enrolment data for student12 in both course1 and course2 should have been deleted.
534
        $this->assertEquals(
535
                1,
536
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
537
        );
538
        $this->assertEquals(
539
                0,
540
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id, 'userid' => $this->student12->id])
541
        );
542
        $this->assertEquals(
543
                2,
544
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
545
        );
546
        $this->assertEquals(
547
                0,
548
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id, 'userid' => $this->student12->id])
549
        );
550
    }
551
 
552
    /**
553
     * Test for provider::delete_data_for_user() when user is not enrolled, but is the receiver of the payment.
554
     */
11 efrain 555
    public function test_delete_data_for_user_for_business_user(): void {
1 efrain 556
        global $DB;
557
 
558
        $coursecontext1 = \context_course::instance($this->course1->id);
559
 
560
        $this->setUser($this->businessuser1);
561
 
562
        // Before deletion, we should have 5 PayPal enrolments.
563
        // 3 of which paid to businessuser1 and 2 of which paid to businessuser2.
564
        $this->assertEquals(
565
                3,
566
                $DB->count_records('enrol_paypal', ['business' => $this->businessuser1->email])
567
        );
568
        $this->assertEquals(
569
                2,
570
                $DB->count_records('enrol_paypal', ['business' => $this->businessuser2->email])
571
        );
572
 
573
        // Delete data for user in $coursecontext1.
574
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->businessuser1, 'enrol_paypal',
575
                [$coursecontext1->id]);
576
        provider::delete_data_for_user($contextlist);
577
 
578
        // After deletion, PayPal enrolment data for businessuser1 in course1 should have been deleted.
579
        $this->assertEquals(
580
                0,
581
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id, 'business' => $this->businessuser1->email])
582
        );
583
        $this->assertEquals(
584
                2,
585
                $DB->count_records('enrol_paypal', ['business' => $this->businessuser1->email])
586
        );
587
        $this->assertEquals(
588
                1,
589
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id, 'business' => ''])
590
        );
591
        $this->assertEquals(
592
                2,
593
                $DB->count_records('enrol_paypal', ['business' => $this->businessuser2->email])
594
        );
595
    }
596
 
597
    /**
598
     * Test for provider::delete_data_for_user() when user is not enrolled, but is the receiver of the payment.
599
     */
11 efrain 600
    public function test_delete_data_for_user_for_receiver_user(): void {
1 efrain 601
        global $DB;
602
 
603
        $coursecontext1 = \context_course::instance($this->course1->id);
604
 
605
        $this->setUser($this->receiveruser1);
606
 
607
        // Before deletion, we should have 5 PayPal enrolments.
608
        // 2 of which paid to receiveruser1 and 3 of which paid to receiveruser2.
609
        $this->assertEquals(
610
                2,
611
                $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser1->email])
612
        );
613
        $this->assertEquals(
614
                3,
615
                $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser2->email])
616
        );
617
 
618
        // Delete data for user.
619
        $contextlist = new \core_privacy\local\request\approved_contextlist($this->receiveruser1, 'enrol_paypal',
620
                [$coursecontext1->id]);
621
        provider::delete_data_for_user($contextlist);
622
 
623
        // After deletion, PayPal enrolment data for receiveruser1 in course1 should have been deleted.
624
        $this->assertEquals(
625
                0,
626
                $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser1->email])
627
        );
628
        $this->assertEquals(
629
                2,
630
                $DB->count_records('enrol_paypal', ['receiver_email' => ''])
631
        );
632
        $this->assertEquals(
633
                3,
634
                $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser2->email])
635
        );
636
    }
637
 
638
    /**
639
     * Helper function to create an enrol_paypal record.
640
     *
641
     * @param   \stdClass   $business The user associated with the business
642
     * @param   \stdClass   $receiver The user associated with the receiver
643
     * @param   \stdClass   $course The course to associate with
644
     * @param   \stdClass   $user The user associated with the student
645
     * @param   \stdClass   $enrol The enrolment instance
646
     * @param   String      $txnid The Paypal txnid to use
647
     * @param   int         $time The txn time
648
     */
649
    protected function create_enrol_paypal_record($business, $receiver, $course, $user, $enrol, $txnid, $time) {
650
        global $DB;
651
 
652
        $paypaldata = [
653
            'business'       => $business->email,
654
            'receiver_email' => $receiver->email,
655
            'receiver_id'    => 'SELLERSID',
656
            'item_name'      => $course->fullname,
657
            'courseid'       => $course->id,
658
            'userid'         => $user->id,
659
            'instanceid'     => $enrol->id,
660
            'payment_status' => 'Completed',
661
            'txn_id'         => $txnid,
662
            'payment_type'   => 'instant',
663
            'timeupdated'    => $time,
664
        ];
665
        $DB->insert_record('enrol_paypal', $paypaldata);
666
    }
667
 
668
    /**
669
     * Test for provider::get_users_in_context().
670
     */
11 efrain 671
    public function test_get_users_in_context(): void {
1 efrain 672
        $coursecontext1 = \context_course::instance($this->course1->id);
673
        $coursecontext2 = \context_course::instance($this->course2->id);
674
        $coursecontext3 = \context_course::instance($this->course3->id);
675
 
676
        $userlist1 = new \core_privacy\local\request\userlist($coursecontext1, 'enrol_paypal');
677
        provider::get_users_in_context($userlist1);
678
        $this->assertEqualsCanonicalizing(
679
                [
680
                    $this->businessuser1->id,
681
                    $this->businessuser2->id,
682
                    $this->receiveruser1->id,
683
                    $this->student1->id,
684
                    $this->student12->id
685
                ],
686
                $userlist1->get_userids()
687
        );
688
 
689
        $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, 'enrol_paypal');
690
        provider::get_users_in_context($userlist2);
691
        $this->assertEqualsCanonicalizing(
692
                [
693
                    $this->businessuser1->id,
694
                    $this->businessuser2->id,
695
                    $this->receiveruser2->id,
696
                    $this->student2->id,
697
                    $this->student12->id
698
                ],
699
                $userlist2->get_userids()
700
        );
701
 
702
        $userlist3 = new \core_privacy\local\request\userlist($coursecontext3, 'enrol_paypal');
703
        provider::get_users_in_context($userlist3);
704
        $this->assertEqualsCanonicalizing(
705
                [
706
                    $this->businessuser3->id,
707
                    $this->receiveruser3->id,
708
                    $this->student3->id
709
                ],
710
                $userlist3->get_userids()
711
        );
712
    }
713
 
714
    /**
715
     * Test for provider::delete_data_for_users().
716
     */
11 efrain 717
    public function test_delete_data_for_users(): void {
1 efrain 718
        global $DB;
719
 
720
        $coursecontext1 = \context_course::instance($this->course1->id);
721
 
722
        // Before deletion, we should have 2 PayPal transactions (1 of them for student12) in course1
723
        // and 3 PayPal transactions (1 of them for student12) in course2.
724
        // Student12 is enrolled in course1 and course2.
725
        // There is 1 transaction in course1 and 2 transactions in course2 under the name of businessuser1.
726
        $this->assertEquals(
727
                2,
728
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
729
        );
730
        $this->assertEqualsCanonicalizing(
731
                [$this->course1->id, $this->course2->id],
732
                $DB->get_fieldset_select('enrol_paypal', 'courseid', 'userid = ?', [$this->student12->id])
733
        );
734
        $this->assertEqualsCanonicalizing(
735
                [$this->course1->id, $this->course2->id, $this->course2->id],
736
                $DB->get_fieldset_select('enrol_paypal', 'courseid', 'business = ?',
737
                        [\core_text::strtolower($this->businessuser1->email)])
738
        );
739
        $this->assertEquals(
740
                3,
741
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
742
        );
743
 
744
        // Delete data of student12 and businessuser1 in course1.
745
        $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_paypal',
746
                [$this->student12->id, $this->businessuser1->id]);
747
        provider::delete_data_for_users($approveduserlist);
748
 
749
        // After deletion, PayPal transactions for student12 in course1 should have been deleted.
750
        // Now, we should have 1 PayPal transaction (which is not related to student12) in course1.
751
        // There should still be 3 PayPal transactions (1 of them for student12) in course2.
752
        // Student12 is not enrolled in course1 anymore, but s/he is still enrolled in course2.
753
        // There is no transaction in course1 under the name of businessuser1, but the 2 transactions in course2
754
        // that were under his/her name are intact.
755
        $this->assertEquals(
756
                1,
757
                $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
758
        );
759
        $this->assertEqualsCanonicalizing(
760
                [$this->course2->id],
761
                $DB->get_fieldset_select('enrol_paypal', 'courseid', 'userid = ?', [$this->student12->id])
762
        );
763
        $this->assertEqualsCanonicalizing(
764
                [$this->course2->id, $this->course2->id],
765
                $DB->get_fieldset_select('enrol_paypal', 'courseid', 'business = ?',
766
                        [\core_text::strtolower($this->businessuser1->email)])
767
        );
768
        $this->assertEquals(
769
                3,
770
                $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
771
        );
772
    }
773
 
774
    /**
775
     * Test for provider::delete_data_for_users() for business user deletion.
776
     */
11 efrain 777
    public function test_delete_data_for_users_business(): void {
1 efrain 778
        global $DB;
779
 
780
        $coursecontext1 = \context_course::instance($this->course1->id);
781
 
782
        // Before deletion, there are 3 transactions under the name of businessuser1 and one of them is in course1.
783
        $this->assertEquals(3, $DB->count_records('enrol_paypal', ['business' => $this->businessuser1->email]));
784
        $transactions = $DB->get_records('enrol_paypal', [
785
            'courseid' => $this->course1->id,
786
            'business' => $this->businessuser1->email
787
        ]);
788
        $this->assertCount(1, $transactions);
789
        $transaction = reset($transactions);
790
 
791
        // Delete data of businessuser1 in course1.
792
        $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_paypal',
793
                [$this->businessuser1->id]);
794
        provider::delete_data_for_users($approveduserlist);
795
 
796
        // After deletion, there should be 2 transactions under the name of businessuser1 and none of them should be in course1.
797
        $this->assertEquals(2, $DB->count_records('enrol_paypal', ['business' => $this->businessuser1->email]));
798
        $this->assertEquals(0, $DB->count_records('enrol_paypal', [
799
            'courseid' => $this->course1->id,
800
            'business' => $this->businessuser1->email
801
        ]));
802
 
803
        // Also, the transaction in course1 that was under the name of businessuser1 should still exist,
804
        // but it should not be under the name of businessuser1 anymore.
805
        $newtransaction = $DB->get_record('enrol_paypal', ['id' => $transaction->id]);
806
        $this->assertEquals('', $newtransaction->business);
807
    }
808
 
809
    /**
810
     * Test for provider::delete_data_for_users() for receiver user deletion.
811
     */
11 efrain 812
    public function test_delete_data_for_users_receiver(): void {
1 efrain 813
        global $DB;
814
 
815
        $coursecontext1 = \context_course::instance($this->course1->id);
816
 
817
        // Before deletion, there are 2 transactions under the name of receiveruser1 and both of them are in course1.
818
        $this->assertEquals(2, $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser1->email]));
819
        $transactions = $DB->get_records('enrol_paypal', [
820
            'courseid' => $this->course1->id,
821
            'receiver_email' => $this->receiveruser1->email
822
        ]);
823
        $this->assertCount(2, $transactions);
824
 
825
        // Delete data of receiveruser1 in course1.
826
        $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_paypal',
827
                [$this->receiveruser1->id]);
828
        provider::delete_data_for_users($approveduserlist);
829
 
830
        // After deletion, there should be no transaction under the name of receiveruser1.
831
        $this->assertEquals(0, $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser1->email]));
832
 
833
        // Also, the transactions in course1 that were under the name of receiveruser1 should still exist,
834
        // but they should not be under the name of receiveruser1 anymore.
835
        foreach ($transactions as $transaction) {
836
            $newtransaction = $DB->get_record('enrol_paypal', ['id' => $transaction->id]);
837
            $this->assertEquals('', $newtransaction->receiver_email);
838
        }
839
    }
840
}