|
| 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 | + * CMS search unit tests. |
| 19 | + * |
| 20 | + * @package mod_cms |
| 21 | + * @category test |
| 22 | + * @author Tomo Tsuyuki <[email protected]> |
| 23 | + * @copyright 2024 Catalyst IT |
| 24 | + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later |
| 25 | + * @covers \mod_cms\search\cms |
| 26 | + */ |
| 27 | + |
| 28 | +namespace mod_cms\search; |
| 29 | + |
| 30 | +use mod_cms\local\model\cms_types; |
| 31 | +use mod_cms\customfield\cmsfield_handler; |
| 32 | + |
| 33 | +defined('MOODLE_INTERNAL') || die(); |
| 34 | + |
| 35 | +global $CFG; |
| 36 | +require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php'); |
| 37 | + |
| 38 | +class search_test extends \advanced_testcase { |
| 39 | + |
| 40 | + /** |
| 41 | + * @var string Area id |
| 42 | + */ |
| 43 | + protected $cmsareaid = null; |
| 44 | + |
| 45 | + /** |
| 46 | + * Set up. |
| 47 | + */ |
| 48 | + public function setUp(): void { |
| 49 | + $this->resetAfterTest(); |
| 50 | + set_config('enableglobalsearch', true); |
| 51 | + |
| 52 | + $this->cmsareaid = \core_search\manager::generate_areaid('mod_cms', 'cmsfield'); |
| 53 | + |
| 54 | + // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this. |
| 55 | + $search = \testable_core_search::instance(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * Test search enabled. |
| 60 | + * |
| 61 | + * @return void |
| 62 | + */ |
| 63 | + public function test_search_enabled() { |
| 64 | + |
| 65 | + $searcharea = \core_search\manager::get_search_area($this->cmsareaid); |
| 66 | + list($componentname, $varname) = $searcharea->get_config_var_name(); |
| 67 | + |
| 68 | + // Enabled by default once global search is enabled. |
| 69 | + $this->assertTrue($searcharea->is_enabled()); |
| 70 | + |
| 71 | + set_config($varname . '_enabled', 0, $componentname); |
| 72 | + $this->assertFalse($searcharea->is_enabled()); |
| 73 | + |
| 74 | + set_config($varname . '_enabled', 1, $componentname); |
| 75 | + $this->assertTrue($searcharea->is_enabled()); |
| 76 | + } |
| 77 | + |
| 78 | + /** |
| 79 | + * Indexing mod cms contents. |
| 80 | + * |
| 81 | + * @return void |
| 82 | + */ |
| 83 | + public function test_get_document_recordset() { |
| 84 | + global $DB; |
| 85 | + |
| 86 | + // Returns the instance as long as the area is supported. |
| 87 | + $searcharea = \core_search\manager::get_search_area($this->cmsareaid); |
| 88 | + $this->assertInstanceOf('\mod_cms\search\cmsfield', $searcharea); |
| 89 | + |
| 90 | + $course = self::getDataGenerator()->create_course(); |
| 91 | + |
| 92 | + $cmstype = new cms_types(); |
| 93 | + $cmstype->set('name', 'Overview') |
| 94 | + ->set('idnumber', 'overview') |
| 95 | + ->set('title_mustache', 'Overview'); |
| 96 | + $cmstype->save(); |
| 97 | + |
| 98 | + $fieldcategory = self::getDataGenerator()->create_custom_field_category([ |
| 99 | + 'name' => 'Other fields', |
| 100 | + 'component' => 'mod_cms', |
| 101 | + 'area' => 'cmsfield', |
| 102 | + 'itemid' => $cmstype->get('id'), |
| 103 | + ]); |
| 104 | + $field = self::getDataGenerator()->create_custom_field([ |
| 105 | + 'name' => 'Overview', |
| 106 | + 'shortname' => 'overview', |
| 107 | + 'type' => 'text', |
| 108 | + 'categoryid' => $fieldcategory->get('id'), |
| 109 | + ]); |
| 110 | + |
| 111 | + // Name for cms is coming from "title_mustache" in cms_type. |
| 112 | + $generator = self::getDataGenerator()->get_plugin_generator('mod_cms'); |
| 113 | + $record = new \stdClass(); |
| 114 | + $record->course = $course->id; |
| 115 | + $record->customfield_overview = 'Test overview text 1'; |
| 116 | + $record->typeid = $cmstype->get('id'); |
| 117 | + $generator->create_instance_with_data($record); |
| 118 | + |
| 119 | + $record = new \stdClass(); |
| 120 | + $record->course = $course->id; |
| 121 | + $record->customfield_overview = 'Test overview text 2'; |
| 122 | + $record->typeid = $cmstype->get('id'); |
| 123 | + $generator->create_instance_with_data($record); |
| 124 | + |
| 125 | + // All records. |
| 126 | + $recordset = $searcharea->get_document_recordset(); |
| 127 | + $this->assertTrue($recordset->valid()); |
| 128 | + foreach ($recordset as $record) { |
| 129 | + $this->assertInstanceOf('stdClass', $record); |
| 130 | + $data = $DB->get_record('customfield_data', ['id' => $record->id]); |
| 131 | + $doc = $searcharea->get_document($record); |
| 132 | + $this->assertInstanceOf('\core_search\document', $doc); |
| 133 | + $this->assertEquals('mod_cms-cmsfield-' . $data->id, $doc->get('id')); |
| 134 | + $this->assertEquals($data->id, $doc->get('itemid')); |
| 135 | + $this->assertEquals($course->id, $doc->get('courseid')); |
| 136 | + $this->assertEquals($data->contextid, $doc->get('contextid')); |
| 137 | + $this->assertEquals($field->get('name'), $doc->get('title')); |
| 138 | + $this->assertEquals($data->value, $doc->get('content')); |
| 139 | + |
| 140 | + // Static caches are working. |
| 141 | + $dbreads = $DB->perf_get_reads(); |
| 142 | + $doc = $searcharea->get_document($record); |
| 143 | + $this->assertEquals($dbreads, $DB->perf_get_reads()); |
| 144 | + $this->assertInstanceOf('\core_search\document', $doc); |
| 145 | + } |
| 146 | + $recordset->close(); |
| 147 | + |
| 148 | + // The +2 is to prevent race conditions. |
| 149 | + $recordset = $searcharea->get_document_recordset(time() + 2); |
| 150 | + |
| 151 | + // No new records. |
| 152 | + $this->assertFalse($recordset->valid()); |
| 153 | + $recordset->close(); |
| 154 | + |
| 155 | + // Wait 1 sec to have new search string. |
| 156 | + sleep(1); |
| 157 | + $record = new \stdClass(); |
| 158 | + $record->course = $course->id; |
| 159 | + $record->customfield_overview = 'Test overview text 3'; |
| 160 | + $record->typeid = $cmstype->get('id'); |
| 161 | + $generator->create_instance_with_data($record); |
| 162 | + |
| 163 | + // Return only new search. |
| 164 | + $recordset = $searcharea->get_document_recordset(time()); |
| 165 | + foreach ($recordset as $record) { |
| 166 | + $this->assertInstanceOf('stdClass', $record); |
| 167 | + $data = $DB->get_record('customfield_data', ['id' => $record->id]); |
| 168 | + $doc = $searcharea->get_document($record); |
| 169 | + $this->assertInstanceOf('\core_search\document', $doc); |
| 170 | + $this->assertEquals('mod_cms-cmsfield-' . $data->id, $doc->get('id')); |
| 171 | + $this->assertEquals($data->id, $doc->get('itemid')); |
| 172 | + $this->assertEquals($course->id, $doc->get('courseid')); |
| 173 | + $this->assertEquals($data->contextid, $doc->get('contextid')); |
| 174 | + $this->assertEquals($field->get('name'), $doc->get('title')); |
| 175 | + $this->assertEquals($data->value, $doc->get('content')); |
| 176 | + |
| 177 | + // Static caches are working. |
| 178 | + $dbreads = $DB->perf_get_reads(); |
| 179 | + $doc = $searcharea->get_document($record); |
| 180 | + $this->assertEquals($dbreads, $DB->perf_get_reads()); |
| 181 | + $this->assertInstanceOf('\core_search\document', $doc); |
| 182 | + } |
| 183 | + $recordset->close(); |
| 184 | + } |
| 185 | +} |
0 commit comments