Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 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() {
  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->assertEquals([$coursecontext1->id, $coursecontext2->id], $contextids, '', 0.0, 1, true);
 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->assertEquals(
 372                  ['STUDENT2-IN-COURSE2-00', 'STUDENT2-IN-COURSE2-01'],
 373                  array_column($data->transactions, 'txn_id'),
 374                  '', 0.0, 10, true
 375          );
 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->assertEquals(
 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                  '', 0.0, 10, true
 687          );
 688  
 689          $userlist2 = new \core_privacy\local\request\userlist($coursecontext2, 'enrol_paypal');
 690          provider::get_users_in_context($userlist2);
 691          $this->assertEquals(
 692                  [
 693                      $this->businessuser1->id,
 694                      $this->businessuser2->id,
 695                      $this->receiveruser2->id,
 696                      $this->student2->id,
 697                      $this->student12->id
 698                  ],
 699                  $userlist2->get_userids(),
 700                  '', 0.0, 10, true
 701          );
 702  
 703          $userlist3 = new \core_privacy\local\request\userlist($coursecontext3, 'enrol_paypal');
 704          provider::get_users_in_context($userlist3);
 705          $this->assertEquals(
 706                  [
 707                      $this->businessuser3->id,
 708                      $this->receiveruser3->id,
 709                      $this->student3->id
 710                  ],
 711                  $userlist3->get_userids(),
 712                  '', 0.0, 10, true
 713          );
 714      }
 715  
 716      /**
 717       * Test for provider::delete_data_for_users().
 718       */
 719      public function test_delete_data_for_users() {
 720          global $DB;
 721  
 722          $coursecontext1 = context_course::instance($this->course1->id);
 723  
 724          // Before deletion, we should have 2 PayPal transactions (1 of them for student12) in course1
 725          // and 3 PayPal transactions (1 of them for student12) in course2.
 726          // Student12 is enrolled in course1 and course2.
 727          // There is 1 transaction in course1 and 2 transactions in course2 under the name of businessuser1.
 728          $this->assertEquals(
 729                  2,
 730                  $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
 731          );
 732          $this->assertEquals(
 733                  [$this->course1->id, $this->course2->id],
 734                  $DB->get_fieldset_select('enrol_paypal', 'courseid', 'userid = ?', [$this->student12->id]),
 735                  '', 0.0, 10, true
 736          );
 737          $this->assertEquals(
 738                  [$this->course1->id, $this->course2->id, $this->course2->id],
 739                  $DB->get_fieldset_select('enrol_paypal', 'courseid', 'business = ?',
 740                          [\core_text::strtolower($this->businessuser1->email)]),
 741                  '', 0.0, 10, true
 742          );
 743          $this->assertEquals(
 744                  3,
 745                  $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
 746          );
 747  
 748          // Delete data of student12 and businessuser1 in course1.
 749          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_paypal',
 750                  [$this->student12->id, $this->businessuser1->id]);
 751          provider::delete_data_for_users($approveduserlist);
 752  
 753          // After deletion, PayPal transactions for student12 in course1 should have been deleted.
 754          // Now, we should have 1 PayPal transaction (which is not related to student12) in course1.
 755          // There should still be 3 PayPal transactions (1 of them for student12) in course2.
 756          // Student12 is not enrolled in course1 anymore, but s/he is still enrolled in course2.
 757          // There is no transaction in course1 under the name of businessuser1, but the 2 transactions in course2
 758          // that were under his/her name are intact.
 759          $this->assertEquals(
 760                  1,
 761                  $DB->count_records('enrol_paypal', ['courseid' => $this->course1->id])
 762          );
 763          $this->assertEquals(
 764                  [$this->course2->id],
 765                  $DB->get_fieldset_select('enrol_paypal', 'courseid', 'userid = ?', [$this->student12->id]),
 766                  '', 0.0, 10, true
 767          );
 768          $this->assertEquals(
 769                  [$this->course2->id, $this->course2->id],
 770                  $DB->get_fieldset_select('enrol_paypal', 'courseid', 'business = ?',
 771                          [\core_text::strtolower($this->businessuser1->email)]),
 772                  '', 0.0, 10, true
 773          );
 774          $this->assertEquals(
 775                  3,
 776                  $DB->count_records('enrol_paypal', ['courseid' => $this->course2->id])
 777          );
 778      }
 779  
 780      /**
 781       * Test for provider::delete_data_for_users() for business user deletion.
 782       */
 783      public function test_delete_data_for_users_business() {
 784          global $DB;
 785  
 786          $coursecontext1 = context_course::instance($this->course1->id);
 787  
 788          // Before deletion, there are 3 transactions under the name of businessuser1 and one of them is in course1.
 789          $this->assertEquals(3, $DB->count_records('enrol_paypal', ['business' => $this->businessuser1->email]));
 790          $transactions = $DB->get_records('enrol_paypal', [
 791              'courseid' => $this->course1->id,
 792              'business' => $this->businessuser1->email
 793          ]);
 794          $this->assertCount(1, $transactions);
 795          $transaction = reset($transactions);
 796  
 797          // Delete data of businessuser1 in course1.
 798          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_paypal',
 799                  [$this->businessuser1->id]);
 800          provider::delete_data_for_users($approveduserlist);
 801  
 802          // After deletion, there should be 2 transactions under the name of businessuser1 and none of them should be in course1.
 803          $this->assertEquals(2, $DB->count_records('enrol_paypal', ['business' => $this->businessuser1->email]));
 804          $this->assertEquals(0, $DB->count_records('enrol_paypal', [
 805              'courseid' => $this->course1->id,
 806              'business' => $this->businessuser1->email
 807          ]));
 808  
 809          // Also, the transaction in course1 that was under the name of businessuser1 should still exist,
 810          // but it should not be under the name of businessuser1 anymore.
 811          $newtransaction = $DB->get_record('enrol_paypal', ['id' => $transaction->id]);
 812          $this->assertEquals('', $newtransaction->business);
 813      }
 814  
 815      /**
 816       * Test for provider::delete_data_for_users() for receiver user deletion.
 817       */
 818      public function test_delete_data_for_users_receiver() {
 819          global $DB;
 820  
 821          $coursecontext1 = context_course::instance($this->course1->id);
 822  
 823          // Before deletion, there are 2 transactions under the name of receiveruser1 and both of them are in course1.
 824          $this->assertEquals(2, $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser1->email]));
 825          $transactions = $DB->get_records('enrol_paypal', [
 826              'courseid' => $this->course1->id,
 827              'receiver_email' => $this->receiveruser1->email
 828          ]);
 829          $this->assertCount(2, $transactions);
 830  
 831          // Delete data of receiveruser1 in course1.
 832          $approveduserlist = new \core_privacy\local\request\approved_userlist($coursecontext1, 'enrol_paypal',
 833                  [$this->receiveruser1->id]);
 834          provider::delete_data_for_users($approveduserlist);
 835  
 836          // After deletion, there should be no transaction under the name of receiveruser1.
 837          $this->assertEquals(0, $DB->count_records('enrol_paypal', ['receiver_email' => $this->receiveruser1->email]));
 838  
 839          // Also, the transactions in course1 that were under the name of receiveruser1 should still exist,
 840          // but they should not be under the name of receiveruser1 anymore.
 841          foreach ($transactions as $transaction) {
 842              $newtransaction = $DB->get_record('enrol_paypal', ['id' => $transaction->id]);
 843              $this->assertEquals('', $newtransaction->receiver_email);
 844          }
 845      }
 846  }