Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 311] [Versions 310 and 400] [Versions 310 and 401] [Versions 310 and 402] [Versions 310 and 403] [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   * Repository API unit tests
  19   *
  20   * @package   repository
  21   * @category  phpunit
  22   * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  global $CFG;
  29  require_once("$CFG->dirroot/repository/lib.php");
  30  
  31  class core_repositorylib_testcase extends advanced_testcase {
  32  
  33      /**
  34       * Installing repository tests
  35       *
  36       * @copyright 2012 Dongsheng Cai {@link http://dongsheng.org}
  37       */
  38      public function test_install_repository() {
  39          global $CFG, $DB;
  40  
  41          $this->resetAfterTest(true);
  42  
  43          $syscontext = context_system::instance();
  44          $repositorypluginname = 'boxnet';
  45          // override repository permission
  46          $capability = 'repository/' . $repositorypluginname . ':view';
  47          $guestroleid = $DB->get_field('role', 'id', array('shortname' => 'guest'));
  48          assign_capability($capability, CAP_ALLOW, $guestroleid, $syscontext->id, true);
  49  
  50          $plugintype = new repository_type($repositorypluginname);
  51          $pluginid = $plugintype->create(false);
  52          $this->assertIsInt($pluginid);
  53          $args = array();
  54          $args['type'] = $repositorypluginname;
  55          $repos = repository::get_instances($args);
  56          $repository = reset($repos);
  57          $this->assertInstanceOf('repository', $repository);
  58          $info = $repository->get_meta();
  59          $this->assertEquals($repositorypluginname, $info->type);
  60      }
  61  
  62      public function test_get_unused_filename() {
  63          global $USER;
  64  
  65          $this->resetAfterTest(true);
  66  
  67          $this->setAdminUser();
  68          $fs = get_file_storage();
  69  
  70          $draftitemid = null;
  71          $context = context_user::instance($USER->id);
  72          file_prepare_draft_area($draftitemid, $context->id, 'phpunit', 'test_get_unused_filename', 1);
  73  
  74          $dummy = array(
  75              'contextid' => $context->id,
  76              'component' => 'user',
  77              'filearea' => 'draft',
  78              'itemid' => $draftitemid,
  79              'filepath' => '/',
  80              'filename' => ''
  81          );
  82  
  83          // Create some files.
  84          $existingfiles = array(
  85              'test',
  86              'test.txt',
  87              'test (1).txt',
  88              'test1.txt',
  89              'test1 (1).txt',
  90              'test1 (2).txt',
  91              'test1 (3).txt',
  92              'test1 (My name is Bob).txt',
  93              'test2 (555).txt',
  94              'test3 (1000).txt',
  95              'test3 (1000MB).txt',
  96          );
  97          foreach ($existingfiles as $filename) {
  98              $dummy['filename'] = $filename;
  99              $file = $fs->create_file_from_string($dummy, 'blah! ' . $filename);
 100              $this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
 101          }
 102  
 103          // Actual testing.
 104          $this->assertEquals('free.txt', repository::get_unused_filename($draftitemid, '/', 'free.txt'));
 105          $this->assertEquals('test (1)', repository::get_unused_filename($draftitemid, '/', 'test'));
 106          $this->assertEquals('test (2).txt', repository::get_unused_filename($draftitemid, '/', 'test.txt'));
 107          $this->assertEquals('test1 (4).txt', repository::get_unused_filename($draftitemid, '/', 'test1.txt'));
 108          $this->assertEquals('test1 (8).txt', repository::get_unused_filename($draftitemid, '/', 'test1 (8).txt'));
 109          $this->assertEquals('test1 ().txt', repository::get_unused_filename($draftitemid, '/', 'test1 ().txt'));
 110          $this->assertEquals('test2 (556).txt', repository::get_unused_filename($draftitemid, '/', 'test2 (555).txt'));
 111          $this->assertEquals('test3 (1001).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000).txt'));
 112          $this->assertEquals('test3 (1000MB) (1).txt', repository::get_unused_filename($draftitemid, '/', 'test3 (1000MB).txt'));
 113          $this->assertEquals('test4 (1).txt', repository::get_unused_filename($draftitemid, '/', 'test4 (1).txt'));
 114      }
 115  
 116      public function test_draftfile_exists() {
 117          global $USER;
 118  
 119          $this->resetAfterTest(true);
 120  
 121          $this->setAdminUser();
 122          $fs = get_file_storage();
 123  
 124          $draftitemid = file_get_unused_draft_itemid();
 125          $context = context_user::instance($USER->id);
 126  
 127          $dummy = array(
 128              'contextid' => $context->id,
 129              'component' => 'user',
 130              'filearea' => 'draft',
 131              'itemid' => $draftitemid,
 132              'filepath' => '/',
 133              'filename' => ''
 134          );
 135  
 136          // Create some files.
 137          $existingfiles = array(
 138              'The Matrix.movie',
 139              'Astalavista.txt',
 140              'foobar',
 141          );
 142          foreach ($existingfiles as $filename) {
 143              $dummy['filename'] = $filename;
 144              $file = $fs->create_file_from_string($dummy, 'Content of ' . $filename);
 145              $this->assertInstanceOf('stored_file', $file);
 146          }
 147  
 148          // Doing the text.
 149          foreach ($existingfiles as $filename) {
 150              $this->assertTrue(repository::draftfile_exists($draftitemid, '/', $filename));
 151          }
 152          foreach (array('Terminator.movie', 'Where is Wally?', 'barfoo') as $filename) {
 153              $this->assertFalse(repository::draftfile_exists($draftitemid, '/', $filename));
 154          }
 155      }
 156  
 157      public function test_delete_selected_files() {
 158          global $USER;
 159  
 160          $this->resetAfterTest(true);
 161  
 162          $this->setAdminUser();
 163          $fs = get_file_storage();
 164  
 165          $draftitemid = file_get_unused_draft_itemid();
 166          $context = context_user::instance($USER->id);
 167  
 168          $dummy = [
 169              'contextid' => $context->id,
 170              'component' => 'user',
 171              'filearea' => 'draft',
 172              'itemid' => $draftitemid,
 173              'filepath' => '/',
 174              'filename' => ''
 175          ];
 176  
 177          // Create some files.
 178          $existingfiles = [
 179              'The Matrix.movie',
 180              'Astalavista.txt',
 181              'foobar',
 182          ];
 183  
 184          $selectedfiles = [
 185              'The Matrix.movie' => [],
 186              'Astalavista.txt' => []
 187          ];
 188          foreach ($existingfiles as $filename) {
 189              $dummy['filename'] = $filename;
 190              $file = $fs->create_file_from_string($dummy, 'Content of ' . $filename);
 191              if (array_key_exists($filename, $selectedfiles)) {
 192                  $selectedfiles[$filename] = (object)[
 193                      'filename' => $filename,
 194                      'filepath' => $file->get_filepath()
 195                  ];
 196              }
 197          }
 198  
 199          // Get area files with default options.
 200          $areafiles = $fs->get_area_files($context->id, 'user', 'draft', $draftitemid);
 201          // Should be the 3 files we added plus the folder.
 202          $this->assertEquals(4, count($areafiles));
 203  
 204          repository_delete_selected_files($context, 'user', 'draft', $draftitemid, $selectedfiles);
 205  
 206          $areafiles = $fs->get_area_files($context->id, 'user', 'draft', $draftitemid);
 207          // Should be the 1 file left plus the folder.
 208          $this->assertEquals(2, count($areafiles));
 209      }
 210  
 211      public function test_can_be_edited_by_user() {
 212          $this->resetAfterTest(true);
 213  
 214          $syscontext = context_system::instance();
 215          $course = $this->getDataGenerator()->create_course();
 216          $coursecontext = context_course::instance($course->id);
 217          $roleid = create_role('A role', 'arole', 'A role', '');
 218          $user = $this->getDataGenerator()->create_user();
 219          $this->setUser($user);
 220  
 221          // Instance on a site level.
 222          $this->getDataGenerator()->create_repository_type('flickr_public');
 223          $repoid = $this->getDataGenerator()->create_repository('flickr_public')->id;
 224          $systemrepo = repository::get_repository_by_id($repoid, $syscontext);
 225  
 226          role_assign($roleid, $user->id, $syscontext->id);
 227          assign_capability('moodle/site:config', CAP_ALLOW, $roleid, $syscontext, true);
 228          assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
 229          accesslib_clear_all_caches_for_unit_testing();
 230          $this->assertTrue($systemrepo->can_be_edited_by_user());
 231  
 232          assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $syscontext, true);
 233          assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
 234          accesslib_clear_all_caches_for_unit_testing();
 235          $this->assertFalse($systemrepo->can_be_edited_by_user());
 236  
 237          assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
 238          assign_capability('moodle/site:config', CAP_PROHIBIT, $roleid, $syscontext, true);
 239          accesslib_clear_all_caches_for_unit_testing();
 240          $this->assertFalse($systemrepo->can_be_edited_by_user());
 241  
 242          role_unassign($roleid, $user->id, $syscontext->id);
 243          accesslib_clear_all_caches_for_unit_testing();
 244  
 245          // Instance on a course level.
 246          $this->getDataGenerator()->enrol_user($user->id, $course->id, $roleid);
 247  
 248          $params = array('contextid' => $coursecontext->id);
 249          $repoid = $this->getDataGenerator()->create_repository('flickr_public', $params)->id;
 250          $courserepo = repository::get_repository_by_id($repoid, $coursecontext);
 251  
 252          assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
 253          assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $coursecontext, true);
 254          accesslib_clear_all_caches_for_unit_testing();
 255          $this->assertTrue($courserepo->can_be_edited_by_user());
 256  
 257          assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
 258          accesslib_clear_all_caches_for_unit_testing();
 259          $this->assertFalse($courserepo->can_be_edited_by_user());
 260  
 261          assign_capability('moodle/course:update', CAP_ALLOW, $roleid, $coursecontext, true);
 262          assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $coursecontext, true);
 263          accesslib_clear_all_caches_for_unit_testing();
 264          $this->assertFalse($courserepo->can_be_edited_by_user());
 265  
 266          role_unassign($roleid, $user->id, $coursecontext->id);
 267          accesslib_clear_all_caches_for_unit_testing();
 268  
 269          // Instance on a user level.
 270          $otheruser = $this->getDataGenerator()->create_user();
 271          $otherusercontext = context_user::instance($otheruser->id);
 272          role_assign($roleid, $user->id, $syscontext->id);
 273          assign_capability('repository/flickr_public:view', CAP_ALLOW, $roleid, $syscontext, true);
 274          accesslib_clear_all_caches_for_unit_testing();
 275  
 276          // Editing someone else's instance.
 277          $record = array('contextid' => $otherusercontext->id);
 278          $repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
 279          $userrepo = repository::get_repository_by_id($repoid, $syscontext);
 280          $this->assertFalse($userrepo->can_be_edited_by_user());
 281  
 282          // Editing my own instance.
 283          $usercontext = context_user::instance($user->id);
 284          $record = array('contextid' => $usercontext->id);
 285          $repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
 286          $userrepo = repository::get_repository_by_id($repoid, $syscontext);
 287          $this->assertTrue($userrepo->can_be_edited_by_user());
 288  
 289      }
 290  
 291      public function test_check_capability() {
 292          $this->resetAfterTest(true);
 293  
 294          $syscontext = context_system::instance();
 295          $course1 = $this->getDataGenerator()->create_course();
 296          $course1context = context_course::instance($course1->id);
 297          $course2 = $this->getDataGenerator()->create_course();
 298          $course2context = context_course::instance($course2->id);
 299  
 300          $forumdata = new stdClass();
 301          $forumdata->course = $course1->id;
 302          $forumc1 = $this->getDataGenerator()->create_module('forum', $forumdata);
 303          $forumc1context = context_module::instance($forumc1->cmid);
 304          $forumdata->course = $course2->id;
 305          $forumc2 = $this->getDataGenerator()->create_module('forum', $forumdata);
 306          $forumc2context = context_module::instance($forumc2->cmid);
 307  
 308          $blockdata = new stdClass();
 309          $blockdata->parentcontextid = $course1context->id;
 310          $blockc1 = $this->getDataGenerator()->create_block('online_users', $blockdata);
 311          $blockc1context = context_block::instance($blockc1->id);
 312          $blockdata->parentcontextid = $course2context->id;
 313          $blockc2 = $this->getDataGenerator()->create_block('online_users', $blockdata);
 314          $blockc2context = context_block::instance($blockc2->id);
 315  
 316          $user1 = $this->getDataGenerator()->create_user();
 317          $user1context = context_user::instance($user1->id);
 318          $user2 = $this->getDataGenerator()->create_user();
 319          $user2context = context_user::instance($user2->id);
 320  
 321          // New role prohibiting Flickr Public access.
 322          $roleid = create_role('No Flickr Public', 'noflickrpublic', 'No Flickr Public', '');
 323          assign_capability('repository/flickr_public:view', CAP_PROHIBIT, $roleid, $syscontext, true);
 324  
 325          // Disallow system access to Flickr Public to user 2.
 326          role_assign($roleid, $user2->id, $syscontext->id);
 327          accesslib_clear_all_caches_for_unit_testing();
 328  
 329          // Enable repositories.
 330          $this->getDataGenerator()->create_repository_type('flickr_public');
 331          $this->getDataGenerator()->create_repository_type('dropbox');
 332  
 333          // Instance on a site level.
 334          $repoid = $this->getDataGenerator()->create_repository('flickr_public')->id;
 335          $systemrepo = repository::get_repository_by_id($repoid, $syscontext);
 336  
 337          // Check that everyone with right capability can view a site-wide repository.
 338          $this->setUser($user1);
 339          $this->assertTrue($systemrepo->check_capability());
 340  
 341          // Without the capability, we cannot view a site-wide repository.
 342          $this->setUser($user2);
 343          $caughtexception = false;
 344          try {
 345              $systemrepo->check_capability();
 346          } catch (repository_exception $e) {
 347              $caughtexception = true;
 348          }
 349          $this->assertTrue($caughtexception);
 350  
 351          // Instance on a course level.
 352          $record = new stdClass();
 353          $record->contextid = $course1context->id;
 354          $courserepoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
 355  
 356          // Within the course, I can view the repository.
 357          $courserepo = repository::get_repository_by_id($courserepoid, $course1context);
 358          $this->setUser($user1);
 359          $this->assertTrue($courserepo->check_capability());
 360          // But not without the capability.
 361          $this->setUser($user2);
 362          $caughtexception = false;
 363          try {
 364              $courserepo->check_capability();
 365          } catch (repository_exception $e) {
 366              $caughtexception = true;
 367          }
 368          $this->assertTrue($caughtexception);
 369  
 370          // From another course I cannot, with or without the capability.
 371          $courserepo = repository::get_repository_by_id($courserepoid, $course2context);
 372          $this->setUser($user1);
 373          $caughtexception = false;
 374          try {
 375              $courserepo->check_capability();
 376          } catch (repository_exception $e) {
 377              $caughtexception = true;
 378          }
 379          $this->assertTrue($caughtexception);
 380          $this->setUser($user2);
 381          $caughtexception = false;
 382          try {
 383              $courserepo->check_capability();
 384          } catch (repository_exception $e) {
 385              $caughtexception = true;
 386          }
 387          $this->assertTrue($caughtexception);
 388  
 389          // From a module within the course, I can view the repository.
 390          $courserepo = repository::get_repository_by_id($courserepoid, $forumc1context);
 391          $this->setUser($user1);
 392          $this->assertTrue($courserepo->check_capability());
 393          // But not without the capability.
 394          $this->setUser($user2);
 395          $caughtexception = false;
 396          try {
 397              $courserepo->check_capability();
 398          } catch (repository_exception $e) {
 399              $caughtexception = true;
 400          }
 401          $this->assertTrue($caughtexception);
 402  
 403          // From a module in the wrong course, I cannot view the repository.
 404          $courserepo = repository::get_repository_by_id($courserepoid, $forumc2context);
 405          $this->setUser($user1);
 406          $caughtexception = false;
 407          try {
 408              $courserepo->check_capability();
 409          } catch (repository_exception $e) {
 410              $caughtexception = true;
 411          }
 412          $this->assertTrue($caughtexception);
 413  
 414          // From a block within the course, I can view the repository.
 415          $courserepo = repository::get_repository_by_id($courserepoid, $blockc1context);
 416          $this->setUser($user1);
 417          $this->assertTrue($courserepo->check_capability());
 418          // But not without the capability.
 419          $this->setUser($user2);
 420          $caughtexception = false;
 421          try {
 422              $courserepo->check_capability();
 423          } catch (repository_exception $e) {
 424              $caughtexception = true;
 425          }
 426          $this->assertTrue($caughtexception);
 427  
 428          // From a block in the wrong course, I cannot view the repository.
 429          $courserepo = repository::get_repository_by_id($courserepoid, $blockc2context);
 430          $this->setUser($user1);
 431          $caughtexception = false;
 432          try {
 433              $courserepo->check_capability();
 434          } catch (repository_exception $e) {
 435              $caughtexception = true;
 436          }
 437          $this->assertTrue($caughtexception);
 438  
 439          // Instance on a user level.
 440          // Instance on a course level.
 441          $record = new stdClass();
 442          $record->contextid = $user1context->id;
 443          $user1repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
 444          $record->contextid = $user2context->id;
 445          $user2repoid = $this->getDataGenerator()->create_repository('flickr_public', $record)->id;
 446  
 447          // Check that a user can see its own repository.
 448          $userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
 449          $this->setUser($user1);
 450          $this->assertTrue($userrepo->check_capability());
 451          // But not without the capability.
 452          $userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
 453          $this->setUser($user2);
 454          $caughtexception = false;
 455          try {
 456              $userrepo->check_capability();
 457          } catch (repository_exception $e) {
 458              $caughtexception = true;
 459          }
 460          $this->assertTrue($caughtexception);
 461  
 462          // Check that a user cannot see someone's repository.
 463          $userrepo = repository::get_repository_by_id($user2repoid, $syscontext);
 464          $this->setUser($user1);
 465          $caughtexception = false;
 466          try {
 467              $userrepo->check_capability();
 468          } catch (repository_exception $e) {
 469              $caughtexception = true;
 470          }
 471          $this->assertTrue($caughtexception);
 472          // Make sure the repo from user 2 was accessible.
 473          role_unassign($roleid, $user2->id, $syscontext->id);
 474          accesslib_clear_all_caches_for_unit_testing();
 475          $this->setUser($user2);
 476          $this->assertTrue($userrepo->check_capability());
 477          role_assign($roleid, $user2->id, $syscontext->id);
 478          accesslib_clear_all_caches_for_unit_testing();
 479  
 480          // Check that a user can view SOME repositories when logged in as someone else.
 481          $params = new stdClass();
 482          $params->name = 'Dropbox';
 483          $params->dropbox_key = 'key';
 484          $params->dropbox_secret = 'secret';
 485          $privaterepoid = $this->getDataGenerator()->create_repository('dropbox')->id;
 486          $notprivaterepoid = $this->getDataGenerator()->create_repository('upload')->id;
 487  
 488          $privaterepo = repository::get_repository_by_id($privaterepoid, $syscontext);
 489          $notprivaterepo = repository::get_repository_by_id($notprivaterepoid, $syscontext);
 490          $userrepo = repository::get_repository_by_id($user1repoid, $syscontext);
 491  
 492          $this->setAdminUser();
 493          \core\session\manager::loginas($user1->id, $syscontext);
 494  
 495          // Logged in as, I cannot view a user instance.
 496          $caughtexception = false;
 497          try {
 498              $userrepo->check_capability();
 499          } catch (repository_exception $e) {
 500              $caughtexception = true;
 501          }
 502          $this->assertTrue($caughtexception);
 503  
 504          // Logged in as, I cannot view a private instance.
 505          $caughtexception = false;
 506          try {
 507              $privaterepo->check_capability();
 508          } catch (repository_exception $e) {
 509              $caughtexception = true;
 510          }
 511          $this->assertTrue($caughtexception);
 512  
 513          // Logged in as, I can view a non-private instance.
 514          $this->assertTrue($notprivaterepo->check_capability());
 515      }
 516  
 517      function test_delete_all_for_context() {
 518          global $DB;
 519          $this->resetAfterTest(true);
 520  
 521          $this->setAdminUser();
 522          $course = $this->getDataGenerator()->create_course();
 523          $user = $this->getDataGenerator()->create_user();
 524          $this->getDataGenerator()->create_repository_type('flickr_public');
 525          $this->getDataGenerator()->create_repository_type('filesystem');
 526          $coursecontext = context_course::instance($course->id);
 527          $usercontext = context_user::instance($user->id);
 528  
 529          // Creating course instances.
 530          $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
 531          $courserepo1 = repository::get_repository_by_id($repo->id, $coursecontext);
 532          $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
 533  
 534          $repo = $this->getDataGenerator()->create_repository('filesystem', array('contextid' => $coursecontext->id));
 535          $courserepo2 = repository::get_repository_by_id($repo->id, $coursecontext);
 536          $this->assertEquals(2, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
 537  
 538          // Creating user instances.
 539          $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
 540          $userrepo1 = repository::get_repository_by_id($repo->id, $usercontext);
 541          $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
 542  
 543          $repo = $this->getDataGenerator()->create_repository('filesystem', array('contextid' => $usercontext->id));
 544          $userrepo2 = repository::get_repository_by_id($repo->id, $usercontext);
 545          $this->assertEquals(2, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
 546  
 547          // Simulation of course deletion.
 548          repository::delete_all_for_context($coursecontext->id);
 549          $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
 550          $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $courserepo1->id)));
 551          $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $courserepo2->id)));
 552          $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $courserepo1->id)));
 553          $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $courserepo2->id)));
 554  
 555          // Simulation of user deletion.
 556          repository::delete_all_for_context($usercontext->id);
 557          $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
 558          $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $userrepo1->id)));
 559          $this->assertEquals(0, $DB->count_records('repository_instances', array('id' => $userrepo2->id)));
 560          $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $userrepo1->id)));
 561          $this->assertEquals(0, $DB->count_records('repository_instance_config', array('instanceid' => $userrepo2->id)));
 562  
 563          // Checking deletion upon course context deletion.
 564          $course = $this->getDataGenerator()->create_course();
 565          $coursecontext = context_course::instance($course->id);
 566          $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
 567          $courserepo = repository::get_repository_by_id($repo->id, $coursecontext);
 568          $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
 569          $coursecontext->delete();
 570          $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
 571  
 572          // Checking deletion upon user context deletion.
 573          $user = $this->getDataGenerator()->create_user();
 574          $usercontext = context_user::instance($user->id);
 575          $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
 576          $userrepo = repository::get_repository_by_id($repo->id, $usercontext);
 577          $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
 578          $usercontext->delete();
 579          $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
 580  
 581          // Checking deletion upon course deletion.
 582          $course = $this->getDataGenerator()->create_course();
 583          $coursecontext = context_course::instance($course->id);
 584          $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $coursecontext->id));
 585          $courserepo = repository::get_repository_by_id($repo->id, $coursecontext);
 586          $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
 587          delete_course($course, false);
 588          $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $coursecontext->id)));
 589  
 590          // Checking deletion upon user deletion.
 591          $user = $this->getDataGenerator()->create_user();
 592          $usercontext = context_user::instance($user->id);
 593          $repo = $this->getDataGenerator()->create_repository('flickr_public', array('contextid' => $usercontext->id));
 594          $userrepo = repository::get_repository_by_id($repo->id, $usercontext);
 595          $this->assertEquals(1, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
 596          delete_user($user);
 597          $this->assertEquals(0, $DB->count_records('repository_instances', array('contextid' => $usercontext->id)));
 598      }
 599  }