From: Tim Otten Date: Wed, 10 Feb 2016 03:20:31 +0000 (-0800) Subject: CRM-17860 - CiviEnvBuilder - Allow variations on install() and uninstall() X-Git-Url: https://vcs.fsf.org/?a=commitdiff_plain;h=ca7abfcccd77892fdb70fee02f23f178976663fe;p=civicrm-core.git CRM-17860 - CiviEnvBuilder - Allow variations on install() and uninstall() --- diff --git a/Civi/Test/CiviEnvBuilder.php b/Civi/Test/CiviEnvBuilder.php index ce2d496288..fe5d0e6e0c 100644 --- a/Civi/Test/CiviEnvBuilder.php +++ b/Civi/Test/CiviEnvBuilder.php @@ -2,7 +2,7 @@ namespace Civi\Test; use Civi\Test\CiviEnvBuilder\CallbackStep; -use Civi\Test\CiviEnvBuilder\ExtensionStep; +use Civi\Test\CiviEnvBuilder\ExtensionsStep; use Civi\Test\CiviEnvBuilder\SqlFileStep; use Civi\Test\CiviEnvBuilder\SqlStep; use Civi\Test\CiviEnvBuilder\StepInterface; @@ -50,31 +50,51 @@ class CiviEnvBuilder { } /** - * Require an extension (based on its name). + * Require that an extension be installed. * - * @param string $name - * @return \CiviEnvBuilder + * @param string|array $names + * One or more extension names. You may use a wildcard '*'. + * @return $this */ - public function ext($name) { - return $this->addStep(new ExtensionStep($name)); + public function install($names) { + return $this->addStep(new ExtensionsStep('install', $names)); } /** - * Require an extension (based on its directory). + * Require an extension be installed (identified by its directory). * - * @param $dir - * @return \CiviEnvBuilder + * @param string $dir + * The current test directory. We'll search for info.xml to + * see what this extension is. + * @return $this * @throws \CRM_Extension_Exception_ParseException */ - public function extDir($dir) { - while ($dir && dirname($dir) !== $dir && !file_exists("$dir/info.xml")) { - $dir = dirname($dir); - } - if (file_exists("$dir/info.xml")) { - $info = \CRM_Extension_Info::loadFromFile("$dir/info.xml"); - $name = $info->key; - } - return $this->addStep(new ExtensionStep($name)); + public function installMe($dir) { + return $this->addStep(new ExtensionsStep('install', $this->whoAmI($dir))); + } + + /** + * Require an extension be uninstalled. + * + * @param string|array $names + * One or more extension names. You may use a wildcard '*'. + * @return $this + */ + public function uninstall($names) { + return $this->addStep(new ExtensionsStep('uninstall', $names)); + } + + /** + * Require an extension be uninstalled (identified by its directory). + * + * @param string $dir + * The current test directory. We'll search for info.xml to + * see what this extension is. + * @return $this + * @throws \CRM_Extension_Exception_ParseException + */ + public function uninstallMe($dir) { + return $this->addStep(new ExtensionsStep('uninstall', $this->whoAmI($dir))); } protected function assertValid() { @@ -137,9 +157,14 @@ class CiviEnvBuilder { } /** - * Determine if the schema is correct. If necessary, destroy and recreate. + * Determine if there's been a change in the preferred configuration. + * If the preferred-configuration matches the last test, keep it. Otherwise, + * destroy and recreate. * * @param bool $force + * Forcibly execute the build, even if the configuration hasn't changed. + * This will slow-down the tests, but it may be appropriate for some very sloppy + * tests. * @return $this */ public function apply($force = FALSE) { @@ -163,4 +188,21 @@ class CiviEnvBuilder { return $this; } + /** + * @param $dir + * @return null + * @throws \CRM_Extension_Exception_ParseException + */ + protected function whoAmI($dir) { + while ($dir && dirname($dir) !== $dir && !file_exists("$dir/info.xml")) { + $dir = dirname($dir); + } + if (file_exists("$dir/info.xml")) { + $info = \CRM_Extension_Info::loadFromFile("$dir/info.xml"); + $name = $info->key; + return $name; + } + return $name; + } + } diff --git a/Civi/Test/CiviEnvBuilder/ExtensionStep.php b/Civi/Test/CiviEnvBuilder/ExtensionStep.php deleted file mode 100644 index afc4d6c3f5..0000000000 --- a/Civi/Test/CiviEnvBuilder/ExtensionStep.php +++ /dev/null @@ -1,28 +0,0 @@ -name = $name; - } - - public function getSig() { - return 'ext:' . $this->name; - } - - public function isValid() { - return is_string($this->name); - } - - public function run($ctx) { - \CRM_Extension_System::singleton()->getManager()->install(array( - $this->name, - )); - } - -} diff --git a/Civi/Test/CiviEnvBuilder/ExtensionsStep.php b/Civi/Test/CiviEnvBuilder/ExtensionsStep.php new file mode 100644 index 0000000000..9b9440a235 --- /dev/null +++ b/Civi/Test/CiviEnvBuilder/ExtensionsStep.php @@ -0,0 +1,51 @@ +action = $action; + $this->names = (array) $names; + } + + public function getSig() { + return 'ext:' . implode(',', $this->names); + } + + public function isValid() { + if (!in_array($this->action, array('install', 'uninstall'))) { + return FALSE; + } + foreach ($this->names as $name) { + if (!is_string($name)) { + return FALSE; + } + } + return TRUE; + } + + public function run($ctx) { + $allKeys = \CRM_Extension_System::singleton()->getFullContainer()->getKeys(); + $names = \CRM_Utils_String::filterByWildcards($this->names, $allKeys, TRUE); + + $manager = \CRM_Extension_System::singleton()->getManager(); + switch ($this->action) { + case 'install': + $manager->install($names); + break; + + case 'uninstall': + $manager->disable($names); + $manager->uninstall($names); + break; + } + } + +}