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 311 and 402] [Versions 39 and 402] [Versions 400 and 402] [Versions 401 and 402] [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      }
  65  
  66      public function delete($areaid = null) {
  67          return null;
  68      }
  69  
  70      public function to_document(\core_search\base $searcharea, $docdata) {
  71          return parent::to_document($searcharea, $docdata);
  72      }
  73  
  74      public function get_course($courseid) {
  75          return parent::get_course($courseid);
  76      }
  77  
  78      public function get_search_area($areaid) {
  79          return parent::get_search_area($areaid);
  80      }
  81  
  82      public function get_query_total_count() {
  83          return 0;
  84      }
  85  
  86      /**
  87       * Sets an add delay to simulate time taken indexing.
  88       *
  89       * @param float $seconds Delay in seconds for each document
  90       */
  91      public function set_add_delay($seconds) {
  92          $this->adddelay = $seconds;
  93      }
  94  
  95      /**
  96       * Gets the list of indexed (added) documents since last time this function
  97       * was called.
  98       *
  99       * @return \core_search\document[] List of documents, in order added.
 100       */
 101      public function get_and_clear_added_documents() {
 102          $added = $this->added;
 103          $this->added = [];
 104          return $added;
 105      }
 106  
 107      public function update_schema($oldversion, $newversion) {
 108          $this->schemaupdates[] = [$oldversion, $newversion];
 109      }
 110  
 111      /**
 112       * Gets all schema updates applied, as an array. Each entry has an array with two values,
 113       * old and new version.
 114       *
 115       * @return array List of schema updates for comparison
 116       */
 117      public function get_and_clear_schema_updates() {
 118          $result = $this->schemaupdates;
 119          $this->schemaupdates = [];
 120          return $result;
 121      }
 122  
 123      /**
 124       * Records delete of course index so it can be checked later.
 125       *
 126       * @param int $oldcourseid Course id
 127       * @return bool True to indicate action taken
 128       */
 129      public function delete_index_for_course(int $oldcourseid) {
 130          $this->deletes[] = ['course', $oldcourseid];
 131          return true;
 132      }
 133  
 134      /**
 135       * Records delete of context index so it can be checked later.
 136       *
 137       * @param int $oldcontextid Context id
 138       * @return bool True to indicate action taken
 139       */
 140      public function delete_index_for_context(int $oldcontextid) {
 141          $this->deletes[] = ['context', $oldcontextid];
 142          return true;
 143      }
 144  
 145      /**
 146       * Gets all course/context deletes applied, as an array. Each entry is an array with two
 147       * values, the first is either 'course' or 'context' and the second is the id deleted.
 148       *
 149       * @return array List of deletes for comparison
 150       */
 151      public function get_and_clear_deletes() {
 152          $deletes = $this->deletes;
 153          $this->deletes = [];
 154          return $deletes;
 155      }
 156  }