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