Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

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