MDL-29602 unittest - fixing other tests to work with new accesslib
authorEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 18 Oct 2011 16:23:00 +0000 (18:23 +0200)
committerEloy Lafuente (stronk7) <stronk7@moodle.org>
Tue, 18 Oct 2011 23:37:30 +0000 (01:37 +0200)
admin/tool/unittest/simpletestlib.php
lib/simpletest/testblocklib_blockmanager.php
lib/simpletest/testfilterconfig.php
rating/simpletest/testrating.php

index d920503..e2c573f 100644 (file)
@@ -745,6 +745,52 @@ class UnitTestCaseUsingDatabase extends UnitTestCase {
         }
     }
 
+    /**
+     * Recreates the system context record in the 'context' table
+     *
+     * Once we have switched to test db, if we have recreated the
+     * context table and it's empty, it may be necessary to manually
+     * create the system context record if unittests are going to
+     * play with contexts.
+     *
+     * This is needed because the context_system::instance() method
+     * is exceptional and always requires the record to exist, never
+     * creating it :-( No problem for other contexts.
+     *
+     * Altenatively one complete install can be done, like
+     * {@see accesslib_test::test_everything_in_accesslib} does, but that's
+     * to much for some tests not requiring all the roles/caps/friends
+     * to be present.
+     *
+     * Ideally some day we'll move a lot of these UnitTests to a complete
+     * cloned installation with real data to play with. That day this
+     * won't be necessary anymore.
+     */
+    protected function create_system_context_record() {
+        global $DB;
+
+        // If, for any reason, the record exists, do nothing
+        if ($DB->record_exists('context', array('contextlevel'=>CONTEXT_SYSTEM))) {
+            return;
+        }
+
+        $record = new stdClass();
+        $record->contextlevel = CONTEXT_SYSTEM;
+        $record->instanceid   = 0;
+        $record->depth        = 1;
+        $record->path         = null;
+        if (defined('SYSCONTEXTID')) {
+            $record->id = SYSCONTEXTID;
+            $DB->import_record('context', $record);
+            $DB->get_manager()->reset_sequence('context');
+        } else {
+            $record->id = $DB->insert_record('context', $record);
+        }
+        // fix path
+        $record->path  = '/'.$record->id;
+        $DB->set_field('context', 'path', $record->path, array('id' => $record->id));
+    }
+
     /**
      * Check that the user has not forgotten to clean anything up, and if they
      * have, display a rude message and clean it up for them.
index 3d4b292..818f3d1 100644 (file)
@@ -170,8 +170,12 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
             unset($USER->editing);
         }
         parent::setUp();
-        $this->create_test_tables(array('block', 'block_instances', 'block_positions', 'context'), 'lib');
+        $this->create_test_tables(array('block', 'block_instances', 'block_positions', 'context', 'course_categories', 'course'), 'lib');
         $this->switch_to_test_db();
+        // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily)
+        $this->create_system_context_record();
+        // Reset all caches
+        accesslib_clear_all_caches_for_unit_testing();
     }
 
     public function tearDown() {
@@ -184,17 +188,6 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
         parent::tearDown();
     }
 
-    /**
-     * Saves the context in the DB, setting $contextid.
-     * @param $context. Context. Path should be set to /parent/path/, that is with a traling /.
-     * This context's id will be appended.
-     */
-    protected function insert_context_in_db($context) {
-        $context->id = $this->testdb->insert_record('context', $context);
-        $context->path .= $context->id;
-        $this->testdb->set_field('context', 'path', $context->path, array('id' => $context->id));
-    }
-
     protected function get_a_page_and_block_manager($regions, $context, $pagetype, $subpage = '') {
         $page = new moodle_page;
         $page->set_context($context);
@@ -245,8 +238,6 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
         $context = get_context_instance(CONTEXT_SYSTEM);
-        $context->path = '/';
-        $this->insert_context_in_db($context);
 
         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
                 $context, 'page-type');
@@ -264,8 +255,6 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
         $context = get_context_instance(CONTEXT_SYSTEM);
-        $context->path = '/';
-        $this->insert_context_in_db($context);
 
         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
                 $context, 'page-type');
@@ -280,14 +269,18 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
     }
 
     public function test_block_not_included_in_different_context() {
+        global $DB;
         // Set up fixture.
         $syscontext = get_context_instance(CONTEXT_SYSTEM);
-        $syscontext->path = '/';
-        $this->insert_context_in_db($syscontext);
-        $fakecontext = new stdClass;
-        $fakecontext->contextlevel = CONTEXT_COURSECAT;
-        $fakecontext->path = $syscontext->path . '/';
-        $this->insert_context_in_db($fakecontext);
+        $cat = new stdClass();
+        $cat->name         = 'testcategory';
+        $cat->parent       = 0;
+        $cat->depth        = 1;
+        $cat->sortorder    = 100;
+        $cat->timemodified = time();
+        $catid = $DB->insert_record('course_categories', $cat);
+        $DB->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid));
+        $fakecontext = context_coursecat::instance($catid);
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
 
@@ -304,14 +297,18 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
     }
 
     public function test_block_included_in_sub_context() {
+        global $DB;
         // Set up fixture.
         $syscontext = get_context_instance(CONTEXT_SYSTEM);
-        $syscontext->path = '/';
-        $this->insert_context_in_db($syscontext);
-        $childcontext = new stdClass;
-        $childcontext->contextlevel = CONTEXT_COURSECAT;
-        $childcontext->path = $syscontext->path . '/';
-        $this->insert_context_in_db($childcontext);
+        $cat = new stdClass();
+        $cat->name         = 'testcategory';
+        $cat->parent       = 0;
+        $cat->depth        = 1;
+        $cat->sortorder    = 100;
+        $cat->timemodified = time();
+        $catid = $DB->insert_record('course_categories', $cat);
+        $DB->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid));
+        $childcontext = context_coursecat::instance($catid);
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
 
@@ -330,8 +327,6 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
     public function test_block_not_included_on_different_page_type() {
         // Set up fixture.
         $syscontext = get_context_instance(CONTEXT_SYSTEM);
-        $syscontext->path = '/';
-        $this->insert_context_in_db($syscontext);
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
 
@@ -352,8 +347,6 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
         $syscontext = get_context_instance(CONTEXT_SYSTEM);
-        $syscontext->path = '/';
-        $this->insert_context_in_db($syscontext);
 
         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
                 $syscontext, 'page-type', 'sub-page');
@@ -372,8 +365,6 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
         $syscontext = get_context_instance(CONTEXT_SYSTEM);
-        $syscontext->path = '/';
-        $this->insert_context_in_db($syscontext);
 
         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
                 $syscontext, 'page-type', 'sub-page');
@@ -392,8 +383,6 @@ class moodle_block_manager_test_saving_loading extends UnitTestCaseUsingDatabase
         $regionname = 'a-region';
         $blockname = $this->get_a_known_block_type();
         $syscontext = get_context_instance(CONTEXT_SYSTEM);
-        $syscontext->path = '/';
-        $this->insert_context_in_db($syscontext);
 
         list($page, $blockmanager) = $this->get_a_page_and_block_manager(array($regionname),
                 $syscontext, 'page-type', 'sub-page');
index 9f1ad0a..63d174a 100644 (file)
@@ -372,35 +372,41 @@ class filter_get_active_available_in_context_test extends UnitTestCaseUsingDatab
     public function setUp() {
         parent::setUp();
 
-        // Make sure accesslib has cached a sensible system context object
-        // before we switch to the test DB.
-        $this->syscontext = get_context_instance(CONTEXT_SYSTEM);
-
         // Create the table we need and switch to test DB.
-        $this->create_test_tables(array('filter_active', 'filter_config', 'context'), 'lib');
+        $this->create_test_tables(array('filter_active', 'filter_config', 'context', 'course_categories', 'course'), 'lib');
         $this->switch_to_test_db();
+        // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily)
+        $this->create_system_context_record();
+        // Reset all caches
+        accesslib_clear_all_caches_for_unit_testing();
 
-        // Set up systcontext in the test database.
-        $this->testdb->insert_record('context', $this->syscontext);
-        $this->syscontext->id = 1;
+        $this->syscontext = get_context_instance(CONTEXT_SYSTEM);
 
         // Set up a child context.
-        $this->childcontext = new stdClass;
-        $this->childcontext->contextlevel = CONTEXT_COURSECAT;
-        $this->childcontext->instanceid = 1;
-        $this->childcontext->depth = 2;
-        $this->childcontext->path = '/1/2';
-        $this->testdb->insert_record('context', $this->childcontext);
-        $this->childcontext->id = 2;
+        $cat = new stdClass();
+        $cat->name         = 'testcategory';
+        $cat->parent       = 0;
+        $cat->depth        = 1;
+        $cat->sortorder    = 100;
+        $cat->timemodified = time();
+        $catid = $this->testdb->insert_record('course_categories', $cat);
+        $this->testdb->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid));
+        $this->childcontext = context_coursecat::instance($catid);
 
         // Set up a grandchild context.
-        $this->childcontext2 = new stdClass;
-        $this->childcontext2->contextlevel = CONTEXT_COURSE;
-        $this->childcontext2->instanceid = 2;
-        $this->childcontext2->depth = 3;
-        $this->childcontext2->path = '/1/2/3';
-        $this->testdb->insert_record('context', $this->childcontext2);
-        $this->childcontext2->id = 3;
+        $course = new stdClass();
+        $course->fullname     = 'testcourse';
+        $course->shortname    = 'tc';
+        $course->summary      = 'testcourse summary';
+        $course->newsitems    = 0;
+        $course->numsections  = 1;
+        $course->category     = $catid;
+        $course->format       = 'topics';
+        $course->timecreated  = time();
+        $course->visible      = 1;
+        $course->timemodified = $course->timecreated;
+        $courseid = $this->testdb->insert_record('course', $course);
+        $this->childcontext2 = context_course::instance($courseid);
     }
 
     private function assert_filter_list($expectedfilters, $filters) {
@@ -560,36 +566,37 @@ class filter_preload_activities_test extends UnitTestCaseUsingDatabase {
 
         // Create the table we need and switch to test DB.
         $this->create_test_tables(array('filter_active', 'filter_config', 'context',
-            'course', 'course_modules', 'modules', 'course_sections',
-            'course_modules_availability', 'grade_items'), 'lib');
+            'course', 'course_categories', 'course_modules', 'modules', 'course_sections',
+            'course_modules_availability', 'grade_items', 'cache_text'), 'lib');
         $this->create_test_tables(array('label'), 'mod/label');
         $this->switch_to_test_db();
+        // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily)
+        $this->create_system_context_record();
+        // Reset all caches
+        accesslib_clear_all_caches_for_unit_testing();
 
-        // Set up systcontext in the test database.
-        $this->syscontext->id = $this->testdb->insert_record('context', $this->syscontext);
+        $this->syscontext = get_context_instance(CONTEXT_SYSTEM);
+
+        // Make the category
+        $cat = new stdClass();
+        $cat->name         = 'testcategory';
+        $cat->parent       = 0;
+        $cat->depth        = 1;
+        $cat->sortorder    = 100;
+        $cat->timemodified = time();
+        $catid = $this->testdb->insert_record('course_categories', $cat);
+        $this->testdb->set_field('course_categories', 'path', '/' . $catid, array('id' => $catid));
+        $this->catcontext = context_coursecat::instance($catid);
 
         // Make the course
         $course = (object)array(
             'shortname' => 'TEST101');
-        $course->id = $this->testdb->insert_record('course', $course);
-
-        // Set up category and course contexts
-        $this->catcontext = (object)array(
-            'contextlevel' => CONTEXT_COURSECAT,
-            'instanceid' => 1,
-            'depth' => 2,
-            'path' => '/1/2');
-        $this->catcontext->id = $this->testdb->insert_record('context', $this->catcontext);
-        $this->coursecontext = (object)array(
-            'contextlevel' => CONTEXT_COURSE,
-            'instanceid' => $course->id,
-            'depth' => 3,
-            'path' => '/1/2/3');
-        $this->coursecontext->id = $this->testdb->insert_record('context', $this->coursecontext);
+        $courseid = $this->testdb->insert_record('course', $course);
+        $this->coursecontext = context_course::instance($courseid);
 
         // Set up section
         $section = (object)array(
-            'course' => $course->id);
+            'course' => $courseid);
         $section->id = $this->testdb->insert_record('course_sections', $section);
 
         // Make course-modules
@@ -598,45 +605,32 @@ class filter_preload_activities_test extends UnitTestCaseUsingDatabase {
             'visible' => 1);
         $mod->id = $this->testdb->insert_record('modules', $mod);
         $label1 = (object)array(
-            'course' => $course->id,
+            'course' => $courseid,
             'intro' => 'Intro 1',
             'name' => 'Label 1');
         $label1->id = $this->testdb->insert_record('label', $label1);
         $cm1 = (object)array(
-            'course' => $course->id,
+            'course' => $courseid,
             'section' => $section->id,
             'module' => $mod->id,
             'instance' => $label1->id);
         $cm1->id = $this->testdb->insert_record('course_modules', $cm1);
+        $this->activity1context = context_module::instance($cm1->id);
+
         $label2 = (object)array(
-            'course' => $course->id,
+            'course' => $courseid,
             'intro' => 'Intro 2',
             'name' => 'Label 2');
         $label2->id = $this->testdb->insert_record('label', $label2);
         $cm2 = (object)array(
-            'course' => $course->id,
+            'course' => $courseid,
             'section' => $section->id,
             'module' => $mod->id,
             'instance' => $label2->id);
         $cm2->id = $this->testdb->insert_record('course_modules', $cm2);
         $this->testdb->set_field('course_sections', 'sequence',
             "$cm1->id,$cm2->id", array('id' => $section->id));
-
-        // Set up activity contexts
-        $this->activity1context = (object)array(
-            'contextlevel' => CONTEXT_MODULE,
-            'instanceid' => $cm1->id,
-            'depth' => 4,
-            'path' => '/1/2/3/4');
-        $this->activity1context->id =
-                $this->testdb->insert_record('context', $this->activity1context);
-        $this->activity2context = (object)array(
-                'contextlevel' => CONTEXT_MODULE,
-                'instanceid' => $cm2->id,
-                'depth' => 4,
-                'path' => '/1/2/3/5');
-        $this->activity2context->id =
-                $this->testdb->insert_record('context', $this->activity2context);
+        $this->activity2context = context_module::instance($cm2->id);
     }
 
     private function assert_matches($modinfo) {
index d903496..88cfc63 100644 (file)
@@ -49,16 +49,19 @@ class rating_db_test extends UnitTestCaseUsingDatabase {
         global $CFG;
         parent::setUp();
 
-        // 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
 
+        // Nasty hack, recreate the system context record (the accesslib API does not allow to create it easily)
+        $this->create_system_context_record();
+        // Reset all caches
+        accesslib_clear_all_caches_for_unit_testing();
+
+        $this->syscontext = get_context_instance(CONTEXT_SYSTEM);
+
         $this->fill_records();
 
         // Ignore any frontpageroleid, that would require to crete more contexts
@@ -76,9 +79,6 @@ class rating_db_test extends UnitTestCaseUsingDatabase {
     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,