2 // This file is part of Moodle - http://moodle.org/
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.
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.
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/>.
18 * Generator for test search area.
20 * @package core_search
22 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
26 defined('MOODLE_INTERNAL') || die();
29 * Mock search area data generator class.
31 * @package core_search
33 * @copyright 2016 Eric Merrill
34 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 class core_search_generator extends component_generator_base {
38 * Creates the mock search area temp table.
40 public function setup() {
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'));
52 $dbman->create_temp_table($table);
57 * Destroys the mock search area temp table.
59 public function teardown() {
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');
67 $dbman->drop_table($table);
72 * Deletes all records in the search area table.
74 public function delete_all() {
77 // Delete any records in the search area.
78 $DB->delete_records('temp_mock_search_area');
82 * Adds a new record to the mock search area based on the provided options.
84 public function create_record($options = null) {
87 $record = new \stdClass();
88 $info = new \stdClass();
90 if (empty($options->timemodified)) {
91 $record->timemodified = time();
93 $record->timemodified = $options->timemodified;
96 if (!isset($options->content)) {
97 $info->content = 'A test message to find.';
99 $info->content = $options->content;
102 if (!isset($options->description1)) {
103 $info->description1 = 'Description 1.';
105 $info->description1 = $options->description1;
108 if (!isset($options->description2)) {
109 $info->description2 = 'Description 2.';
111 $info->description2 = $options->description2;
114 if (!isset($options->title)) {
115 $info->title = 'A basic title';
117 $info->title = $options->title;
120 if (!isset($options->contextid)) {
121 $info->contextid = \context_system::instance()->id;
123 $info->contextid = $options->contextid;
126 if (!isset($options->courseid)) {
127 $info->courseid = SITEID;
129 $info->courseid = $options->courseid;
132 if (!isset($options->userid)) {
133 $info->userid = $USER->id;
135 $info->userid = $options->userid;
138 if (!isset($options->owneruserid)) {
139 $info->owneruserid = \core_search\manager::NO_OWNER_ID;
141 $info->owneruserid = $options->owneruserid;
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();
148 if (is_array($options->denyuserids)) {
149 $info->denyuserids = $options->denyuserids;
151 $info->denyuserids = array($options->denyuserids);
155 // Stored file ids that will be attached when attach_files() is called.
156 if (!isset($options->attachfileids)) {
157 $info->attachfileids = array();
159 if (is_array($options->attachfileids)) {
160 $info->attachfileids = $options->attachfileids;
162 $info->attachfileids = array($options->attachfileids);
166 $record->info = serialize($info);
167 $record->id = $DB->insert_record('temp_mock_search_area', $record);
173 * Creates a stored file that can be added to mock search area records for indexing.
175 public function create_file($options = null) {
176 // Add the searchable file fixture.
177 $syscontext = \context_system::instance();
179 'contextid' => $syscontext->id,
180 'component' => 'core',
181 'filearea' => 'unittest',
184 'filename' => 'searchfile.txt',
187 if (isset($options->filename)) {
188 $filerecord['filename'] = $options->filename;
191 if (isset($options->content)) {
192 $content = $options->content;
194 $content = 'File contents';
197 if (isset($options->timemodified)) {
198 $filerecord['timemodified'] = $options->timemodified;
201 $fs = get_file_storage();
202 $file = $fs->create_file_from_string($filerecord, $content);