FIX: remove_extension should work on parameterized extensions

This commit is contained in:
Will Rossiter 2013-04-06 19:01:18 +13:00
parent b25eda83c4
commit 1427a0637b
2 changed files with 38 additions and 0 deletions

View File

@ -492,6 +492,7 @@ abstract class Object {
/** /**
* Remove an extension from a class. * Remove an extension from a class.
*
* Keep in mind that this won't revert any datamodel additions * Keep in mind that this won't revert any datamodel additions
* of the extension at runtime, unless its used before the * of the extension at runtime, unless its used before the
* schema building kicks in (in your _config.php). * schema building kicks in (in your _config.php).
@ -509,6 +510,20 @@ abstract class Object {
$class = get_called_class(); $class = get_called_class();
Config::inst()->remove($class, 'extensions', Config::anything(), $extension); Config::inst()->remove($class, 'extensions', Config::anything(), $extension);
// remove any instances of the extension with parameters
$config = Config::inst()->get($class, 'extensions');
if($config) {
foreach($config as $k => $v) {
// extensions with parameters will be stored in config as
// ExtensionName("Param").
if(preg_match(sprintf("/^(%s)\(/", preg_quote($extension, '/')), $v)) {
Config::inst()->remove($class, 'extensions', Config::anything(), $v);
}
}
}
Config::inst()->extraConfigSourcesChanged($class); Config::inst()->extraConfigSourcesChanged($class);
// unset singletons to avoid side-effects // unset singletons to avoid side-effects

View File

@ -265,10 +265,12 @@ class ObjectTest extends SapphireTest {
// ObjectTest_ExtendTest1 is already present in $extensions // ObjectTest_ExtendTest1 is already present in $extensions
ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest1'); ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest1');
$this->assertFalse( $this->assertFalse(
ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest1'), ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest1'),
"Extension added through \$extensions are detected as removed in has_extension()" "Extension added through \$extensions are detected as removed in has_extension()"
); );
$objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest(); $objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest();
$this->assertFalse( $this->assertFalse(
$objectTest_ExtensionRemoveTest->hasExtension('ObjectTest_ExtendTest1'), $objectTest_ExtensionRemoveTest->hasExtension('ObjectTest_ExtendTest1'),
@ -276,6 +278,27 @@ class ObjectTest extends SapphireTest {
); );
} }
public function testRemoveExtensionWithParameters() {
ObjectTest_ExtensionRemoveTest::add_extension('ObjectTest_ExtendTest2("MyParam")');
$this->assertTrue(
ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest2'),
"Extension added through \$add_extension() are added correctly"
);
ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest2');
$this->assertFalse(
Object::has_extension('ObjectTest_ExtensionRemoveTest', 'ObjectTest_ExtendTest2'),
"Extension added through \$add_extension() are detected as removed in has_extension()"
);
$objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest();
$this->assertFalse(
$objectTest_ExtensionRemoveTest->hasExtension('ObjectTest_ExtendTest2'),
"Extensions added through \$extensions are detected as removed in instances through hasExtension()"
);
}
public function testParentClass() { public function testParentClass() {
$this->assertEquals(ObjectTest_MyObject::create()->parentClass(), 'Object'); $this->assertEquals(ObjectTest_MyObject::create()->parentClass(), 'Object');
} }