From 635c65bbd5d5543912752a6ed3ece65768de927b Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 23 Sep 2011 00:03:56 +0200 Subject: [PATCH 1/4] MDL-29478 blog - fixed some recent regressions introduced by MDL-29186 --- blog/lib.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/blog/lib.php b/blog/lib.php index e247e4d3dd2..49ccdc6f27a 100644 --- a/blog/lib.php +++ b/blog/lib.php @@ -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')); From 40a897491518373329d44c9e93de784b23b50ade Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 23 Sep 2011 00:11:50 +0200 Subject: [PATCH 2/4] MDL-29478 unittest - we aren't a report anymore but one admin/tool --- lib/simpletest/testpagelib_moodlepage.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/simpletest/testpagelib_moodlepage.php b/lib/simpletest/testpagelib_moodlepage.php index 8f699b0571e..2c66b454610 100644 --- a/lib/simpletest/testpagelib_moodlepage.php +++ b/lib/simpletest/testpagelib_moodlepage.php @@ -225,7 +225,7 @@ class moodle_page_test extends UnitTestCase { public function test_pagetype_defaults_to_script() { // Exercise SUT and validate - $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype); + $this->assertEqual('admin-tool-unittest-index', $this->testpage->pagetype); } public function test_set_pagetype() { @@ -239,7 +239,7 @@ class moodle_page_test extends UnitTestCase { // Exercise SUT $this->testpage->initialise_default_pagetype('admin/tool/unittest/index.php'); // Validate - $this->assertEqual('admin-report-unittest-index', $this->testpage->pagetype); + $this->assertEqual('admin-tool-unittest-index', $this->testpage->pagetype); } public function test_initialise_default_pagetype_fp() { From 5169a039c7f2512127ce767f3ddf88762991df8b Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 23 Sep 2011 01:35:46 +0200 Subject: [PATCH 3/4] MDL-29478 unittest - explicity include blog/lib.php. Was causing "random" errors --- blog/simpletest/testbloglib.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/blog/simpletest/testbloglib.php b/blog/simpletest/testbloglib.php index 4d552cceea3..fdc379ee89a 100644 --- a/blog/simpletest/testbloglib.php +++ b/blog/simpletest/testbloglib.php @@ -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; From e437d9f1d9883fa4ea3929ba43060faccedd26f9 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Fri, 23 Sep 2011 01:46:38 +0200 Subject: [PATCH 4/4] MDL-29478 unittest - fix rating tests so they can be executed with accesslib caches cleaned --- rating/simpletest/testrating.php | 63 +++++++++++++++++++++++++++++--- 1 file changed, 58 insertions(+), 5 deletions(-) diff --git a/rating/simpletest/testrating.php b/rating/simpletest/testrating.php index 910544c08da..d903496a1fd 100644 --- a/rating/simpletest/testrating.php +++ b/rating/simpletest/testrating.php @@ -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 @@ class rating_db_test extends UnitTestCaseUsingDatabase { 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 @@ class rating_db_test extends UnitTestCaseUsingDatabase { // 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 @@ class rating_db_test extends UnitTestCaseUsingDatabase { $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); + } + +}