Skip to content

Commit

Permalink
MDL-49566 core: Write unittests for MDL-45898
Browse files Browse the repository at this point in the history
  • Loading branch information
ankitagarwal authored and mdjnelson committed Dec 29, 2015
1 parent d955a20 commit bef0d6b
Show file tree
Hide file tree
Showing 11 changed files with 674 additions and 1 deletion.
52 changes: 52 additions & 0 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}
37 changes: 36 additions & 1 deletion blog/tests/bloglib_test.php → blog/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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);
}
}

67 changes: 67 additions & 0 deletions grade/report/user/tests/lib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Tests for gradereport_user library functions.
*
* @package gradereport_user
* @copyright 2015 onwards Ankit agarwal <[email protected]>
* @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 <[email protected]>
* @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);
}
}
189 changes: 189 additions & 0 deletions lib/tests/myprofilelib_test.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,189 @@
<?php
// This file is part of Moodle - http://moodle.org/
//
// Moodle is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Moodle is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Moodle. If not, see <http://www.gnu.org/licenses/>.

/**
* Tests for myprofilelib apis.
*
* @package core
* @copyright 2015 onwards Ankit agarwal <[email protected]>
* @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 <[email protected]>
* @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' => '[email protected]',
'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);
}
}
Loading

0 comments on commit bef0d6b

Please sign in to comment.