Skip to content

Commit

Permalink
MDL-73659 phpunit: restore_date, api, rule, plugin, manager & helper
Browse files Browse the repository at this point in the history
All restore_date_test, api_test, rule_test, plugin_test,
manager_test, helper_test testcase classes:

- Namespaced with component[\level2-API]
- Moved to level2-API subdirectory when required.
- Fixed incorrect use statements with leading backslash.
- Remove file phpdoc block
- Remove MOODLE_INTERNAL if not needed.
- Changed code to point to global scope when needed.
- Fix some relative paths and comments here and there.
- All them passing individually.
- Complete runs passing too.

Special mention to:

- All restore_date_test cases have been put under xxx\backup
  level 2 (valid API) namespace.
  • Loading branch information
stronk7 committed Feb 4, 2022
1 parent 6652ec1 commit 7a0d024
Show file tree
Hide file tree
Showing 58 changed files with 1,605 additions and 1,973 deletions.
30 changes: 10 additions & 20 deletions admin/tool/cohortroles/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* API tests.
*
* @package tool_cohortroles
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();

use tool_cohortroles\api;
namespace tool_cohortroles;

/**
* API tests.
Expand All @@ -33,17 +23,17 @@
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_cohortroles_api_testcase extends advanced_testcase {
/** @var stdClass $cohort */
class api_test extends \advanced_testcase {
/** @var \stdClass $cohort */
protected $cohort = null;

/** @var stdClass $userassignto */
/** @var \stdClass $userassignto */
protected $userassignto = null;

/** @var stdClass $userassignover */
/** @var \stdClass $userassignover */
protected $userassignover = null;

/** @var stdClass $role */
/** @var \stdClass $role */
protected $role = null;

/**
Expand All @@ -67,7 +57,7 @@ public function test_create_cohort_role_assignment_without_permission() {
'roleid' => $this->roleid,
'cohortid' => $this->cohort->id
);
$this->expectException(required_capability_exception::class);
$this->expectException(\required_capability_exception::class);
api::create_cohort_role_assignment($params);
}

Expand Down Expand Up @@ -105,7 +95,7 @@ public function test_delete_cohort_role_assignment_without_permission() {
);
$result = api::create_cohort_role_assignment($params);
$this->setUser($this->userassignto);
$this->expectException(required_capability_exception::class);
$this->expectException(\required_capability_exception::class);
api::delete_cohort_role_assignment($result->get('id'));
}

Expand All @@ -117,7 +107,7 @@ public function test_delete_cohort_role_assignment_with_invalid_data() {
'cohortid' => $this->cohort->id
);
$result = api::create_cohort_role_assignment($params);
$this->expectException(dml_missing_record_exception::class);
$this->expectException(\dml_missing_record_exception::class);
api::delete_cohort_role_assignment($result->get('id') + 1);
}

Expand Down Expand Up @@ -274,7 +264,7 @@ public function test_sync_all_cohort_roles() {
$this->assertEquals($sync, $expected);

// Verify roles assigned by any other component are not removed.
$usercontext = context_user::instance($this->userassignover->id);
$usercontext = \context_user::instance($this->userassignover->id);
role_assign($this->roleid, $this->userassignto->id, $usercontext->id);
$sync = api::sync_all_cohort_roles();

Expand Down
67 changes: 26 additions & 41 deletions admin/tool/dataprivacy/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,37 +14,22 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* API tests.
*
* @package tool_dataprivacy
* @copyright 2018 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_dataprivacy;

use core\invalid_persistent_exception;
use core\task\manager;
use tool_dataprivacy\context_instance;
use tool_dataprivacy\api;
use tool_dataprivacy\data_registry;
use tool_dataprivacy\expired_context;
use tool_dataprivacy\data_request;
use tool_dataprivacy\purpose;
use tool_dataprivacy\category;
use testing_data_generator;
use tool_dataprivacy\local\helper;
use tool_dataprivacy\task\process_data_request_task;

defined('MOODLE_INTERNAL') || die();
global $CFG;

/**
* API tests.
*
* @package tool_dataprivacy
* @copyright 2018 Jun Pataleta
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_dataprivacy_api_testcase extends advanced_testcase {
class api_test extends \advanced_testcase {

/**
* Ensure that the check_can_manage_data_registry function fails cap testing when a user without capabilities is
Expand All @@ -68,7 +53,7 @@ public function test_check_can_manage_data_registry_without_cap_default() {
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

$this->expectException(required_capability_exception::class);
$this->expectException(\required_capability_exception::class);
api::check_can_manage_data_registry();
}

Expand All @@ -82,7 +67,7 @@ public function test_check_can_manage_data_registry_without_cap_system() {
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

$this->expectException(required_capability_exception::class);
$this->expectException(\required_capability_exception::class);
api::check_can_manage_data_registry(\context_system::instance()->id);
}

Expand All @@ -96,7 +81,7 @@ public function test_check_can_manage_data_registry_without_cap_own_user() {
$user = $this->getDataGenerator()->create_user();
$this->setUser($user);

$this->expectException(required_capability_exception::class);
$this->expectException(\required_capability_exception::class);
api::check_can_manage_data_registry(\context_user::instance($user->id)->id);
}

Expand Down Expand Up @@ -174,7 +159,7 @@ public function test_get_site_dpos() {
$u1 = $generator->create_user();
$u2 = $generator->create_user();

$context = context_system::instance();
$context = \context_system::instance();

// Give the manager role with the capability to manage data requests.
$managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
Expand Down Expand Up @@ -215,7 +200,7 @@ public function test_get_assigned_privacy_officer_roles() {
// Confirm that the returned list is empty.
$this->assertEmpty($roleids);

$context = context_system::instance();
$context = \context_system::instance();

// Give the manager role with the capability to manage data requests.
assign_capability('tool/dataprivacy:managedatarequests', CAP_ALLOW, $managerroleid, $context->id, true);
Expand Down Expand Up @@ -254,7 +239,7 @@ public function test_approve_data_request() {
$s1 = $generator->create_user();
$u1 = $generator->create_user();

$context = context_system::instance();
$context = \context_system::instance();

// Manager role.
$managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
Expand Down Expand Up @@ -304,7 +289,7 @@ public function test_approve_data_request_non_dpo_user() {

// Login as a user without DPO role.
$this->setUser($teacher);
$this->expectException(required_capability_exception::class);
$this->expectException(\required_capability_exception::class);
api::approve_data_request($requestid);
}

Expand All @@ -327,8 +312,8 @@ public function test_reject_data_deletion_request_primary_admin() {
$this->assertEquals(api::DATAREQUEST_STATUS_REJECTED, $request->get('status'));

// Confirm they weren't deleted.
$user = core_user::get_user($request->get('userid'));
core_user::require_active_user($user);
$user = \core_user::get_user($request->get('userid'));
\core_user::require_active_user($user);
}

/**
Expand Down Expand Up @@ -366,7 +351,7 @@ public function test_can_manage_data_requests() {
$nondpocapable = $generator->create_user();
$nondpoincapable = $generator->create_user();

$context = context_system::instance();
$context = \context_system::instance();

// Manager role.
$managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
Expand Down Expand Up @@ -666,8 +651,8 @@ public function test_create_data_request_by_parent() {
$comment = 'sample comment';

// Get the teacher role pretend it's the parent roles ;).
$systemcontext = context_system::instance();
$usercontext = context_user::instance($user->id);
$systemcontext = \context_system::instance();
$usercontext = \context_user::instance($user->id);
$parentroleid = $DB->get_field('role', 'id', array('shortname' => 'teacher'));
// Give the manager role with the capability to manage data requests.
assign_capability('tool/dataprivacy:makedatarequestsforchildren', CAP_ALLOW, $parentroleid, $systemcontext->id, true);
Expand Down Expand Up @@ -923,7 +908,7 @@ public function test_is_site_dpo() {
$dpo = $generator->create_user();
$nondpo = $generator->create_user();

$context = context_system::instance();
$context = \context_system::instance();

// Manager role.
$managerroleid = $DB->get_field('role', 'id', array('shortname' => 'manager'));
Expand Down Expand Up @@ -1222,7 +1207,7 @@ public function test_get_effective_context_unset() {
*/
public function test_set_contextlevel_invalid_contextlevels($contextlevel) {

$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
api::set_contextlevel((object) [
'contextlevel' => $contextlevel,
]);
Expand Down Expand Up @@ -1291,7 +1276,7 @@ public function test_effective_contextlevel_invalid_contextlevels($contextlevel)
'categoryid' => $category1->get('id'),
]);

$this->expectException(coding_exception::class);
$this->expectException(\coding_exception::class);
api::get_effective_contextlevel_purpose($contextlevel);
}

Expand Down Expand Up @@ -1908,14 +1893,14 @@ public function test_set_context_defaults($contextlevel, $inheritcategory, $inhe
$assign = $generator->create_module('assign', ['course' => $course->id]);
$forum = $generator->create_module('forum', ['course' => $course->id]);

$coursecatcontext = context_coursecat::instance($coursecat->id);
$coursecontext = context_course::instance($course->id);
$blockcontext = context_block::instance($block->id);
$coursecatcontext = \context_coursecat::instance($coursecat->id);
$coursecontext = \context_course::instance($course->id);
$blockcontext = \context_block::instance($block->id);

list($course, $assigncm) = get_course_and_cm_from_instance($assign->id, 'assign');
list($course, $forumcm) = get_course_and_cm_from_instance($forum->id, 'forum');
$assigncontext = context_module::instance($assigncm->id);
$forumcontext = context_module::instance($forumcm->id);
$assigncontext = \context_module::instance($assigncm->id);
$forumcontext = \context_module::instance($forumcm->id);

// Generate purposes and categories.
$category1 = api::create_category((object)['name' => 'Test category 1']);
Expand Down Expand Up @@ -2199,8 +2184,8 @@ public function test_can_create_data_deletion_request_for_self_no() {
$this->resetAfterTest();
$userid = $this->getDataGenerator()->create_user()->id;
$roleid = $this->getDataGenerator()->create_role();
assign_capability('tool/dataprivacy:requestdelete', CAP_PROHIBIT, $roleid, context_user::instance($userid));
role_assign($roleid, $userid, context_user::instance($userid));
assign_capability('tool/dataprivacy:requestdelete', CAP_PROHIBIT, $roleid, \context_user::instance($userid));
role_assign($roleid, $userid, \context_user::instance($userid));
$this->setUser($userid);
$this->assertFalse(api::can_create_data_deletion_request_for_self());
}
Expand Down Expand Up @@ -2269,7 +2254,7 @@ public function test_can_create_data_deletion_request_for_other_yes() {
$this->resetAfterTest();
$userid = $this->getDataGenerator()->create_user()->id;
$roleid = $this->getDataGenerator()->create_role();
$contextsystem = context_system::instance();
$contextsystem = \context_system::instance();
assign_capability('tool/dataprivacy:requestdeleteforotheruser', CAP_ALLOW, $roleid, $contextsystem);
role_assign($roleid, $userid, $contextsystem);
$this->setUser($userid);
Expand Down
7 changes: 3 additions & 4 deletions admin/tool/log/tests/manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,16 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

namespace tool_log;

/**
* Log manager and log API tests.
*
* @package tool_log
* @copyright 2014 Petr Skoda {@link http://skodak.org/}
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die;

class tool_log_manager_testcase extends advanced_testcase {
class manager_test extends \advanced_testcase {
public function test_get_log_manager() {
global $CFG;
$this->resetAfterTest();
Expand Down
15 changes: 2 additions & 13 deletions admin/tool/messageinbound/tests/manager_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Manager tests.
*
* @package tool_messageinbound
* @category test
* @copyright 2018 Frédéric Massart
* @author Frédéric Massart <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

defined('MOODLE_INTERNAL') || die();
global $CFG;
namespace tool_messageinbound;

use core_privacy\tests\provider_testcase;
use core_privacy\local\request\approved_contextlist;
Expand All @@ -42,7 +31,7 @@
* @author Frédéric Massart <[email protected]>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_messageinbound_manager_testcase extends provider_testcase {
class manager_test extends provider_testcase {

public function setUp(): void {
global $CFG;
Expand Down
14 changes: 2 additions & 12 deletions admin/tool/mobile/tests/api_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,24 +14,14 @@
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Moodle Mobile admin tool api tests.
*
* @package tool_mobile
* @category external
* @copyright 2016 Juan Leyva
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
namespace tool_mobile;

defined('MOODLE_INTERNAL') || die();

global $CFG;

require_once($CFG->dirroot . '/webservice/tests/helpers.php');

use tool_mobile\api;

/**
* Moodle Mobile admin tool api tests.
*
Expand All @@ -40,7 +30,7 @@
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
* @since Moodle 3.1
*/
class tool_mobile_api_testcase extends externallib_advanced_testcase {
class api_test extends \externallib_advanced_testcase {

/**
* Test get_autologin_key.
Expand Down
Loading

0 comments on commit 7a0d024

Please sign in to comment.