Search moodle.org's
Developer Documentation

See Release Notes
Long Term Support Release

  • Bug fixes for general core bugs in 4.1.x will end 13 November 2023 (12 months).
  • Bug fixes for security issues in 4.1.x will end 10 November 2025 (36 months).
  • PHP version: minimum PHP 7.4.0 Note: minimum PHP version has increased since Moodle 4.0. PHP 8.0.x is supported too.

Differences Between: [Versions 39 and 401]

   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   * Generator for test search area.
  19   *
  20   * @package   core_search
  21   * @category  phpunit
  22   * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com}
  23   * @license   http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  24   */
  25  
  26  defined('MOODLE_INTERNAL') || die();
  27  
  28  /**
  29   * Mock search area data generator class.
  30   *
  31   * @package    core_search
  32   * @category   test
  33   * @copyright  2016 Eric Merrill
  34   * @license    http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
  35   */
  36  class core_search_generator extends component_generator_base {
  37      /**
  38       * Creates the mock search area temp table.
  39       */
  40      public function setUp(): void {
  41          global $DB;
  42  
  43          $dbman = $DB->get_manager();
  44          // Make our temp table if we need it.
  45          if (!$dbman->table_exists('temp_mock_search_area')) {
  46              $table = new \xmldb_table('temp_mock_search_area');
  47              $table->add_field('id', XMLDB_TYPE_INTEGER, '10', null, XMLDB_NOTNULL, XMLDB_SEQUENCE, null);
  48              $table->add_field('timemodified', XMLDB_TYPE_INTEGER, 10, null, XMLDB_NOTNULL, null, '0');
  49              $table->add_field('info', XMLDB_TYPE_TEXT, null, null, XMLDB_NOTNULL, null, null);
  50              $table->add_key('primary', XMLDB_KEY_PRIMARY, array('id'));
  51  
  52              $dbman->create_temp_table($table);
  53          }
  54      }
  55  
  56      /**
  57       * Destroys the mock search area temp table.
  58       */
  59      public function tearDown(): void {
  60          global $DB;
  61  
  62          $dbman = $DB->get_manager();
  63          // Make our temp table if we need it.
  64          if ($dbman->table_exists('temp_mock_search_area')) {
  65              $table = new \xmldb_table('temp_mock_search_area');
  66  
  67              $dbman->drop_table($table);
  68          }
  69      }
  70  
  71      /**
  72       * Deletes all records in the search area table.
  73       */
  74      public function delete_all() {
  75          global $DB;
  76  
  77          // Delete any records in the search area.
  78          $DB->delete_records('temp_mock_search_area');
  79      }
  80  
  81      /**
  82       * Adds a new record to the mock search area based on the provided options.
  83       */
  84      public function create_record($options = null) {
  85          global $DB, $USER;
  86  
  87          $record = new \stdClass();
  88          $info = new \stdClass();
  89  
  90          if (empty($options->timemodified)) {
  91              $record->timemodified = time();
  92          } else {
  93              $record->timemodified = $options->timemodified;
  94          }
  95  
  96          if (!isset($options->content)) {
  97              $info->content = 'A test message to find.';
  98          } else {
  99              $info->content = $options->content;
 100          }
 101  
 102          if (!isset($options->description1)) {
 103              $info->description1 = 'Description 1.';
 104          } else {
 105              $info->description1 = $options->description1;
 106          }
 107  
 108          if (!isset($options->description2)) {
 109              $info->description2 = 'Description 2.';
 110          } else {
 111              $info->description2 = $options->description2;
 112          }
 113  
 114          if (!isset($options->title)) {
 115              $info->title = 'A basic title';
 116          } else {
 117              $info->title = $options->title;
 118          }
 119  
 120          if (!isset($options->contextid)) {
 121              $info->contextid = \context_course::instance(SITEID)->id;
 122          } else {
 123              $info->contextid = $options->contextid;
 124          }
 125  
 126          if (!isset($options->courseid)) {
 127              $info->courseid = SITEID;
 128          } else {
 129              $info->courseid = $options->courseid;
 130          }
 131  
 132          if (!isset($options->userid)) {
 133              $info->userid = $USER->id;
 134          } else {
 135              $info->userid = $options->userid;
 136          }
 137  
 138          if (!isset($options->owneruserid)) {
 139              $info->owneruserid = \core_search\manager::NO_OWNER_ID;
 140          } else {
 141              $info->owneruserid = $options->owneruserid;
 142          }
 143  
 144          // This takes a userid (or array of) that will be denied when check_access() is called.
 145          if (!isset($options->denyuserids)) {
 146              $info->denyuserids = array();
 147          } else {
 148              if (is_array($options->denyuserids)) {
 149                  $info->denyuserids = $options->denyuserids;
 150              } else {
 151                  $info->denyuserids = array($options->denyuserids);
 152              }
 153          }
 154  
 155          // Stored file ids that will be attached when attach_files() is called.
 156          if (!isset($options->attachfileids)) {
 157              $info->attachfileids = array();
 158          } else {
 159              if (is_array($options->attachfileids)) {
 160                  $info->attachfileids = $options->attachfileids;
 161              } else {
 162                  $info->attachfileids = array($options->attachfileids);
 163              }
 164          }
 165  
 166          $record->info = serialize($info);
 167          $record->id = $DB->insert_record('temp_mock_search_area', $record);
 168  
 169          return $record;
 170      }
 171  
 172      /**
 173       * Creates a stored file that can be added to mock search area records for indexing.
 174       */
 175      public function create_file($options = null) {
 176          // Add the searchable file fixture.
 177          $syscontext = \context_system::instance();
 178          $filerecord = array(
 179              'contextid' => $syscontext->id,
 180              'component' => 'core',
 181              'filearea'  => 'unittest',
 182              'itemid'    => 0,
 183              'filepath'  => '/',
 184              'filename'  => 'searchfile.txt',
 185          );
 186  
 187          if (isset($options->filename)) {
 188              $filerecord['filename'] = $options->filename;
 189          }
 190  
 191          if (isset($options->content)) {
 192              $content = $options->content;
 193          } else {
 194              $content = 'File contents';
 195          }
 196  
 197          if (isset($options->timemodified)) {
 198              $filerecord['timemodified'] = $options->timemodified;
 199          }
 200  
 201          $fs = get_file_storage();
 202          $file = $fs->create_file_from_string($filerecord, $content);
 203  
 204          return $file;
 205      }
 206  }