The Toolkit for WordPress BDD


To do Behavior Driven Development [BDD] in WordPress, you need to get the tools! Here is the stack you should use:

Operating System

Ubuntu 16 LTS. I personally use an Ubuntu 17 development server on my laptop. You can run Ubuntu on most machines with a USB memory stick, and it’s free. I use Ubuntu on the Amzaon AWS remote cloud servers and production servers. Ubuntu is a brand, but it’s just a Debian based Linux distro, so it’s future proof. If you stopped liking Ubuntu, you could switch to another distro without hassel.

Get the stack

Once you have a machine with Ubuntu running, you can load the entire development stack by running a single command:
source <(curl -s https://raw.githubusercontent.com/johndeebdd/Remote-BDD-Setup/master/installScripts/wordpress.sh)
Directions

Test Framework

Codeception with WordPress modules - PHPunit is the standard and default library for doing unit testing in PHP. CodeCeption INCLUDES PHPunit, as well as a whole series of libraries for doing BDD.

Acceptance testing

You should use the WPWebDriver and WPDb modules. You can use Selenium to drive a full browser if you want, but generally you should use PhantomJS, because it is much faster. You can write your test in the Gherkin format, or in procedural PHP. Generally, I consider Gherkin cumbersome and not worth the candle.
Sample acceptance.yml

WP-Unit testing

You should use the WPloader and WPQueries modules.
Sample unit.yml

Where does code live?

Use a Git repo. Use Github for code that has a public face. Use Bitbucket for private repos [or pay Github for a private repo, either way].

How do you control outsourcer Git commit access?

First setup a few emails: freelancer1@yourdomain.com, freelancer2@yourdomain.com etc. These accounts are granted direct commit access to the repo. Give the outsourcer access to the email account. The repo itself can set a webhook to pull to the production server upon commit. When you want to freeze the outsourcer from the project, you just change the password on Github and email accounts. The project manager retains access to pull to production. The outsourcer can make a commit and it appears directly on the development server. When the commit is approved by the project manager, he does a manual pull to the production machine.

How does the outsourcer work on the code?

Some programmers will have their own IDE setups that they like and know how to use. Eclipse is the best free IDE, while PHPStorm is probably the best commercial IDE. If they know how to pull down from Github and run the code on a local host, great. If they don't know how to do that, setup a development server in the cloud for them. Our stack include the Codiad IDE, which is an open source IDE similar to PHPStorm. TO access it, just go to /codiad/ on your development server, it's all setup already.

How do you run the test suite?

cd /var/www/html/wp-content/plugins/{project dir}
bin/codecept run -vvv --html

How do I see the test results?

They appear in the terminal, or at http://yourdomain.com/wp-content/plugins/{project dir}/tests/_output/
Usually in the terminal you can use the up arrow shortcut.

How do I commit to the repo?

git add --all
git commit -m "some kind of message!"
git push origin master

How do I log in to the WordPress site?

You can log in by using the FastRegister plugin. Just enter an email in the sidebar form and viola! You're logged in as an admin.

How do I log in to a terminal?

The stack will preconfigured a user called "freelancer" with a password "password". Just SSH into the system without a pem file.

Running Codeception tests on multiple WordPress environments

Using the –env flag in Codeception. Running tests against multiple versions of WordPress.

Suppose you want to run your test suite against multiple environments? Perhaps with different browsers or with different versions of WordPress or PHP? Codeception makes this easy with the –env flag.

This is a plugin I made that rolls back a new feature added to WordPress 4.9. I want to run an acceptance test against the latest version of WordPress, and also against a legacy version of WordPress.

Setup multiple environments

I use an Ubuntu laptop with apache2. Your mileage will vary. When I develop a website, say site.com, I setup my laptop to redirect site.dev to a local directory on my laptop. There I keep a copy of the production site to work on.

Directions to setup multiple hosts from Rackspace.com

On my machine, I setup two distinct WordPress installs, one at http://localhost and the other at http://wp.dev. One running version 4.9 and the other running version 3.9.

WordPress v 3.9.23
WordPress v 3.9.23

WordPress v 4.9
WordPress v 4.9

In this example, I’ll be running an acceptance test against both versions of WordPress. I’ll need to setup the YML file like this:

I use this command:
bin/codecept run acceptance -vvv --html --env remotehost --env localhost

bin/codecept is the executable [might be wpcept or codecept]
acceptance is the suite
remotehost and localhost are the envs
-vvv very very verbose [one dash]
–html report to html [two dashes]

Codeception runs:

Passing Tests!
Passing Tests!

WordPress Plugin: Change Admin Email

WordPress Plugin: Change Admin Email Setting Without Outbound Email



As of WordPress 4.9, the administrator cannot change the site admin email without outgoing email setup on the server, and recipient email credentials. This plugin restores the admin’s ability to change this setting without sending a confirmation email.

Plugin in the .org repo
Plugin on Github


Bonus BDD!
As a bonus, for developers I’m distributing some BDD Codeception tests with this plugin. The tests are designed to run via the WordPress module from Codeception. To run them, you’ll need composer, selenium, and chrome. To pull the framework, use the instructions here.

I use this as an example of running tests in multiple environments.

When building this, I used TDD / BDD by creating an acceptance test that worked on v3.9 so that I could run the same test against v4.9. This is overkill, but I wanted to show how to run tests against different environments.

Tutorial: Setup cloud based Dev server for PHP / WordPress using Codeception

Part of a BDD WordPress article.

These directions will setup a a Behavior Driven Development (BDD) environment, for WordPress in the cloud using Codeception. It will setup a WordPress install in the cloud, pull down a plugin from Github, and setup an IDE as well as a test suite. You can use this dev cloud environment to engage outsourcers or remote teams.

  1. Setup an Ubuntu instance, i.e. on AWS. You should be able to do this on any cloud based Ubuntu machine or laptop.
  2. Create a local key pair on your machine and connect to your instance. This is the Amazon security authentication system. The goal is a clean Ubuntu machine. You could use another service [like Google or Cloud9] or even a localhost. Open ports 4444 and 80.
  3. Install and update software.

The IDE is at:

  • http://your-domain.com/codiad/