Skip to content

Commit

Permalink
MDL-49566 core: used reflection in unit tests
Browse files Browse the repository at this point in the history
Also split the tests into multiple functions for each scenario.
  • Loading branch information
mdjnelson committed Dec 29, 2015
1 parent bef0d6b commit be5e011
Show file tree
Hide file tree
Showing 11 changed files with 639 additions and 315 deletions.
86 changes: 50 additions & 36 deletions badges/tests/badgeslib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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));
}
}
73 changes: 53 additions & 20 deletions blog/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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));
}
}

58 changes: 40 additions & 18 deletions grade/report/user/tests/lib_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

/**
Expand All @@ -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));
}
}
Loading

0 comments on commit be5e011

Please sign in to comment.