Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 3.9.x will end* 10 May 2021 (12 months).
  • Bug fixes for security issues in 3.9.x will end* 8 May 2023 (36 months).
  • PHP version: minimum PHP 7.2.0 Note: minimum PHP version has increased since Moodle 3.8. PHP 7.3.x and 7.4.x are supported too.

Differences Between: [Versions 39 and 310] [Versions 39 and 311] [Versions 39 and 400] [Versions 39 and 401] [Versions 39 and 402] [Versions 39 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  /**
  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  
  25  defined('MOODLE_INTERNAL') || die();
  26  
  27  require_once (__DIR__ . '/mock_search_engine.php');
  28  
  29  /**
  30   * Core search class adapted to unit test.
  31   *
  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   *
  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   */
  41  class 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       */
  75      public function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
  76          return parent::get_areas_user_accesses($limitcourseids, $limitcontextids);
  77      }
  78  
  79      /**
  80       * Adds an enabled search component to the search areas list.
  81       *
  82       * @param string $areaid
  83       * @param \core_search\base $searcharea
  84       * @return void
  85       */
  86      public function add_search_area($areaid, \core_search\base $searcharea) {
  87         self::$enabledsearchareas[$areaid] = $searcharea;
  88         self::$allsearchareas[$areaid] = $searcharea;
  89      }
  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      }
 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  
 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      }
 121  
 122      /**
 123       * Makes build_limitcourseids method public for testing.
 124       *
 125       * @param \stdClass $formdata Submitted search form data.
 126       *
 127       * @return array|bool
 128       */
 129      public function build_limitcourseids(\stdClass $formdata) {
 130          $limitcourseids = parent::build_limitcourseids($formdata);
 131  
 132          return $limitcourseids;
 133      }
 134  }