Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.0.x will end 8 May 2023 (12 months).
  • Bug fixes for security issues in 4.0.x will end 13 November 2023 (18 months).
  • PHP version: minimum PHP 7.3.0 Note: the minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is also supported.
   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 tests for core_favourites.
  19   *
  20   * @package    core_favourites
  21   * @category   test
  22   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
  23   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  namespace core_favourites\privacy;
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  use core_privacy\tests\provider_testcase;
  30  use core_favourites\privacy\provider;
  31  use core_privacy\local\request\transform;
  32  
  33  /**
  34   * Unit tests for favourites/classes/privacy/provider
  35   *
  36   * @copyright  2018 Jake Dallimore <jrhdallimore@gmail.com>
  37   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class provider_test extends provider_testcase {
  40  
  41      public function setUp(): void {
  42          $this->resetAfterTest(true);
  43      }
  44  
  45      /**
  46       * Helper to set up some sample users and courses.
  47       */
  48      protected function set_up_courses_and_users() {
  49          $user1 = self::getDataGenerator()->create_user();
  50          $user1context = \context_user::instance($user1->id);
  51          $user2 = self::getDataGenerator()->create_user();
  52          $user2context = \context_user::instance($user2->id);
  53          $course1 = self::getDataGenerator()->create_course();
  54          $course2 = self::getDataGenerator()->create_course();
  55          $course1context = \context_course::instance($course1->id);
  56          $course2context = \context_course::instance($course2->id);
  57          return [$user1, $user2, $user1context, $user2context, $course1context, $course2context];
  58      }
  59  
  60      /**
  61       * Test confirming that contexts of favourited items can be added to the contextlist.
  62       */
  63      public function test_add_contexts_for_userid() {
  64          list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
  65  
  66          // Favourite 2 courses for user1 and 1 course for user2, all at the site context.
  67          $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context);
  68          $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context);
  69          $systemcontext = \context_system::instance();
  70          $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $systemcontext);
  71          $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext);
  72          $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext);
  73          $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses'));
  74          $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses'));
  75  
  76          // Now, just for variety, let's assume you can favourite a course at user context, and do so for user1.
  77          $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $user1context);
  78  
  79          // Now, ask the favourites privacy api to export contexts for favourites of the type we just created, for user1.
  80          $contextlist = new \core_privacy\local\request\contextlist();
  81          \core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user1->id, 'core_course', 'courses');
  82  
  83          // Verify we have two contexts in the list for user1.
  84          $this->assertCount(2, $contextlist->get_contextids());
  85  
  86          // And verify we only have the system context returned for user2.
  87          $contextlist = new \core_privacy\local\request\contextlist();
  88          \core_favourites\privacy\provider::add_contexts_for_userid($contextlist, $user2->id, 'core_course', 'courses');
  89          $this->assertCount(1, $contextlist->get_contextids());
  90      }
  91  
  92      /**
  93       * Test deletion of user favourites based on an approved_contextlist and component area.
  94       */
  95      public function test_delete_favourites_for_user() {
  96          list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
  97  
  98          // Favourite 2 courses for user1 and 1 course for user2, all at the user context.
  99          $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context);
 100          $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context);
 101          $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $user1context);
 102          $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $user1context);
 103          $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $user2context);
 104          $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses'));
 105          $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses'));
 106  
 107          // Now, delete the favourites for user1 only.
 108          $approvedcontextlist = new \core_privacy\local\request\approved_contextlist($user1, 'core_course', [$user1context->id]);
 109          provider::delete_favourites_for_user($approvedcontextlist, 'core_course', 'courses');
 110  
 111          // Verify that we have no favourite courses for user1 but that the records are in tact for user2.
 112          $this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'courses'));
 113          $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses'));
 114      }
 115  
 116      public function test_delete_favourites_for_all_users() {
 117          list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
 118  
 119          // Favourite 2 course modules for user1 and 1 course module for user2 all in course 1 context.
 120          $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context);
 121          $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context);
 122          $ufservice1->create_favourite('core_course', 'modules', 1, $course1context);
 123          $ufservice1->create_favourite('core_course', 'modules', 2, $course1context);
 124          $ufservice2->create_favourite('core_course', 'modules', 3, $course1context);
 125  
 126          // Now, favourite a different course module for user2 in course 2.
 127          $ufservice2->create_favourite('core_course', 'modules', 5, $course2context);
 128  
 129          $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'modules'));
 130          $this->assertCount(2, $ufservice2->find_favourites_by_type('core_course', 'modules'));
 131  
 132          // Now, delete all course module favourites in the 'course1' context only.
 133          provider::delete_favourites_for_all_users($course1context, 'core_course', 'modules');
 134  
 135          // Verify that only a single favourite for user1 in course 1 remains.
 136          $this->assertCount(0, $ufservice1->find_favourites_by_type('core_course', 'modules'));
 137          $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'modules'));
 138      }
 139  
 140      /**
 141       * Test confirming that user ID's of favourited items can be added to the userlist.
 142       */
 143      public function test_add_userids_for_context() {
 144          list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
 145  
 146          // Favourite 2 courses for user1 and 1 course for user2, all at the site context.
 147          $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context);
 148          $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context);
 149          $systemcontext = \context_system::instance();
 150          $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $systemcontext);
 151          $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext);
 152          $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext);
 153          $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses'));
 154          $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses'));
 155  
 156          // Now, just for variety, let's assume you can favourite a course at user context, and do so for user1.
 157          $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $user1context);
 158  
 159          // Now, ask the favourites privacy api to export userids for favourites of the type we just created, in the system context.
 160          $userlist = new \core_privacy\local\request\userlist($systemcontext, 'core_course');
 161          provider::add_userids_for_context($userlist, 'courses');
 162          // Verify we have two userids in the list for system context.
 163          $this->assertCount(2, $userlist->get_userids());
 164          $expected = [
 165              $user1->id,
 166              $user2->id
 167          ];
 168          $this->assertEqualsCanonicalizing($expected, $userlist->get_userids());
 169  
 170          // Ask the favourites privacy api to export userids for favourites of the type we just created, in the user1 context.
 171          $userlist = new \core_privacy\local\request\userlist($user1context, 'core_course');
 172          provider::add_userids_for_context($userlist, 'courses');
 173          // Verify we have one userid in the list for user1 context.
 174          $this->assertCount(1, $userlist->get_userids());
 175          $expected = [$user1->id];
 176          $this->assertEquals($expected, $userlist->get_userids());
 177  
 178          // Ask the favourites privacy api to export userids for favourites of the type we just created, in the user2 context.
 179          $userlist = new \core_privacy\local\request\userlist($user2context, 'core_favourites');
 180          provider::add_userids_for_context($userlist, 'core_course', 'courses');
 181          // Verify we do not have any userids in the list for user2 context.
 182          $this->assertCount(0, $userlist->get_userids());
 183      }
 184  
 185      /**
 186       * Test deletion of user favourites based on an approved_userlist, component area and item type.
 187       */
 188      public function test_delete_favourites_for_userlist() {
 189          list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
 190  
 191          // Favourite 2 courses for user1 and 1 course for user2.
 192          $systemcontext = \context_system::instance();
 193          $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context);
 194          $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context);
 195          $ufservice1->create_favourite('core_course', 'courses', $course1context->instanceid, $systemcontext);
 196          $ufservice1->create_favourite('core_course', 'courses', $course2context->instanceid, $user1context);
 197          $ufservice2->create_favourite('core_course', 'courses', $course2context->instanceid, $systemcontext);
 198          $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses'));
 199          $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses'));
 200  
 201          // Ask the favourites privacy api to export userids for favourites of the type we just created, in the system context.
 202          $userlist1 = new \core_privacy\local\request\userlist($systemcontext, 'core_course');
 203          provider::add_userids_for_context($userlist1, 'courses');
 204          // Verify we have two userids in the list for system context.
 205          $this->assertCount(2, $userlist1->get_userids());
 206  
 207          // Ask the favourites privacy api to export userids for favourites of the type we just created, in the user1 context.
 208          $userlist2 = new \core_privacy\local\request\userlist($user1context, 'core_course');
 209          provider::add_userids_for_context($userlist2, 'courses');
 210          // Verify we have one userid in the list for user1 context.
 211          $this->assertCount(1, $userlist2->get_userids());
 212  
 213          // Now, delete the favourites for user1 only in the system context.
 214          $approveduserlist = new \core_privacy\local\request\approved_userlist($systemcontext, 'core_course',
 215                  [$user1->id]);
 216          provider::delete_favourites_for_userlist($approveduserlist, 'courses');
 217          // Ensure user1's data was deleted and user2 is still returned for system context.
 218          $userlist1 = new \core_privacy\local\request\userlist($systemcontext, 'core_course');
 219          provider::add_userids_for_context($userlist1, 'courses');
 220          $this->assertCount(1, $userlist1->get_userids());
 221          // Verify that user2 is still in the list for system context.
 222          $expected = [$user2->id];
 223          $this->assertEquals($expected, $userlist1->get_userids());
 224          // Verify that the data of user1 was not deleted in the user1context.
 225          $userlist2 = new \core_privacy\local\request\userlist($user1context, 'core_course');
 226          provider::add_userids_for_context($userlist2, 'courses');
 227          $expected = [$user1->id];
 228          $this->assertEquals($expected, $userlist2->get_userids());
 229  
 230          // Now, delete the favourites for user2 only in the user1 context.
 231          // Make sure favourites are only being deleted in the right context.
 232          $approveduserlist = new \core_privacy\local\request\approved_userlist($user1context, 'core_course',
 233                  [$user2->id]);
 234          provider::delete_favourites_for_userlist($approveduserlist, 'courses');
 235          // Verify we have one userid in the list for system context.
 236          $userlist2 = new \core_privacy\local\request\userlist($systemcontext, 'core_course');
 237          provider::add_userids_for_context($userlist2, 'courses');
 238          $this->assertCount(1, $userlist2->get_userids());
 239          // Verify that user2 is still in the list for system context.
 240          $expected = [$user2->id];
 241          $this->assertEquals($expected, $userlist2->get_userids());
 242  
 243          // Verify that user1 is still present in the list for user1 context.
 244          $userlist3 = new \core_privacy\local\request\userlist($user1context, 'core_course');
 245          provider::add_userids_for_context($userlist3, 'courses');
 246          $this->assertCount(1, $userlist3->get_userids());
 247          // Verify that user1 is still in the list for user1 context.
 248          $expected = [$user1->id];
 249          $this->assertEquals($expected, $userlist3->get_userids());
 250      }
 251  
 252      /**
 253       * Test fetching the favourites data for a specified user in a specified component, item type and item ID.
 254       */
 255      public function test_get_favourites_info_for_user() {
 256          list($user1, $user2, $user1context, $user2context, $course1context, $course2context) = $this->set_up_courses_and_users();
 257  
 258          // Favourite 2 courses for user1 and 1 course for user2.
 259          $ufservice1 = \core_favourites\service_factory::get_service_for_user_context($user1context);
 260          $ufservice2 = \core_favourites\service_factory::get_service_for_user_context($user2context);
 261          $coursefavourite1 = $ufservice1->create_favourite('core_course', 'courses',
 262                  $course1context->instanceid, $course1context);
 263          $this->waitForSecond();
 264          $coursefavourite2 = $ufservice1->create_favourite('core_course', 'courses',
 265                  $course2context->instanceid, $course2context);
 266          $this->waitForSecond();
 267          $coursefavourite3 = $ufservice2->create_favourite('core_course', 'courses',
 268                  $course2context->instanceid, $course2context);
 269          $this->assertCount(2, $ufservice1->find_favourites_by_type('core_course', 'courses'));
 270          $this->assertCount(1, $ufservice2->find_favourites_by_type('core_course', 'courses'));
 271  
 272          // Get the favourites info for user1 in the course1 context.
 273          $favouriteinfo1 = (object) provider::get_favourites_info_for_user($user1->id, $course1context,
 274                  'core_course', 'courses', $course1context->instanceid);
 275          // Ensure the correct data has been returned.
 276          $this->assertEquals(transform::yesno(true), $favouriteinfo1->starred);
 277          $this->assertEquals('', $favouriteinfo1->ordering);
 278          $this->assertEquals(transform::datetime($coursefavourite1->timecreated), $favouriteinfo1->timecreated);
 279          $this->assertEquals(transform::datetime($coursefavourite1->timemodified), $favouriteinfo1->timemodified);
 280  
 281          // Get the favourites info for user1 in the course2 context.
 282          $favouriteinfo2 = (object) provider::get_favourites_info_for_user($user1->id, $course2context,
 283                  'core_course', 'courses', $course2context->instanceid);
 284          // Ensure the correct data has been returned.
 285          $this->assertEquals(transform::yesno(true), $favouriteinfo2->starred);
 286          $this->assertEquals('', $favouriteinfo2->ordering);
 287          $this->assertEquals(transform::datetime($coursefavourite2->timecreated), $favouriteinfo2->timecreated);
 288          $this->assertEquals(transform::datetime($coursefavourite2->timemodified), $favouriteinfo2->timemodified);
 289  
 290          // Get the favourites info for user2 in the course2 context.
 291          $favouriteinfo3 = (object) provider::get_favourites_info_for_user($user2->id, $course2context,
 292                  'core_course', 'courses', $course2context->instanceid);
 293          // Ensure the correct data has been returned.
 294          $this->assertEquals(transform::yesno(true), $favouriteinfo3->starred);
 295          $this->assertEquals('', $favouriteinfo3->ordering);
 296          $this->assertEquals(transform::datetime($coursefavourite3->timecreated), $favouriteinfo3->timecreated);
 297          $this->assertEquals(transform::datetime($coursefavourite3->timemodified), $favouriteinfo3->timemodified);
 298  
 299          // Get the favourites info for user2 in the course1 context (user2 has not favourited course1).
 300          $favouriteinfo4 = provider::get_favourites_info_for_user($user2->id, $course1context,
 301                  'core_course', 'courses', $course1context->instanceid);
 302          // Ensure that data has not been returned.
 303          $this->assertEmpty($favouriteinfo4);
 304      }
 305  }