31
31
use coding_exception ;
32
32
use Exception ;
33
33
use local_wunderbyte_table \external \load_data ;
34
+ use local_wunderbyte_table \filters \types \datepicker ;
34
35
use local_wunderbyte_table \filters \types \standardfilter ;
35
36
use moodle_exception ;
36
37
41
42
* @category test
42
43
* @copyright 2025 Wunderbyte GmbH <[email protected] >
43
44
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45
+ * // @runTestsInSeparateProcesses
44
46
*
45
47
*/
46
48
final class base_test extends advanced_testcase {
@@ -52,6 +54,15 @@ public function setUp(): void {
52
54
$ this ->resetAfterTest (true );
53
55
}
54
56
57
+ /**
58
+ * Mandatory clean-up after each test.
59
+ */
60
+ public function tearDown (): void {
61
+ parent ::tearDown ();
62
+ // Mandatory clean-up.
63
+ cache_helper::purge_by_event ('changesinwunderbytetable ' );
64
+ }
65
+
55
66
/**
56
67
* Test wb base functionality via webservice external class.
57
68
*
@@ -63,8 +74,6 @@ public function setUp(): void {
63
74
*
64
75
*/
65
76
public function test_query_db_cached (): void {
66
- global $ DB , $ CFG ;
67
-
68
77
// First, we create ten courses.
69
78
$ this ->create_test_courses (10 );
70
79
@@ -93,6 +102,62 @@ public function test_query_db_cached(): void {
93
102
// After purging, we expect 13.
94
103
$ this ->assertEquals (13 , $ nrofrows );
95
104
105
+ // Now we want to test pagination.
106
+ $ this ->create_test_courses (30 );
107
+
108
+ // Now we purge the cache.
109
+ cache_helper::purge_by_event ('changesinwunderbytetable ' );
110
+
111
+ $ nrofrows = $ this ->get_rowscount_for_table ($ table );
112
+
113
+ $ this ->assertEquals (20 , $ nrofrows );
114
+
115
+ // Now we fetch the third page. With 43 coures, we expect only three rows now.
116
+ $ nrofrows = $ this ->get_rowscount_for_table ($ table , 2 );
117
+
118
+ $ this ->assertEquals (3 , $ nrofrows );
119
+ }
120
+
121
+ /**
122
+ * Test wb base search and filtering functionality via webservice external class.
123
+ *
124
+ * @covers \wunderbyte_table::query_db_cached
125
+ * // @runInSeparateProcess
126
+ *
127
+ * @throws \coding_exception
128
+ * @throws \dml_exception
129
+ *
130
+ */
131
+ public function test_basic_search_filtering_cached (): void {
132
+ // First, we create ten courses.
133
+ $ this ->create_test_courses (10 );
134
+ // Now we create another three courses for basic searching and filtering.
135
+ $ this ->create_test_courses (3 , ['fullname ' => 'filtercourse ' ]);
136
+ // Create 2 courses for end date filtering.
137
+ $ this ->create_test_courses (1 , [
138
+ 'fullname ' => 'ended1 ' ,
139
+ 'startdate ' => strtotime ('2 May 2010 ' ),
140
+ 'enddate ' => strtotime ('20 May 2010 ' ),
141
+ ]);
142
+ $ this ->create_test_courses (1 , [
143
+ 'fullname ' => 'ended2 ' ,
144
+ 'startdate ' => strtotime ('5 Jun 2020 14:00 ' ),
145
+ 'enddate ' => strtotime ('15 Jun 2020 15:00 ' ),
146
+ ]);
147
+ $ this ->create_test_courses (1 , [
148
+ 'fullname ' => 'future1 ' ,
149
+ 'startdate ' => strtotime ('1 March 2050 14:00 ' ),
150
+ 'enddate ' => strtotime ('10 March 2050 15:00 ' ),
151
+ ]);
152
+
153
+ $ user = $ this ->getDataGenerator ()->create_user ();
154
+ $ this ->setUser ($ user );
155
+
156
+ $ table = $ this ->create_demo2_table ();
157
+
158
+ $ nrofrows = $ this ->get_rowscount_for_table ($ table );
159
+ $ this ->assertEquals (16 , $ nrofrows );
160
+
96
161
// Search for courses by name.
97
162
$ nrofrows = $ this ->get_rowscount_for_table (
98
163
$ table ,
@@ -120,22 +185,32 @@ public function test_query_db_cached(): void {
120
185
);
121
186
$ this ->assertEquals (1 , $ nrofrows );
122
187
123
- // Now we want to test pagination.
124
- $ this -> create_test_courses ( 30 );
125
-
126
- // Now we purge the cache.
127
- cache_helper:: purge_by_event ( ' changesinwunderbytetable ' );
128
-
129
- $ nrofrows = $ this -> get_rowscount_for_table ( $ table );
130
-
131
- $ this -> assertEquals ( 20 , $ nrofrows );
132
-
133
- // Now we fetch the third page. With 43 coures, we expect only three rows now.
134
- $ nrofrows = $ this ->get_rowscount_for_table ( $ table , 2 );
188
+ $ nrofrows = $ this -> get_rowscount_for_table (
189
+ $ table ,
190
+ 0 ,
191
+ null ,
192
+ null ,
193
+ null ,
194
+ null ,
195
+ null ,
196
+ null ,
197
+ ' ended '
198
+ );
199
+ $ this ->assertEquals ( 2 , $ nrofrows );
135
200
201
+ // Validate basic filtering by course fullname.
202
+ $ nrofrows = $ this ->get_rowscount_for_table (
203
+ $ table ,
204
+ 0 ,
205
+ null ,
206
+ null ,
207
+ null ,
208
+ null ,
209
+ null ,
210
+ '{"fullname":["filtercourse"]} '
211
+ );
136
212
$ this ->assertEquals (3 , $ nrofrows );
137
213
138
- // Now we fetch the third page. With 43 coures, we expect only three rows now.
139
214
$ nrofrows = $ this ->get_rowscount_for_table (
140
215
$ table ,
141
216
0 ,
@@ -144,10 +219,22 @@ public function test_query_db_cached(): void {
144
219
null ,
145
220
null ,
146
221
null ,
147
- '{"fullname":["filtercourse "]} '
222
+ '{"fullname":["ended2 "]} '
148
223
);
224
+ $ this ->assertEquals (1 , $ nrofrows );
149
225
150
- $ this ->assertEquals (3 , $ nrofrows );
226
+ $ nrofrows = $ this ->get_rowscount_for_table (
227
+ $ table ,
228
+ 0 ,
229
+ null ,
230
+ null ,
231
+ null ,
232
+ null ,
233
+ null ,
234
+ //'{"enddate":{"Course end date":{"<":' . strtotime('today') . '}}}'
235
+ "{ \"enddate \":{ \"Course end date \":{ \"< \":1763528940}}} "
236
+ );
237
+ $ this ->assertEquals (2 , $ nrofrows );
151
238
}
152
239
153
240
/**
@@ -178,6 +265,16 @@ public function create_demo2_table() {
178
265
$ standardfilter = new standardfilter ('fullname ' , 'fullname ' );
179
266
$ table ->add_filter ($ standardfilter );
180
267
268
+ $ datepicker = new datepicker ('enddate ' , get_string ('enddate ' ));
269
+ // For the datepicker, we need to add special options.
270
+ $ datepicker ->add_options (
271
+ 'standard ' ,
272
+ '< ' ,
273
+ get_string ('apply_filter ' , 'local_wunderbyte_table ' ),
274
+ 'now ' ,
275
+ );
276
+ $ table ->add_filter ($ datepicker );
277
+
181
278
$ table ->set_filter_sql ('* ' , "(SELECT * FROM {course} ORDER BY id ASC LIMIT 112) as s1 " , 'id > 1 ' , '' );
182
279
183
280
$ table ->pageable (true );
0 commit comments