See Release Notes
Long Term Support Release
Differences Between: [Versions 39 and 310]
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 * Glossary search unit tests. 19 * 20 * @package mod_glossary 21 * @category test 22 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com} 23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 24 */ 25 26 defined('MOODLE_INTERNAL') || die(); 27 28 global $CFG; 29 require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php'); 30 require_once($CFG->dirroot . '/mod/glossary/tests/generator/lib.php'); 31 32 /** 33 * Provides the unit tests for glossary search. 34 * 35 * @package mod_glossary 36 * @category test 37 * @copyright 2016 David Monllao {@link http://www.davidmonllao.com} 38 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 39 */ 40 class mod_glossary_search_testcase extends advanced_testcase { 41 42 /** 43 * @var string Area id 44 */ 45 protected $entryareaid = null; 46 47 public function setUp() { 48 $this->resetAfterTest(true); 49 set_config('enableglobalsearch', true); 50 51 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this. 52 $search = testable_core_search::instance(); 53 54 $this->entryareaid = \core_search\manager::generate_areaid('mod_glossary', 'entry'); 55 } 56 57 /** 58 * Availability. 59 * 60 * @return void 61 */ 62 public function test_search_enabled() { 63 64 $searcharea = \core_search\manager::get_search_area($this->entryareaid); 65 list($componentname, $varname) = $searcharea->get_config_var_name(); 66 67 // Enabled by default once global search is enabled. 68 $this->assertTrue($searcharea->is_enabled()); 69 70 set_config($varname . '_enabled', 0, $componentname); 71 $this->assertFalse($searcharea->is_enabled()); 72 73 set_config($varname . '_enabled', 1, $componentname); 74 $this->assertTrue($searcharea->is_enabled()); 75 } 76 77 /** 78 * Indexing contents. 79 * 80 * @return void 81 */ 82 public function test_entries_indexing() { 83 global $DB; 84 85 $searcharea = \core_search\manager::get_search_area($this->entryareaid); 86 $this->assertInstanceOf('\mod_glossary\search\entry', $searcharea); 87 88 $user1 = self::getDataGenerator()->create_user(); 89 $user2 = self::getDataGenerator()->create_user(); 90 91 $course1 = self::getDataGenerator()->create_course(); 92 $course2 = self::getDataGenerator()->create_course(); 93 94 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student'); 95 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student'); 96 97 $record = new stdClass(); 98 $record->course = $course1->id; 99 100 $this->setUser($user1); 101 102 // Approved entries by default glossary. 103 $glossary1 = self::getDataGenerator()->create_module('glossary', $record); 104 $entry1 = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1); 105 $entry2 = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1); 106 107 // All records. 108 $recordset = $searcharea->get_recordset_by_timestamp(0); 109 $this->assertTrue($recordset->valid()); 110 $nrecords = 0; 111 foreach ($recordset as $record) { 112 $this->assertInstanceOf('stdClass', $record); 113 $doc = $searcharea->get_document($record); 114 $this->assertInstanceOf('\core_search\document', $doc); 115 116 // Static caches are working. 117 $dbreads = $DB->perf_get_reads(); 118 $doc = $searcharea->get_document($record); 119 120 // The +1 is because we are not caching glossary alias (keywords) as they depend on a single entry. 121 $this->assertEquals($dbreads + 1, $DB->perf_get_reads()); 122 $this->assertInstanceOf('\core_search\document', $doc); 123 $nrecords++; 124 } 125 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown. 126 $recordset->close(); 127 $this->assertEquals(2, $nrecords); 128 129 // The +2 is to prevent race conditions. 130 $recordset = $searcharea->get_recordset_by_timestamp(time() + 2); 131 132 // No new records. 133 $this->assertFalse($recordset->valid()); 134 $recordset->close(); 135 136 // Create a second glossary with one entry. 137 $glossary2 = self::getDataGenerator()->create_module('glossary', ['course' => $course1->id]); 138 self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary2); 139 140 // Test indexing with each activity then combined course context. 141 $rs = $searcharea->get_document_recordset(0, context_module::instance($glossary1->cmid)); 142 $this->assertEquals(2, iterator_count($rs)); 143 $rs->close(); 144 $rs = $searcharea->get_document_recordset(0, context_module::instance($glossary2->cmid)); 145 $this->assertEquals(1, iterator_count($rs)); 146 $rs->close(); 147 $rs = $searcharea->get_document_recordset(0, context_course::instance($course1->id)); 148 $this->assertEquals(3, iterator_count($rs)); 149 $rs->close(); 150 } 151 152 /** 153 * Document contents. 154 * 155 * @return void 156 */ 157 public function test_entries_document() { 158 global $DB; 159 160 $searcharea = \core_search\manager::get_search_area($this->entryareaid); 161 162 $user = self::getDataGenerator()->create_user(); 163 $course1 = self::getDataGenerator()->create_course(); 164 $this->getDataGenerator()->enrol_user($user->id, $course1->id, 'teacher'); 165 166 $record = new stdClass(); 167 $record->course = $course1->id; 168 169 $this->setUser($user); 170 $glossary = self::getDataGenerator()->create_module('glossary', $record); 171 $entry = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary); 172 $entry->course = $glossary->course; 173 174 $doc = $searcharea->get_document($entry); 175 $this->assertInstanceOf('\core_search\document', $doc); 176 $this->assertEquals($entry->id, $doc->get('itemid')); 177 $this->assertEquals($course1->id, $doc->get('courseid')); 178 $this->assertEquals($user->id, $doc->get('userid')); 179 $this->assertEquals($entry->concept, $doc->get('title')); 180 $this->assertEquals($entry->definition, $doc->get('content')); 181 } 182 183 /** 184 * Document accesses. 185 * 186 * @return void 187 */ 188 public function test_entries_access() { 189 global $DB; 190 191 // Returns the instance as long as the component is supported. 192 $searcharea = \core_search\manager::get_search_area($this->entryareaid); 193 194 $user1 = self::getDataGenerator()->create_user(); 195 $user2 = self::getDataGenerator()->create_user(); 196 197 $course1 = self::getDataGenerator()->create_course(); 198 $course2 = self::getDataGenerator()->create_course(); 199 200 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'teacher'); 201 $this->getDataGenerator()->enrol_user($user2->id, $course1->id, 'student'); 202 203 $record = new stdClass(); 204 $record->course = $course1->id; 205 206 // Approved entries by default glossary, created by teacher. 207 $this->setUser($user1); 208 $glossary1 = self::getDataGenerator()->create_module('glossary', $record); 209 $teacherapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1); 210 $teachernotapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary1, array('approved' => false)); 211 212 // Entries need to be approved and created by student. 213 $glossary2 = self::getDataGenerator()->create_module('glossary', $record); 214 $this->setUser($user2); 215 $studentapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary2); 216 $studentnotapproved = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary2, array('approved' => false)); 217 218 // Activity hidden to students. 219 $this->setUser($user1); 220 $glossary3 = self::getDataGenerator()->create_module('glossary', $record); 221 $hidden = self::getDataGenerator()->get_plugin_generator('mod_glossary')->create_content($glossary3); 222 set_coursemodule_visible($glossary3->cmid, 0); 223 224 $this->setUser($user2); 225 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($teacherapproved->id)); 226 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($teachernotapproved->id)); 227 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($studentapproved->id)); 228 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($studentnotapproved->id)); 229 $this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($hidden->id)); 230 } 231 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body