Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310]

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