diff --git a/src/Context/Initializer/WordPressContextInitializer.php b/src/Context/Initializer/WordPressContextInitializer.php index 7f6c6c7..90cd608 100644 --- a/src/Context/Initializer/WordPressContextInitializer.php +++ b/src/Context/Initializer/WordPressContextInitializer.php @@ -2,18 +2,14 @@ namespace Tmf\WordPressExtension\Context\Initializer; -use Behat\Behat\Context\Context, - Behat\Behat\Context\Initializer\ContextInitializer; - -use Symfony\Component\Finder\Finder, - Symfony\Component\Filesystem\Filesystem; - +use Behat\Behat\Context\Context; +use Behat\Behat\Context\Initializer\ContextInitializer; +use Symfony\Component\Finder\Finder; +use Symfony\Component\Filesystem\Filesystem; use Tmf\WordPressExtension\Context\WordPressContext; /** - * Class FeatureListener - * - * @package Tmf\WordPressExtension\Listener + * Class FeatureListener. */ class WordPressContextInitializer implements ContextInitializer { @@ -22,7 +18,7 @@ class WordPressContextInitializer implements ContextInitializer private $basePath; /** - * inject the wordpress extension parameters and the mink parameters + * inject the wordpress extension parameters and the mink parameters. * * @param array $wordpressParams * @param array $minkParams @@ -36,7 +32,7 @@ public function __construct($wordpressParams, $minkParams, $basePath) } /** - * setup the wordpress environment / stack if the context is a wordpress context + * setup the wordpress environment / stack if the context is a wordpress context. * * @param Context $context */ @@ -52,23 +48,43 @@ public function initializeContext(Context $context) } /** - * prepare environment variables + * prepare environment variables. */ private function prepareEnvironment() { // wordpress uses these superglobal fields everywhere... $urlComponents = parse_url($this->minkParams['base_url']); - $_SERVER['HTTP_HOST'] = $urlComponents['host'] . (isset($urlComponents['port']) ? ':' . $urlComponents['port'] : ''); + $_SERVER['HTTP_HOST'] = $urlComponents['host'].(isset($urlComponents['port']) ? ':'.$urlComponents['port'] : ''); $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + if ($this->wordpressParams['prepare_constants']) { + if (!defined('ABSPATH')) { + define('ABSPATH', $this->wordpressParams['path']); + } + + if (!defined('DB_HOST')) { + define('DB_HOST', $this->wordpressParams['connection']['dbhost']); + } + if (!defined('DB_NAME')) { + define('DB_NAME', $this->wordpressParams['connection']['db']); + } + + if (!defined('DB_USER')) { + define('DB_USER', $this->wordpressParams['connection']['username']); + } + + if (!defined('DB_PASSWORD')) { + define('DB_PASSWORD', $this->wordpressParams['connection']['password']); + } + } // we don't have a request uri in headless scenarios: // wordpress will try to "fix" php_self variable based on the request uri, if not present $PHP_SELF = $GLOBALS['PHP_SELF'] = $_SERVER['PHP_SELF'] = '/index.php'; } /** - * actually load the wordpress stack + * actually load the wordpress stack. */ private function loadStack() { @@ -88,7 +104,7 @@ private function loadStack() } /** - * create a wp-config.php and link plugins / themes + * create a wp-config.php and link plugins / themes. */ public function installFileFixtures() { @@ -101,21 +117,21 @@ public function installFileFixtures() "'DB_NAME', 'database_name_here'", "'DB_USER', 'username_here'", "'DB_PASSWORD', 'password_here'", - "'DB_HOST', 'localhost'" + "'DB_HOST', 'localhost'", ), array( sprintf("'DB_NAME', '%s'", $this->wordpressParams['connection']['db']), sprintf("'DB_USER', '%s'", $this->wordpressParams['connection']['username']), sprintf("'DB_PASSWORD', '%s'", $this->wordpressParams['connection']['password']), sprintf("'DB_HOST', '%s'", $this->wordpressParams['connection']['dbhost']), ), $file->getContents()); - $fs->dumpFile($file->getPath() . '/wp-config.php', $configContent); + $fs->dumpFile($file->getPath().'/wp-config.php', $configContent); } if (isset($this->wordpressParams['symlink']['from']) && isset($this->wordpressParams['symlink']['to'])) { $from = $this->wordpressParams['symlink']['from']; if (substr($from, 0, 1) != '/') { - $from = $this->basePath . DIRECTORY_SEPARATOR . $from; + $from = $this->basePath.DIRECTORY_SEPARATOR.$from; } if ($fs->exists($this->wordpressParams['symlink']['from'])) { $fs->symlink($from, $this->wordpressParams['symlink']['to']); @@ -124,7 +140,7 @@ public function installFileFixtures() } /** - * flush the database if specified by flush_database parameter + * flush the database if specified by flush_database parameter. */ public function flushDatabase() { diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index 0567bd7..58aa625 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -16,7 +16,7 @@ class WordPressContext extends MinkContext /** * Create a new WordPress website from scratch * - * @Given /^\w+ have|has a vanilla wordpress installation$/ + * @Given /^I have a vanilla wordpress installation$/ */ public function installWordPress(TableNode $table = null) { @@ -86,9 +86,14 @@ public function thereArePlugins(TableNode $table) { foreach ($table->getHash() as $row) { if ($row["status"] == "enabled") { - activate_plugin(WP_PLUGIN_DIR . "/" . $row["plugin"]); + //$result = activate_plugin(WP_PLUGIN_DIR . "/" . $row["plugin"]); + deactivate_plugins($row["plugin"]); + $result = activate_plugin($row["plugin"]); + if ( is_wp_error( $result ) ) { + throw new \Exception($row["plugin"] . ': ' . $result->get_error_message()); + } } else { - deactivate_plugins(WP_PLUGIN_DIR . "/" . $row["plugin"]); + deactivate_plugins($row["plugin"]); } } } @@ -101,14 +106,43 @@ public function thereArePlugins(TableNode $table) */ public function login($username, $password) { + $this->getSession()->reset(); $this->visit(get_site_url()."/wp-login.php"); $currentPage = $this->getSession()->getPage(); + $i = 0; + while($i < 3){ + $currentPage->fillField('Username', $username); + $currentPage->fillField('Password', $password); + $currentPage->fillField('user_login', $username); + $currentPage->fillField('user_pass', $password); + $currentPage->findButton('wp-submit')->click(); + $p = $this->getSession()->getPage(); + if(!$p->hasContent('ERROR')) + return; + echo $err."\r\n"; + $i++; + } + throw new \Exception($err); + } - $currentPage->fillField('user_login', $username); - $currentPage->fillField('user_pass', $password); - $currentPage->findButton('wp-submit')->click(); + /** + * @Given /^I enable permalinks$/ + */ + public function iEnablePermalinks() + { + $this->visit(get_site_url()."/wp-admin/options-permalink.php"); + $currentPage = $this->getSession()->getPage(); + $currentPage->fillField('selection', '/%postname%/'); + $currentPage->pressButton('Save Changes'); + } - assertTrue($this->getSession()->getPage()->hasContent('Dashboard')); + /** + * @Given /^I am logged out$/ + */ + public function iAmLoggedOut() + { + $this->visit(wp_logout_url()); + $this->getSession()->getPage()->clickLink('log out'); } } diff --git a/src/ServiceContainer/WordPressExtension.php b/src/ServiceContainer/WordPressExtension.php index 344732e..3a79f3a 100644 --- a/src/ServiceContainer/WordPressExtension.php +++ b/src/ServiceContainer/WordPressExtension.php @@ -55,6 +55,9 @@ public function configure(ArrayNodeDefinition $builder) ->scalarNode('path') ->defaultValue(__DIR__ . 'vendor') ->end() + ->booleanNode('prepare_constants') + ->defaultValue(true) + ->end() ->arrayNode('symlink') ->children() ->scalarNode('from')