22
22
* @author Tomo Tsuyuki <[email protected] >
23
23
* @copyright 2024 Catalyst IT
24
24
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
25
- * @covers \mod_cms\search\cms
26
25
*/
27
26
28
27
namespace mod_cms \search ;
29
28
30
29
use mod_cms \local \model \cms_types ;
31
- use mod_cms \customfield \cmsfield_handler ;
32
30
33
31
defined ('MOODLE_INTERNAL ' ) || die ();
34
32
35
33
global $ CFG ;
36
34
require_once ($ CFG ->dirroot . '/search/tests/fixtures/testable_core_search.php ' );
37
35
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
+ */
38
46
class search_test extends \advanced_testcase {
39
47
40
48
/**
41
49
* @var string Area id
42
50
*/
43
51
protected $ cmsareaid = null ;
44
52
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
+
45
68
/**
46
69
* Set up.
47
70
*/
@@ -53,15 +76,35 @@ public function setUp(): void {
53
76
54
77
// Set \core_search::instance to the mock_search_engine as we don't require the search engine to be working to test this.
55
78
$ 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 ;
56
100
}
57
101
58
102
/**
59
103
* Test search enabled.
60
104
*
61
105
* @return void
62
106
*/
63
- public function test_search_enabled () {
64
-
107
+ public function test_search_enabled (): void {
65
108
$ searcharea = \core_search \manager::get_search_area ($ this ->cmsareaid );
66
109
list ($ componentname , $ varname ) = $ searcharea ->get_config_var_name ();
67
110
@@ -80,7 +123,7 @@ public function test_search_enabled() {
80
123
*
81
124
* @return void
82
125
*/
83
- public function test_get_document_recordset () {
126
+ public function test_get_document_recordset (): void {
84
127
global $ DB ;
85
128
86
129
// Returns the instance as long as the area is supported.
@@ -89,60 +132,24 @@ public function test_get_document_recordset() {
89
132
90
133
$ course = self ::getDataGenerator ()->create_course ();
91
134
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
135
// Name for cms is coming from "title_mustache" in cms_type.
112
136
$ generator = self ::getDataGenerator ()->get_plugin_generator ('mod_cms ' );
113
137
$ record = new \stdClass ();
114
138
$ record ->course = $ course ->id ;
115
139
$ record ->customfield_overview = 'Test overview text 1 ' ;
116
- $ record ->typeid = $ cmstype ->get ('id ' );
140
+ $ record ->typeid = $ this -> cmstype ->get ('id ' );
117
141
$ generator ->create_instance_with_data ($ record );
118
142
119
143
$ record = new \stdClass ();
120
144
$ record ->course = $ course ->id ;
121
145
$ record ->customfield_overview = 'Test overview text 2 ' ;
122
- $ record ->typeid = $ cmstype ->get ('id ' );
146
+ $ record ->typeid = $ this -> cmstype ->get ('id ' );
123
147
$ generator ->create_instance_with_data ($ record );
124
148
125
149
// All records.
126
150
$ recordset = $ searcharea ->get_document_recordset ();
127
151
$ 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 ));
146
153
$ recordset ->close ();
147
154
148
155
// The +2 is to prevent race conditions.
@@ -157,11 +164,12 @@ public function test_get_document_recordset() {
157
164
$ record = new \stdClass ();
158
165
$ record ->course = $ course ->id ;
159
166
$ record ->customfield_overview = 'Test overview text 3 ' ;
160
- $ record ->typeid = $ cmstype ->get ('id ' );
167
+ $ record ->typeid = $ this -> cmstype ->get ('id ' );
161
168
$ generator ->create_instance_with_data ($ record );
162
169
163
170
// Return only new search.
164
171
$ recordset = $ searcharea ->get_document_recordset (time ());
172
+ $ count = 0 ;
165
173
foreach ($ recordset as $ record ) {
166
174
$ this ->assertInstanceOf ('stdClass ' , $ record );
167
175
$ data = $ DB ->get_record ('customfield_data ' , ['id ' => $ record ->id ]);
@@ -171,15 +179,56 @@ public function test_get_document_recordset() {
171
179
$ this ->assertEquals ($ data ->id , $ doc ->get ('itemid ' ));
172
180
$ this ->assertEquals ($ course ->id , $ doc ->get ('courseid ' ));
173
181
$ 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 ' ));
175
183
$ this ->assertEquals ($ data ->value , $ doc ->get ('content ' ));
176
184
177
185
// Static caches are working.
178
186
$ dbreads = $ DB ->perf_get_reads ();
179
187
$ doc = $ searcharea ->get_document ($ record );
180
188
$ this ->assertEquals ($ dbreads , $ DB ->perf_get_reads ());
181
189
$ this ->assertInstanceOf ('\core_search\document ' , $ doc );
190
+ $ count ++;
182
191
}
192
+ $ this ->assertEquals (1 , $ count );
183
193
$ recordset ->close ();
184
194
}
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
+ }
185
234
}
0 commit comments