Skip to content

Commit d6916b9

Browse files
committed
refactoring. Create separate phpunit test_basic_search_filtering_cached() (#69)
1 parent 4d8bd72 commit d6916b9

File tree

1 file changed

+114
-17
lines changed

1 file changed

+114
-17
lines changed

tests/base_test.php

Lines changed: 114 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
use coding_exception;
3232
use Exception;
3333
use local_wunderbyte_table\external\load_data;
34+
use local_wunderbyte_table\filters\types\datepicker;
3435
use local_wunderbyte_table\filters\types\standardfilter;
3536
use moodle_exception;
3637

@@ -41,6 +42,7 @@
4142
* @category test
4243
* @copyright 2025 Wunderbyte GmbH <[email protected]>
4344
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
45+
* // @runTestsInSeparateProcesses
4446
*
4547
*/
4648
final class base_test extends advanced_testcase {
@@ -52,6 +54,15 @@ public function setUp(): void {
5254
$this->resetAfterTest(true);
5355
}
5456

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+
5566
/**
5667
* Test wb base functionality via webservice external class.
5768
*
@@ -63,8 +74,6 @@ public function setUp(): void {
6374
*
6475
*/
6576
public function test_query_db_cached(): void {
66-
global $DB, $CFG;
67-
6877
// First, we create ten courses.
6978
$this->create_test_courses(10);
7079

@@ -93,6 +102,62 @@ public function test_query_db_cached(): void {
93102
// After purging, we expect 13.
94103
$this->assertEquals(13, $nrofrows);
95104

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+
96161
// Search for courses by name.
97162
$nrofrows = $this->get_rowscount_for_table(
98163
$table,
@@ -120,22 +185,32 @@ public function test_query_db_cached(): void {
120185
);
121186
$this->assertEquals(1, $nrofrows);
122187

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);
135200

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+
);
136212
$this->assertEquals(3, $nrofrows);
137213

138-
// Now we fetch the third page. With 43 coures, we expect only three rows now.
139214
$nrofrows = $this->get_rowscount_for_table(
140215
$table,
141216
0,
@@ -144,10 +219,22 @@ public function test_query_db_cached(): void {
144219
null,
145220
null,
146221
null,
147-
'{"fullname":["filtercourse"]}'
222+
'{"fullname":["ended2"]}'
148223
);
224+
$this->assertEquals(1, $nrofrows);
149225

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);
151238
}
152239

153240
/**
@@ -178,6 +265,16 @@ public function create_demo2_table() {
178265
$standardfilter = new standardfilter('fullname', 'fullname');
179266
$table->add_filter($standardfilter);
180267

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+
181278
$table->set_filter_sql('*', "(SELECT * FROM {course} ORDER BY id ASC LIMIT 112) as s1", 'id > 1', '');
182279

183280
$table->pageable(true);

0 commit comments

Comments
 (0)