Search moodle.org's
Developer Documentation

See Release Notes

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

Differences Between: [Versions 310 and 403] [Versions 311 and 403] [Versions 39 and 403] [Versions 400 and 403] [Versions 401 and 403] [Versions 402 and 403]

   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 mock_search;
  18  
  19  /**
  20   * Search engine for testing purposes.
  21   *
  22   * @package   core_search
  23   * @category  phpunit
  24   * @copyright David Monllao {@link http://www.davidmonllao.com}
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  use core_search\manager;
  29  
  30  defined('MOODLE_INTERNAL') || die;
  31  
  32  class engine extends \core_search\engine {
  33  
  34      /** @var float If set, waits when adding each document (seconds) */
  35      protected $adddelay = 0;
  36  
  37      /** @var \core_search\document[] Documents added */
  38      protected $added = [];
  39  
  40      /** @var array Schema updates applied */
  41      protected $schemaupdates = [];
  42  
  43      /** @var array delete of course index. */
  44      protected $deletes = [];
  45  
  46      public function is_installed() {
  47          return true;
  48      }
  49  
  50      public function is_server_ready() {
  51          return true;
  52      }
  53  
  54      public function add_document($document, $fileindexing = false) {
  55          if ($this->adddelay) {
  56              \testable_core_search::fake_current_time(manager::get_current_time() + $this->adddelay);
  57          }
  58          $this->added[] = $document;
  59          return true;
  60      }
  61  
  62      public function execute_query($data, $usercontexts, $limit = 0) {
  63          // No need to implement.
  64          return [];
  65      }
  66  
  67      public function delete($areaid = null) {
  68          return null;
  69      }
  70  
  71      public function to_document(\core_search\base $searcharea, $docdata) {
  72          return parent::to_document($searcharea, $docdata);
  73      }
  74  
  75      public function get_course($courseid) {
  76          return parent::get_course($courseid);
  77      }
  78  
  79      public function get_search_area($areaid) {
  80          return parent::get_search_area($areaid);
  81      }
  82  
  83      public function get_query_total_count() {
  84          return 0;
  85      }
  86  
  87      /**
  88       * Sets an add delay to simulate time taken indexing.
  89       *
  90       * @param float $seconds Delay in seconds for each document
  91       */
  92      public function set_add_delay($seconds) {
  93          $this->adddelay = $seconds;
  94      }
  95  
  96      /**
  97       * Gets the list of indexed (added) documents since last time this function
  98       * was called.
  99       *
 100       * @return \core_search\document[] List of documents, in order added.
 101       */
 102      public function get_and_clear_added_documents() {
 103          $added = $this->added;
 104          $this->added = [];
 105          return $added;
 106      }
 107  
 108      public function update_schema($oldversion, $newversion) {
 109          $this->schemaupdates[] = [$oldversion, $newversion];
 110      }
 111  
 112      /**
 113       * Gets all schema updates applied, as an array. Each entry has an array with two values,
 114       * old and new version.
 115       *
 116       * @return array List of schema updates for comparison
 117       */
 118      public function get_and_clear_schema_updates() {
 119          $result = $this->schemaupdates;
 120          $this->schemaupdates = [];
 121          return $result;
 122      }
 123  
 124      /**
 125       * Records delete of course index so it can be checked later.
 126       *
 127       * @param int $oldcourseid Course id
 128       * @return bool True to indicate action taken
 129       */
 130      public function delete_index_for_course(int $oldcourseid) {
 131          $this->deletes[] = ['course', $oldcourseid];
 132          return true;
 133      }
 134  
 135      /**
 136       * Records delete of context index so it can be checked later.
 137       *
 138       * @param int $oldcontextid Context id
 139       * @return bool True to indicate action taken
 140       */
 141      public function delete_index_for_context(int $oldcontextid) {
 142          $this->deletes[] = ['context', $oldcontextid];
 143          return true;
 144      }
 145  
 146      /**
 147       * Gets all course/context deletes applied, as an array. Each entry is an array with two
 148       * values, the first is either 'course' or 'context' and the second is the id deleted.
 149       *
 150       * @return array List of deletes for comparison
 151       */
 152      public function get_and_clear_deletes() {
 153          $deletes = $this->deletes;
 154          $this->deletes = [];
 155          return $deletes;
 156      }
 157  }