MDL-49566 core: used reflection in unit tests

Also split the tests into multiple functions for each scenario.
This commit is contained in:
Mark Nelson
2015-12-29 17:49:34 +08:00
parent 75bc323bf1
commit 7238ffc65d
11 changed files with 647 additions and 323 deletions
+51 -37
View File
@@ -29,7 +29,6 @@ defined('MOODLE_INTERNAL') || die();
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 @@ class core_badges_badgeslib_testcase extends advanced_testcase {
}
/**
* 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));
}
// Course badge.
$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();
/**
* 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;
core_badges_myprofile_navigation($tree, $user, $iscurrentuser, $course);
$nodes = $tree->get_nodes();
$this->assertArrayHasKey('localbadges', $nodes);
$course = null;
// Disable badges.
set_config('enablebadges', false);
// 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($this->user->id, true);
$iscurrentuser = false;
// 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));
}
}
+53 -20
View File
@@ -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
@@ -475,37 +474,71 @@ class core_blog_lib_testcase extends advanced_testcase {
}
/**
* 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();
// 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();
// No blogs for guest users.
$tree = new phpunit_fixture_myprofile_tree();
$course = null;
$iscurrentuser = false;
// 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));
}
/**
* 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.
$this->setAdminUser();
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));
}
}
+41 -19
View File
@@ -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 @@ require_once($CFG->dirroot . '/grade/report/user/lib.php');
class gradereport_user_lib_testcase extends advanced_testcase {
/**
* Tests for gradereport_user_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 gradereport_user_myprofile_navigation() function.
*/
public function test_gradereport_user_myprofile_navigation() {
$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();
$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));
}
}
+159 -73
View File
@@ -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 @@ require_once($CFG->dirroot . '/lib/myprofilelib.php');
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 @@ class core_myprofilelib_testcase extends advanced_testcase {
$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;
// Edit profile link.
$this->setUser($user);
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));
}
/**
* 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 @@ class core_myprofilelib_testcase extends advanced_testcase {
'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));
}
}
+58 -25
View File
@@ -27,7 +27,6 @@ defined('MOODLE_INTERNAL') || die();
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 {
@@ -1500,42 +1499,76 @@ class mod_forum_lib_testcase extends advanced_testcase {
}
/**
* 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->resetAfterTest(true);
// Set up the test.
$tree = new \core_user\output\myprofile\tree();
$course = $this->getDataGenerator()->create_course();
$iscurrentuser = true;
// Set user as guest.
$this->setGuestUser();
$tree = new phpunit_fixture_myprofile_tree();
// 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->assertArrayNotHasKey('forumposts', $nodes->getValue($tree));
$this->assertArrayNotHasKey('forumdiscussions', $nodes->getValue($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 = false;
// Nothing for guest users.
mod_forum_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$nodes = $tree->get_nodes();
$this->assertArrayNotHasKey('forumposts', $nodes);
$this->assertArrayNotHasKey('forumdiscussions', $nodes);
// Current user.
$this->setUser($user);
$tree = new phpunit_fixture_myprofile_tree();
$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));
}
}
+69 -24
View File
@@ -26,7 +26,6 @@ defined('MOODLE_INTERNAL') || die();
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 @@ require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php');
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();
$this->setGuestUser();
// No notes for guest users.
$tree = new phpunit_fixture_myprofile_tree();
$course = null;
$iscurrentuser = false;
core_notes_myprofile_navigation($tree, $USER, $iscurrentuser, $course);
$nodes = $tree->get_nodes();
$this->assertArrayNotHasKey('blogs', $nodes);
// Disable notes.
// Set up the test.
$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);
$iscurrentuser = true;
// 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->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));
}
/**
* Tests the core_notes_myprofile_navigation() function.
*/
public function test_core_notes_myprofile_navigation_notes_disabled() {
global $USER;
$this->setAdminUser();
$iscurrentuser = false;
// Disable notes.
set_config('enablenotes', 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));
}
}
+46 -21
View File
@@ -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 @@ require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php');
*/
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 @@ class report_log_lib_testcase extends advanced_testcase {
}
/**
* 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));
}
}
+42 -26
View File
@@ -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 @@ require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php');
*/
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 @@ class report_outline_lib_testcase extends advanced_testcase {
}
/**
* 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));
}
}
+62 -37
View File
@@ -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 @@ require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php');
*/
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 @@ class report_stats_lib_testcase extends advanced_testcase {
}
/**
* 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.
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();
$iscurrentuser = false;
report_stats_myprofile_navigation($tree, $user, $iscurrentuser, $course);
$nodes = $tree->get_nodes();
$this->assertArrayHasKey('stats', $nodes);
$tree = new phpunit_fixture_myprofile_tree();
$iscurrentuser = true;
report_stats_myprofile_navigation($tree, $user, $iscurrentuser, $course);
$nodes = $tree->get_nodes();
$this->assertArrayHasKey('stats', $nodes);
// Disabling 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->assertArrayNotHasKey('stats', $nodes);
// Enabling stats.
// Enable stats.
set_config('enablestats', true);
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;
// Disable stats.
set_config('enablestats', false);
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));
}
/**
* 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($user2);
$tree = new phpunit_fixture_myprofile_tree();
$this->setUser($this->user);
$iscurrentuser = true;
report_stats_myprofile_navigation($tree, $user, $iscurrentuser, $course);
$nodes = $tree->get_nodes();
$this->assertArrayNotHasKey('stats', $nodes);
// Enable stats.
set_config('enablestats', true);
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));
}
}
+66 -27
View File
@@ -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 @@ require_once($CFG->dirroot. '/report/usersessions/lib.php');
class report_usersessions_lib_testcase extends advanced_testcase {
/**
* Tests for report_userssesions_myprofile_navigation() api.
* @var stdClass The user.
*/
public function test_report_usersessions_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();
}
$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.
/**
* 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));
}
/**
* Tests the report_userssesions_myprofile_navigation() function as a guest.
*/
public function test_report_usersessions_myprofile_navigation_as_guest() {
$this->setGuestUser();
report_usersessions_myprofile_navigation($tree, $user, $iscurrentuser, $course);
$nodes = $tree->get_nodes();
$this->assertArrayNotHasKey('usersessions', $nodes);
// As current user.
$this->setUser($user);
$tree = new phpunit_fixture_myprofile_tree();
$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));
}
}
-14
View File
@@ -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;
}
}