Merge branch 'MDL-55725-master' of git://github.com/merrill-oakland/moodle
[moodle.git] / search / tests / generator / lib.php
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/>.
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  */
26 defined('MOODLE_INTERNAL') || die();
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() {
41         global $DB;
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);
53         }
54     }
56     /**
57      * Destroys the mock search area temp table.
58      */
59     public function teardown() {
60         global $DB;
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);
68         }
69     }
71     /**
72      * Deletes all records in the search area table.
73      */
74     public function delete_all() {
75         global $DB;
77         // Delete any records in the search area.
78         $DB->delete_records('temp_mock_search_area');
79     }
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;
87         $record = new \stdClass();
88         $info = new \stdClass();
90         if (empty($options->timemodified)) {
91             $record->timemodified = time();
92         } else {
93             $record->timemodified = $options->timemodified;
94         }
96         if (!isset($options->content)) {
97             $info->content = 'A test message to find.';
98         } else {
99             $info->content = $options->content;
100         }
102         if (!isset($options->description1)) {
103             $info->description1 = 'Description 1.';
104         } else {
105             $info->description1 = $options->description1;
106         }
108         if (!isset($options->description2)) {
109             $info->description2 = 'Description 2.';
110         } else {
111             $info->description2 = $options->description2;
112         }
114         if (!isset($options->title)) {
115             $info->title = 'A basic title';
116         } else {
117             $info->title = $options->title;
118         }
120         if (!isset($options->contextid)) {
121             $info->contextid = \context_system::instance()->id;
122         } else {
123             $info->contextid = $options->contextid;
124         }
126         if (!isset($options->courseid)) {
127             $info->courseid = SITEID;
128         } else {
129             $info->courseid = $options->courseid;
130         }
132         if (!isset($options->userid)) {
133             $info->userid = $USER->id;
134         } else {
135             $info->userid = $options->userid;
136         }
138         if (!isset($options->owneruserid)) {
139             $info->owneruserid = \core_search\manager::NO_OWNER_ID;
140         } else {
141             $info->owneruserid = $options->owneruserid;
142         }
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         }
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         }
166         $record->info = serialize($info);
167         $record->id = $DB->insert_record('temp_mock_search_area', $record);
169         return $record;
170     }
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         );
187         if (isset($options->filename)) {
188             $filerecord['filename'] = $options->filename;
189         }
191         if (isset($options->content)) {
192             $content = $options->content;
193         } else {
194             $content = 'File contents';
195         }
197         if (isset($options->timemodified)) {
198             $filerecord['timemodified'] = $options->timemodified;
199         }
201         $fs = get_file_storage();
202         $file = $fs->create_file_from_string($filerecord, $content);
204         return $file;
205     }