2 // This file is part of Moodle - http://moodle.org/
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.
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/>.
18 * Core search class adapted to unit test.
20 * @package core_search
21 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
22 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25 defined('MOODLE_INTERNAL') ||
die();
27 require_once(__DIR__
. '/mock_search_engine.php');
30 * Core search class adapted to unit test.
32 * Note that by default all core search areas are returned when calling get_search_areas_list,
33 * if you want to use the mock search area you can use testable_core_search::add_search_area
34 * although if you want to add mock search areas on top of the core ones you should call
35 * testable_core_search::add_core_search_areas before calling testable_core_search::add_search_area.
37 * @package core_search
38 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
39 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
41 class testable_core_search
extends \core_search\manager
{
44 * Attaches the mock engine to search.
46 * Auto enables global search.
48 * @param \core_search\engine|bool $searchengine
49 * @param bool $ignored Second param just to make this compatible with base class
50 * @return testable_core_search
52 public static function instance($searchengine = false, bool $ignored = false) {
54 // One per request, this should be purged during testing.
55 if (self
::$instance !== null) {
56 return self
::$instance;
59 set_config('enableglobalsearch', true);
61 // Default to the mock one.
62 if ($searchengine === false) {
63 $searchengine = new \mock_search\
engine();
66 self
::$instance = new testable_core_search($searchengine);
68 return self
::$instance;
76 public function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
77 return parent
::get_areas_user_accesses($limitcourseids, $limitcontextids);
81 * Adds an enabled search component to the search areas list.
83 * @param string $areaid
84 * @param \core_search\base $searcharea
87 public function add_search_area($areaid, \core_search\base
$searcharea) {
88 self
::$enabledsearchareas[$areaid] = $searcharea;
89 self
::$allsearchareas[$areaid] = $searcharea;
93 * Loads all core search areas.
97 public function add_core_search_areas() {
98 self
::get_search_areas_list(false);
99 self
::get_search_areas_list(true);
103 * Changes visibility.
105 * @param string $classname
108 public static function is_search_area($classname) {
109 return parent
::is_search_area($classname);
113 * Fakes the current time for PHPunit. Turns off faking time if called with default parameter.
115 * Note: This should be replaced with core functionality once possible (see MDL-60644).
117 * @param float $faketime Current time
119 public static function fake_current_time($faketime = 0.0) {
120 static::$phpunitfaketime = $faketime;
124 * Makes build_limitcourseids method public for testing.
126 * @param \stdClass $formdata Submitted search form data.
130 public function build_limitcourseids(\stdClass
$formdata) {
131 $limitcourseids = parent
::build_limitcourseids($formdata);
133 return $limitcourseids;