Skip to content

Commit

Permalink
MDL-73352 phpunit: lib_test and locallib_test testcase names
Browse files Browse the repository at this point in the history
All lib_test and locallib_test classes:

- Namespaced with component (and API whenever makes sense).
- Fixed incorrect use statements with leading backslash.
- Changed code to point to global scope when needed or add new uses.
- All them passing individually.
- Complete runs passing too.

Special mention to tests under login/tests:

1) The core_login component doesn't exist.
2) But login/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 login_xxxx as prefix.
  • Loading branch information
stronk7 committed Dec 30, 2021
1 parent 541d999 commit 511801c
Show file tree
Hide file tree
Showing 62 changed files with 970 additions and 839 deletions.
9 changes: 5 additions & 4 deletions admin/tool/capability/tests/locallib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @copyright 2021 The Open University
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later.
*/
namespace tool_capability;

defined('MOODLE_INTERNAL') || die();
global $CFG;
Expand All @@ -30,7 +31,7 @@
/**
* Tests for the capability overview helper functions.
*/
class tool_capability_locallib_testcase extends advanced_testcase {
class locallib_test extends \advanced_testcase {

/**
* Test the function that gets the data - simple case.
Expand All @@ -40,7 +41,7 @@ public function test_tool_capability_calculate_role_data() {

$data = tool_capability_calculate_role_data('mod/quiz:attempt', get_all_roles());

$systcontext = context_system::instance();
$systcontext = \context_system::instance();
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);

$this->assertArrayHasKey($systcontext->id, $data);
Expand All @@ -63,10 +64,10 @@ public function test_tool_capability_calculate_role_data_orphan_contexts() {

// This simulates a situation that seems to happen sometimes, where
// we end up with contexts with path = NULL in the database.
$systcontext = context_system::instance();
$systcontext = \context_system::instance();
$generator = $this->getDataGenerator();
$course = $generator->create_course();
$coursecontext = context_course::instance($course->id);
$coursecontext = \context_course::instance($course->id);
$studentroleid = $DB->get_field('role', 'id', ['shortname' => 'student']);
role_change_permission($studentroleid, $coursecontext, 'mod/quiz:attempt', CAP_PREVENT);
// This is where we simulate the breakage.
Expand Down
3 changes: 2 additions & 1 deletion admin/tool/moodlenet/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @copyright 2020 Peter Dias
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace tool_moodlenet;

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

Expand All @@ -30,7 +31,7 @@
/**
* Test moodlenet functions
*/
class tool_moodlenet_lib_testcase extends advanced_testcase {
class lib_test extends \advanced_testcase {

/**
* Test the generate_mnet_endpoint function
Expand Down
25 changes: 14 additions & 11 deletions blog/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
* @copyright 2009 Nicolas Connault
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_blog;

use blog_listing;

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

Expand All @@ -32,7 +35,7 @@
/**
* Test functions that rely on the DB tables
*/
class core_blog_lib_testcase extends advanced_testcase {
class lib_test extends \advanced_testcase {

private $courseid;
private $cmid;
Expand All @@ -54,7 +57,7 @@ protected function setUp(): void {
$this->assertNotEmpty($page);

// Create default group.
$group = new stdClass();
$group = new \stdClass();
$group->courseid = $course->id;
$group->name = 'ANON';
$group->id = $DB->insert_record('groups', $group);
Expand All @@ -71,7 +74,7 @@ protected function setUp(): void {
'rawname' => 'Testtagname', 'isstandard' => 1));

// Create default post.
$post = new stdClass();
$post = new \stdClass();
$post->userid = $user->id;
$post->groupid = $group->id;
$post->content = 'test post content text';
Expand Down Expand Up @@ -170,7 +173,7 @@ public function test_core_blog_myprofile_navigation() {

// Check the node tree is correct.
core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$reflector = new ReflectionObject($tree);
$reflector = new \ReflectionObject($tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayHasKey('blogs', $nodes->getValue($tree));
Expand All @@ -192,7 +195,7 @@ public function test_core_blog_myprofile_navigation_as_guest() {

// Check the node tree is correct.
core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$reflector = new ReflectionObject($tree);
$reflector = new \ReflectionObject($tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
Expand All @@ -215,19 +218,19 @@ public function test_core_blog_myprofile_navigation_blogs_disabled() {

// Check the node tree is correct.
core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$reflector = new ReflectionObject($tree);
$reflector = new \ReflectionObject($tree);
$nodes = $reflector->getProperty('nodes');
$nodes->setAccessible(true);
$this->assertArrayNotHasKey('blogs', $nodes->getValue($tree));
}

public function test_blog_get_listing_course() {
$this->setAdminUser();
$coursecontext = context_course::instance($this->courseid);
$coursecontext = \context_course::instance($this->courseid);
$anothercourse = $this->getDataGenerator()->create_course();

// Add blog associations with a course.
$blog = new blog_entry($this->postid);
$blog = new \blog_entry($this->postid);
$blog->add_association($coursecontext->id);

// There is one entry associated with a course.
Expand All @@ -253,12 +256,12 @@ public function test_blog_get_listing_course() {

public function test_blog_get_listing_module() {
$this->setAdminUser();
$coursecontext = context_course::instance($this->courseid);
$contextmodule = context_module::instance($this->cmid);
$coursecontext = \context_course::instance($this->courseid);
$contextmodule = \context_module::instance($this->cmid);
$anothermodule = $this->getDataGenerator()->create_module('page', array('course' => $this->courseid));

// Add blog associations with a course.
$blog = new blog_entry($this->postid);
$blog = new \blog_entry($this->postid);
$blog->add_association($contextmodule->id);

// There is no entry associated with a course.
Expand Down
57 changes: 29 additions & 28 deletions calendar/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
namespace core_calendar;

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

Expand All @@ -33,7 +34,7 @@
* @copyright 2017 Mark Nelson <markn@moodle.com>
* @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
*/
class core_calendar_lib_testcase extends advanced_testcase {
class lib_test extends \advanced_testcase {

/**
* Tests set up
Expand Down Expand Up @@ -89,7 +90,7 @@ public function test_get_events_with_disabled_module() {
]
];
foreach ($events as $event) {
calendar_event::create($event, false);
\calendar_event::create($event, false);
}
$timestart = time() - 60;
$timeend = time() + 60;
Expand Down Expand Up @@ -145,7 +146,7 @@ public function test_get_course_cached() {
public function test_update_subscription() {
$this->resetAfterTest(true);

$subscription = new stdClass();
$subscription = new \stdClass();
$subscription->eventtype = 'site';
$subscription->name = 'test';
$id = calendar_add_subscription($subscription);
Expand All @@ -164,7 +165,7 @@ public function test_update_subscription() {
$this->assertEquals($subscription->name, $sub->name);
$this->assertEquals($subscription->pollinterval, $sub->pollinterval);

$subscription = new stdClass();
$subscription = new \stdClass();
$subscription->name = 'awesome4';
$this->expectException('coding_exception');
calendar_update_subscription($subscription);
Expand All @@ -178,14 +179,14 @@ public function test_add_subscription() {
$this->resetAfterTest(true);

// Test for Microsoft Outlook 2010.
$subscription = new stdClass();
$subscription = new \stdClass();
$subscription->name = 'Microsoft Outlook 2010';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = calendar_add_subscription($subscription);

$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/ms_outlook_2010.ics');
$ical = new iCalendar();
$ical = new \iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());

Expand All @@ -195,14 +196,14 @@ public function test_add_subscription() {
$this->assertEquals($count, 1);

// Test for OSX Yosemite.
$subscription = new stdClass();
$subscription = new \stdClass();
$subscription->name = 'OSX Yosemite';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = calendar_add_subscription($subscription);

$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/osx_yosemite.ics');
$ical = new iCalendar();
$ical = new \iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());

Expand All @@ -212,14 +213,14 @@ public function test_add_subscription() {
$this->assertEquals($count, 1);

// Test for Google Gmail.
$subscription = new stdClass();
$subscription = new \stdClass();
$subscription->name = 'Google Gmail';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = calendar_add_subscription($subscription);

$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/google_gmail.ics');
$ical = new iCalendar();
$ical = new \iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, array());

Expand All @@ -229,13 +230,13 @@ public function test_add_subscription() {
$this->assertEquals($count, 1);

// Test for ICS file with repeated events.
$subscription = new stdClass();
$subscription = new \stdClass();
$subscription->name = 'Repeated events';
$subscription->importfrom = CALENDAR_IMPORT_FROM_FILE;
$subscription->eventtype = 'site';
$id = calendar_add_subscription($subscription);
$calendar = file_get_contents($CFG->dirroot . '/lib/tests/fixtures/repeated_events.ics');
$ical = new iCalendar();
$ical = new \iCalendar();
$ical->unserialize($calendar);
$this->assertEquals($ical->parser_errors, []);

Expand Down Expand Up @@ -356,7 +357,7 @@ public function test_get_legacy_events_with_overrides() {
];

foreach ($events as $event) {
calendar_event::create($event, false);
\calendar_event::create($event, false);
}

$timestart = $now - 100;
Expand Down Expand Up @@ -432,7 +433,7 @@ public function test_get_legacy_events_with_overrides() {
];

foreach ($repeatingevents as $event) {
calendar_event::create($event, false);
\calendar_event::create($event, false);
}

// Make sure repeating events are not filtered out.
Expand All @@ -450,7 +451,7 @@ public function test_calendar_get_default_courses() {
$course1 = $generator->create_course();
$course2 = $generator->create_course();
$course3 = $generator->create_course();
$context = context_course::instance($course1->id);
$context = \context_course::instance($course1->id);

$this->setAdminUser();
$admin = clone $USER;
Expand Down Expand Up @@ -551,9 +552,9 @@ public function test_calendar_get_allowed_event_types_course() {
$course1 = $generator->create_course(); // Has capability.
$course2 = $generator->create_course(); // Doesn't have capability.
$course3 = $generator->create_course(); // Not enrolled.
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$context3 = context_course::instance($course3->id);
$context1 = \context_course::instance($course1->id);
$context2 = \context_course::instance($course2->id);
$context3 = \context_course::instance($course3->id);
$roleid = $generator->create_role();
$contexts = [$context1, $context2, $context3];
$enrolledcourses = [$course1, $course2];
Expand Down Expand Up @@ -600,7 +601,7 @@ public function test_calendar_get_allowed_event_types_group_no_acces_to_diff_gro
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$roleid = $generator->create_role();

$generator->enrol_user($user->id, $course->id, 'student');
Expand All @@ -627,7 +628,7 @@ public function test_calendar_get_allowed_event_types_group_no_groups() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$roleid = $generator->create_role();
$generator->enrol_user($user->id, $course->id, 'student');
$generator->role_assign($roleid, $user->id, $context->id);
Expand All @@ -652,8 +653,8 @@ public function test_calendar_get_allowed_event_types_group_access_all_groups()
$course2 = $generator->create_course();
$generator->create_group(array('courseid' => $course1->id));
$generator->create_group(array('courseid' => $course2->id));
$context1 = context_course::instance($course1->id);
$context2 = context_course::instance($course2->id);
$context1 = \context_course::instance($course1->id);
$context2 = \context_course::instance($course2->id);
$roleid = $generator->create_role();
$generator->enrol_user($user->id, $course1->id, 'student');
$generator->enrol_user($user->id, $course2->id, 'student');
Expand All @@ -678,7 +679,7 @@ public function test_calendar_get_allowed_event_types_group_no_access_all_groups
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$group1 = $generator->create_group(array('courseid' => $course->id));
$group2 = $generator->create_group(array('courseid' => $course->id));
$roleid = $generator->create_role();
Expand Down Expand Up @@ -711,7 +712,7 @@ public function test_calendar_get_allowed_event_types_group_cap_no_groups() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$roleid = $generator->create_role();
$group = $generator->create_group(['courseid' => $course->id]);
$generator->enrol_user($user->id, $course->id, 'student');
Expand All @@ -733,7 +734,7 @@ public function test_calendar_get_allowed_event_types_group_cap_has_group() {
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$roleid = $generator->create_role();
$group = $generator->create_group(['courseid' => $course->id]);
$generator->enrol_user($user->id, $course->id, 'student');
Expand All @@ -756,7 +757,7 @@ public function test_calendar_get_allowed_event_types_group_cap_access_all_group
$generator = $this->getDataGenerator();
$user = $generator->create_user();
$course = $generator->create_course();
$context = context_course::instance($course->id);
$context = \context_course::instance($course->id);
$roleid = $generator->create_role();
$group = $generator->create_group(['courseid' => $course->id]);
$generator->enrol_user($user->id, $course->id, 'student');
Expand Down Expand Up @@ -941,13 +942,13 @@ public function test_calendar_view_event_allowed_course_event() {
'timeduration' => 86400,
'visible' => 1
];
$caleventmanual = calendar_event::create($manualevent, false);
$caleventmanual = \calendar_event::create($manualevent, false);

// Create a course event for the course with guest access.
$guestevent = clone $manualevent;
$guestevent->name = 'Guest course event';
$guestevent->courseid = $guestcourse->id;
$caleventguest = calendar_event::create($guestevent, false);
$caleventguest = \calendar_event::create($guestevent, false);

// Viewing as admin.
$this->assertTrue(calendar_view_event_allowed($caleventmanual));
Expand Down
Loading

0 comments on commit 511801c

Please sign in to comment.