From 8d85e8d69feb887ef46e0ecb751a0ad4fbd4947c Mon Sep 17 00:00:00 2001 From: Andrew Taylor Date: Thu, 25 Oct 2018 09:31:53 -0700 Subject: [PATCH] Add a context for editing the latest post This context allows for easy verification of new content by editing the latest post of a specific type and status. I use it to check that new posts exist after specific previous steps. For example, checking that a new order has the correct information after an e-commerce purchase or checking that a donation exists after a donation form is submitted on the front-end. --- src/Context/EditPostContext.php | 54 +++++++++++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/Context/EditPostContext.php b/src/Context/EditPostContext.php index 5b6c9921..a3b630f8 100644 --- a/src/Context/EditPostContext.php +++ b/src/Context/EditPostContext.php @@ -225,4 +225,58 @@ public function iShouldNotSeeTheMetabox(string $title) $this->getSession()->getDriver() ); } + + /** + * Go to the edit post admin page for the latest post of a specific post status and post type. + * + * This step allows you to specify the post type to consider. + * This step allows you to specify the post status to consider. + * + * Example: Given I am editing the latest published post + * Example: Given I am editing the latest draft event + * Example: Given I am editing the latest scheduled post + * + * @Given /^I am editing the latest ([a-z_-]+) ([a-z_-]+)$/ + * + * @param string $post_status The post status of the referenced 'post' being edited. + * @param string $post_type The post type of the referenced 'post' being edited. + */ + public function iEditTheLatestPostOfPostTypeAndStatus(string $post_status='publish', string $post_type) + { + + /** + * Removed "ed" from the end of $post_status if it is present. + * This allows words like "published" to be translated into the proper post status. + */ + $post_status_ending = substr($post_status, -2); + if ( 'ed' == $post_status_ending ) { + $post_status = substr($post_status, 0, -2); + } + + $args = array ( + "--post_type=$post_type", + '--field=ID', + '--orderby=date', + '--order=DESC', + "--post_status=$post_status", + '--posts_per_page=1', + '--ignore_sticky_posts=1', + ); + + $wpcli_output = $this->getDriver()->wpcli('post', 'list', $args); + + // Throw an exception if the result from wp-cli is empty or is not number. + if( + empty( $wpcli_output['stdout'] ) || + 1 !== preg_match( '/^[0-9]+$/', $wpcli_output['stdout'] ) + ){ + throw new \Exception("No posts with the post type '$post_type' and post status '$post_status' found"); + } + + $post_id = (int) $wpcli_output['stdout']; + + $this->edit_post_page->open(array( + 'id' => $post_id, + )); + } }