Browse Source

Merge pull request #14416 from cakephp/fix-unload

Fix unload command removing too much content.
othercorey 6 years ago
parent
commit
9677be6b0b

+ 10 - 1
src/Command/PluginUnloadCommand.php

@@ -70,7 +70,16 @@ class PluginUnloadCommand extends Command
      */
     protected function modifyApplication(string $app, string $plugin): bool
     {
-        $finder = "@\\\$this\-\>addPlugin\(\s*'$plugin'(.|.\n|)+\);+@";
+        $plugin = preg_quote($plugin, '/');
+        $finder = "/
+            # whitespace and addPlugin call
+            \s*\\\$this\-\>addPlugin\(
+            # plugin name in quotes of any kind
+            \s*['\"]{$plugin}['\"]
+            # method arguments assuming a literal array with multiline args
+            (\s*,[\s\\n]*\[(\\n.*|.*){0,5}\][\\n\s]*)?
+            # closing paren of method
+            \);/mx";
 
         $content = file_get_contents($app);
         $newContent = preg_replace($finder, '', $content);

+ 21 - 5
tests/TestCase/Command/PluginUnloadCommandTest.php

@@ -76,10 +76,6 @@ class PluginUnloadCommandTest extends TestCase
         $plugin2 = "\$this->addPlugin('TestPluginTwo', ['bootstrap' => false, 'routes' => false]);";
         $this->addPluginToApp($plugin1);
         $this->addPluginToApp($plugin2);
-
-        $contents = file_get_contents($this->app);
-        $this->assertStringContainsString($plugin1, $contents);
-
         $this->exec('plugin unload TestPlugin');
 
         $this->assertExitCode(Command::CODE_SUCCESS);
@@ -90,6 +86,26 @@ class PluginUnloadCommandTest extends TestCase
     }
 
     /**
+     * test removing the first plugin leaves the second behind.
+     *
+     * @return void
+     */
+    public function testUnloadFirstPlugin()
+    {
+        $plugin1 = "\$this->addPlugin('TestPlugin');";
+        $plugin2 = "\$this->addPlugin('Vendor/TestPluginTwo');";
+        $this->addPluginToApp($plugin1);
+        $this->addPluginToApp($plugin2);
+        $this->exec('plugin unload Vendor/TestPluginTwo');
+
+        $this->assertExitCode(Command::CODE_SUCCESS);
+        $contents = file_get_contents($this->app);
+
+        $this->assertStringNotContainsString($plugin2, $contents);
+        $this->assertStringContainsString($plugin1, $contents);
+    }
+
+    /**
      * Data provider for various forms.
      *
      * @return array
@@ -176,7 +192,7 @@ class PluginUnloadCommandTest extends TestCase
     protected function addPluginToApp($insert)
     {
         $contents = file_get_contents($this->app);
-        $contents = preg_replace('/(function bootstrap\(\)(?:\s*)\:(?:\s*)void(?:\s+)\{)/m', '$1' . $insert, $contents);
+        $contents = preg_replace('/(function bootstrap\(\)(?:\s*)\:(?:\s*)void(?:\s+)\{)/m', "\$1\n        " . $insert, $contents);
         file_put_contents($this->app, $contents);
     }
 }