Skip to content

Commit 21f57ef

Browse files
committed
Issue #66: Add phpunit test for check_access
1 parent 919f596 commit 21f57ef

File tree

1 file changed

+95
-46
lines changed

1 file changed

+95
-46
lines changed

tests/search/search_test.php

+95-46
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,49 @@
2222
* @author Tomo Tsuyuki <[email protected]>
2323
* @copyright 2024 Catalyst IT
2424
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25-
* @covers \mod_cms\search\cms
2625
*/
2726

2827
namespace mod_cms\search;
2928

3029
use mod_cms\local\model\cms_types;
31-
use mod_cms\customfield\cmsfield_handler;
3230

3331
defined('MOODLE_INTERNAL') || die();
3432

3533
global $CFG;
3634
require_once($CFG->dirroot . '/search/tests/fixtures/testable_core_search.php');
3735

36+
/**
37+
* Test class for cmsfield search.
38+
*
39+
* @package mod_cms
40+
* @category test
41+
* @author Tomo Tsuyuki <[email protected]>
42+
* @copyright 2024 Catalyst IT
43+
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
44+
* @coversDefaultClass \mod_cms\search\cmsfield
45+
*/
3846
class search_test extends \advanced_testcase {
3947

4048
/**
4149
* @var string Area id
4250
*/
4351
protected $cmsareaid = null;
4452

53+
/**
54+
* @var cms_types CMS type object
55+
*/
56+
protected $cmstype = null;
57+
58+
/**
59+
* @var \core_customfield\category_controller Custom field category object
60+
*/
61+
protected $fieldcategory = null;
62+
63+
/**
64+
* @var \core_customfield\field_controller Custom field object
65+
*/
66+
protected $field = null;
67+
4568
/**
4669
* Set up.
4770
*/
@@ -53,15 +76,35 @@ public function setUp(): void {
5376

5477
// Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
5578
$search = \testable_core_search::instance();
79+
80+
$cmstype = new cms_types();
81+
$cmstype->set('name', 'Overview')
82+
->set('idnumber', 'overview')
83+
->set('title_mustache', 'Overview');
84+
$cmstype->save();
85+
$fieldcategory = self::getDataGenerator()->create_custom_field_category([
86+
'name' => 'Other fields',
87+
'component' => 'mod_cms',
88+
'area' => 'cmsfield',
89+
'itemid' => $cmstype->get('id'),
90+
]);
91+
$field = self::getDataGenerator()->create_custom_field([
92+
'name' => 'Overview',
93+
'shortname' => 'overview',
94+
'type' => 'text',
95+
'categoryid' => $fieldcategory->get('id'),
96+
]);
97+
$this->cmstype = $cmstype;
98+
$this->fieldcategory = $fieldcategory;
99+
$this->field = $field;
56100
}
57101

58102
/**
59103
* Test search enabled.
60104
*
61105
* @return void
62106
*/
63-
public function test_search_enabled() {
64-
107+
public function test_search_enabled(): void {
65108
$searcharea = \core_search\manager::get_search_area($this->cmsareaid);
66109
list($componentname, $varname) = $searcharea->get_config_var_name();
67110

@@ -80,7 +123,7 @@ public function test_search_enabled() {
80123
*
81124
* @return void
82125
*/
83-
public function test_get_document_recordset() {
126+
public function test_get_document_recordset(): void {
84127
global $DB;
85128

86129
// Returns the instance as long as the area is supported.
@@ -89,60 +132,24 @@ public function test_get_document_recordset() {
89132

90133
$course = self::getDataGenerator()->create_course();
91134

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-
111135
// Name for cms is coming from "title_mustache" in cms_type.
112136
$generator = self::getDataGenerator()->get_plugin_generator('mod_cms');
113137
$record = new \stdClass();
114138
$record->course = $course->id;
115139
$record->customfield_overview = 'Test overview text 1';
116-
$record->typeid = $cmstype->get('id');
140+
$record->typeid = $this->cmstype->get('id');
117141
$generator->create_instance_with_data($record);
118142

119143
$record = new \stdClass();
120144
$record->course = $course->id;
121145
$record->customfield_overview = 'Test overview text 2';
122-
$record->typeid = $cmstype->get('id');
146+
$record->typeid = $this->cmstype->get('id');
123147
$generator->create_instance_with_data($record);
124148

125149
// All records.
126150
$recordset = $searcharea->get_document_recordset();
127151
$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-
}
152+
$this->assertEquals(2, iterator_count($recordset));
146153
$recordset->close();
147154

148155
// The +2 is to prevent race conditions.
@@ -157,11 +164,12 @@ public function test_get_document_recordset() {
157164
$record = new \stdClass();
158165
$record->course = $course->id;
159166
$record->customfield_overview = 'Test overview text 3';
160-
$record->typeid = $cmstype->get('id');
167+
$record->typeid = $this->cmstype->get('id');
161168
$generator->create_instance_with_data($record);
162169

163170
// Return only new search.
164171
$recordset = $searcharea->get_document_recordset(time());
172+
$count = 0;
165173
foreach ($recordset as $record) {
166174
$this->assertInstanceOf('stdClass', $record);
167175
$data = $DB->get_record('customfield_data', ['id' => $record->id]);
@@ -171,15 +179,56 @@ public function test_get_document_recordset() {
171179
$this->assertEquals($data->id, $doc->get('itemid'));
172180
$this->assertEquals($course->id, $doc->get('courseid'));
173181
$this->assertEquals($data->contextid, $doc->get('contextid'));
174-
$this->assertEquals($field->get('name'), $doc->get('title'));
182+
$this->assertEquals($this->field->get('name'), $doc->get('title'));
175183
$this->assertEquals($data->value, $doc->get('content'));
176184

177185
// Static caches are working.
178186
$dbreads = $DB->perf_get_reads();
179187
$doc = $searcharea->get_document($record);
180188
$this->assertEquals($dbreads, $DB->perf_get_reads());
181189
$this->assertInstanceOf('\core_search\document', $doc);
190+
$count++;
182191
}
192+
$this->assertEquals(1, $count);
183193
$recordset->close();
184194
}
195+
196+
/**
197+
* Document contents.
198+
*
199+
* @return void
200+
*/
201+
public function test_check_access(): void {
202+
global $DB;
203+
204+
// Returns the instance as long as the area is supported.
205+
$searcharea = \core_search\manager::get_search_area($this->cmsareaid);
206+
207+
$user1 = self::getDataGenerator()->create_user();
208+
$user2 = self::getDataGenerator()->create_user();
209+
$course = self::getDataGenerator()->create_course();
210+
211+
$this->getDataGenerator()->enrol_user($user1->id, $course->id, 'student');
212+
213+
// Name for cms is coming from "title_mustache" in cms_type.
214+
$generator = self::getDataGenerator()->get_plugin_generator('mod_cms');
215+
$record = new \stdClass();
216+
$record->course = $course->id;
217+
$record->customfield_overview = 'Test overview text 1';
218+
$record->typeid = $this->cmstype->get('id');
219+
$generator->create_instance_with_data($record);
220+
221+
$records = $DB->get_records('customfield_data', ['fieldid' => $this->field->get('id')]);
222+
$this->assertCount(1, $records);
223+
$data = current($records);
224+
225+
$this->setAdminUser();
226+
$this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data->id));
227+
228+
$this->setUser($user1);
229+
$this->assertEquals(\core_search\manager::ACCESS_GRANTED, $searcharea->check_access($data->id));
230+
231+
$this->setUser($user2);
232+
$this->assertEquals(\core_search\manager::ACCESS_DENIED, $searcharea->check_access($data->id));
233+
}
185234
}

0 commit comments

Comments
 (0)