Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.3.x will end 7 October 2024 (12 months).
  • Bug fixes for security issues in 4.3.x will end 21 April 2025 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.2.x is supported too.
   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   * Resource search unit tests.
  19   *
  20   * @package     mod_resource
  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  namespace mod_resource\search;
  27  
  28  defined('MOODLE_INTERNAL') || die();
  29  
  30  global $CFG;
  31  require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
  32  
  33  /**
  34   * Provides the unit tests for forum search.
  35   *
  36   * @package     mod_resource
  37   * @category    test
  38   * @copyright   2016 Eric Merrill {@link http://www.merrilldigital.com}
  39   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  40   */
  41  class search_test extends \advanced_testcase {
  42  
  43      /**
  44       * @var string Area id
  45       */
  46      protected $resourceareaid = null;
  47  
  48      public function setUp(): void {
  49          $this->resetAfterTest(true);
  50          set_config('enableglobalsearch', true);
  51  
  52          $this->resourceareaid = \core_search\manager::generate_areaid('mod_resource', 'activity');
  53  
  54          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  55          $search = \testable_core_search::instance();
  56      }
  57  
  58      /**
  59       * Test for resource file attachments.
  60       *
  61       * @return void
  62       */
  63      public function test_attach_files() {
  64          global $USER;
  65  
  66          $this->setAdminUser();
  67          // Setup test data.
  68          $course = $this->getDataGenerator()->create_course();
  69  
  70          $fs = get_file_storage();
  71          $usercontext = \context_user::instance($USER->id);
  72  
  73          $record = new \stdClass();
  74          $record->course = $course->id;
  75          $record->files = file_get_unused_draft_itemid();
  76  
  77          // Attach the main file. We put them in the draft area, create_module will move them.
  78          $filerecord = array(
  79              'contextid' => $usercontext->id,
  80              'component' => 'user',
  81              'filearea'  => 'draft',
  82              'itemid'    => $record->files,
  83              'filepath'  => '/',
  84              'filename'  => 'mainfile',
  85              'sortorder' => 1
  86          );
  87          $fs->create_file_from_string($filerecord, 'Test resource file');
  88  
  89          // Attach a second file.
  90          $filerecord['filename'] = 'extrafile';
  91          $filerecord['sortorder'] = 0;
  92          $fs->create_file_from_string($filerecord, 'Test resource file 2');
  93  
  94          $resource = $this->getDataGenerator()->create_module('resource', $record);
  95  
  96          $searcharea = \core_search\manager::get_search_area($this->resourceareaid);
  97          $this->assertInstanceOf('\mod_resource\search\activity', $searcharea);
  98  
  99          $recordset = $searcharea->get_recordset_by_timestamp(0);
 100          $nrecords = 0;
 101          foreach ($recordset as $record) {
 102              $doc = $searcharea->get_document($record);
 103              $searcharea->attach_files($doc);
 104              $files = $doc->get_files();
 105  
 106              // Resources should return all added files.
 107              $this->assertCount(2, $files);
 108  
 109              $filenames = array();
 110              foreach ($files as $file) {
 111                  $filenames[] = $file->get_filename();
 112              }
 113              $this->assertContains('mainfile', $filenames);
 114              $this->assertContains('extrafile', $filenames);
 115  
 116              $nrecords++;
 117          }
 118  
 119          $recordset->close();
 120          $this->assertEquals(1, $nrecords);
 121      }
 122  
 123  }