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