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 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   * Folder search unit tests.
  19   *
  20   * @package     mod_folder
  21   * @category    test
  22   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  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 . '/search/tests/fixtures/testable_core_search.php');
  30  
  31  /**
  32   * Provides the unit tests for forum search.
  33   *
  34   * @package     mod_folder
  35   * @category    test
  36   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  37   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  38   */
  39  class mod_folder_search_testcase extends advanced_testcase {
  40  
  41      /**
  42       * @var string Area id
  43       */
  44      protected $folderareaid = null;
  45  
  46      public function setUp(): void {
  47          $this->resetAfterTest(true);
  48          set_config('enableglobalsearch', true);
  49  
  50          $this->folderareaid = \core_search\manager::generate_areaid('mod_folder', 'activity');
  51  
  52          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  53          $search = testable_core_search::instance();
  54      }
  55  
  56      /**
  57       * Test for folder file attachments.
  58       *
  59       * @return void
  60       */
  61      public function test_attach_files() {
  62          global $USER;
  63  
  64          $this->setAdminUser();
  65          // Setup test data.
  66          $course = $this->getDataGenerator()->create_course();
  67  
  68          $fs = get_file_storage();
  69          $usercontext = context_user::instance($USER->id);
  70  
  71          $record = new stdClass();
  72          $record->course = $course->id;
  73          $record->files = file_get_unused_draft_itemid();
  74  
  75          // Attach the main file. We put them in the draft area, create_module will move them.
  76          $filerecord = array(
  77              'contextid' => $usercontext->id,
  78              'component' => 'user',
  79              'filearea'  => 'draft',
  80              'itemid'    => $record->files,
  81              'filepath'  => '/'
  82          );
  83  
  84          // Attach 4 files.
  85          for ($i = 1; $i <= 4; $i++) {
  86              $filerecord['filename'] = 'myfile'.$i;
  87              $fs->create_file_from_string($filerecord, 'Test folder file '.$i);
  88          }
  89  
  90          // And a fifth in a sub-folder.
  91          $filerecord['filename'] = 'myfile5';
  92          $filerecord['filepath'] = '/subfolder/';
  93          $fs->create_file_from_string($filerecord, 'Test folder file 5');
  94  
  95          $this->getDataGenerator()->create_module('folder', $record);
  96  
  97          // Returns the instance as long as the area is supported.
  98          $searcharea = \core_search\manager::get_search_area($this->folderareaid);
  99          $this->assertInstanceOf('\mod_folder\search\activity', $searcharea);
 100  
 101          $recordset = $searcharea->get_recordset_by_timestamp(0);
 102          $nrecords = 0;
 103          foreach ($recordset as $record) {
 104              $doc = $searcharea->get_document($record);
 105              $searcharea->attach_files($doc);
 106              $files = $doc->get_files();
 107  
 108              // Folder should return all files attached.
 109              $this->assertCount(5, $files);
 110  
 111              // We don't know the order, so get all the names, then sort, then check.
 112              $filenames = array();
 113              foreach ($files as $file) {
 114                  $filenames[] = $file->get_filename();
 115              }
 116              sort($filenames);
 117  
 118              for ($i = 1; $i <= 5; $i++) {
 119                  $this->assertEquals('myfile'.$i, $filenames[($i - 1)]);
 120              }
 121  
 122              $nrecords++;
 123          }
 124  
 125          // If there would be an error/failure in the foreach above the recordset would be closed on shutdown.
 126          $recordset->close();
 127          $this->assertEquals(1, $nrecords);
 128      }
 129  
 130  }