Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 4.2.x will end 22 April 2024 (12 months).
  • Bug fixes for security issues in 4.2.x will end 7 October 2024 (18 months).
  • PHP version: minimum PHP 8.0.0 Note: minimum PHP version has increased since Moodle 4.1. PHP 8.1.x is supported too.

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

   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  namespace core_search;
  18  
  19  defined('MOODLE_INTERNAL') || die();
  20  
  21  global $CFG;
  22  require_once (__DIR__ . '/fixtures/testable_core_search.php');
  23  require_once($CFG->dirroot . '/search/tests/fixtures/mock_search_area.php');
  24  
  25  /**
  26   * Search engine base unit tests.
  27   *
  28   * @package     core_search
  29   * @copyright   2017 Matt Porritt <mattp@catalyst-au.net>
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class base_test extends \advanced_testcase {
  33      /**
  34       * @var \core_search::manager
  35       */
  36      protected $search = null;
  37  
  38      /**
  39       * @var Instace of core_search_generator.
  40       */
  41      protected $generator = null;
  42  
  43      /**
  44       * @var Instace of testable_engine.
  45       */
  46      protected $engine = null;
  47  
  48      public function setUp(): void {
  49          $this->resetAfterTest();
  50          set_config('enableglobalsearch', true);
  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          $this->generator = self::getDataGenerator()->get_plugin_generator('core_search');
  56          $this->generator->setup();
  57      }
  58  
  59      public function tearDown(): void {
  60          // For unit tests before PHP 7, teardown is called even on skip. So only do our teardown if we did setup.
  61          if ($this->generator) {
  62              // Moodle DML freaks out if we don't teardown the temp table after each run.
  63              $this->generator->teardown();
  64              $this->generator = null;
  65          }
  66      }
  67  
  68      /**
  69       * Test base get search fileareas
  70       */
  71      public function test_get_search_fileareas_base() {
  72  
  73          $builder = $this->getMockBuilder('\core_search\base');
  74          $builder->disableOriginalConstructor();
  75          $stub = $builder->getMockForAbstractClass();
  76  
  77          $result = $stub->get_search_fileareas();
  78  
  79          $this->assertEquals(array(), $result);
  80      }
  81  
  82      /**
  83       * Test base attach files
  84       */
  85      public function test_attach_files_base() {
  86          $filearea = 'search';
  87          $component = 'mod_test';
  88  
  89          // Create file to add.
  90          $fs = get_file_storage();
  91          $filerecord = array(
  92                  'contextid' => 1,
  93                  'component' => $component,
  94                  'filearea' => $filearea,
  95                  'itemid' => 1,
  96                  'filepath' => '/',
  97                  'filename' => 'testfile.txt');
  98          $content = 'All the news that\'s fit to print';
  99          $file = $fs->create_file_from_string($filerecord, $content);
 100  
 101          // Construct the search document.
 102          $rec = new \stdClass();
 103          $rec->contextid = 1;
 104          $area = new \core_mocksearch\search\mock_search_area();
 105          $record = $this->generator->create_record($rec);
 106          $document = $area->get_document($record);
 107  
 108          // Create a mock from the abstract class,
 109          // with required methods stubbed.
 110          $builder = $this->getMockBuilder('\core_search\base');
 111          $builder->disableOriginalConstructor();
 112          $builder->onlyMethods(array('get_search_fileareas', 'get_component_name'));
 113          $stub = $builder->getMockForAbstractClass();
 114          $stub->method('get_search_fileareas')->willReturn(array($filearea));
 115          $stub->method('get_component_name')->willReturn($component);
 116  
 117          // Attach file to our test document.
 118          $stub->attach_files($document);
 119  
 120          // Verify file is attached.
 121          $files = $document->get_files();
 122          $file = array_values($files)[0];
 123  
 124          $this->assertEquals(1, count($files));
 125          $this->assertEquals($content, $file->get_content());
 126      }
 127  
 128      /**
 129       * Tests the base version (stub) of get_contexts_to_reindex.
 130       */
 131      public function test_get_contexts_to_reindex() {
 132          $area = new \core_mocksearch\search\mock_search_area();
 133          $this->assertEquals([\context_system::instance()],
 134                  iterator_to_array($area->get_contexts_to_reindex(), false));
 135      }
 136  
 137      /**
 138       * Test default document icon.
 139       */
 140      public function test_get_default_doc_icon() {
 141          $basearea = $this->getMockBuilder('\core_search\base')
 142              ->disableOriginalConstructor()
 143              ->getMockForAbstractClass();
 144  
 145          $document = $this->getMockBuilder('\core_search\document')
 146              ->disableOriginalConstructor()
 147              ->getMock();
 148  
 149          $result = $basearea->get_doc_icon($document);
 150  
 151          $this->assertEquals('i/empty', $result->get_name());
 152          $this->assertEquals('moodle', $result->get_component());
 153      }
 154  
 155      /**
 156       * Test base search area category names.
 157       */
 158      public function test_get_category_names() {
 159          $builder = $this->getMockBuilder('\core_search\base');
 160          $builder->disableOriginalConstructor();
 161          $stub = $builder->getMockForAbstractClass();
 162  
 163          $expected = ['core-other'];
 164          $this->assertEquals($expected, $stub->get_category_names());
 165      }
 166  
 167      /**
 168       * Test getting all required search area setting names.
 169       */
 170      public function test_get_settingnames() {
 171          $expected = array('_enabled', '_indexingstart', '_indexingend', '_lastindexrun',
 172              '_docsignored', '_docsprocessed', '_recordsprocessed', '_partial');
 173          $this->assertEquals($expected, \core_search\base::get_settingnames());
 174      }
 175  }