Skip to content

Commit

Permalink
MDL-73485 phpunit: externallib, generator and filter tescase names
Browse files Browse the repository at this point in the history
All externallib_test, generator_test and filter_test classes:

- Namespaced with component[\level2-API]
- Moved to level2-API subdirectory when required.
- Fixed incorrect use statements with leading backslash.
- 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 tests under testing/tests:

1) The core_testing component doesn't exist.
2) But testing/tests are allowed because there is a suite pointing to it (phpunit.xml).
3) So, the only possible namespace for them is "core".
4) And to avoid problems with other core testcases (under lib/tests)
   they have been renamed to have testing_xxxx as prefix.

Finally, also modified calendar/tests/events/events_test.php because it uses
some renamed (core_calendar_externallib_testcase => \core_calendar\externallib_test)
classes.
  • Loading branch information
stronk7 committed Jan 21, 2022
1 parent e63604f commit 100bc51
Show file tree
Hide file tree
Showing 85 changed files with 2,338 additions and 2,605 deletions.
97 changes: 46 additions & 51 deletions admin/tool/lp/tests/externallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,22 +13,10 @@
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.
/**
* External learning plans webservice API tests.
*
* @package tool_lp
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/

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

global $CFG;

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

use core_competency\api;
use tool_lp\external;
use core_competency\invalid_persistent_exception;
use core_competency\plan;
use core_competency\related_competency;
Expand All @@ -38,6 +26,13 @@
use core_competency\template;
use core_competency\template_competency;
use core_competency\course_competency_settings;
use externallib_advanced_testcase;

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

global $CFG;

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

/**
* External learning plans webservice API tests.
Expand All @@ -46,24 +41,24 @@
* @copyright 2015 Damyon Wiese
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class tool_lp_external_testcase extends externallib_advanced_testcase {
class externallib_test extends externallib_advanced_testcase {

/** @var stdClass $creator User with enough permissions to create insystem context. */
/** @var \stdClass $creator User with enough permissions to create insystem context. */
protected $creator = null;

/** @var stdClass $catcreator User with enough permissions to create incategory context. */
/** @var \stdClass $catcreator User with enough permissions to create incategory context. */
protected $catcreator = null;

/** @var stdClass $category Category */
/** @var \stdClass $category Category */
protected $category = null;

/** @var stdClass $category Category */
/** @var \stdClass $category Category */
protected $othercategory = null;

/** @var stdClass $user User with enough permissions to view insystem context */
/** @var \stdClass $user User with enough permissions to view insystem context */
protected $user = null;

/** @var stdClass $catuser User with enough permissions to view incategory context */
/** @var \stdClass $catuser User with enough permissions to view incategory context */
protected $catuser = null;

/** @var int Creator role id */
Expand All @@ -87,8 +82,8 @@ protected function setUp(): void {
$catcreator = $this->getDataGenerator()->create_user();
$category = $this->getDataGenerator()->create_category();
$othercategory = $this->getDataGenerator()->create_category();
$syscontext = context_system::instance();
$catcontext = context_coursecat::instance($category->id);
$syscontext = \context_system::instance();
$catcontext = \context_coursecat::instance($category->id);

// Fetching default authenticated user role.
$authrole = $DB->get_record('role', array('id' => $CFG->defaultuserroleid));
Expand Down Expand Up @@ -157,33 +152,33 @@ public function test_search_users_by_capability() {
// First we search with no capability assigned.
$this->setUser($ux);
$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(0, $result['users']);
$this->assertEquals(0, $result['count']);

// Now we assign a different capability.
$usercontext = context_user::instance($u1->id);
$systemcontext = context_system::instance();
$usercontext = \context_user::instance($u1->id);
$systemcontext = \context_system::instance();
$customrole = $this->assignUserCapability('moodle/competency:planview', $usercontext->id);

$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(0, $result['users']);
$this->assertEquals(0, $result['count']);

// Now we assign a matching capability in the same role.
$usercontext = context_user::instance($u1->id);
$usercontext = \context_user::instance($u1->id);
$this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $customrole);

$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);

// Now assign another role with the same capability (test duplicates).
role_assign($this->creatorrole, $ux->id, $usercontext->id);
$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);

Expand All @@ -192,7 +187,7 @@ public function test_search_users_by_capability() {
role_assign($this->creatorrole, $ux2->id, $systemcontext->id);
$this->setUser($ux2);
$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);

Expand All @@ -201,7 +196,7 @@ public function test_search_users_by_capability() {
role_assign($this->creatorrole, $ux3->id, $usercontext->id);
$this->setUser($ux3);
$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);

Expand All @@ -211,14 +206,14 @@ public function test_search_users_by_capability() {
// Now add a prevent override (will change nothing because we still have an ALLOW).
assign_capability('moodle/competency:planmanage', CAP_PREVENT, $customrole, $usercontext->id);
$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);

// Now change to a prohibit override (should prevent access).
assign_capability('moodle/competency:planmanage', CAP_PROHIBIT, $customrole, $usercontext->id);
$result = external::search_users('yyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);

Expand All @@ -237,10 +232,10 @@ public function test_search_users_by_capability_the_comeback() {
$slave2 = $dg->create_user(array('lastname' => 'MOODLER'));
$slave3 = $dg->create_user(array('lastname' => 'MOODLER'));

$syscontext = context_system::instance();
$slave1context = context_user::instance($slave1->id);
$slave2context = context_user::instance($slave2->id);
$slave3context = context_user::instance($slave3->id);
$syscontext = \context_system::instance();
$slave1context = \context_user::instance($slave1->id);
$slave2context = \context_user::instance($slave2->id);
$slave3context = \context_user::instance($slave3->id);

// Creating a role giving the site config.
$roleid = $dg->create_role();
Expand Down Expand Up @@ -306,34 +301,34 @@ public function test_search_users() {

// We need to give the user the capability we are searching for on each of the test users.
$this->setAdminUser();
$usercontext = context_user::instance($u1->id);
$usercontext = \context_user::instance($u1->id);
$dummyrole = $this->assignUserCapability('moodle/competency:planmanage', $usercontext->id);
$usercontext = context_user::instance($u2->id);
$usercontext = \context_user::instance($u2->id);
$this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
$usercontext = context_user::instance($u3->id);
$usercontext = \context_user::instance($u3->id);
$this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);

$this->setUser($ux);
$usercontext = context_user::instance($u1->id);
$usercontext = \context_user::instance($u1->id);
$this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
$usercontext = context_user::instance($u2->id);
$usercontext = \context_user::instance($u2->id);
$this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);
$usercontext = context_user::instance($u3->id);
$usercontext = \context_user::instance($u3->id);
$this->assignUserCapability('moodle/competency:planmanage', $usercontext->id, $dummyrole);

$this->setAdminUser();

// No identity fields.
$CFG->showuseridentity = '';
$result = external::search_users('cats', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(0, $result['users']);
$this->assertEquals(0, $result['count']);

// Filter by name.
$CFG->showuseridentity = '';
$result = external::search_users('dyyylan', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(2, $result['users']);
$this->assertEquals(2, $result['count']);
$this->assertEquals($u2->id, $result['users'][0]['id']);
Expand All @@ -342,7 +337,7 @@ public function test_search_users() {
// Filter by institution and name.
$CFG->showuseridentity = 'institution';
$result = external::search_users('bob', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(2, $result['users']);
$this->assertEquals(2, $result['count']);
$this->assertEquals($u1->id, $result['users'][0]['id']);
Expand All @@ -351,7 +346,7 @@ public function test_search_users() {
// Filter by id number.
$CFG->showuseridentity = 'idnumber';
$result = external::search_users('cats', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);
$this->assertEquals($u1->id, $result['users'][0]['id']);
Expand All @@ -365,7 +360,7 @@ public function test_search_users() {
// Filter by email.
$CFG->showuseridentity = 'email';
$result = external::search_users('yyy', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(3, $result['users']);
$this->assertEquals(3, $result['count']);
$this->assertEquals($u2->id, $result['users'][0]['id']);
Expand All @@ -378,7 +373,7 @@ public function test_search_users() {
// Filter by any.
$CFG->showuseridentity = 'idnumber,email,phone1,phone2,department,institution';
$result = external::search_users('yyy', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(3, $result['users']);
$this->assertEquals(3, $result['count']);
$this->assertArrayHasKey('idnumber', $result['users'][0]);
Expand All @@ -394,12 +389,12 @@ public function test_search_users() {

// Only names are included.
$result = external::search_users('fish');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(0, $result['users']);
$this->assertEquals(0, $result['count']);

$result = external::search_users('bob', 'moodle/competency:planmanage');
$result = external_api::clean_returnvalue(external::search_users_returns(), $result);
$result = \external_api::clean_returnvalue(external::search_users_returns(), $result);
$this->assertCount(1, $result['users']);
$this->assertEquals(1, $result['count']);
$this->assertEquals($u1->id, $result['users'][0]['id']);
Expand Down
Loading

0 comments on commit 100bc51

Please sign in to comment.