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 * Wiki global search unit tests. 19 * 20 * @package mod_wiki 21 * @category test 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 global $CFG; 29 require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php'); 30 31 /** 32 * Provides the unit tests for wiki global search. 33 * 34 * @package mod_wiki 35 * @category test 36 * @copyright 2016 Eric Merrill {@link http://www.merrilldigital.com} 37 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later 38 */ 39 class mod_wiki_search_testcase extends advanced_testcase { 40 41 /** 42 * @var string Area id 43 */ 44 protected $wikicollabpageareaid = null; 45 46 public function setUp() { 47 $this->resetAfterTest(true); 48 $this->setAdminUser(); 49 set_config('enableglobalsearch', true); 50 51 $this->wikicollabpageareaid = \core_search\manager::generate_areaid('mod_wiki', 'collaborative_page'); 52 53 // Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this. 54 $search = testable_core_search::instance(); 55 } 56 57 /** 58 * Availability. 59 * 60 * @return void 61 */ 62 public function test_search_enabled() { 63 $searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid); 64 list($componentname, $varname) = $searcharea->get_config_var_name(); 65 66 // Enabled by default once global search is enabled. 67 $this->assertTrue($searcharea->is_enabled()); 68 69 set_config($varname . '_enabled', 0, $componentname); 70 $this->assertFalse($searcharea->is_enabled()); 71 72 set_config($varname . '_enabled', 1, $componentname); 73 $this->assertTrue($searcharea->is_enabled()); 74 } 75 76 /** 77 * Indexing collaborative page contents. 78 * 79 * @return void 80 */ 81 public function test_collaborative_page_indexing() { 82 global $DB; 83 84 // Returns the instance as long as the area is supported. 85 $searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid); 86 $this->assertInstanceOf('\mod_wiki\search\collaborative_page', $searcharea); 87 88 $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); 89 $course1 = self::getDataGenerator()->create_course(); 90 91 $collabwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course1->id)); 92 $cpage1 = $wikigenerator->create_first_page($collabwiki); 93 $cpage2 = $wikigenerator->create_content($collabwiki); 94 $cpage3 = $wikigenerator->create_content($collabwiki); 95 96 $indwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course1->id, 'wikimode' => 'individual')); 97 $ipage1 = $wikigenerator->create_first_page($indwiki); 98 $ipage2 = $wikigenerator->create_content($indwiki); 99 $ipage3 = $wikigenerator->create_content($indwiki); 100 101 // All records. 102 $recordset = $searcharea->get_recordset_by_timestamp(0); 103 $this->assertTrue($recordset->valid()); 104 $nrecords = 0; 105 foreach ($recordset as $record) { 106 $this->assertInstanceOf('stdClass', $record); 107 $doc = $searcharea->get_document($record); 108 $this->assertInstanceOf('\core_search\document', $doc); 109 110 // Static caches are working. 111 $dbreads = $DB->perf_get_reads(); 112 $doc = $searcharea->get_document($record); 113 $this->assertEquals($dbreads, $DB->perf_get_reads()); 114 $this->assertInstanceOf('\core_search\document', $doc); 115 $nrecords++; 116 } 117 // If there would be an error/failure in the foreach above the recordset would be closed on shutdown. 118 $recordset->close(); 119 120 // We expect 3 (not 6) pages. 121 $this->assertEquals(3, $nrecords); 122 123 // The +2 is to prevent race conditions. 124 $recordset = $searcharea->get_recordset_by_timestamp(time() + 2); 125 126 // No new records. 127 $this->assertFalse($recordset->valid()); 128 $recordset->close(); 129 130 // Add another wiki with one page. 131 $collabwiki2 = $this->getDataGenerator()->create_module('wiki', ['course' => $course1->id]); 132 $wikigenerator->create_first_page($collabwiki2); 133 134 // Test indexing contexts. 135 $rs = $searcharea->get_document_recordset(0, context_module::instance($collabwiki->cmid)); 136 $this->assertEquals(3, iterator_count($rs)); 137 $rs->close(); 138 $rs = $searcharea->get_document_recordset(0, context_module::instance($collabwiki2->cmid)); 139 $this->assertEquals(1, iterator_count($rs)); 140 $rs->close(); 141 $rs = $searcharea->get_document_recordset(0, context_course::instance($course1->id)); 142 $this->assertEquals(4, iterator_count($rs)); 143 $rs->close(); 144 } 145 146 /** 147 * Group support for wiki entries. 148 */ 149 public function test_collaborative_page_group_support() { 150 // Get the search area and test generators. 151 $searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid); 152 $generator = $this->getDataGenerator(); 153 $wikigenerator = $generator->get_plugin_generator('mod_wiki'); 154 155 // Create a course, a user, and two groups. 156 $course = $generator->create_course(); 157 $user = $generator->create_user(); 158 $generator->enrol_user($user->id, $course->id, 'teacher'); 159 $group1 = $generator->create_group(['courseid' => $course->id]); 160 $group2 = $generator->create_group(['courseid' => $course->id]); 161 162 // Separate groups wiki. 163 $wiki = self::getDataGenerator()->create_module('wiki', ['course' => $course->id, 164 'groupmode' => SEPARATEGROUPS]); 165 166 // Create page with each group and one for all groups. 167 $wikigenerator->create_page($wiki, ['title' => 'G1', 'group' => $group1->id]); 168 $wikigenerator->create_page($wiki, ['title' => 'G2', 'group' => $group2->id]); 169 $wikigenerator->create_page($wiki, ['title' => 'ALLGROUPS']); 170 171 // Do the indexing of all 3 pages. 172 $rs = $searcharea->get_recordset_by_timestamp(0); 173 $results = []; 174 foreach ($rs as $rec) { 175 $results[$rec->title] = $rec; 176 } 177 $rs->close(); 178 $this->assertCount(3, $results); 179 180 // Check each document has the correct groupid. 181 $doc = $searcharea->get_document($results['G1']); 182 $this->assertTrue($doc->is_set('groupid')); 183 $this->assertEquals($group1->id, $doc->get('groupid')); 184 $doc = $searcharea->get_document($results['G2']); 185 $this->assertTrue($doc->is_set('groupid')); 186 $this->assertEquals($group2->id, $doc->get('groupid')); 187 $doc = $searcharea->get_document($results['ALLGROUPS']); 188 $this->assertFalse($doc->is_set('groupid')); 189 190 // While we're here, also test that the search area requests restriction by group. 191 $modinfo = get_fast_modinfo($course); 192 $this->assertTrue($searcharea->restrict_cm_access_by_group($modinfo->get_cm($wiki->cmid))); 193 194 // In visible groups mode, it won't request restriction by group. 195 set_coursemodule_groupmode($wiki->cmid, VISIBLEGROUPS); 196 $modinfo = get_fast_modinfo($course); 197 $this->assertFalse($searcharea->restrict_cm_access_by_group($modinfo->get_cm($wiki->cmid))); 198 } 199 200 /** 201 * Check collaborative_page check access. 202 * 203 * @return void 204 */ 205 public function test_collaborative_page_check_access() { 206 global $DB; 207 208 // Returns the instance as long as the area is supported. 209 $searcharea = \core_search\manager::get_search_area($this->wikicollabpageareaid); 210 $this->assertInstanceOf('\mod_wiki\search\collaborative_page', $searcharea); 211 212 $user1 = self::getDataGenerator()->create_user(); 213 $course1 = self::getDataGenerator()->create_course(); 214 $this->getDataGenerator()->enrol_user($user1->id, $course1->id, 'student'); 215 216 $wikigenerator = $this->getDataGenerator()->get_plugin_generator('mod_wiki'); 217 218 $collabwiki = $this->getDataGenerator()->create_module('wiki', array('course' => $course1->id)); 219 $cpage1 = $wikigenerator->create_first_page($collabwiki); 220 221 $this->setAdminUser(); 222 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($cpage1->id)); 223 224 $this->setUser($user1); 225 $this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($cpage1->id)); 226 227 $this->assertEquals(\core_search\manager::ACCESS_DELETED, $searcharea->check_access($cpage1->id + 10)); 228 } 229 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body