MDL-31989 search: Search API and search engine API
[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 *
32 * @package core_search
33 * @copyright 2015 David Monllao {@link http://www.davidmonllao.com}
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
35 */
36class testable_core_search extends \core_search\manager {
37
38 /**
39 * Attaches the mock engine to search.
40 *
41 * Auto enables global search.
42 *
43 * @param \core_search\engine|bool $searchengine
44 * @return testable_core_search
45 */
46 public static function instance($searchengine = false) {
47
48 // One per request, this should be purged during testing.
49 if (self::$instance !== null) {
50 return self::$instance;
51 }
52
53 set_config('enableglobalsearch', true);
54
55 // Default to the mock one.
56 if ($searchengine === false) {
57 $searchengine = new \mock_search\engine();
58 }
59
60 self::$instance = new testable_core_search($searchengine);
61
62 return self::$instance;
63 }
64
65 /**
66 * Changes visibility.
67 *
68 * @return array
69 */
70 public function get_areas_user_accesses() {
71 return parent::get_areas_user_accesses();
72 }
73
74 /**
75 * Adds an enabled search component to the search areas list.
76 *
77 * @param string $areaid
78 * @param \core_search\area\base $searcharea
79 * @return void
80 */
81 public function add_search_area($areaid, \core_search\area\base $searcharea) {
82 self::$enabledsearchareas[$areaid] = $searcharea;
83 self::$allsearchareas[$areaid] = $searcharea;
84 }
85}