array($users['s1']->id, $users['s2']->id)),
array_map(create_function('$o', 'return $o->id;'),
get_users_by_capability($contexts[3], array('mod/quiz:attempt', 'mod/quiz:reviewmyattempts'), '', '', '', '', '', '', false)));
-
- // Clean up everything we added.
- $this->revert_to_real_db();
- $this->drop_test_tables($tablenames);
- accesslib_clear_all_caches_for_unit_testing();
}
function test_get_switchable_roles() {
accesslib_clear_all_caches_for_unit_testing();
$this->assert(new ArraysHaveSameValuesExpectation(array()), array_keys(get_switchable_roles($syscontext)));
$this->assert(new ArraysHaveSameValuesExpectation(array($r2id)), array_keys(get_switchable_roles($context)));
- $this->revert_global_user_id();
-
- // Clean up everything we added.
- $this->revert_to_real_db();
- $this->drop_test_tables($tablenames);
- accesslib_clear_all_caches_for_unit_testing();
}
function test_get_allowed_switchable_roles() {
$this->switch_to_test_db();
$this->assert(new ArraysHaveSameValuesExpectation(array(2, 5)), array_keys(get_allowed_switchable_roles()));
-
- $this->revert_to_real_db();
- $this->drop_test_table('role_capabilities');
}
}
?>
* This class lets you write unit tests that access a separate set of test
* tables with a different prefix. Only those tables you explicitly ask to
* be created will be.
+ *
+ * This class has failities for flipping $USER->id.
+ *
+ * The tear-down method for this class should automatically revert any changes
+ * you make during test set-up using the metods defined here. That is, it will
+ * drop tables for you automatically and revert to the real $DB and $USER->id.
*/
class UnitTestCaseUsingDatabase extends UnitTestCase {
private $realdb;
/**
* Switch to using the test database for all queries until further notice.
- * You must remember to switch back using revert_to_real_db() before the end of the test.
*/
protected function switch_to_test_db() {
global $DB;
/**
* Switch $USER->id to a test value.
- * You must remember to switch back using revert_global_user_id() before the end of the test.
*
* It might be worth making this method do more robuse $USER switching in future,
* however, this is sufficient for my needs at present.
* 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.
*/
- private function emergency_clean_up() {
+ private function automatic_clean_up() {
global $DB;
$cleanmore = false;
- // Check that they did not forget to drop any test tables.
- if (!empty($this->tables)) {
- debugging('You did not clean up all your test tables in your UnitTestCaseUsingDatabase. Tables remaining: ' .
- implode(', ', array_keys($this->tables)), DEBUG_DEVELOPER);
- }
+ // Drop any test tables that were created.
foreach ($this->tables as $tablename => $notused) {
$this->drop_test_table($tablename);
}
- // Check that they did not forget to switch page to the real DB.
+ // Switch back to the real DB if necessary.
if ($DB !== $this->realdb) {
- debugging('You did not switch back to the real database using revert_to_real_db in your UnitTestCaseUsingDatabase.', DEBUG_DEVELOPER);
$this->revert_to_real_db();
$cleanmore = true;
}
- // Check for forgetting to call revert_global_user_id.
+ // revert_global_user_id if necessary.
if (!is_null($this->realuserid)) {
- debugging('You did not switch back to the real $USER->id using revert_global_user_id in your UnitTestCaseUsingDatabase.', DEBUG_DEVELOPER);
$this->revert_global_user_id();
$cleanmore = true;
}
}
public function tearDown() {
- $this->emergency_clean_up();
+ $this->automatic_clean_up();
parent::tearDown();
}
public function __destruct() {
- $this->emergency_clean_up();
+ // Should not be necessary thanks to tearDown, but no harm in belt and braces.
+ $this->automatic_clean_up();
}
/**