Permalink
Browse files

Merge branch 'MDL-29478_21' of git://github.com/stronk7/moodle into M…

…OODLE_21_STABLE
  • Loading branch information...
Sam Hemelryk
Sam Hemelryk committed Sep 25, 2011
2 parents 65072bf + 4d1e35d commit d7749a10a62a1e31b861d3da4c1d743db851e85f
Showing with 63 additions and 9 deletions.
  1. +3 −3 blog/lib.php
  2. +2 −1 blog/simpletest/testbloglib.php
  3. +58 −5 rating/simpletest/testrating.php
View
@@ -844,7 +844,7 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu
$a = new stdClass();
$a->user = fullname($user);
- $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $courseid)));
+ $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
$a->type = get_string('course');
$headers['heading'] = get_string('blogentriesbyuseraboutcourse', 'blog', $a);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
@@ -870,7 +870,7 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu
$a = new stdClass();
$a->group = $group->name;
- $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $courseid)));
+ $a->course = format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id)));
$a->type = get_string('course');
$headers['heading'] = get_string('blogentriesbygroupaboutcourse', 'blog', $a);
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
@@ -927,7 +927,7 @@ function blog_get_headers($courseid=null, $groupid=null, $userid=null, $tagid=nu
$PAGE->set_heading("$siteshortname: $courseshortname: " . get_string('blogentries', 'blog'));
$a = new stdClass();
$a->type = get_string('course');
- $headers['heading'] = get_string('blogentriesabout', 'blog', format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $courseid))));
+ $headers['heading'] = get_string('blogentriesabout', 'blog', format_string($course->fullname, true, array('context' => get_context_instance(CONTEXT_COURSE, $course->id))));
$headers['stradd'] = get_string('blogaboutthis', 'blog', $a);
$headers['strview'] = get_string('viewblogentries', 'blog', $a);
$blogurl->remove_params(array('userid'));
@@ -26,13 +26,14 @@
*/
require_once($CFG->dirroot . '/blog/locallib.php');
+require_once($CFG->dirroot . '/blog/lib.php');
/**
* Test functions that rely on the DB tables
*/
class bloglib_test extends UnitTestCaseUsingDatabase {
- public static $includecoverage = array('blog/locallib.php');
+ public static $includecoverage = array('blog/locallib.php', 'blog/lib.php');
private $courseid; // To store important ids to be used in tests
private $groupid;
@@ -39,16 +39,51 @@ class rating_db_test extends UnitTestCaseUsingDatabase {
protected $testtables = array(
'lib' => array(
- 'rating', 'scale'));
+ 'rating', 'scale', 'context', 'capabilities', 'role_assignments', 'role_capabilities', 'course'));
+
+ protected $syscontext;
+ protected $neededcaps = array('view', 'viewall', 'viewany', 'rate');
+ protected $originaldefaultfrontpageroleid;
public function setUp() {
+ global $CFG;
parent::setUp();
- $this->switch_to_test_db(); // Switch to test DB for all the execution
+ // Make sure accesslib has cached a sensible system context object
+ // before we switch to the test DB.
+ $this->syscontext = get_context_instance(CONTEXT_SYSTEM);
foreach ($this->testtables as $dir => $tables) {
$this->create_test_tables($tables, $dir); // Create tables
}
+
+ $this->switch_to_test_db(); // Switch to test DB for all the execution
+
+ $this->fill_records();
+
+ // Ignore any frontpageroleid, that would require to crete more contexts
+ $this->originaldefaultfrontpageroleid = $CFG->defaultfrontpageroleid;
+ $CFG->defaultfrontpageroleid = null;
+ }
+
+ public function tearDown() {
+ global $CFG;
+ // Recover original frontpageroleid
+ $CFG->defaultfrontpageroleid = $this->originaldefaultfrontpageroleid;
+ parent::tearDown();
+ }
+
+ private function fill_records() {
+ global $DB;
+
+ // Set up systcontext in the test database.
+ $this->syscontext->id = $this->testdb->insert_record('context', $this->syscontext);
+
+ // Add the capabilities used by ratings
+ foreach ($this->neededcaps as $neededcap) {
+ $this->testdb->insert_record('capabilities', (object)array('name' => 'moodle/rating:' . $neededcap,
+ 'contextlevel' => CONTEXT_COURSE));
+ }
}
/**
@@ -57,7 +92,7 @@ public function setUp() {
function test_get_ratings_sql() {
// We load 3 items. Each is rated twice. For simplicity itemid == user id of the item owner
- $ctxid = SYSCONTEXTID;
+ $ctxid = $this->syscontext->id;
$this->load_test_data('rating',
array('contextid', 'component', 'ratingarea', 'itemid', 'scaleid', 'rating', 'userid', 'timecreated', 'timemodified'), array(
@@ -86,13 +121,13 @@ function test_get_ratings_sql() {
// Prepare the default options
$defaultoptions = array (
- 'context' => get_context_instance(CONTEXT_SYSTEM),
+ 'context' => $this->syscontext,
'component' => 'mod_forum',
'ratingarea' => 'post',
'scaleid' => 10,
'aggregate' => RATING_AGGREGATE_AVERAGE);
- $rm = new rating_manager();
+ $rm = new mockup_rating_manager();
// STEP 1: Retreive ratings using the current user
@@ -223,3 +258,21 @@ function test_get_ratings_sql() {
$this->assertEqual($result[0]->rating->aggregate, 3);//should still get the aggregate
}
}
+
+/**
+ * rating_manager subclass for unit testing without requiring capabilities to be loaded
+ */
+class mockup_rating_manager extends rating_manager {
+
+ /**
+ * Overwrite get_plugin_permissions_array() so it always return granted perms for unit testing
+ */
+ public function get_plugin_permissions_array($contextid, $component, $ratingarea) {
+ return array(
+ 'rate' => true,
+ 'view' => true,
+ 'viewany' => true,
+ 'viewall' => true);
+ }
+
+}

0 comments on commit d7749a1

Please sign in to comment.