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.

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

   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  require_once (__DIR__ . '/fixtures/testable_core_search.php');
  22  require_once (__DIR__ . '/fixtures/mock_search_area.php');
  23  
  24  /**
  25   * Search engine base unit tests.
  26   *
  27   * @package     core_search
  28   * @category    phpunit
  29   * @copyright   2015 David Monllao {@link http://www.davidmonllao.com}
  30   * @license     http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  31   */
  32  class engine_test extends \advanced_testcase {
  33  
  34      public function setUp(): void {
  35          $this->resetAfterTest();
  36          set_config('enableglobalsearch', true);
  37  
  38          // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
  39          $search = \testable_core_search::instance();
  40      }
  41  
  42      /**
  43       * Engine basic info.
  44       *
  45       * @return void
  46       */
  47      public function test_engine_info() {
  48          $engine = new \mock_search\engine();
  49  
  50          $this->assertEquals('mock_search', $engine->get_plugin_name());
  51  
  52          // Resolves to the default one.
  53          $this->assertEquals('\\core_search\\document', $engine->get_document_classname());
  54      }
  55  
  56      /**
  57       * Test engine caches.
  58       *
  59       * @return void
  60       */
  61      public function test_engine_caches() {
  62          global $DB;
  63  
  64          $engine = new \mock_search\engine();
  65  
  66          $course1 = self::getDataGenerator()->create_course();
  67  
  68          $this->assertEquals($course1->id, $engine->get_course($course1->id)->id);
  69          $dbreads = $DB->perf_get_reads();
  70          $engine->get_course($course1->id);
  71          $this->assertEquals($dbreads, $DB->perf_get_reads());
  72          $fakearea1 = \core_search\manager::generate_areaid('plugintype_unexisting', 'fakearea');
  73          $fakearea2 = \core_search\manager::generate_areaid('mod_unexisting', 'morefake');
  74          $this->assertFalse($engine->get_search_area($fakearea1));
  75          $this->assertFalse($engine->get_search_area($fakearea2));
  76          $this->assertFalse($engine->get_search_area($fakearea2));
  77  
  78          $areaid = \core_search\manager::generate_areaid('mod_forum', 'post');
  79          $this->assertInstanceOf('\\mod_forum\\search\\post', $engine->get_search_area($areaid));
  80          $dbreads = $DB->perf_get_reads();
  81          $this->assertInstanceOf('\\mod_forum\\search\\post', $engine->get_search_area($areaid));
  82          $this->assertEquals($dbreads, $DB->perf_get_reads());
  83  
  84      }
  85  
  86      /**
  87       * Tests the core functions related to schema updates.
  88       */
  89      public function test_engine_schema_modification() {
  90          // Apply a schema update starting from no version.
  91          $engine = new \mock_search\engine();
  92          $engine->check_latest_schema();
  93          $updates = $engine->get_and_clear_schema_updates();
  94          $this->assertCount(1, $updates);
  95          $this->assertEquals(0, $updates[0][0]);
  96          $this->assertEquals(\core_search\document::SCHEMA_VERSION, $updates[0][1]);
  97  
  98          // Store older version and check that.
  99          $engine->record_applied_schema_version(1066101400);
 100  
 101          $engine = new \mock_search\engine();
 102          $engine->check_latest_schema();
 103          $updates = $engine->get_and_clear_schema_updates();
 104          $this->assertCount(1, $updates);
 105          $this->assertEquals(1066101400, $updates[0][0]);
 106          $this->assertEquals(\core_search\document::SCHEMA_VERSION, $updates[0][1]);
 107  
 108          // Store current version and check no updates.
 109          $engine->record_applied_schema_version(\core_search\document::SCHEMA_VERSION);
 110  
 111          $engine = new \mock_search\engine();
 112          $engine->check_latest_schema();
 113          $updates = $engine->get_and_clear_schema_updates();
 114          $this->assertCount(0, $updates);
 115      }
 116  
 117      /**
 118       * Tests the get_supported_orders stub function.
 119       */
 120      public function test_get_supported_orders() {
 121          $engine = new \mock_search\engine();
 122          $orders = $engine->get_supported_orders(\context_system::instance());
 123          $this->assertCount(1, $orders);
 124          $this->assertArrayHasKey('relevance', $orders);
 125      }
 126  
 127      /**
 128       * Test that search engine sets an icon before render a document.
 129       */
 130      public function test_engine_sets_doc_icon() {
 131          $generator = self::getDataGenerator()->get_plugin_generator('core_search');
 132          $generator->setup();
 133  
 134          $area = new \core_mocksearch\search\mock_search_area();
 135          $engine = new \mock_search\engine();
 136  
 137          $record = $generator->create_record();
 138          $docdata = $area->get_document($record)->export_for_engine();
 139  
 140          $doc = $engine->to_document($area, $docdata);
 141  
 142          $this->assertNotNull($doc->get_doc_icon());
 143  
 144          $generator->teardown();
 145      }
 146  }