Automated Testing Tool for WordPress Plugin/Theme using PHPUnit

Creating WordPress plugins could be a challenging task as it needs constant monitoring of the performance in terms of how it functional with the WordPress. A plugin needs proper testing to ensure that it works fine as an individual unit in the WordPress ecosystem. This means that a plugin may perform well on its own, but a small error may backfire its functionality on the website. This small loophole may result in a havoc situation where an incorrect piece of code may run into a snowball effect and bring down the entire website.

There are multiple testing frameworks, but PHPUnit is considered to be the best of the lot and is often touted as the official, defacto standard for WordPress. As WordPress is open source, the developer community is widespread and comes up with new themes and plugins on a daily basis. They may not go through unit testing and this may result in a faulty WordPress plugin. To avoid this, a genuine practice of unit testing through PHPUnit is advisable.

Unit testing is done to check a block of code for an expected functionality. It weeds out errors in the beginning stages to prevent issues cropping up during the production stages. A well-tested plugin will pose fewer errors and would be easy to integrate and even customize. There are plenty of benefits of debugging especially automated testing and unit testing.

Major Benefits of Automated Testing:

1) Spotting bugs at the initial stages

By detecting errors during development stages, it becomes easy to correct them compared to when they are deployed. Also, correcting errors in an entire website with the incorporated plugin, would be tough as developers need to work on the implementation level issues. Therefore, rule them out in budding stages to save time and energy in the future.

2) Optimum level of software development

Automate testing is efficient at correcting the loopholes presented by manual tests. These written scripts can be run to detect any coding errors and weed them out from the extension. It improves accuracy of the entire software and ensures that the plugin is bug free.

3) Logging errors for quick solutions

Every test script prepared by WordPress developers can be easily checked through automated testing. The visual logs can be checked to spot the status and error in the script. Automated testing also hints how to fix a certain error which helps the developers to fix the problem.

When components are fixed in isolation, they need to go through integrated automatic testing to check if they work well in the entire system, which could be a website, CMS, eCommerce site or more.

PHPUnit contains library to test chunks or blocks of code.

To start, WordPress developers require 3 things.

Step 1 – To install PHPUnit you require composer. It can be download on this official site https://getcomposer.org/download/.

1) The following command helps to install PHPUnit.

composer global require phpunit/phpunit:5.*

Please note that we have run this command to install 5.x version as it is recommended by the PHP7.0 and above.

2) Run phpunit –version to verify that it’s been installed.

Step 2 -WP-CLI is required to perform PHPUnit test with the WordPress plugin/theme.

The following are the steps help to install WP-CLI.

1) – Use the below commands to install WP-CLI:

curl -O https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar

chmod +x wp-cli.phar

sudo mv wp-cli.phar /usr/local/bin/wp

2) – Run wp –info to verify to verify that it has been installed.

As both the pre-requisites are acquired, we will start the unit test for the plugin.

Step 3 -The following are the steps help to run or install PHPUnit demo.

1. Change your terminal’s directory to the root of your WordPress installation, and run the command below to generate the plugin test files.

wp scaffold plugin-tests php-unit-demo

Below is what the structure of the plugin will look like after the command above generates the test files.

|-bin/
|—-install-wp-tests.sh
|-tests/
|—-bootstrap.php
|—-bootstrap.php
|—-test-sample.php
|-.travis.yml
|-phpcs.xml.dist
|-phpunit.xml.dist
|-wp-meta-verify.php

Note: By default, the wp scaffold plugin-tests command generates a Travis CI configuration file. You can specify a –ci flag to generate a configuration file for the CI service you use, like so: wp scaffold plugin-tests –c gitlab. As at the time of writing, only Travis CI, CircleCI and GitLab CI are supported.

2. Change your terminal’s directory to your plugin’s directory, and run the installation script:

cd /var/www/html/php-unit-demo
bin/install-wp-tests.sh wordpress_test root “” localhost latest

If you’re like me, then your MySQL username is not root, and the password is not empty. For example, suppose the username is homestead and the password is secret. You would run the installation script like:

bin/install-wp-tests.sh wordpress_test homestead ‘secret’ localhost latest

3. Run the phpunit command to run the default test in tests/test-sample.php


Example.

Create a test-php-unit-demo.php file in the tests folder. It will contain our plugin tests with the following setUp class.

<?php

class WP_Demo_Test extends WP_UnitTestCase {

public function setUp() {

parent::setUp();

$this->class_instance = new WP_Demo_Admin();

}

public function test_email_validation() {}

public function test_email_exists() {}

}

It is worth noting that in order for a method to be considered a unit test, it must be prefixed with test. A best practice is to add a Test suffix to every test class, although it is not required. See WP_Demo_Test .

Confused about what setUp() does? Just know that PHPUnit runs it once before each test method (and on fresh instances) of the test case class. There is also tearDown(), but it is run after each test method. There are also setUpBeforeClass() and tearDownAfterClass(), which run before and after each test case, respectively. A test case is basically a class that contains a number of test methods. See the WordPress Handbook and the PHPUnit documentation for more information.

From the class above, it is pretty obvious we are going to be writing tests for the  test_email_validation and  test_email_exists methods of our plugin class.

public function test_email_validation() {

$is_email_valid = $this→class_instance→is_email_valid( ‘[email protected]’ );

$this→assertTrue( $is_email_valid, ‘Message here…’ );

}

public function test_email_exists() {

$is_email_ exists = $this→class_instance→email_exists( ‘[email protected]’ );

$this→assertFalse( $is_email_exists, ‘Message here…’ );

}

Basically, the tests will ensure that both methods return the correct meta tag when Google and Bing webmaster verification codes are passed to them as arguments.

Run phpunit, and you should see an output in terminal screen.

Admin Tour WordPress Plugin

Admin Tour Plugin has been created for providing an idea to the non-technical admin user. These individuals do not have enough knowledge on operating the admin panel even after being provided with a comprehensive demo by the developer. When the admin next logs in the panel, they often find the panel difficult for operating. With the help of Admin Tour, they can feel comfortable and also navigate easily.

Admin Tour Plugin Features

Admin Tour was created for helping the non-technical admin users. The non-technical admin users do not possess ideas on operating the admin panel. The developer might have provided them with the detailed demo but still, they might not be so comfortable with it. When they log in to the panel, they find it confusing and difficult. With the help of the Admin Tour, things will be easy for them.

This plugin has more advantages for the developers. There are hooks present that can be used for adding steps in the tour as per customization done in the admin panel. The wp-admin bar helps in starting a manual tour. The Dashboard widget consists of the list of tours and one can start any tour by clicking a button. As a default, whenever the admin will log in to this admin panel after a month, the tour will start immediately. One can change this by using a filter.

The features are…

How to Install?

In this section, we will discuss how to install the plugin and make it work.

You can download this Plugin here:- Admin Tour. Also, find here more plugins from KrishaWeb.

Plugin FAQs

  1. Is Admin Tour an open-source software?
    Yes, it is open-source software.
  2. Can a new step be included in the course of the admin tour?
    Yes, a user can add a new step in the admin tour with the help of a filter. One can keep the screen info array used for the dashboard widget. They can keep any text in that place.
  3. Is there a way to change the default login interval?
    A user can make changes to the default login interval. One needs to use a filter for this purpose. You just need to add them to the function file.
  4. Is Admin Tour WordPress Plugin easy to use?
    Yes, it is very easy to use. This has been devised keeping in mind the need of the non-technical users. Hence, you can very well understand that this is a simple plugin meant for all kinds of users.

Conclusion

With the help of the Admin Tour WordPress Plugin, non-technical users can confidently navigate to the admin panel.

 

Introducing Password Reset Log Add-on for MUPR
MASS Users Password Reset is a WordPress plugin that lets website admin reset the multiple user’s passwords in a single click. It can group the users according to their role and resets the password of that group. It supports multiple languages and also compatible with the WooCommerce, BuddyPress, and Dokan – Multivendor Marketplace.

Mass User Password Reset is one of our premium plugins having more than 15000 downloads and 2100+ active installs. It is a plugin that offers you various ways to reset the user’s password. Managing different user’s security and updating the password based on the user role becomes a matter of a few clicks with this Plugin.

We always try and make the plugin better and extend its functionality for users. We have recently introduced an add-on to Mass User Password Reset called ‘Scheduled Password Reset Add-on’. This add-on lets you schedule the password reset process and automate it for you.

We always try and enhance Plugin features. We are glad to announce that we have come up with one more add-on to enhance the capabilities of Mass User Password Reset Plugin.

Password Reset Log Add-on

Password reset log for Mass User Password Reset gives you the complete log of password reset history of each user in your system. You can also check who has reset the password for user. This add-on is compatible for both Mass User Password Reset and Mass user Password Reset Pro. It maintains the most accurate password reset logs for both Mass User Password Reset and Mass User Password Reset Pro.