weekly release 3.7dev
[moodle.git] / search / tests / fixtures / testable_core_search.php
CommitLineData
db48207e
DM
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/**
18 * Core search class adapted to unit test.
19 *
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
23 */
24
25defined('MOODLE_INTERNAL') || die();
26
27require_once(__DIR__ . '/mock_search_engine.php');
28
29/**
30 * Core search class adapted to unit test.
31 *
25ba053f
DM
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.
36 *
db48207e
DM
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
40 */
41class testable_core_search extends \core_search\manager {
42
43 /**
44 * Attaches the mock engine to search.
45 *
46 * Auto enables global search.
47 *
48 * @param \core_search\engine|bool $searchengine
49 * @return testable_core_search
50 */
51 public static function instance($searchengine = false) {
52
53 // One per request, this should be purged during testing.
54 if (self::$instance !== null) {
55 return self::$instance;
56 }
57
58 set_config('enableglobalsearch', true);
59
60 // Default to the mock one.
61 if ($searchengine === false) {
62 $searchengine = new \mock_search\engine();
63 }
64
65 self::$instance = new testable_core_search($searchengine);
66
67 return self::$instance;
68 }
69
70 /**
71 * Changes visibility.
72 *
73 * @return array
74 */
cfa00fc5 75 public function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
76 return parent::get_areas_user_accesses($limitcourseids, $limitcontextids);
db48207e
DM
77 }
78
79 /**
80 * Adds an enabled search component to the search areas list.
81 *
82 * @param string $areaid
0bd8383a 83 * @param \core_search\base $searcharea
db48207e
DM
84 * @return void
85 */
0bd8383a 86 public function add_search_area($areaid, \core_search\base $searcharea) {
db48207e
DM
87 self::$enabledsearchareas[$areaid] = $searcharea;
88 self::$allsearchareas[$areaid] = $searcharea;
89 }
25ba053f
DM
90
91 /**
92 * Loads all core search areas.
93 *
94 * @return void
95 */
96 public function add_core_search_areas() {
97 self::get_search_areas_list(false);
98 self::get_search_areas_list(true);
99 }
f3d38863
DM
100
101 /**
102 * Changes visibility.
103 *
104 * @param string $classname
105 * @return bool
106 */
107 public static function is_search_area($classname) {
108 return parent::is_search_area($classname);
109 }
110
82735dec 111 /**
112 * Fakes the current time for PHPunit. Turns off faking time if called with default parameter.
113 *
114 * Note: This should be replaced with core functionality once possible (see MDL-60644).
115 *
116 * @param float $faketime Current time
117 */
118 public static function fake_current_time($faketime = 0.0) {
119 static::$phpunitfaketime = $faketime;
120 }
db48207e 121}