}
}
+ /**
+ * 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.
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() {
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);
$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');
$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');
}
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();
}
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();
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();
$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');
$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');
$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');
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) {
// 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
'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) {
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
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,