Merge branch 'MDL-81601-main' of https://github.com/aanabit/moodle
[moodle.git] / search / tests / fixtures / mock_search_engine.php
blob0b8b3e12ffcb684c456bf1bedbb6f56dccffed25
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.
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/>.
17 namespace mock_search;
19 /**
20 * Search engine for testing purposes.
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
28 use core_search\manager;
30 defined('MOODLE_INTERNAL') || die;
32 class engine extends \core_search\engine {
34 /** @var float If set, waits when adding each document (seconds) */
35 protected $adddelay = 0;
37 /** @var \core_search\document[] Documents added */
38 protected $added = [];
40 /** @var array Schema updates applied */
41 protected $schemaupdates = [];
43 /** @var array delete of course index. */
44 protected $deletes = [];
46 public function is_installed() {
47 return true;
50 public function is_server_ready() {
51 return true;
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);
58 $this->added[] = $document;
59 return true;
62 public function execute_query($data, $usercontexts, $limit = 0) {
63 // No need to implement.
64 return [];
67 public function delete($areaid = null) {
68 return null;
71 public function to_document(\core_search\base $searcharea, $docdata) {
72 return parent::to_document($searcharea, $docdata);
75 public function get_course($courseid) {
76 return parent::get_course($courseid);
79 public function get_search_area($areaid) {
80 return parent::get_search_area($areaid);
83 public function get_query_total_count() {
84 return 0;
87 /**
88 * Sets an add delay to simulate time taken indexing.
90 * @param float $seconds Delay in seconds for each document
92 public function set_add_delay($seconds) {
93 $this->adddelay = $seconds;
96 /**
97 * Gets the list of indexed (added) documents since last time this function
98 * was called.
100 * @return \core_search\document[] List of documents, in order added.
102 public function get_and_clear_added_documents() {
103 $added = $this->added;
104 $this->added = [];
105 return $added;
108 public function update_schema($oldversion, $newversion) {
109 $this->schemaupdates[] = [$oldversion, $newversion];
113 * Gets all schema updates applied, as an array. Each entry has an array with two values,
114 * old and new version.
116 * @return array List of schema updates for comparison
118 public function get_and_clear_schema_updates() {
119 $result = $this->schemaupdates;
120 $this->schemaupdates = [];
121 return $result;
125 * Records delete of course index so it can be checked later.
127 * @param int $oldcourseid Course id
128 * @return bool True to indicate action taken
130 public function delete_index_for_course(int $oldcourseid) {
131 $this->deletes[] = ['course', $oldcourseid];
132 return true;
136 * Records delete of context index so it can be checked later.
138 * @param int $oldcontextid Context id
139 * @return bool True to indicate action taken
141 public function delete_index_for_context(int $oldcontextid) {
142 $this->deletes[] = ['context', $oldcontextid];
143 return true;
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.
150 * @return array List of deletes for comparison
152 public function get_and_clear_deletes() {
153 $deletes = $this->deletes;
154 $this->deletes = [];
155 return $deletes;