Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 311] [Versions 311 and 402] [Versions 311 and 403] [Versions 39 and 311]

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