diff --git a/badges/tests/badgeslib_test.php b/badges/tests/badgeslib_test.php index 2de11228a5602..0f24489e0f1d3 100644 --- a/badges/tests/badgeslib_test.php +++ b/badges/tests/badgeslib_test.php @@ -28,6 +28,8 @@ 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; @@ -472,4 +474,54 @@ public function test_badges_assertion() { $this->assertStringMatchesFormat($testassertion->class, json_encode($assertion->get_badge_class())); $this->assertStringMatchesFormat($testassertion->issuer, json_encode($assertion->get_issuer())); } + + /** + * Tests for core_badges_myprofile_navigation() api. + */ + public function test_core_badges_myprofile_navigation() { + $this->resetAfterTest(); + $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(); + $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); + + // 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(); + $iscurrentuser = false; + core_badges_myprofile_navigation($tree, $user, $iscurrentuser, $course); + $nodes = $tree->get_nodes(); + $this->assertArrayHasKey('localbadges', $nodes); + } } diff --git a/blog/tests/bloglib_test.php b/blog/tests/lib_test.php similarity index 93% rename from blog/tests/bloglib_test.php rename to blog/tests/lib_test.php index 73c81f25f214f..23192986538ab 100644 --- a/blog/tests/bloglib_test.php +++ b/blog/tests/lib_test.php @@ -26,11 +26,12 @@ 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 */ -class core_bloglib_testcase extends advanced_testcase { +class core_blog_lib_testcase extends advanced_testcase { private $courseid; private $cmid; @@ -476,5 +477,39 @@ public function test_blog_comment_deleted_event() { $this->assertEquals($url, $event->get_url()); $this->assertEventContextNotUsed($event); } + + /** + * Tests for core_blog_myprofile_navigation() api. + */ + public function test_core_blog_myprofile_navigation() { + global $USER; + + $this->resetAfterTest(); + $this->setGuestUser(); + + // No blogs for guest users. + $tree = new phpunit_fixture_myprofile_tree(); + $course = null; + $iscurrentuser = false; + core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course); + $nodes = $tree->get_nodes(); + $this->assertArrayNotHasKey('blogs', $nodes); + + // 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; + core_blog_myprofile_navigation($tree, $USER, $iscurrentuser, $course); + $nodes = $tree->get_nodes(); + $this->assertArrayHasKey('blogs', $nodes); + } } diff --git a/grade/report/user/tests/lib_test.php b/grade/report/user/tests/lib_test.php new file mode 100644 index 0000000000000..b96226a1b9022 --- /dev/null +++ b/grade/report/user/tests/lib_test.php @@ -0,0 +1,67 @@ +. + +/** + * Tests for gradereport_user library functions. + * + * @package gradereport_user + * @copyright 2015 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +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'); + +/** + * Class gradereport_user_lib_testcase. + * + * @package gradereport_user + * @copyright 2015 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class gradereport_user_lib_testcase extends advanced_testcase { + + /** + * Tests for gradereport_user_myprofile_navigation() api. + */ + 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); + + // Try to see as a user without permission. + $this->setUser($user2); + $tree = new phpunit_fixture_myprofile_tree(); + $iscurrentuser = true; + gradereport_user_myprofile_navigation($tree, $user, $iscurrentuser, $course); + $nodes = $tree->get_nodes(); + $this->assertArrayNotHasKey('grade', $nodes); + } +} diff --git a/lib/tests/myprofilelib_test.php b/lib/tests/myprofilelib_test.php new file mode 100644 index 0000000000000..74f18298ebbeb --- /dev/null +++ b/lib/tests/myprofilelib_test.php @@ -0,0 +1,189 @@ +. + +/** + * Tests for myprofilelib apis. + * + * @package core + * @copyright 2015 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); +require_once($CFG->dirroot . '/lib/myprofilelib.php'); + +/** + * Tests for myprofilelib apis. + * + * @package core + * @copyright 2015 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class core_myprofilelib_testcase extends advanced_testcase { + + /** + * Tests for report_log_myprofile_navigation() api. + */ + public function test_core_myprofile_navigation() { + global $CFG; + + $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; + + // Test tree as admin user. + core_myprofile_navigation($tree, $user, $iscurrentuser, $course); + $cats = $tree->get_categories(); + $this->assertArrayHasKey('contact', $cats); + $this->assertArrayHasKey('coursedetails', $cats); + $this->assertArrayHasKey('miscellaneous', $cats); + $this->assertArrayHasKey('reports', $cats); + $this->assertArrayHasKey('administration', $cats); + $this->assertArrayHasKey('loginactivity', $cats); + + // Course node. + $nodes = $tree->get_nodes(); + $this->assertArrayHasKey('fullprofile', $nodes); + + // 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); + + // Edit profile link. + $this->setUser($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. + $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. + $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(); + $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); + + // User contact fields. + set_config("hiddenuserfields", "country,city,webpage,icqnumber,skypeid,yahooid,aimid,msnid"); + set_config("showuseridentity", "email,address,phone1,phone2,institution,department,idnumber"); + $hiddenfields = explode(',', $CFG->hiddenuserfields); + $identityfields = explode(',', $CFG->showuseridentity); + $this->setAdminUser(); + $iscurrentuser = false; + + // Make sure fields are not empty. + $fields = array( + 'country' => 'AU', + 'city' => 'Silent hill', + 'url' => 'Ghosts', + 'icq' => 'Wth is ICQ?', + 'skype' => 'derp', + 'yahoo' => 'are you living in the 90\'s?', + 'aim' => 'are you for real?', + 'msn' => '...', + 'email' => 'Rulelikeaboss@example.com', + 'address' => 'Didn\'t I mention silent hill already ?', + 'phone1' => '123', + 'phone2' => '234', + 'institution' => 'strange land', + 'department' => 'video game/movie', + 'idnumber' => 'SLHL' + ); + foreach ($fields as $field => $value) { + $user->$field = $value; + } + + // User with proper permissions. + $tree = new phpunit_fixture_myprofile_tree(); + core_myprofile_navigation($tree, $user, $iscurrentuser, null); + $nodes = $tree->get_nodes(); + foreach ($hiddenfields as $field) { + $this->assertArrayHasKey($field, $nodes); + } + foreach ($identityfields as $field) { + $this->assertArrayHasKey($field, $nodes); + } + + // User without permission. + $this->setUser($user2); + $tree = new phpunit_fixture_myprofile_tree(); + core_myprofile_navigation($tree, $user, $iscurrentuser, null); + $nodes = $tree->get_nodes(); + foreach ($hiddenfields as $field) { + $this->assertArrayNotHasKey($field, $nodes); + } + foreach ($identityfields as $field) { + $this->assertArrayNotHasKey($field, $nodes); + } + + // 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); + + // User without permission. + set_config("hiddenuserfields", "firstaccess,lastaccess,lastip"); + $this->setUser($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); + } +} diff --git a/mod/forum/tests/lib_test.php b/mod/forum/tests/lib_test.php index 20b04e5e4dd9e..8eddf979bd7d3 100644 --- a/mod/forum/tests/lib_test.php +++ b/mod/forum/tests/lib_test.php @@ -27,6 +27,7 @@ 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 { @@ -2114,4 +2115,44 @@ public function test_forum_user_can_post_discussion() { $can = forum_user_can_post_discussion($forum, null, -1, $cm, $context); $this->assertTrue($can); } + + /** + * Tests for mod_forum_myprofile_navigation() api. + */ + public function test_mod_forum_myprofile_navigation() { + global $USER; + + $this->resetAfterTest(); + $this->setGuestUser(); + + $tree = new phpunit_fixture_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. + $this->setUser($user2); + $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); + } } diff --git a/notes/tests/lib_test.php b/notes/tests/lib_test.php new file mode 100644 index 0000000000000..bd03b16fffa1f --- /dev/null +++ b/notes/tests/lib_test.php @@ -0,0 +1,72 @@ +. + +/** + * Tests for notes library functions. + * + * @package core_notes + * @copyright 2015 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +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 + * + * @package core_notes + * @copyright 2015 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class core_notes_lib_testcase extends advanced_testcase { + + /** + * Tests for core_notes_myprofile_navigation() api. + */ + 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. + $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); + } +} \ No newline at end of file diff --git a/report/log/tests/lib_test.php b/report/log/tests/lib_test.php index 4c0996de99b6b..0cb41e064ded8 100644 --- a/report/log/tests/lib_test.php +++ b/report/log/tests/lib_test.php @@ -24,6 +24,9 @@ defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); + /** * Class report_log_events_testcase. * @@ -54,4 +57,34 @@ public function test_report_log_supports_logstore() { $this->assertContains($expectedstore, $stores); } } + + /** + * Tests for report_log_myprofile_navigation() api. + */ + public function test_report_log_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; + + // As user with permissions. + report_log_myprofile_navigation($tree, $user, $iscurrentuser, $course); + $nodes = $tree->get_nodes(); + $this->assertArrayHasKey('alllogs', $nodes); + $this->assertArrayHasKey('todayslogs', $nodes); + + // Try to see as a user without permission. + $this->setUser($user2); + $tree = new phpunit_fixture_myprofile_tree(); + $iscurrentuser = true; + report_log_myprofile_navigation($tree, $user, $iscurrentuser, $course); + $nodes = $tree->get_nodes(); + $this->assertArrayNotHasKey('alllogs', $nodes); + $this->assertArrayNotHasKey('todayslogs', $nodes); + } } diff --git a/report/outline/tests/lib_test.php b/report/outline/tests/lib_test.php index 9f1f143895740..6b4417f5e5cbb 100644 --- a/report/outline/tests/lib_test.php +++ b/report/outline/tests/lib_test.php @@ -24,6 +24,9 @@ defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); + /** * Class report_outline_lib_testcase * @@ -53,4 +56,40 @@ public function test_report_participation_supports_logstore() { $this->assertContains($expectedstore, $stores); } } + + /** + * Tests for report_outline_myprofile_navigation() api. + */ + 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); + + $tree = new phpunit_fixture_myprofile_tree(); + $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); + } } diff --git a/report/stats/tests/lib_test.php b/report/stats/tests/lib_test.php index a7eb50086097c..deb14ff00dd20 100644 --- a/report/stats/tests/lib_test.php +++ b/report/stats/tests/lib_test.php @@ -24,6 +24,9 @@ defined('MOODLE_INTERNAL') || die(); +global $CFG; +require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); + /** * Class report_stats_lib_testcase * @@ -53,4 +56,50 @@ public function test_report_participation_supports_logstore() { $this->assertContains($expectedstore, $stores); } } + + /** + * Tests for report_stats_myprofile_navigation() api. + */ + 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. + 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); + } } diff --git a/report/usersessions/tests/lib_test.php b/report/usersessions/tests/lib_test.php new file mode 100644 index 0000000000000..7ea173a579e4c --- /dev/null +++ b/report/usersessions/tests/lib_test.php @@ -0,0 +1,82 @@ +. + +/** + * Tests for report library functions. + * + * @package report_usersessions + * @copyright 2015 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ + +defined('MOODLE_INTERNAL') || die(); + +global $CFG; +require_once($CFG->dirroot . '/user/tests/fixtures/myprofile_fixtures.php'); +require_once($CFG->dirroot. '/report/usersessions/lib.php'); + +/** + * Class report_stats_lib_testcase + * + * @package report_usersessions + * @copyright 2014 onwards Ankit agarwal + * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later. + */ +class report_usersessions_lib_testcase extends advanced_testcase { + + /** + * Tests for report_userssesions_myprofile_navigation() api. + */ + public function test_report_usersessions_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; + + // 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); + + // Try as guest user. + $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); + + // Try to see as a user without permission. + $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); + + } +} diff --git a/user/tests/fixtures/myprofile_fixtures.php b/user/tests/fixtures/myprofile_fixtures.php index 30ed596777525..6a5181deeaeaa 100644 --- a/user/tests/fixtures/myprofile_fixtures.php +++ b/user/tests/fixtures/myprofile_fixtures.php @@ -70,4 +70,18 @@ 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