Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

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