Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.11.x will end 14 Nov 2022 (12 months plus 6 months extension).
  • Bug fixes for security issues in 3.11.x will end 13 Nov 2023 (18 months plus 12 months extension).
  • PHP version: minimum PHP 7.3.0 Note: minimum PHP version has increased since Moodle 3.10. PHP 7.4.x is supported too.

Differences Between: [Versions 39 and 311]

   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       * @param bool $ignored Second param just to make this compatible with base class
  50       * @return testable_core_search
  51       */
  52      public static function instance($searchengine = false, bool $ignored = false) {
  53  
  54          // One per request, this should be purged during testing.
  55          if (self::$instance !== null) {
  56              return self::$instance;
  57          }
  58  
  59          set_config('enableglobalsearch', true);
  60  
  61          // Default to the mock one.
  62          if ($searchengine === false) {
  63              $searchengine = new \mock_search\engine();
  64          }
  65  
  66          self::$instance = new testable_core_search($searchengine);
  67  
  68          return self::$instance;
  69      }
  70  
  71      /**
  72       * Changes visibility.
  73       *
  74       * @return array
  75       */
  76      public function get_areas_user_accesses($limitcourseids = false, $limitcontextids = false) {
  77          return parent::get_areas_user_accesses($limitcourseids, $limitcontextids);
  78      }
  79  
  80      /**
  81       * Adds an enabled search component to the search areas list.
  82       *
  83       * @param string $areaid
  84       * @param \core_search\base $searcharea
  85       * @return void
  86       */
  87      public function add_search_area($areaid, \core_search\base $searcharea) {
  88         self::$enabledsearchareas[$areaid] = $searcharea;
  89         self::$allsearchareas[$areaid] = $searcharea;
  90      }
  91  
  92      /**
  93       * Loads all core search areas.
  94       *
  95       * @return void
  96       */
  97      public function add_core_search_areas() {
  98          self::get_search_areas_list(false);
  99          self::get_search_areas_list(true);
 100      }
 101  
 102      /**
 103       * Changes visibility.
 104       *
 105       * @param string $classname
 106       * @return bool
 107       */
 108      public static function is_search_area($classname) {
 109          return parent::is_search_area($classname);
 110      }
 111  
 112      /**
 113       * Fakes the current time for PHPunit. Turns off faking time if called with default parameter.
 114       *
 115       * Note: This should be replaced with core functionality once possible (see MDL-60644).
 116       *
 117       * @param float $faketime Current time
 118       */
 119      public static function fake_current_time($faketime = 0.0) {
 120          static::$phpunitfaketime = $faketime;
 121      }
 122  
 123      /**
 124       * Makes build_limitcourseids method public for testing.
 125       *
 126       * @param \stdClass $formdata Submitted search form data.
 127       *
 128       * @return array|bool
 129       */
 130      public function build_limitcourseids(\stdClass $formdata) {
 131          $limitcourseids = parent::build_limitcourseids($formdata);
 132  
 133          return $limitcourseids;
 134      }
 135  }