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   * Unit test for recent repository
  19   *
  20   * @package repository_recent
  21   *
  22   * @author  Nathan Nguyen <nathannguyen@catalyst-au.net>
  23   * @copyright  Catalyst IT
  24   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  25   */
  26  
  27  defined('MOODLE_INTERNAL') || die();
  28  
  29  global $CFG;
  30  require_once($CFG->dirroot . '/repository/lib.php');
  31  require_once($CFG->dirroot . '/files/externallib.php');
  32  /**
  33   * Unit test for recent repository
  34   *
  35   * @package repository_recent
  36   *
  37   * @author  Nathan Nguyen <nathannguyen@catalyst-au.net>
  38   * @copyright  Catalyst IT
  39   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class repository_recent_lib_testcase extends advanced_testcase {
  42  
  43      /** @var repository Recent repository */
  44      private $repo;
  45  
  46      /** @var context repository */
  47      private $usercontext;
  48  
  49      /**
  50       * SetUp to create an repository instance.
  51       */
  52      protected function setUp(): void {
  53          global $USER;
  54          $this->setAdminUser();
  55          $this->usercontext = context_user::instance($USER->id);
  56          $repoid = $this->getDataGenerator()->create_repository('recent')->id;
  57          $this->repo = repository::get_repository_by_id($repoid, $this->usercontext);
  58      }
  59  
  60      /**
  61       * Test get listing
  62       */
  63      public function test_get_listing_with_duplicate_file() {
  64          global $itemid;
  65          $this->resetAfterTest(true);
  66  
  67          // Set global itemid for draft file (file manager mockup).
  68          $itemid = file_get_unused_draft_itemid();
  69  
  70          // No recent file.
  71          $filelist = $this->repo->get_listing()['list'];
  72          $this->assertCount(0, $filelist);
  73  
  74          // Create test file 1.
  75          $this->create_test_file('TestFile1', 'draft', $itemid);
  76          $filelist = $this->repo->get_listing()['list'];
  77          $this->assertCount(1, $filelist);
  78  
  79          // Due to create_test_file function, same filename means same content as the content is the filename hash.
  80          $this->create_test_file('TestFile1', 'private');
  81          $filelist = $this->repo->get_listing()['list'];
  82          $this->assertCount(1, $filelist);
  83  
  84          // Create test file 2, different area.
  85          $this->create_test_file('TestFile2', 'private');
  86          $filelist = $this->repo->get_listing()['list'];
  87          $this->assertCount(2, $filelist);
  88      }
  89  
  90      /**
  91       * Test get listing reference file
  92       */
  93      public function test_get_listing_with_reference_file() {
  94          $this->resetAfterTest(true);
  95          // Create test file 1.
  96          $file1 = $this->create_test_file('TestFile1', 'private');
  97          $filelist = $this->repo->get_listing()['list'];
  98          $this->assertCount(1, $filelist);
  99  
 100          // Create reference file.
 101          $file2 = $this->create_reference_file($file1, 'TestFile2', 'private');
 102          $filelist = $this->repo->get_listing()['list'];
 103          $this->assertCount(1, $filelist);
 104  
 105          // Delete reference.
 106          $file2->delete_reference();
 107          $filelist = $this->repo->get_listing()['list'];
 108          $this->assertCount(2, $filelist);
 109      }
 110  
 111      /**
 112       * Test number limit
 113       */
 114      public function test_get_listing_number_limit() {
 115          $this->resetAfterTest(true);
 116          $this->create_multiple_test_files('private', 75);
 117          $filelist = $this->repo->get_listing()['list'];
 118          $this->assertCount(50, $filelist);
 119  
 120          // The number limit is set as property of the repo, so we need to create new repo instance.
 121          set_config('recentfilesnumber', 100, 'recent');
 122          $repoid = $this->getDataGenerator()->create_repository('recent')->id;
 123          $repo = repository::get_repository_by_id($repoid, $this->usercontext);
 124          $filelist = $repo->get_listing()['list'];
 125          $this->assertCount(75, $filelist);
 126      }
 127  
 128      /**
 129       * Test time limit
 130       */
 131      public function test_get_listing_time_limit() {
 132          $this->resetAfterTest(true);
 133          $this->create_multiple_test_files('private', 25);
 134          $file1 = $this->create_test_file('TestFileTimeLimit', 'private');
 135          // Set time modified back to a year ago.
 136          $file1->set_timemodified(time() - YEARSECS);
 137  
 138          // There is no time limit by default.
 139          $filelist = $this->repo->get_listing()['list'];
 140          $this->assertCount(26, $filelist);
 141  
 142          // The time limit is set as property of the repo, so we need to create new repo instance.
 143          set_config('recentfilestimelimit', 3600, 'recent');
 144          $repoid = $this->getDataGenerator()->create_repository('recent')->id;
 145          $repo = repository::get_repository_by_id($repoid, $this->usercontext);
 146          $filelist = $repo->get_listing()['list'];
 147          // Only get the recent files in the last hour.
 148          $this->assertCount(25, $filelist);
 149      }
 150  
 151      /**
 152       * Create multiple test file
 153       *
 154       * @param string $filearea file area
 155       * @param int $numberoffiles number of files to be created
 156       */
 157      private function create_multiple_test_files($filearea, $numberoffiles) {
 158          for ($i = 0; $i < $numberoffiles; ++$i) {
 159              $filename = "TestFile$i" . time();
 160              $this->create_test_file($filename, $filearea);
 161          }
 162      }
 163  
 164      /**
 165       * Create test file
 166       *
 167       * @param string $filename file name
 168       * @param string $filearea file area
 169       * @param int $itemid item id
 170       * @return stored_file the newly created file
 171       */
 172      private function create_test_file($filename, $filearea, $itemid = 0) {
 173          global $USER;
 174  
 175          $filerecord = array();
 176          $filerecord['contextid'] = $this->usercontext->id;
 177          $filerecord['component'] = 'user';
 178          $filerecord['filearea'] = $filearea;
 179          $filerecord['itemid'] = $itemid;
 180          $filerecord['filepath'] = '/';
 181          $filerecord['filename'] = $filename;
 182          $filerecord['userid'] = $USER->id;
 183  
 184          $fs = get_file_storage();
 185          $content = hash("md5", $filename);
 186          return $fs->create_file_from_string($filerecord, $content);
 187      }
 188  
 189      /**
 190       * Create reference file
 191       *
 192       * @param stored_file $file source file
 193       * @param string $filename file name
 194       * @param string $filearea file area
 195       * @param int $itemid item id
 196       * @return stored_file the newly created file
 197       */
 198      private function create_reference_file($file, $filename, $filearea, $itemid = 0) {
 199          global $USER, $DB;
 200  
 201          $newfilerecord = array();
 202          $newfilerecord['contextid'] = $this->usercontext->id;
 203          $newfilerecord['component'] = 'user';
 204          $newfilerecord['filearea'] = $filearea;
 205          $newfilerecord['itemid'] = $itemid;
 206          $newfilerecord['filepath'] = '/';
 207          $newfilerecord['filename'] = $filename;
 208          $newfilerecord['userid'] = $USER->id;
 209  
 210          $fs = get_file_storage();
 211          $oldfilerecord = $DB->get_record('files', ['id' => $file->get_id()]);
 212          $ref = $fs->pack_reference($oldfilerecord);
 213          return $fs->create_file_from_reference($newfilerecord, $this->repo->id, $ref);
 214      }
 215  }