Search moodle.org's
Developer Documentation

See Release Notes

  • Bug fixes for general core bugs in 3.10.x will end 8 November 2021 (12 months).
  • Bug fixes for security issues in 3.10.x will end 9 May 2022 (18 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.
   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  namespace core_mocksearch\search;
  18  
  19  /**
  20   * Component implementing search for testing purposes.
  21   *
  22   * @package   core_search
  23   * @category  phpunit
  24   * @copyright David Monllao {@link http://www.davidmonllao.com}
  25   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  26   */
  27  
  28  defined('MOODLE_INTERNAL') || die;
  29  
  30  class mock_search_area extends \core_search\base {
  31  
  32      /** @var float If set, waits when doing the indexing query (seconds) */
  33      protected $indexingdelay = 0;
  34  
  35      /**
  36       * Multiple context level so we can test get_areas_user_accesses.
  37       * @var int[]
  38       */
  39      protected static $levels = [CONTEXT_COURSE, CONTEXT_USER];
  40  
  41      /**
  42       * To make things easier, base class required config stuff.
  43       *
  44       * @return bool
  45       */
  46      public function is_enabled() {
  47          return true;
  48      }
  49  
  50      public function get_recordset_by_timestamp($modifiedfrom = 0) {
  51          global $DB;
  52  
  53          if ($this->indexingdelay) {
  54              \testable_core_search::fake_current_time(
  55                      \core_search\manager::get_current_time() + $this->indexingdelay);
  56          }
  57  
  58          $sql = "SELECT * FROM {temp_mock_search_area} WHERE timemodified >= ? ORDER BY timemodified ASC";
  59          return $DB->get_recordset_sql($sql, array($modifiedfrom));
  60      }
  61  
  62  
  63      /**
  64       * A helper function that will turn a record into 'data array', for use with document building.
  65       */
  66      public function convert_record_to_doc_array($record) {
  67          $docdata = (array)unserialize($record->info);
  68          $docdata['areaid'] = $this->get_area_id();
  69          $docdata['itemid'] = $record->id;
  70          $docdata['modified'] = $record->timemodified;
  71  
  72          return $docdata;
  73      }
  74  
  75      public function get_document($record, $options = array()) {
  76          global $USER;
  77  
  78          $info = unserialize($record->info);
  79  
  80          // Prepare associative array with data from DB.
  81          $doc = \core_search\document_factory::instance($record->id, $this->componentname, $this->areaname);
  82          $doc->set('title', $info->title);
  83          $doc->set('content', $info->content);
  84          $doc->set('description1', $info->description1);
  85          $doc->set('description2', $info->description2);
  86          $doc->set('contextid', $info->contextid);
  87          $doc->set('courseid', $info->courseid);
  88          $doc->set('userid', $info->userid);
  89          $doc->set('owneruserid', $info->owneruserid);
  90          $doc->set('modified', $record->timemodified);
  91  
  92          return $doc;
  93      }
  94  
  95      public function attach_files($document) {
  96          global $DB;
  97  
  98          if (!$record = $DB->get_record('temp_mock_search_area', array('id' => $document->get('itemid')))) {
  99              return;
 100          }
 101  
 102          $info = unserialize($record->info);
 103          foreach ($info->attachfileids as $fileid) {
 104              $document->add_stored_file($fileid);
 105          }
 106      }
 107  
 108      public function uses_file_indexing() {
 109          return true;
 110      }
 111  
 112      public function check_access($id) {
 113          global $DB, $USER;
 114  
 115          if ($record = $DB->get_record('temp_mock_search_area', array('id' => $id))) {
 116              $info = unserialize($record->info);
 117  
 118              if (in_array($USER->id, $info->denyuserids)) {
 119                  return \core_search\manager::ACCESS_DENIED;
 120              }
 121              return \core_search\manager::ACCESS_GRANTED;
 122          }
 123          return \core_search\manager::ACCESS_DELETED;
 124      }
 125  
 126      public function get_doc_url(\core_search\document $doc) {
 127          return new \moodle_url('/index.php');
 128      }
 129  
 130      public function get_context_url(\core_search\document $doc) {
 131          return new \moodle_url('/index.php');
 132      }
 133  
 134      public function get_visible_name($lazyload = false) {
 135          return 'Mock search area';
 136      }
 137  
 138      /**
 139       * Sets a fake delay to simulate time taken doing the indexing query.
 140       *
 141       * @param float $seconds Delay in seconds for each time indexing query is called
 142       */
 143      public function set_indexing_delay($seconds) {
 144          $this->indexingdelay = $seconds;
 145      }
 146  
 147  }