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