-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactoring. Create separate phpunit test_basic_search_filtering_cach…
…ed() (#69)
- Loading branch information
1 parent
007b58d
commit 97e2a0c
Showing
1 changed file
with
114 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -31,6 +31,7 @@ | |
use coding_exception; | ||
use Exception; | ||
use local_wunderbyte_table\external\load_data; | ||
use local_wunderbyte_table\filters\types\datepicker; | ||
use local_wunderbyte_table\filters\types\standardfilter; | ||
use moodle_exception; | ||
|
||
|
@@ -41,6 +42,7 @@ | |
* @category test | ||
* @copyright 2025 Wunderbyte GmbH <[email protected]> | ||
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later | ||
* // @runTestsInSeparateProcesses | ||
* | ||
*/ | ||
final class base_test extends advanced_testcase { | ||
|
@@ -52,6 +54,15 @@ public function setUp(): void { | |
$this->resetAfterTest(true); | ||
} | ||
|
||
/** | ||
* Mandatory clean-up after each test. | ||
*/ | ||
public function tearDown(): void { | ||
parent::tearDown(); | ||
// Mandatory clean-up. | ||
cache_helper::purge_by_event('changesinwunderbytetable'); | ||
} | ||
|
||
/** | ||
* Test wb base functionality via webservice external class. | ||
* | ||
|
@@ -63,8 +74,6 @@ public function setUp(): void { | |
* | ||
*/ | ||
public function test_query_db_cached(): void { | ||
global $DB, $CFG; | ||
|
||
// First, we create ten courses. | ||
$this->create_test_courses(10); | ||
|
||
|
@@ -93,6 +102,62 @@ public function test_query_db_cached(): void { | |
// After purging, we expect 13. | ||
$this->assertEquals(13, $nrofrows); | ||
|
||
// Now we want to test pagination. | ||
$this->create_test_courses(30); | ||
|
||
// Now we purge the cache. | ||
cache_helper::purge_by_event('changesinwunderbytetable'); | ||
|
||
$nrofrows = $this->get_rowscount_for_table($table); | ||
|
||
$this->assertEquals(20, $nrofrows); | ||
|
||
// Now we fetch the third page. With 43 coures, we expect only three rows now. | ||
$nrofrows = $this->get_rowscount_for_table($table, 2); | ||
|
||
$this->assertEquals(3, $nrofrows); | ||
} | ||
|
||
/** | ||
* Test wb base search and filtering functionality via webservice external class. | ||
* | ||
* @covers \wunderbyte_table::query_db_cached | ||
* // @runInSeparateProcess | ||
* | ||
* @throws \coding_exception | ||
* @throws \dml_exception | ||
* | ||
*/ | ||
public function test_basic_search_filtering_cached(): void { | ||
// First, we create ten courses. | ||
$this->create_test_courses(10); | ||
// Now we create another three courses for basic searching and filtering. | ||
$this->create_test_courses(3, ['fullname' => 'filtercourse']); | ||
// Create 2 courses for end date filtering. | ||
$this->create_test_courses(1, [ | ||
'fullname' => 'ended1', | ||
'startdate' => strtotime('2 May 2010'), | ||
'enddate' => strtotime('20 May 2010'), | ||
]); | ||
$this->create_test_courses(1, [ | ||
'fullname' => 'ended2', | ||
'startdate' => strtotime('5 Jun 2020 14:00'), | ||
'enddate' => strtotime('15 Jun 2020 15:00'), | ||
]); | ||
$this->create_test_courses(1, [ | ||
'fullname' => 'future1', | ||
'startdate' => strtotime('1 March 2050 14:00'), | ||
'enddate' => strtotime('10 March 2050 15:00'), | ||
]); | ||
|
||
$user = $this->getDataGenerator()->create_user(); | ||
$this->setUser($user); | ||
|
||
$table = $this->create_demo2_table(); | ||
|
||
$nrofrows = $this->get_rowscount_for_table($table); | ||
$this->assertEquals(16, $nrofrows); | ||
|
||
// Search for courses by name. | ||
$nrofrows = $this->get_rowscount_for_table( | ||
$table, | ||
|
@@ -120,22 +185,32 @@ public function test_query_db_cached(): void { | |
); | ||
$this->assertEquals(1, $nrofrows); | ||
|
||
// Now we want to test pagination. | ||
$this->create_test_courses(30); | ||
|
||
// Now we purge the cache. | ||
cache_helper::purge_by_event('changesinwunderbytetable'); | ||
|
||
$nrofrows = $this->get_rowscount_for_table($table); | ||
|
||
$this->assertEquals(20, $nrofrows); | ||
|
||
// Now we fetch the third page. With 43 coures, we expect only three rows now. | ||
$nrofrows = $this->get_rowscount_for_table($table, 2); | ||
$nrofrows = $this->get_rowscount_for_table( | ||
$table, | ||
0, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
'ended' | ||
); | ||
$this->assertEquals(2, $nrofrows); | ||
|
||
// Validate basic filtering by course fullname. | ||
$nrofrows = $this->get_rowscount_for_table( | ||
$table, | ||
0, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
'{"fullname":["filtercourse"]}' | ||
); | ||
$this->assertEquals(3, $nrofrows); | ||
|
||
// Now we fetch the third page. With 43 coures, we expect only three rows now. | ||
$nrofrows = $this->get_rowscount_for_table( | ||
$table, | ||
0, | ||
|
@@ -144,10 +219,22 @@ public function test_query_db_cached(): void { | |
null, | ||
null, | ||
null, | ||
'{"fullname":["filtercourse"]}' | ||
'{"fullname":["ended2"]}' | ||
); | ||
$this->assertEquals(1, $nrofrows); | ||
|
||
$this->assertEquals(3, $nrofrows); | ||
$nrofrows = $this->get_rowscount_for_table( | ||
$table, | ||
0, | ||
null, | ||
null, | ||
null, | ||
null, | ||
null, | ||
//'{"enddate":{"Course end date":{"<":' . strtotime('today') . '}}}' | ||
"{\"enddate\":{\"Course end date\":{\"<\":1763528940}}}" | ||
); | ||
$this->assertEquals(2, $nrofrows); | ||
} | ||
|
||
/** | ||
|
@@ -178,6 +265,16 @@ public function create_demo2_table() { | |
$standardfilter = new standardfilter('fullname', 'fullname'); | ||
$table->add_filter($standardfilter); | ||
|
||
$datepicker = new datepicker('enddate', get_string('enddate')); | ||
// For the datepicker, we need to add special options. | ||
$datepicker->add_options( | ||
'standard', | ||
'<', | ||
get_string('apply_filter', 'local_wunderbyte_table'), | ||
'now', | ||
); | ||
$table->add_filter($datepicker); | ||
|
||
$table->set_filter_sql('*', "(SELECT * FROM {course} ORDER BY id ASC LIMIT 112) as s1", 'id > 1', ''); | ||
|
||
$table->pageable(true); | ||
|