Skip to content

Commit

Permalink
MDL-29602 unittest - fixing other tests to work with new accesslib
Browse files Browse the repository at this point in the history
  • Loading branch information
stronk7 committed Oct 18, 2011
1 parent c2551ed commit 2c6da73
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 105 deletions.
46 changes: 46 additions & 0 deletions admin/tool/unittest/simpletestlib.php
Original file line number Diff line number Diff line change
Expand Up @@ -745,6 +745,52 @@ protected function revert_global_user_id() {
}
}

/**
* 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.
Expand Down
61 changes: 25 additions & 36 deletions lib/simpletest/testblocklib_blockmanager.php
Original file line number Diff line number Diff line change
Expand Up @@ -170,8 +170,12 @@ public function setUp() {
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() {
Expand All @@ -184,17 +188,6 @@ 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);
Expand Down Expand Up @@ -245,8 +238,6 @@ public function test_adding_and_retrieving_one_block() {
$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');
Expand All @@ -264,8 +255,6 @@ public function test_adding_and_retrieving_two_blocks() {
$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');
Expand All @@ -280,14 +269,18 @@ public function test_adding_and_retrieving_two_blocks() {
}

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();

Expand All @@ -304,14 +297,18 @@ public function test_block_not_included_in_different_context() {
}

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();

Expand All @@ -330,8 +327,6 @@ public function test_block_included_in_sub_context() {
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();

Expand All @@ -352,8 +347,6 @@ public function test_block_not_included_on_different_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');
Expand All @@ -372,8 +365,6 @@ public function test_block_included_with_explicit_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');
Expand All @@ -392,8 +383,6 @@ public function test_block_included_with_page_type_pattern() {
$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');
Expand Down
118 changes: 56 additions & 62 deletions lib/simpletest/testfilterconfig.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -560,36 +566,37 @@ public function setUp() {

// 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
Expand All @@ -598,45 +605,32 @@ public function setUp() {
'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) {
Expand Down
14 changes: 7 additions & 7 deletions rating/simpletest/testrating.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,19 @@ public function setUp() {
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
Expand All @@ -76,9 +79,6 @@ public function 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,
Expand Down

0 comments on commit 2c6da73

Please sign in to comment.