Skip to content

Commit

Permalink
refactoring. Create separate phpunit test_basic_search_filtering_cach…
Browse files Browse the repository at this point in the history
…ed() (#69)
  • Loading branch information
semteacher committed Jan 9, 2025
1 parent 4d8bd72 commit d6916b9
Showing 1 changed file with 114 additions and 17 deletions.
131 changes: 114 additions & 17 deletions tests/base_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand All @@ -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 {
Expand All @@ -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.
*
Expand All @@ -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);

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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);
}

/**
Expand Down Expand Up @@ -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);
Expand Down

0 comments on commit d6916b9

Please sign in to comment.