mirror of
https://github.com/moodle/moodle.git
synced 2025-08-07 09:56:38 +02:00
MDL-78807 reportbuilder: ensure report base conditions are non-empty.
This commit is contained in:
parent
a1d5d1b2f7
commit
e4e7e59d90
3 changed files with 83 additions and 3 deletions
|
@ -21,6 +21,7 @@ namespace core_reportbuilder\local\report;
|
|||
use advanced_testcase;
|
||||
use coding_exception;
|
||||
use context_system;
|
||||
use core_reportbuilder\local\helpers\database;
|
||||
use core_reportbuilder\system_report_available;
|
||||
use core_reportbuilder\system_report_factory;
|
||||
use lang_string;
|
||||
|
@ -70,6 +71,80 @@ class base_test extends advanced_testcase {
|
|||
$this->assertEmpty($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for adding SQL base condition to a report
|
||||
*/
|
||||
public function test_add_base_condition_sql(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$parameter = database::generate_param_name();
|
||||
|
||||
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
||||
$systemreport->add_base_condition_sql("username = :{$parameter}", [$parameter => 'admin']);
|
||||
|
||||
[$where, $params] = $systemreport->get_base_condition();
|
||||
$this->assertEquals("username = :{$parameter}", $where);
|
||||
$this->assertEquals([$parameter => 'admin'], $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for adding multiple SQL base condition to a report
|
||||
*/
|
||||
public function test_add_base_condition_sql_multiple(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
[$paramusername, $paramemail] = database::generate_param_names(2);
|
||||
|
||||
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
||||
$systemreport->add_base_condition_sql("username = :{$paramusername}", [$paramusername => 'admin']);
|
||||
$systemreport->add_base_condition_sql("email = :{$paramemail}", [$paramemail => 'admin@example.com']);
|
||||
|
||||
[$where, $params] = $systemreport->get_base_condition();
|
||||
$this->assertEquals("username = :{$paramusername} AND email = :{$paramemail}", $where);
|
||||
$this->assertEquals([$paramusername => 'admin', $paramemail => 'admin@example.com'], $params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for adding empty SQL base condition to a report
|
||||
*/
|
||||
public function test_add_base_condition_sql_empty_clause(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
||||
$systemreport->add_base_condition_sql('username IS NOT NULL');
|
||||
$systemreport->add_base_condition_sql('');
|
||||
|
||||
[$where, $params] = $systemreport->get_base_condition();
|
||||
$this->assertEquals("username IS NOT NULL", $where);
|
||||
$this->assertEmpty($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for adding SQL base condition to a report with invalid parameter
|
||||
*/
|
||||
public function test_add_base_condition_sql_invalid_parameter(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
||||
|
||||
$this->expectException(coding_exception::class);
|
||||
$this->expectExceptionMessage('Invalid parameter names');
|
||||
$systemreport->add_base_condition_sql("username = :param", ['param' => 'admin']);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test getting report base conditions, where none have been set
|
||||
*/
|
||||
public function test_get_base_condition_default(): void {
|
||||
$this->resetAfterTest();
|
||||
|
||||
$systemreport = system_report_factory::create(system_report_available::class, context_system::instance());
|
||||
|
||||
[$where, $params] = $systemreport->get_base_condition();
|
||||
$this->assertEmpty($where);
|
||||
$this->assertEmpty($params);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test for get_filter_instances
|
||||
*/
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue