Skip to content

Commit

Permalink
Move unit tests into the recommended namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
timhunt committed Mar 18, 2022
1 parent ef784d9 commit 658cc04
Show file tree
Hide file tree
Showing 6 changed files with 93 additions and 100 deletions.
7 changes: 2 additions & 5 deletions changes.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
# Change log for the Ad-hoc database queries report

## Changes in 4.3

* This version works with Moodle 4.0.

## Changes in 4.2

* ...
* This version works with Moodle 4.0.
* There is now an option to fetch query results via webservice/pluginfile.php, once the permissions are set up right.
* Better navigation when there are a lot of reports. There is now a separate page for each category,
as well as the main index page.
* When editing queries, the input box for the report name is bigger.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

defined('MOODLE_INTERNAL') || die();
namespace report_customsql\local;

use report_customsql\local\category;
defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/report/customsql/locallib.php');
Expand All @@ -28,7 +28,7 @@
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_customsql_local_category_testcase extends advanced_testcase {
class _category_test extends \advanced_testcase {
/**
* Test create category.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,21 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

defined('MOODLE_INTERNAL') || die();
namespace report_customsql\local;

use report_customsql\local\query;
defined('MOODLE_INTERNAL') || die();

global $CFG;
require_once($CFG->dirroot . '/report/customsql/locallib.php');


/**
* Tests for the report_customsql\local\query.
*
* @package report_customsql
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class report_customsql_local_query_testcase extends advanced_testcase {
class query_test extends \advanced_testcase {
/**
* Test create query.
*/
Expand All @@ -56,7 +55,7 @@ public function test_create_query() {
$query->get_time_note());
$this->assertEquals('Only administrators (moodle/site:config)', $query->get_capability_string());
// Admin user should have capability to edit and view queries.
$this->assertEquals(true, $query->can_edit(context_system::instance()));
$this->assertEquals(true, $query->can_view(context_system::instance()));
$this->assertEquals(true, $query->can_edit(\context_system::instance()));
$this->assertEquals(true, $query->can_view(\context_system::instance()));
}
}
54 changes: 27 additions & 27 deletions tests/privacy_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,31 +14,31 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Unit tests for the report_customsql implementation of the privacy API.
*
* @package report_customsql
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace report_customsql;

use \report_customsql\privacy\provider;
use report_customsql\privacy\provider;
use core_privacy\local\request;

/**
* Unit tests for the report_customsql implementation of the privacy API.
*
* @package report_customsql
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class privacy_test extends \core_privacy\tests\provider_testcase {

/** @var \stdClass test user. */
protected $user1;
/** @var \stdClass test user. */
protected $user2;
/** @var \stdClass test user. */
protected $user3;

public function setUp(): void {
$this->resetAfterTest();
$this->setAdminUser();

$this->component = 'report_customsql';
$this->systemcontext = \context_system::instance();
$this->user1 = $this->getDataGenerator()->create_user(['username' => 'manager1']);
$this->create_customsql_row($this->user1->id, 'Report of user 1');
$this->user2 = $this->getDataGenerator()->create_user(['username' => 'manager2']);
Expand All @@ -50,7 +50,7 @@ public function setUp(): void {
/**
* Test for provider::get_contexts_for_userid().
*/
public function test_get_contexts_for_userid() {
public function test_get_contexts_for_userid(): void {
$contexts = provider::get_contexts_for_userid($this->user1->id)->get_contexts();
$this->assertEquals(1, count($contexts));
$this->assertEquals(CONTEXT_SYSTEM, reset($contexts)->contextlevel);
Expand All @@ -59,8 +59,8 @@ public function test_get_contexts_for_userid() {
/**
* Test fetching users within a context.
*/
public function test_get_users_in_context() {
$userlist = new request\userlist($this->systemcontext, $this->component);
public function test_get_users_in_context(): void {
$userlist = new request\userlist(\context_system::instance(), 'report_customsql');
provider::get_users_in_context($userlist);
$actual = $userlist->get_userids();
sort($actual);
Expand All @@ -71,13 +71,12 @@ public function test_get_users_in_context() {

/**
* Test Export all user data for the specified user.
*
* @return null
*/
public function test_export_user_data() {
$approvedcontextlist = new request\approved_contextlist($this->user1, $this->component, [$this->systemcontext->id]);
public function test_export_user_data(): void {
$context = \context_system::instance();
$approvedcontextlist = new request\approved_contextlist($this->user1, 'report_customsql', [$context->id]);
provider::export_user_data($approvedcontextlist);
$writer = request\writer::with_context($this->systemcontext);
$writer = request\writer::with_context($context);
$this->assertTrue($writer->has_any_data());
$subcontext = [
get_string('privacy:metadata:reportcustomsqlqueries', 'report_customsql')
Expand All @@ -89,13 +88,13 @@ public function test_export_user_data() {
/**
* Test for provider::delete_data_for_all_users_in_context().
*/
public function test_delete_data_for_all_users_in_context() {
public function test_delete_data_for_all_users_in_context(): void {
global $DB;

$adminuserid = get_admin()->id;
$count = $DB->count_records('report_customsql_queries', ['usermodified' => $adminuserid]);
$this->assertEquals(0, $count);
provider::delete_data_for_all_users_in_context($this->systemcontext);
provider::delete_data_for_all_users_in_context(\context_system::instance());

// All records should be set usermodified to adminuserid.
$count = $DB->count_records('report_customsql_queries', ['usermodified' => $adminuserid]);
Expand All @@ -105,13 +104,13 @@ public function test_delete_data_for_all_users_in_context() {
/**
* Test for provider::delete_data_for_user().
*/
public function test_delete_data_for_user() {
public function test_delete_data_for_user(): void {
global $DB;

$count = $DB->count_records('report_customsql_queries', ['usermodified' => $this->user1->id]);
$this->assertEquals(1, $count);
$contextlist = provider::get_contexts_for_userid($this->user1->id);
$approvedcontextlist = new request\approved_contextlist($this->user1, $this->component, $contextlist->get_contextids());
$approvedcontextlist = new request\approved_contextlist($this->user1, 'report_customsql', $contextlist->get_contextids());
provider::delete_data_for_user($approvedcontextlist);
$count = $DB->count_records('report_customsql_queries', ['usermodified' => $this->user1->id]);
$this->assertEquals(0, $count);
Expand All @@ -120,13 +119,13 @@ public function test_delete_data_for_user() {
/**
* Test that data for users in approved userlist is deleted.
*/
public function test_delete_data_for_users() {
public function test_delete_data_for_users(): void {
global $DB;

$userlistids = [$this->user1->id, $this->user2->id];

// We just delete reports of user1 and user2.
$approvedlist = new request\approved_userlist($this->systemcontext, $this->component, $userlistids);
$approvedlist = new request\approved_userlist(\context_system::instance(), 'report_customsql', $userlistids);
provider::delete_data_for_users($approvedlist);

$count = $DB->count_records('report_customsql_queries', ['usermodified' => $this->user1->id]);
Expand All @@ -139,12 +138,13 @@ public function test_delete_data_for_users() {
* Create an entry in 'report_customsql_queries' table and return the id
*
* @param int $userid
* @param string $displayname
* @return int the new query id.
*/
private function create_customsql_row($userid, $displayname) {
private function create_customsql_row(int $userid, string $displayname): int {
global $DB;
$report = new stdClass();
$time = \report_customsql\utils::time();
$report = new \stdClass();
$time = utils::time();
$report->displayname = $displayname;
$report->description = 'test description';
$report->descriptionformat = '1';
Expand Down
Loading

0 comments on commit 658cc04

Please sign in to comment.