From be5e0110ff5965f05383db2740119f03cad7196e Mon Sep 17 00:00:00 2001 From: Mark Nelson Date: Tue, 22 Dec 2015 14:46:02 +0800 Subject: [PATCH] MDL-49566 core: used reflection in unit tests Also split the tests into multiple functions for each scenario. --- badges/tests/badgeslib_test.php | 86 ++++---- blog/tests/lib_test.php | 73 +++++-- grade/report/user/tests/lib_test.php | 58 +++-- lib/tests/myprofilelib_test.php | 234 ++++++++++++++------- mod/forum/tests/lib_test.php | 83 +++++--- notes/tests/lib_test.php | 87 ++++++-- report/log/tests/lib_test.php | 67 ++++-- report/outline/tests/lib_test.php | 68 +++--- report/stats/tests/lib_test.php | 91 +++++--- report/usersessions/tests/lib_test.php | 93 +++++--- user/tests/fixtures/myprofile_fixtures.php | 14 -- 11 files changed, 639 insertions(+), 315 deletions(-) diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index 0f24489e0f1d3..a8d43821d9eb5 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -29,7 +29,6 @@ global $CFG; require_once($CFG->libdir . '/badgeslib.php'); require_once($CFG->dirroot . '/badges/lib.php'); -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); class core_badges_badgeslib_testcase extends advanced_testcase { protected $badgeid; @@ -476,52 +475,67 @@ public function test_badges_assertion() { } /** - * Tests for core_badges_myprofile_navigation() api. + * Tests the core_badges_myprofile_navigation() function. */ public function test_core_badges_myprofile_navigation() { - $this->resetAfterTest(); + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); $this->setAdminUser(); - $user = $this->user; $badge = new badge($this->badgeid); - $badge->issue($user->id, true); - $this->assertTrue($badge->is_issued($user->id)); - - // Disable badges. - set_config('enablebadges', false); - $tree = new phpunit_fixture_myprofile_tree(); - $user2 = $this->getDataGenerator()->create_user(); + $badge->issue($this->user->id, true); + $iscurrentuser = true; $course = null; - $iscurrentuser = false; - - core_badges_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('localbadges', $nodes); // Enable badges. set_config('enablebadges', true); - $this->setUser($user); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; - core_badges_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('localbadges', $nodes); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; - core_badges_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('localbadges', $nodes); + // Check the node tree is correct. + core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('localbadges', $nodes->getValue($tree)); + } + + /** + * Tests the core_badges_myprofile_navigation() function with badges disabled.. + */ + public function test_core_badges_myprofile_navigation_badges_disabled() { + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); + $this->setAdminUser(); + $badge = new badge($this->badgeid); + $badge->issue($this->user->id, true); + $iscurrentuser = false; + $course = null; + + // Disable badges. + set_config('enablebadges', false); - // Course badge. + // Check the node tree is correct. + core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $course); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('localbadges', $nodes->getValue($tree)); + } + + /** + * Tests the core_badges_myprofile_navigation() function with a course badge. + */ + public function test_core_badges_myprofile_navigation_with_course_badge() { + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); + $this->setAdminUser(); $badge = new badge($this->coursebadge); - $badge->issue($user->id, true); - $this->assertTrue($badge->is_issued($user->id)); - $course = $this->course; - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); + $badge->issue($this->user->id, true); $iscurrentuser = false; - core_badges_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('localbadges', $nodes); + + // Check the node tree is correct. + core_badges_myprofile_navigation($tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('localbadges', $nodes->getValue($tree)); } } diff --git a/blog/tests/lib_test.php b/blog/tests/lib_test.php index 23192986538ab..1eb6583868fc3 100644 --- a/blog/tests/lib_test.php +++ b/blog/tests/lib_test.php @@ -26,7 +26,6 @@ global $CFG; require_once($CFG->dirroot . '/blog/locallib.php'); require_once($CFG->dirroot . '/blog/lib.php'); -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); /** * Test functions that rely on the DB tables @@ -479,37 +478,71 @@ public function test_blog_comment_deleted_event() { } /** - * Tests for core_blog_myprofile_navigation() api. + * Tests the core_blog_myprofile_navigation() function. */ public function test_core_blog_myprofile_navigation() { global $USER; - $this->resetAfterTest(); - $this->setGuestUser(); - - // No blogs for guest users. - $tree = new phpunit_fixture_myprofile_tree(); + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); + $this->setAdminUser(); + $iscurrentuser = true; $course = null; + + // Enable blogs. + set_config('enableblogs', true); + + // Check the node tree is correct. + core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('blogs', $nodes->getValue($tree)); + } + + /** + * Tests the core_blog_myprofile_navigation() function as a guest. + */ + public function test_core_blog_myprofile_navigation_as_guest() { + global $USER; + + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); $iscurrentuser = false; + $course = null; + + // Set user as guest. + $this->setGuestUser(); + + // Check the node tree is correct. core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('blogs', $nodes); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree)); + } - // Disable blogs. + /** + * Tests the core_blog_myprofile_navigation() function when blogs are disabled. + */ + public function test_core_blog_myprofile_navigation_blogs_disabled() { + global $USER; + + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); $this->setAdminUser(); + $iscurrentuser = false; + $course = null; + + // Disable blogs. set_config('enableblogs', false); - $tree = new phpunit_fixture_myprofile_tree(); - core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('blogs', $nodes); - // Enable badges. - set_config('enableblogs', true); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; + // Check the node tree is correct. core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('blogs', $nodes); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('blogs', $nodes->getValue($tree)); } } diff --git a/grade/report/user/tests/lib_test.php b/grade/report/user/tests/lib_test.php index b96226a1b9022..7af1a71dac210 100644 --- a/grade/report/user/tests/lib_test.php +++ b/grade/report/user/tests/lib_test.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); require_once($CFG->dirroot . '/grade/report/user/lib.php'); /** @@ -38,30 +37,53 @@ class gradereport_user_lib_testcase extends advanced_testcase { /** - * Tests for gradereport_user_myprofile_navigation() api. + * @var stdClass The user. */ - public function test_gradereport_user_myprofile_navigation() { + private $user; + /** + * @var stdClass The course. + */ + private $course; + + /** + * @var \core_user\output\myprofile\tree The navigation tree. + */ + private $tree; + + public function setUp() { + $this->user = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + $this->tree = new \core_user\output\myprofile\tree(); $this->resetAfterTest(); - $this->setAdminUser(); + } - // User with all permissions. - $tree = new phpunit_fixture_myprofile_tree(); - $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); - $course = $this->getDataGenerator()->create_course(); + /** + * Tests the gradereport_user_myprofile_navigation() function. + */ + public function test_gradereport_user_myprofile_navigation() { + $this->setAdminUser(); $iscurrentuser = false; - gradereport_user_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('grade', $nodes); + gradereport_user_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('grade', $nodes->getValue($this->tree)); + } - // Try to see as a user without permission. - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); + /** + * Tests the gradereport_user_myprofile_navigation() function for a user + * without permission to view the grade node. + */ + public function test_gradereport_user_myprofile_navigation_without_permission() { + $this->setUser($this->user); $iscurrentuser = true; - gradereport_user_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('grade', $nodes); + + gradereport_user_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('grade', $nodes->getValue($this->tree)); } } diff --git a/lib/tests/myprofilelib_test.php b/lib/tests/myprofilelib_test.php index 74f18298ebbeb..6cb5dc91be39a 100644 --- a/lib/tests/myprofilelib_test.php +++ b/lib/tests/myprofilelib_test.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); require_once($CFG->dirroot . '/lib/myprofilelib.php'); /** @@ -38,23 +37,45 @@ class core_myprofilelib_testcase extends advanced_testcase { /** - * Tests for report_log_myprofile_navigation() api. + * @var stdClass The user. */ - public function test_core_myprofile_navigation() { - global $CFG; + private $user; + + /** + * @var stdClass The course. + */ + private $course; + + /** + * @var \core_user\output\myprofile\tree The navigation tree. + */ + private $tree; + + public function setUp() { + // Set the $PAGE->url value so core_myprofile_navigation() doesn't complain. + global $PAGE; + $PAGE->set_url('/test'); + $this->user = $this->getDataGenerator()->create_user(); + $this->user2 = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + $this->tree = new \core_user\output\myprofile\tree(); $this->resetAfterTest(); - $this->setAdminUser(); + } - $tree = new phpunit_fixture_myprofile_tree(); - $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); - $course = $this->getDataGenerator()->create_course(); + /** + * Tests the core_myprofile_navigation() function as an admin viewing a user's course profile. + */ + public function test_core_myprofile_navigation_as_admin() { + $this->setAdminUser(); $iscurrentuser = false; // Test tree as admin user. - core_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $cats = $tree->get_categories(); + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $categories = $reflector->getProperty('categories'); + $categories->setAccessible(true); + $cats = $categories->getValue($this->tree); $this->assertArrayHasKey('contact', $cats); $this->assertArrayHasKey('coursedetails', $cats); $this->assertArrayHasKey('miscellaneous', $cats); @@ -62,55 +83,90 @@ public function test_core_myprofile_navigation() { $this->assertArrayHasKey('administration', $cats); $this->assertArrayHasKey('loginactivity', $cats); - // Course node. - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('fullprofile', $nodes); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('fullprofile', $nodes->getValue($this->tree)); + } - // User without permission cannot access full course profile. - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('fullprofile', $nodes); + /** + * Tests the core_myprofile_navigation() function as a user without permission to view the full + * profile of another another user. + */ + public function test_core_myprofile_navigation_course_without_permission() { + $this->setUser($this->user2); + $iscurrentuser = false; + + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('fullprofile', $nodes->getValue($this->tree)); + } - // Edit profile link. - $this->setUser($user); + /** + * Tests the core_myprofile_navigation() function as the currently logged in user. + */ + public function test_core_myprofile_navigation_profile_link_as_current_user() { + $this->setUser($this->user); $iscurrentuser = true; - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('editprofile', $nodes); - // Edit profile link as admin user. + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree)); + } + + /** + * Tests the core_myprofile_navigation() function as the admin viewing another user. + */ + public function test_core_myprofile_navigation_profile_link_as_admin() { $this->setAdminUser(); $iscurrentuser = false; - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('editprofile', $nodes); - // Preference page. + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('editprofile', $nodes->getValue($this->tree)); + } + + /** + * Tests the core_myprofile_navigation() function when viewing the preference page as an admin. + */ + public function test_core_myprofile_navigation_preference_as_admin() { $this->setAdminUser(); $iscurrentuser = false; - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('preferences', $nodes); - // Login as. - $this->setAdminUser(); + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('preferences', $nodes->getValue($this->tree)); + $this->assertArrayHasKey('loginas', $nodes->getValue($this->tree)); + } + + /** + * Tests the core_myprofile_navigation() function when viewing the preference + * page as another user without the ability to use the 'loginas' functionality. + */ + public function test_core_myprofile_navigation_preference_without_permission() { + // Login as link for a user who doesn't have the capability to login as. + $this->setUser($this->user2); $iscurrentuser = false; - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('loginas', $nodes); - - // Login as link for a user who doesn't have the cap. - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('loginas', $nodes); + + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('loginas', $nodes->getValue($this->tree)); + } + + /** + * Tests the core_myprofile_navigation() function as an admin viewing another user's contact details. + */ + public function test_core_myprofile_navigation_contact_fields_as_admin() { + global $CFG; // User contact fields. set_config("hiddenuserfields", "country,city,webpage,icqnumber,skypeid,yahooid,aimid,msnid"); @@ -139,51 +195,81 @@ public function test_core_myprofile_navigation() { 'idnumber' => 'SLHL' ); foreach ($fields as $field => $value) { - $user->$field = $value; + $this->user->$field = $value; } // User with proper permissions. - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, null); - $nodes = $tree->get_nodes(); + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); foreach ($hiddenfields as $field) { - $this->assertArrayHasKey($field, $nodes); + $this->assertArrayHasKey($field, $nodes->getValue($this->tree)); } foreach ($identityfields as $field) { - $this->assertArrayHasKey($field, $nodes); + $this->assertArrayHasKey($field, $nodes->getValue($this->tree)); } + } + + /** + * Tests the core_myprofile_navigation() function as a user viewing another user's profile + * ensuring that the contact details are not shown. + */ + public function test_core_myprofile_navigation_contact_field_without_permission() { + global $CFG; + + $iscurrentuser = false; + $hiddenfields = explode(',', $CFG->hiddenuserfields); + $identityfields = explode(',', $CFG->showuseridentity); // User without permission. - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, null); - $nodes = $tree->get_nodes(); + $this->setUser($this->user2); + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); foreach ($hiddenfields as $field) { - $this->assertArrayNotHasKey($field, $nodes); + $this->assertArrayNotHasKey($field, $nodes->getValue($this->tree)); } foreach ($identityfields as $field) { - $this->assertArrayNotHasKey($field, $nodes); + $this->assertArrayNotHasKey($field, $nodes->getValue($this->tree)); } + } + /** + * Tests the core_myprofile_navigation() function as an admin viewing another user's + * profile ensuring the login activity links are shown. + */ + public function test_core_myprofile_navigation_login_activity() { // First access, last access, last ip. $this->setAdminUser(); $iscurrentuser = false; - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, null); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('firstaccess', $nodes); - $this->assertArrayHasKey('lastaccess', $nodes); - $this->assertArrayHasKey('lastip', $nodes); + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('firstaccess', $nodes->getValue($this->tree)); + $this->assertArrayHasKey('lastaccess', $nodes->getValue($this->tree)); + $this->assertArrayHasKey('lastip', $nodes->getValue($this->tree)); + } + + /** + * Tests the core_myprofile_navigation() function as a user viewing another user's profile + * ensuring the login activity links are not shown. + */ + public function test_core_myprofile_navigationn_login_activity_without_permission() { // User without permission. set_config("hiddenuserfields", "firstaccess,lastaccess,lastip"); - $this->setUser($user2); + $this->setUser($this->user2); $iscurrentuser = false; - $tree = new phpunit_fixture_myprofile_tree(); - core_myprofile_navigation($tree, $user, $iscurrentuser, null); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('firstaccess', $nodes); - $this->assertArrayNotHasKey('lastaccess', $nodes); - $this->assertArrayNotHasKey('lastip', $nodes); + + core_myprofile_navigation($this->tree, $this->user, $iscurrentuser, null); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('firstaccess', $nodes->getValue($this->tree)); + $this->assertArrayNotHasKey('lastaccess', $nodes->getValue($this->tree)); + $this->assertArrayNotHasKey('lastip', $nodes->getValue($this->tree)); } } diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index 8eddf979bd7d3..989d02d90bf65 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -27,7 +27,6 @@ global $CFG; require_once($CFG->dirroot . '/mod/forum/lib.php'); require_once($CFG->dirroot . '/rating/lib.php'); -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); class mod_forum_lib_testcase extends advanced_testcase { @@ -2117,42 +2116,76 @@ public function test_forum_user_can_post_discussion() { } /** - * Tests for mod_forum_myprofile_navigation() api. + * Tests the mod_forum_myprofile_navigation() function. */ public function test_mod_forum_myprofile_navigation() { + $this->resetAfterTest(true); + + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); + $user = $this->getDataGenerator()->create_user(); + $course = $this->getDataGenerator()->create_course(); + $iscurrentuser = true; + + // Set as the current user. + $this->setUser($user); + + // Check the node tree is correct. + mod_forum_myprofile_navigation($tree, $user, $iscurrentuser, $course); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('forumposts', $nodes->getValue($tree)); + $this->assertArrayHasKey('forumdiscussions', $nodes->getValue($tree)); + } + + /** + * Tests the mod_forum_myprofile_navigation() function as a guest. + */ + public function test_mod_forum_myprofile_navigation_as_guest() { global $USER; - $this->resetAfterTest(); - $this->setGuestUser(); + $this->resetAfterTest(true); - $tree = new phpunit_fixture_myprofile_tree(); - $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); $course = $this->getDataGenerator()->create_course(); - $iscurrentuser = false; + $iscurrentuser = true; + + // Set user as guest. + $this->setGuestUser(); - // Nothing for guest users. + // Check the node tree is correct. mod_forum_myprofile_navigation($tree, $USER, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('forumposts', $nodes); - $this->assertArrayNotHasKey('forumdiscussions', $nodes); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('forumposts', $nodes->getValue($tree)); + $this->assertArrayNotHasKey('forumdiscussions', $nodes->getValue($tree)); + } - // Current user. - $this->setUser($user); - $tree = new phpunit_fixture_myprofile_tree(); + /** + * Tests the mod_forum_myprofile_navigation() function as a user viewing another user's profile. + */ + public function test_mod_forum_myprofile_navigation_different_user() { + $this->resetAfterTest(true); + + // Set up the test. + $tree = new \core_user\output\myprofile\tree(); + $user = $this->getDataGenerator()->create_user(); + $user2 = $this->getDataGenerator()->create_user(); + $course = $this->getDataGenerator()->create_course(); $iscurrentuser = true; - mod_forum_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('forumposts', $nodes); - $this->assertArrayHasKey('forumdiscussions', $nodes); - // Different user's profile. + // Set to different user's profile. $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; + + // Check the node tree is correct. mod_forum_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('forumposts', $nodes); - $this->assertArrayHasKey('forumdiscussions', $nodes); + $reflector = new ReflectionObject($tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('forumposts', $nodes->getValue($tree)); + $this->assertArrayHasKey('forumdiscussions', $nodes->getValue($tree)); } } diff --git a/notes/tests/lib_test.php b/notes/tests/lib_test.php index bd03b16fffa1f..69b0001967be2 100644 --- a/notes/tests/lib_test.php +++ b/notes/tests/lib_test.php @@ -26,7 +26,6 @@ global $CFG; require_once($CFG->dirroot . '/notes/lib.php'); -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); /** * Class core_notes_lib_testcase * @@ -37,36 +36,82 @@ class core_notes_lib_testcase extends advanced_testcase { /** - * Tests for core_notes_myprofile_navigation() api. + * @var stdClass The user. + */ + private $user; + + /** + * @var stdClass The course. + */ + private $course; + + /** + * @var \core_user\output\myprofile\tree The navigation tree. + */ + private $tree; + + public function setUp() { + $this->user = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + $this->tree = new \core_user\output\myprofile\tree(); + $this->resetAfterTest(); + } + + /** + * Tests the core_notes_myprofile_navigation() function. */ public function test_core_notes_myprofile_navigation() { global $USER; - $this->resetAfterTest(); + // Set up the test. + $this->setAdminUser(); + $iscurrentuser = true; + + // Enable notes. + set_config('enablenotes', true); + + // Check the node tree is correct. + core_notes_myprofile_navigation($this->tree, $USER, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('notes', $nodes->getValue($this->tree)); + } + + /** + * Tests the core_notes_myprofile_navigation() function. + */ + public function test_core_notes_myprofile_navigation_as_guest() { + global $USER; + $this->setGuestUser(); + $iscurrentuser = false; + + // Check the node tree is correct. + core_notes_myprofile_navigation($this->tree, $USER, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('notes', $nodes->getValue($this->tree)); + } - // No notes for guest users. - $tree = new phpunit_fixture_myprofile_tree(); - $course = null; + /** + * Tests the core_notes_myprofile_navigation() function. + */ + public function test_core_notes_myprofile_navigation_notes_disabled() { + global $USER; + + $this->setAdminUser(); $iscurrentuser = false; - core_notes_myprofile_navigation($tree, $USER, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('blogs', $nodes); // Disable notes. - $this->setAdminUser(); set_config('enablenotes', false); - $tree = new phpunit_fixture_myprofile_tree(); - core_notes_myprofile_navigation($tree, $USER, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('notes', $nodes); - // Enable notes. - set_config('enablenotes', true); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; - core_notes_myprofile_navigation($tree, $USER, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('notes', $nodes); + // Check the node tree is correct. + core_notes_myprofile_navigation($this->tree, $USER, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('notes', $nodes->getValue($this->tree)); } } \ No newline at end of file diff --git a/report/log/tests/lib_test.php b/report/log/tests/lib_test.php index 0cb41e064ded8..d35bc04c792a3 100644 --- a/report/log/tests/lib_test.php +++ b/report/log/tests/lib_test.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); /** * Class report_log_events_testcase. @@ -36,6 +35,28 @@ */ class report_log_lib_testcase extends advanced_testcase { + /** + * @var stdClass The user. + */ + private $user; + + /** + * @var stdClass The course. + */ + private $course; + + /** + * @var \core_user\output\myprofile\tree The navigation tree. + */ + private $tree; + + public function setUp() { + $this->user = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + $this->tree = new \core_user\output\myprofile\tree(); + $this->resetAfterTest(); + } + /** * Test report_log_supports_logstore. */ @@ -59,32 +80,36 @@ public function test_report_log_supports_logstore() { } /** - * Tests for report_log_myprofile_navigation() api. + * Tests the report_log_myprofile_navigation() function as an admin viewing the logs for a user. */ public function test_report_log_myprofile_navigation() { - - $this->resetAfterTest(); + // Set as the admin. $this->setAdminUser(); - - $tree = new phpunit_fixture_myprofile_tree(); - $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); - $course = $this->getDataGenerator()->create_course(); $iscurrentuser = false; - // As user with permissions. - report_log_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('alllogs', $nodes); - $this->assertArrayHasKey('todayslogs', $nodes); + // Check the node tree is correct. + report_log_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('alllogs', $nodes->getValue($this->tree)); + $this->assertArrayHasKey('todayslogs', $nodes->getValue($this->tree)); + } - // Try to see as a user without permission. - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); + /** + * Tests the report_log_myprofile_navigation() function as a user without permission. + */ + public function test_report_log_myprofile_navigation_without_permission() { + // Set to the other user. + $this->setUser($this->user); $iscurrentuser = true; - report_log_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('alllogs', $nodes); - $this->assertArrayNotHasKey('todayslogs', $nodes); + + // Check the node tree is correct. + report_log_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('alllogs', $nodes->getValue($this->tree)); + $this->assertArrayNotHasKey('todayslogs', $nodes->getValue($this->tree)); } } diff --git a/report/outline/tests/lib_test.php b/report/outline/tests/lib_test.php index 6b4417f5e5cbb..3085bb12dfef6 100644 --- a/report/outline/tests/lib_test.php +++ b/report/outline/tests/lib_test.php @@ -25,7 +25,6 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); /** * Class report_outline_lib_testcase @@ -36,6 +35,29 @@ */ class report_outline_lib_testcase extends advanced_testcase { + /** + * @var stdClass The user. + */ + private $user; + + /** + * @var stdClass The course. + */ + private $course; + + /** + * @var \core_user\output\myprofile\tree The navigation tree. + */ + private $tree; + + public function setUp() { + $this->user = $this->getDataGenerator()->create_user(); + $this->user2 = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + $this->tree = new \core_user\output\myprofile\tree(); + $this->resetAfterTest(); + } + /** * Test report_log_supports_logstore. */ @@ -58,38 +80,32 @@ public function test_report_participation_supports_logstore() { } /** - * Tests for report_outline_myprofile_navigation() api. + * Tests the report_outline_myprofile_navigation() function as an admin user. */ public function test_report_outline_myprofile_navigation() { - - $this->resetAfterTest(); $this->setAdminUser(); - - $tree = new phpunit_fixture_myprofile_tree(); - $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); - $course = $this->getDataGenerator()->create_course(); $iscurrentuser = false; - report_outline_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('outline', $nodes); - $this->assertArrayHasKey('complete', $nodes); + report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('outline', $nodes->getValue($this->tree)); + $this->assertArrayHasKey('complete', $nodes->getValue($this->tree)); + } - $tree = new phpunit_fixture_myprofile_tree(); + /** + * Tests the report_outline_myprofile_navigation() function as a user without permission. + */ + public function test_report_outline_myprofile_navigation_without_permission() { + $this->setUser($this->user); $iscurrentuser = true; - report_outline_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('outline', $nodes); - $this->assertArrayHasKey('complete', $nodes); - // Try to see as a user without permission. - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; - report_outline_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('outline', $nodes); - $this->assertArrayNotHasKey('complete', $nodes); + report_outline_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('outline', $nodes->getValue($this->tree)); + $this->assertArrayNotHasKey('complete', $nodes->getValue($this->tree)); } } diff --git a/report/stats/tests/lib_test.php b/report/stats/tests/lib_test.php index deb14ff00dd20..cd935ecdb76c2 100644 --- a/report/stats/tests/lib_test.php +++ b/report/stats/tests/lib_test.php @@ -24,9 +24,6 @@ defined('MOODLE_INTERNAL') || die(); -global $CFG; -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); - /** * Class report_stats_lib_testcase * @@ -36,6 +33,28 @@ */ class report_stats_lib_testcase extends advanced_testcase { + /** + * @var stdClass The user. + */ + private $user; + + /** + * @var stdClass The course. + */ + private $course; + + /** + * @var \core_user\output\myprofile\tree The navigation tree. + */ + private $tree; + + public function setUp() { + $this->user = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + $this->tree = new \core_user\output\myprofile\tree(); + $this->resetAfterTest(); + } + /** * Test report_log_supports_logstore. */ @@ -58,48 +77,54 @@ public function test_report_participation_supports_logstore() { } /** - * Tests for report_stats_myprofile_navigation() api. + * Tests the report_stats_myprofile_navigation() function. */ public function test_report_stats_myprofile_navigation() { - - $this->resetAfterTest(); $this->setAdminUser(); - // Enabling stats. + $iscurrentuser = false; + + // Enable stats. set_config('enablestats', true); - $tree = new phpunit_fixture_myprofile_tree(); - $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); - $course = $this->getDataGenerator()->create_course(); + report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('stats', $nodes->getValue($this->tree)); + } + + /** + * Tests the report_stats_myprofile_navigation() function when stats are disabled. + */ + public function test_report_stats_myprofile_navigation_stats_disabled() { + $this->setAdminUser(); $iscurrentuser = false; - report_stats_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('stats', $nodes); + // Disable stats. + set_config('enablestats', false); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; - report_stats_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('stats', $nodes); + report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('stats', $nodes->getValue($this->tree)); + } - // Disabling stats. - set_config('enablestats', false); - $tree = new phpunit_fixture_myprofile_tree(); + /** + * Tests the report_stats_myprofile_navigation() function without permission. + */ + public function test_report_stats_myprofile_navigation_without_permission() { + // Try to see as a user without permission. + $this->setUser($this->user); $iscurrentuser = true; - report_stats_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('stats', $nodes); - // Enabling stats. + // Enable stats. set_config('enablestats', true); - // Try to see as a user without permission. - $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; - report_stats_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('stats', $nodes); + report_stats_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('stats', $nodes->getValue($this->tree)); } } diff --git a/report/usersessions/tests/lib_test.php b/report/usersessions/tests/lib_test.php index 7ea173a579e4c..6bade9193b72d 100644 --- a/report/usersessions/tests/lib_test.php +++ b/report/usersessions/tests/lib_test.php @@ -25,7 +25,7 @@ defined('MOODLE_INTERNAL') || die(); global $CFG; -require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); + require_once($CFG->dirroot. '/report/usersessions/lib.php'); /** @@ -38,45 +38,84 @@ class report_usersessions_lib_testcase extends advanced_testcase { /** - * Tests for report_userssesions_myprofile_navigation() api. + * @var stdClass The user. + */ + private $user; + + /** + * @var stdClass The course. */ - public function test_report_usersessions_myprofile_navigation() { + private $course; + /** + * @var \core_user\output\myprofile\tree The navigation tree. + */ + private $tree; + + public function setUp() { + $this->user = $this->getDataGenerator()->create_user(); + $this->course = $this->getDataGenerator()->create_course(); + $this->tree = new \core_user\output\myprofile\tree(); $this->resetAfterTest(); - $this->setAdminUser(); + } - $tree = new phpunit_fixture_myprofile_tree(); - $user = $this->getDataGenerator()->create_user(); - $user2 = $this->getDataGenerator()->create_user(); - $course = $this->getDataGenerator()->create_course(); + /** + * Tests the report_userssesions_myprofile_navigation() function as an admin. + */ + public function test_report_usersessions_myprofile_navigation_as_admin() { + $this->setAdminUser(); $iscurrentuser = false; // Not even admins allowed to pick at other user's sessions. - report_usersessions_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('usersessions', $nodes); + report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree)); + } - // Try as guest user. - $this->setGuestUser(); - report_usersessions_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('usersessions', $nodes); + /** + * Tests the report_userssesions_myprofile_navigation() function as the currently logged in user. + */ + public function test_report_usersessions_myprofile_navigation_as_current_user() { + $this->setUser($this->user); + $iscurrentuser = true; + + report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayHasKey('usersessions', $nodes->getValue($this->tree)); + } - // As current user. - $this->setUser($user); - $tree = new phpunit_fixture_myprofile_tree(); + /** + * Tests the report_userssesions_myprofile_navigation() function as a guest. + */ + public function test_report_usersessions_myprofile_navigation_as_guest() { + $this->setGuestUser(); $iscurrentuser = true; - report_usersessions_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayHasKey('usersessions', $nodes); + report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree)); + } + + /** + * Tests the report_userssesions_myprofile_navigation() function as a user without permission. + */ + public function test_report_usersessions_myprofile_navigation_without_permission() { // Try to see as a user without permission. + $user2 = $this->getDataGenerator()->create_user(); $this->setUser($user2); - $tree = new phpunit_fixture_myprofile_tree(); - $iscurrentuser = true; - report_usersessions_myprofile_navigation($tree, $user, $iscurrentuser, $course); - $nodes = $tree->get_nodes(); - $this->assertArrayNotHasKey('usersessions', $nodes); + $iscurrentuser = false; + + report_usersessions_myprofile_navigation($this->tree, $this->user, $iscurrentuser, $this->course); + $reflector = new ReflectionObject($this->tree); + $nodes = $reflector->getProperty('nodes'); + $nodes->setAccessible(true); + $this->assertArrayNotHasKey('usersessions', $nodes->getValue($this->tree)); } } diff --git a/user/tests/fixtures/myprofile_fixtures.php b/user/tests/fixtures/myprofile_fixtures.php index 6a5181deeaeaa..30ed596777525 100644 --- a/user/tests/fixtures/myprofile_fixtures.php +++ b/user/tests/fixtures/myprofile_fixtures.php @@ -70,18 +70,4 @@ class phpunit_fixture_myprofile_tree extends \core_user\output\myprofile\tree { public function find_categories_after($cat) { return parent::find_categories_after($cat); } - - /** - * @return \core_user\output\myprofile\category[] - */ - public function get_categories() { - return $this->categories; - } - - /** - * @return \core_user\output\myprofile\node[] - */ - public function get_nodes() { - return $this->nodes; - } } \ No newline at end of file