MDL-28006 moodle1 converter provides new get_stash_or_default() method

This commit is contained in:
David Mudrak
2011-06-29 00:11:05 +02:00
parent 477f1d2b78
commit acc9a7b9e2
2 changed files with 48 additions and 0 deletions
+16
View File
@@ -470,6 +470,22 @@ class moodle1_converter extends base_converter {
}
}
/**
* Restores a given stash or returns the given default if there is no such stash
*
* @param string $stashname name of the stash
* @param int $itemid optional id for multiple infos within the same stashname
* @param mixed $default information to return if the info has not been stashed previously
* @return mixed stashed data or the default value
*/
public function get_stash_or_default($stashname, $itemid = 0, $default = null) {
try {
return $this->get_stash($stashname, $itemid);
} catch (moodle1_convert_empty_storage_exception $e) {
return $default;
}
}
/**
* Returns the list of existing stashes
*
@@ -160,6 +160,38 @@ class moodle1_converter_test extends UnitTestCase {
$converter->drop_stash_storage();
}
public function test_get_stash_or_default() {
$converter = convert_factory::get_converter('moodle1', $this->tempdir);
$converter->create_stash_storage();
$this->assertTrue(is_null($converter->get_stash_or_default('stashname')));
$this->assertTrue(is_null($converter->get_stash_or_default('stashname', 7)));
$this->assertTrue('default' === $converter->get_stash_or_default('stashname', 0, 'default'));
$this->assertTrue(array('foo', 'bar') === $converter->get_stash_or_default('stashname', 42, array('foo', 'bar')));
//$converter->set_stash('stashname', 0);
//$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed
//$converter->set_stash('stashname', '');
//$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed
//$converter->set_stash('stashname', array());
//$this->assertFalse(is_null($converter->get_stash_or_default('stashname'))); // todo returns true now, this needs MDL-27713 to be fixed
$converter->set_stash('stashname', 42);
$this->assertTrue(42 === $converter->get_stash_or_default('stashname'));
$this->assertTrue(is_null($converter->get_stash_or_default('stashname', 1)));
$this->assertTrue(42 === $converter->get_stash_or_default('stashname', 0, 61));
$converter->set_stash('stashname', array(42 => (object)array('id' => 42)), 18);
$stashed = $converter->get_stash_or_default('stashname', 18, 1984);
$this->assertIsA($stashed, 'array');
$this->assertTrue(is_object($stashed[42]));
$this->assertTrue($stashed[42]->id === 42);
$converter->drop_stash_storage();
}
public function test_get_contextid() {
$converter = convert_factory::get_converter('moodle1', $this->tempdir);