Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Dec 13, 2023
1 parent 318a026 commit 1150e63
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 36 deletions.
1 change: 1 addition & 0 deletions classes/local/query.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public function get_capability_string() {
*
* @param \context $context The context to check.
* @return bool true if the user has this capability. Otherwise false.
* @covers \report_customsql\local\query
*/
public function can_edit(\context $context): bool {
return has_capability('report/customsql:definequeries', $context);
Expand Down
73 changes: 42 additions & 31 deletions tests/external/external_get_users_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
* @category external
* @copyright 2020 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \report_customsql\external\get_users
* @runTestsInSeparateProcesses
*/
class external_get_users_test extends \externallib_advanced_testcase {
Expand Down Expand Up @@ -67,9 +68,11 @@ protected function setup_users(): array {
return [$admin, $manager, $coursecreateor];
}

public function test_get_users_site_config() {
public function test_get_users_site_config(): void {
global $OUTPUT;
$defaultuserimage = $OUTPUT->image_url('u/f2');

[$admin] = $this->setup_users();
$defaultuserimage = 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f2';

$result = get_users::execute('', 'moodle/site:config');
$result = \external_api::clean_returnvalue(get_users::execute_returns(), $result);
Expand All @@ -85,9 +88,11 @@ public function test_get_users_site_config() {
], $result);
}

public function test_get_users_site_viewreports() {
public function test_get_users_site_viewreports(): void {
global $OUTPUT;
$defaultuserimage = $OUTPUT->image_url('u/f2');

[$admin, $manager] = $this->setup_users();
$defaultuserimage = 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f2';

$result = get_users::execute('', 'moodle/site:viewreports');
$result = \external_api::clean_returnvalue(get_users::execute_returns(), $result);
Expand All @@ -110,43 +115,47 @@ public function test_get_users_site_viewreports() {
], $result);
}

public function test_get_users_customsql_view() {
public function test_get_users_customsql_view(): void {
global $OUTPUT;
$defaultuserimage = $OUTPUT->image_url('u/f2');

[$admin, $manager, $coursecreateor] = $this->setup_users();
$defaultuserimage = 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f2';

$result = get_users::execute('', 'report/customsql:view');
$result = \external_api::clean_returnvalue(get_users::execute_returns(), $result);

$this->assertEquals([
[
'id' => $coursecreateor->id,
'fullname' => fullname($coursecreateor),
'identity' => 'cc@example.com',
'hasidentity' => true,
'profileimageurlsmall' => $defaultuserimage,
],
[
'id' => $manager->id,
'fullname' => fullname($manager),
'identity' => 'manager@example.com',
'hasidentity' => true,
'profileimageurlsmall' => $defaultuserimage,
],
[
'id' => $admin->id,
'fullname' => fullname($admin),
'identity' => 'admin@example.com',
'hasidentity' => true,
'profileimageurlsmall' => $defaultuserimage,
],
[
'id' => $coursecreateor->id,
'fullname' => fullname($coursecreateor),
'identity' => 'cc@example.com',
'hasidentity' => true,
'profileimageurlsmall' => $defaultuserimage,
],
[
'id' => $manager->id,
'fullname' => fullname($manager),
'identity' => 'manager@example.com',
'hasidentity' => true,
'profileimageurlsmall' => $defaultuserimage,
],
[
'id' => $admin->id,
'fullname' => fullname($admin),
'identity' => 'admin@example.com',
'hasidentity' => true,
'profileimageurlsmall' => $defaultuserimage,
],
], $result);
}

public function test_get_users_serch_without_admins() {
public function test_get_users_serch_without_admins(): void {
global $OUTPUT;
$defaultuserimage = $OUTPUT->image_url('u/f2');

global $OUTPUT;

[, $manager] = $this->setup_users();
$defaultuserimage = $OUTPUT->image_url('u/f2');

$result = get_users::execute('Man', 'report/customsql:view');
$result = \external_api::clean_returnvalue(get_users::execute_returns(), $result);
Expand All @@ -162,9 +171,11 @@ public function test_get_users_serch_without_admins() {
], $result);
}

public function test_get_users_serch_with_admin() {
public function test_get_users_serch_with_admin(): void {
global $OUTPUT;
$defaultuserimage = $OUTPUT->image_url('u/f2');

[$admin] = $this->setup_users();
$defaultuserimage = 'https://www.example.com/moodle/theme/image.php/_s/boost/core/1/u/f2';

$result = get_users::execute('n U', 'report/customsql:view');
$result = \external_api::clean_returnvalue(get_users::execute_returns(), $result);
Expand Down
1 change: 1 addition & 0 deletions tests/local/category_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @package report_customsql
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \report_customsql\local\category
*/
class category_test extends \advanced_testcase {
/**
Expand Down
1 change: 1 addition & 0 deletions tests/local/query_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
* @package report_customsql
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers report_customsql\local\query
*/
class query_test extends \advanced_testcase {
/**
Expand Down
1 change: 1 addition & 0 deletions tests/privacy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
* @package report_customsql
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @covers \report_customsql\privacy\provider
*/
class privacy_test extends \core_privacy\tests\provider_testcase {

Expand Down
28 changes: 23 additions & 5 deletions tests/report_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class report_test extends \advanced_testcase {
*
* @return array
*/
public function get_week_starts_provider(): array {
public static function get_week_starts_provider(): array {
return [
// Start weekday is Sunday.
[0, '12:36 11 November 2009', '00:00 8 November 2009', '00:00 1 November 2009'],
Expand All @@ -61,6 +61,7 @@ public function get_week_starts_provider(): array {
* @param string $lastweek
*
* @dataProvider get_week_starts_provider
* @covers ::report_customsql_get_week_starts
*/
public function test_get_week_starts(
int $startwday, string $datestr, string $currentweek, string $lastweek): void {
Expand All @@ -82,6 +83,7 @@ public function test_get_week_starts(
* @return void
*
* @dataProvider get_week_starts_provider
* @covers ::report_customsql_get_week_starts
*/
public function test_get_week_starts_use_calendar_default(
int $startwday, string $datestr, string $currentweek, string $lastweek): void {
Expand All @@ -95,6 +97,7 @@ public function test_get_week_starts_use_calendar_default(
$this->assertEquals($expected, report_customsql_get_week_starts(strtotime($datestr)));
}

/** @covers ::report_customsql_get_month_starts */
public function test_get_month_starts_test(): void {
$this->assertEquals([
strtotime('00:00 1 November 2009'), strtotime('00:00 1 October 2009')],
Expand All @@ -109,6 +112,7 @@ public function test_get_month_starts_test(): void {
report_customsql_get_month_starts(strtotime('23:59 29 November 2009')));
}

/** @covers ::report_customsql_get_element_type */
public function test_report_customsql_get_element_type(): void {
$this->assertEquals('date_time_selector', report_customsql_get_element_type('start_date'));
$this->assertEquals('date_time_selector', report_customsql_get_element_type('startdate'));
Expand All @@ -120,12 +124,14 @@ public function test_report_customsql_get_element_type(): void {
$this->assertEquals('text', report_customsql_get_element_type('mandated'));
}

/** @covers ::report_customsql_substitute_user_token */
public function test_report_customsql_substitute_user_token(): void {
$this->assertEquals('SELECT COUNT(*) FROM oh_quiz_attempts WHERE user = 123',
report_customsql_substitute_user_token('SELECT COUNT(*) FROM oh_quiz_attempts '.
'WHERE user = %%USERID%%', 123));
}

/** @covers ::report_customsql_capability_options */
public function test_report_customsql_capability_options(): void {
$capoptions = [
'report/customsql:view' => get_string('anyonewhocanveiwthisreport', 'report_customsql'),
Expand All @@ -136,6 +142,7 @@ public function test_report_customsql_capability_options(): void {

}

/** @covers ::report_customsql_runable_options */
public function test_report_customsql_runable_options(): void {
$options = [
'manual' => get_string('manual', 'report_customsql'),
Expand All @@ -147,6 +154,7 @@ public function test_report_customsql_runable_options(): void {
$this->assertEquals($options, report_customsql_runable_options());
}

/** @covers ::report_customsql_daily_at_options */
public function test_report_customsql_daily_at_options(): void {
$time = [];
for ($h = 0; $h < 24; $h++) {
Expand All @@ -156,6 +164,7 @@ public function test_report_customsql_daily_at_options(): void {
$this->assertEquals($time, report_customsql_daily_at_options());
}

/** @covers ::report_customsql_email_options */
public function test_report_customsql_email_options(): void {
$options = [
'emailnumberofrows' => get_string('emailnumberofrows', 'report_customsql'),
Expand All @@ -164,16 +173,19 @@ public function test_report_customsql_email_options(): void {
$this->assertEquals($options, report_customsql_email_options());
}

/** @covers ::report_customsql_bad_words_list */
public function test_report_customsql_bad_words_list(): void {
$options = ['ALTER', 'CREATE', 'DELETE', 'DROP', 'GRANT', 'INSERT', 'INTO', 'TRUNCATE', 'UPDATE'];
$this->assertEquals($options, report_customsql_bad_words_list());
}

/** @covers ::report_customsql_bad_words_list */
public function test_report_customsql_contains_bad_word(): void {
$string = 'DELETE * FROM prefix_user u WHERE u.id > 0';
$this->assertEquals(1, report_customsql_contains_bad_word($string));
}

/** @covers ::report_customsql_get_daily_time_starts */
public function test_report_customsql_get_ready_to_run_daily_reports(): void {
global $DB;
$this->resetAfterTest(true);
Expand Down Expand Up @@ -247,13 +259,15 @@ public function test_report_customsql_get_ready_to_run_daily_reports(): void {
$this->assertTrue(report_customsql_is_daily_report_ready($report, $timenow));
}

/** @covers ::report_customsql_is_integer */
public function test_report_customsql_is_integer(): void {
$this->assertTrue(report_customsql_is_integer(1));
$this->assertTrue(report_customsql_is_integer('1'));
$this->assertFalse(report_customsql_is_integer('frog'));
$this->assertFalse(report_customsql_is_integer('2013-10-07'));
}

/** @covers ::report_customsql_get_table_headers */
public function test_report_customsql_get_table_headers(): void {
$rawheaders = [
'String date',
Expand All @@ -280,6 +294,7 @@ public function test_report_customsql_get_table_headers(): void {
$this->assertEquals([3 => 4, 4 => -1, 5 => 7, 7 => -1], $linkcolumns);
}

/** @covers ::report_customsql_pretify_column_names */
public function test_report_customsql_pretify_column_names(): void {
$row = new \stdClass();
$row->column = 1;
Expand All @@ -288,9 +303,9 @@ public function test_report_customsql_pretify_column_names(): void {
$query = "SELECT 1 AS First, 2 AS Column_URL, 3 AS column_3";
$this->assertEquals(['column', 'Column URL', 'column 3'],
report_customsql_pretify_column_names($row, $query));

}

/** @covers ::report_customsql_pretify_column_names */
public function test_report_customsql_pretify_column_names_multi_line(): void {
$row = new \stdClass();
$row->column = 1;
Expand All @@ -303,9 +318,9 @@ public function test_report_customsql_pretify_column_names_multi_line(): void {
FROM table";
$this->assertEquals(['column', 'Column URL', 'column 3'],
report_customsql_pretify_column_names($row, $query));

}

/** @covers ::report_customsql_pretify_column_names */
public function test_report_customsql_pretify_column_names_same_name_diff_capitialisation(): void {
$row = new \stdClass();
$row->course = 'B747-19B';
Expand All @@ -316,6 +331,7 @@ public function test_report_customsql_pretify_column_names_same_name_diff_capiti

}

/** @covers ::report_customsql_pretify_column_names */
public function test_report_customsql_pretify_column_names_issue(): void {
$row = new \stdClass();
$row->website = 'B747-19B';
Expand Down Expand Up @@ -346,6 +362,7 @@ public function test_report_customsql_pretify_column_names_issue(): void {

}

/** @covers ::report_customsql_display_row */
public function test_report_customsql_display_row(): void {
$rawdata = [
'Not a date',
Expand Down Expand Up @@ -373,7 +390,7 @@ public function test_report_customsql_display_row(): void {
/**
* Test plugin emailing of reports
*
* @return void
* @covers ::report_customsql_email_report
*/
public function test_report_customsql_email_report(): void {
global $CFG, $DB;
Expand Down Expand Up @@ -441,7 +458,7 @@ public function test_report_customsql_email_report(): void {
/**
* Test plugin downloading of reports.
*
* @return void
* @covers ::report_customsql_downloadurl
*/
public function test_report_custom_sql_download_report_url(): void {
global $DB;
Expand Down Expand Up @@ -478,6 +495,7 @@ public function test_report_custom_sql_download_report_url(): void {
$this->assertEquals($expected, $url->out(false));
}

/** @covers ::report_customsql_write_csv_row */
public function test_report_customsql_write_csv_row(): void {
global $CFG;
$this->resetAfterTest();
Expand Down

0 comments on commit 1150e63

Please sign in to comment.