From e044c9a942c413f733f4401de9dc23badd8aa947 Mon Sep 17 00:00:00 2001 From: Cameron Hurd Date: Wed, 10 Feb 2016 10:53:16 -0500 Subject: [PATCH 1/8] define constants that were failing for me --- .../WordPressContextInitializer.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Context/Initializer/WordPressContextInitializer.php b/src/Context/Initializer/WordPressContextInitializer.php index 7f6c6c7..a883314 100644 --- a/src/Context/Initializer/WordPressContextInitializer.php +++ b/src/Context/Initializer/WordPressContextInitializer.php @@ -61,6 +61,25 @@ private function prepareEnvironment() $_SERVER['HTTP_HOST'] = $urlComponents['host'] . (isset($urlComponents['port']) ? ':' . $urlComponents['port'] : ''); $_SERVER['SERVER_PROTOCOL'] = 'HTTP/1.1'; + 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 From 90e7c2cc215a042c0b59f3ff7da99e3c7909274f Mon Sep 17 00:00:00 2001 From: Marino Di Clemente Date: Wed, 17 Feb 2016 23:50:28 +0100 Subject: [PATCH 2/8] minor improvements and new permalinks/logout actions --- .../WordPressContextInitializer.php | 65 +++++++++---------- src/Context/WordPressContext.php | 23 ++++++- src/ServiceContainer/WordPressExtension.php | 3 + 3 files changed, 55 insertions(+), 36 deletions(-) diff --git a/src/Context/Initializer/WordPressContextInitializer.php b/src/Context/Initializer/WordPressContextInitializer.php index a883314..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,42 +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 (!defined('ABSPATH')) { - define('ABSPATH', $this->wordpressParams['path']); - } + 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_HOST')) { + define('DB_HOST', $this->wordpressParams['connection']['dbhost']); + } - if (!defined('DB_NAME')) { - define('DB_NAME', $this->wordpressParams['connection']['db']); - } + 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_USER')) { + define('DB_USER', $this->wordpressParams['connection']['username']); + } - if (!defined('DB_PASSWORD')) { - define('DB_PASSWORD', $this->wordpressParams['connection']['password']); + 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() { @@ -107,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() { @@ -120,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']); @@ -143,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..744a271 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,7 +86,11 @@ 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"]); + $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"]); } @@ -110,5 +114,20 @@ public function login($username, $password) assertTrue($this->getSession()->getPage()->hasContent('Dashboard')); } + /** + * @Given /^I enable permalinks$/ + */ + public function iEnablePermalinks() + { + $this->visit(get_site_url()."/wp-admin/options-permalink.php"); + } + + /** + * @Given /^I am logged out$/ + */ + public function iAmLoggedOut() + { + $this->visit(wp_logout_url()); + } } 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') From 5d1fdb28a75fbbd8757da614d2e3dd74b2d407e2 Mon Sep 17 00:00:00 2001 From: Marino Di Clemente Date: Sat, 20 Feb 2016 00:09:06 +0100 Subject: [PATCH 3/8] minor fix on login for normal users --- src/Context/WordPressContext.php | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index 744a271..20f1c14 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -87,12 +87,13 @@ public function thereArePlugins(TableNode $table) foreach ($table->getHash() as $row) { if ($row["status"] == "enabled") { //$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"]); } } } @@ -110,9 +111,10 @@ public function login($username, $password) $currentPage->fillField('user_login', $username); $currentPage->fillField('user_pass', $password); - $currentPage->findButton('wp-submit')->click(); - - assertTrue($this->getSession()->getPage()->hasContent('Dashboard')); + $currentPage->findButton('wp-submit')->click(); + assertTrue($this->getSession()->getPage()->hasContent($username)); + //normal users haven't a dashboard after log in + //assertTrue($this->getSession()->getPage()->hasContent('Dashboard')); } /** * @Given /^I enable permalinks$/ From ec54ae12a56b2ee72b165b770e95e6e33ae0b409 Mon Sep 17 00:00:00 2001 From: Marino Di Clemente Date: Mon, 22 Feb 2016 17:39:45 +0100 Subject: [PATCH 4/8] fix user login --- src/Context/WordPressContext.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index 20f1c14..4ed1425 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -112,7 +112,6 @@ public function login($username, $password) $currentPage->fillField('user_login', $username); $currentPage->fillField('user_pass', $password); $currentPage->findButton('wp-submit')->click(); - assertTrue($this->getSession()->getPage()->hasContent($username)); //normal users haven't a dashboard after log in //assertTrue($this->getSession()->getPage()->hasContent('Dashboard')); } From add379d57e2fb70af94b97740b2b9d52a028726f Mon Sep 17 00:00:00 2001 From: Marino Di Clemente Date: Mon, 29 Feb 2016 20:29:09 +0100 Subject: [PATCH 5/8] little improvement --- src/Context/WordPressContext.php | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index 4ed1425..f9cd518 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -98,7 +98,6 @@ public function thereArePlugins(TableNode $table) } } - /** * Login into the reserved area of this wordpress * @@ -108,13 +107,22 @@ public function login($username, $password) { $this->visit(get_site_url()."/wp-login.php"); $currentPage = $this->getSession()->getPage(); - - $currentPage->fillField('user_login', $username); - $currentPage->fillField('user_pass', $password); - $currentPage->findButton('wp-submit')->click(); - //normal users haven't a dashboard after log in - //assertTrue($this->getSession()->getPage()->hasContent('Dashboard')); + $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); } + /** * @Given /^I enable permalinks$/ */ From b077b248cfe167e70903c3770a09d513fc06cd89 Mon Sep 17 00:00:00 2001 From: Marino Di Clemente Date: Thu, 17 Mar 2016 20:33:51 +0100 Subject: [PATCH 6/8] minor fix --- src/Context/WordPressContext.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index f9cd518..45dda57 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -98,6 +98,7 @@ public function thereArePlugins(TableNode $table) } } + /** * Login into the reserved area of this wordpress * @@ -122,7 +123,7 @@ public function login($username, $password) } throw new \Exception($err); } - + /** * @Given /^I enable permalinks$/ */ @@ -137,6 +138,7 @@ public function iEnablePermalinks() public function iAmLoggedOut() { $this->visit(wp_logout_url()); + $this->getSession()->getPage()->clickLink('log out'); } } From 19e07d2228c316a459de956060da5df9001717ca Mon Sep 17 00:00:00 2001 From: Marino Di Clemente Date: Wed, 20 Apr 2016 20:25:53 +0200 Subject: [PATCH 7/8] reset the session for when login to be sure --- src/Context/WordPressContext.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index 45dda57..1582498 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -106,6 +106,7 @@ 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; From 59b1ed7f9d568c7ef03797e34f960561c81017e1 Mon Sep 17 00:00:00 2001 From: Cameron Hurd Date: Thu, 21 Apr 2016 11:23:37 -0400 Subject: [PATCH 8/8] explicitly enables /%postname%/ permalinks --- src/Context/WordPressContext.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Context/WordPressContext.php b/src/Context/WordPressContext.php index 1582498..58aa625 100644 --- a/src/Context/WordPressContext.php +++ b/src/Context/WordPressContext.php @@ -131,6 +131,9 @@ public function login($username, $password) 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'); } /**