Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.

Differences Between: [Versions 311 and 403] [Versions 400 and 403] [Versions 401 and 403]

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