• Skip to primary navigation
  • Skip to main content
Sal Ferrarello
  • About Sal Ferrarello
  • Speaking
  • Connect
    Mastodon GitHub Twitter (inactive)
You are here: Home / Programming / WordPress Core Unit Tests

WordPress Core Unit Tests

Last updated on April 29, 2015 by Sal Ferrarello

I’ve finally started to delve into WordPress core unit tests and I wanted to document the experience. In this article I create a unit-test for ticket #28435 and submit to the WordPress project at https://core.trac.wordpress.org/ticket/28435

Environment

For the purposes of this article, I’m using the combination of Vagrant, VVV, and VirtualBox.

If you need help getting setup, I recommend starting with The First Vagrant Up

Running Tests

From the command line.
– Navigate to your vvv directory cd ~/vagrant-local/
– Start your Vagrant virtual machine vagrant up
– SSH into your Vagrant virtual machine vagrant ssh
– change to the directory of WordPress development files cd /vagrant/www/wordpress-develop/
– run all tests phpunit

Run a Single Test File

In my case, I’m writing a test for Ticket #28435 so I’m only interested in the tests in the user.php file. Run just these tests with:
phpunit tests/phpunit/tests/user.php

Writing the Test

Ticket #28435 was a great ticket to write a unit test for because the ticket description includes almost all the code to create the test (thanks Ryan McCue).

My Unit Test

/**
* @ticket 28435
*/
function test_wp_update_user_no_change_pwd() {
$testuserid = 1;
$user = get_userdata( $testuserid );
$pwd_before = $user->user_pass;
wp_update_user( $user );
// Reload the data
$user = get_userdata( $testuserid );
$pwd_after = $user->user_pass;
$this->assertEquals( $pwd_before, $pwd_after );
}
view raw user-test-wp-update-user-no-change-pwd.php hosted with ❤ by GitHub

Running Your New Test

By default, WordPress core tests will NOT run any tests associated with a currently open ticket. To run tests associated with an open ticket you need to use the group parameter.

To run all tests marked ticket #28435
phpunit tests/phpunit/tests/user.php --group 28435

At this point, my test fails.

Apply the Patch and Retest

  • Download a copy of the patch to your local directory ~/vagrant-local/www/wordpress-develop/
  • Apply the patch patch -p0 < 28435.diff
    src/wp-includes/user.php
  • Rerun the test phpunit tests/phpunit/tests/user.php --group 28435 and confirm it works

Create the Patch for Your Test

You should not have changed directories through any of this, which is good as it is important your diff file be generated from the root directory /vagrant/www/wordpress-develop/

To create the patch file run
svn diff > 28435-unit-test.diff
or if you’re using git
git diff master... --no-prefix > 28435-unit-test.dif

Add to WordPress Trac

Go to the ticket on WordPress Trac, and upload the unit test diff file.

Sal Ferrarello
Sal Ferrarello (@salcode)
Sal is a PHP developer with a focus on the WordPress platform. He is a conference speaker with a background including Piano Player, Radio DJ, Magician/Juggler, Beach Photographer, and High School Math Teacher. Sal can be found professionally at WebDevStudios, where he works as a senior backend engineer.

Share this post:

Share on TwitterShare on FacebookShare on LinkedInShare on EmailShare on Reddit

Filed Under: Programming Tagged With: unit-test, WordPress

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2023 · Bootstrap4 Genesis on Genesis Framework · WordPress · Log in