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