Permalink
Browse files

FIX: remove_extension should work on parameterized extensions

  • Loading branch information...
1 parent b25eda8 commit 1427a0637b56d763a17b2d402e2b8e04c39e22e4 @wilr wilr committed Apr 6, 2013
Showing with 38 additions and 0 deletions.
  1. +15 −0 core/Object.php
  2. +23 −0 tests/core/ObjectTest.php
View
@@ -492,6 +492,7 @@ public static function add_extension($extension) {
/**
* Remove an extension from a class.
+ *
* Keep in mind that this won't revert any datamodel additions
* of the extension at runtime, unless its used before the
* schema building kicks in (in your _config.php).
@@ -509,6 +510,20 @@ public static function remove_extension($extension) {
$class = get_called_class();
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);
// unset singletons to avoid side-effects
View
@@ -265,17 +265,40 @@ public function testRemoveExtension() {
// ObjectTest_ExtendTest1 is already present in $extensions
ObjectTest_ExtensionRemoveTest::remove_extension('ObjectTest_ExtendTest1');
+
$this->assertFalse(
ObjectTest_ExtensionRemoveTest::has_extension('ObjectTest_ExtendTest1'),
"Extension added through \$extensions are detected as removed in has_extension()"
);
+
$objectTest_ExtensionRemoveTest = new ObjectTest_ExtensionRemoveTest();
$this->assertFalse(
$objectTest_ExtensionRemoveTest->hasExtension('ObjectTest_ExtendTest1'),
"Extensions added through \$extensions are detected as removed in instances through hasExtension()"
);
}
+ 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() {
$this->assertEquals(ObjectTest_MyObject::create()->parentClass(), 'Object');
}

0 comments on commit 1427a06

Please sign in to comment.