Tests with PHPUnit for WordPress

Last update: 26 of April of 2020

Table of contents

One of the forms to collaborate in the code of WordPress is to verify that the PHP code is standardized and works correctly. For it the equipment of development of the nucleus of WordPress has decided to test with PHPUnit.

The examples and code that appears here are based on the installation and configuration of VPS with Subversion. From this configuration we will review the system with PHPUnit.

Installation of PHPUnit

First that we will do it will be to install and to form of global form PHPUnit. For it we only have to follow the following steps.

CD wget - Or phpunit https://phar.phpunit.de/phpunit-7.phar chmod +x phpunit mv phpunit /usr/local/bin/phpunit phpunit --version

This last commando will show the present version to us of software so that we will know that he is working.

Data base of test

These tests eliminate all the content of the data base, reason why, before following, we will create a data base compares in which the tests will be realised. We will use the same model that in the creation of the data base main.

mysql - or root - p

You will need a password for this exclusive data base for our WordPress test. Please, it creates your own password that is something safe.

Wordpresstest CHARACTER CREATES DATABASE to YOU SET = utf8mb4 COLLATE = utf8mb4_bin; GRANT ALL ON wordpresstest.* TO €˜wordpresstest'@'localhost€™ IDENTIFIED BY €˜__PASSWORD€™; GRANT ALL ON wordpresstest.* TO €˜wordpresstest'@'127.0.0.1€™ IDENTIFIED BY €˜__PASSWORD€™; FLUSH PRIVILEGES; quit

Configuration of WordPress for the test

Now that we have installed software and a data base of test, we will create a file of configuration in which to incorporate this information.

CD /webs/wordpress-svn/ vim wp-tests-config.php

The file will incorporate a configuration as this. NOTE: It verifies the data of connection and other elements throughout the file.

<? php/* Path to the WordPress codebase you'd like to test. Add to forward slash in the end. * if (defined (€˜WP_RUN_CORE_TESTS€™) && WP_RUN_CORE_TESTS) \ {defines (€˜ABSPATH€™, dirname (__FILE). €˜/build/€™); } else \ {defines (€˜ABSPATH€™, dirname (__FILE). €˜/src/€™); }/* * Path to the theme to test with. * * The €˜default€™ theme is symlinked from test/phpunit/dates/themedir1/default into * the themes directory of the WordPress installation defined stupefies. * it defines (€˜WP_DEFAULT_THEME€™, €˜default€™); //Test with multisite enabled. //Alternatively, uses the tests/phpunit/multisite.xml configuration cases out. //defines (€˜WP_TESTS_MULTISITE€™, true); //FORCE known bugs to sees run. //Tests with an associated Trac ticket that is still open plows normally skipped. //defines (€˜WP_TESTS_FORCE_KNOWN_BUGS€™, true); //Test with WordPress debug mode (default). it defines (€˜WP_DEBUG€™, true); //** MySQL settings ** ////This configuration cases out will sees used by the Copy of WordPress being tested. //wordpress/wp-config.php will sees ignored. //WARNING WARNING WARNING! //These tests will DROPS ALL TABLES in the database with the prefix named below. //DO NOT uses to production database or one that is shared with something else. it defines (€˜DB_NAME€™, €˜wordpresstest€™); it defines (€˜DB_USER€™, €˜wordpresstest€™); it defines (€˜DB_PASSWORD€™, €˜__PASSWORD€™); it defines (€˜DB_HOST€™, €˜localhost€™); it defines (€˜DB_CHARSET€™, €˜utf8megabyte4€™); it defines (€˜DB_COLLATE€™, €˜utf8megabyte4_bin€™); /** #@+ * Authentication Unique Keys and Salts. * * Change these to different unique phrases! * You dog generates these to you using the \ {@link https://api.wordpress.org/secret-key/1.1/salt/ WordPress.org secret-key service} * defines (€˜AUTH_KEY€™, €˜putt your unique phrase here€™); it defines (€˜SECURE_AUTH_KEY€™, €˜putt your unique phrase here€™); it defines (€˜LOGGED_IN_KEY€™, €˜putt your unique phrase here€™); it defines (€˜NONCE_KEY€™, €˜putt your unique phrase here€™); it defines (€˜AUTH_SALT€™, €˜putt your unique phrase here€™); it defines (€˜SECURE_AUTH_SALT€™, €˜putt your unique phrase here€™); it defines (€˜LOGGED_IN_SALT€™, €˜putt your unique phrase here€™); it defines (€˜NONCE_SALT€™, €˜putt your unique phrase here€™); $table_prefix = €˜wptests_€™; //Only numbers, letters, and underscores please! it defines (€˜WP_TESTS_DOMAIN€™, €˜example.com€™); it defines (€˜WP_TESTS_EMAIL€™, €˜hello@example.com€™); it defines (€˜WP_TESTS_TITLE€™, €˜Test Blog€™); it defines (€˜WP_PHP_BINARY€™, €˜php€™);

Home of the test with PHPUnit

The following thing that we will do is to prepare a ready configuration of WordPress so that tests can be done. For that reason we will send a series of commandos to prepare the system. The last one of the commandos will execute the test of PHPUnit.

CD /webs/wordpress-svn/ npm update npm run grunt build npm run grunt watch phpunit

This would have to give back something like thus:

user@wordpress-svn: /webs/wordpress-svn# phpunit Installing€¦ Running ace single site€¦ To run multisite, uses - c tests/phpunit/multisite.xml Not running ajax tests. To execute these, uses --group ajax. Not running ms-you case out tests. To execute these, uses --group ms-you case out. Not running External-HTTP tests. To execute these, uses --group External-HTTP. PHPUnit 7.5.18 by Sebastian Bergmann and contributors. ........................................................... 59/10082 (0%)€¦ ........................................................... 9086/10082 (90%) ........................................................... 9145/10082 (90%) ........................................................... 9204/10082 (91%) ........................................................... 9263/10082 (91%) ........................................................... 9322/10082 (92%) ........................................................... 9381/10082 (93%) ........................................................... 9440/10082 (93%) ........................................................... 9499/10082 (94%) ........................................................... 9558/10082 (94%) ........................................................... 9617/10082 (95%) ........................................................... 9676/10082 (95%) ........................................................... 9735/10082 (96%) ........................................................... 9794/10082 (97%) ........................................................... 9853/10082 (97%) ........................................................... 9912/10082 (98%) ........................................................... 9971/10082 (98%) ........................................................... 10030/10082 (99%) .................................................... 10082/10082 (100%) You should really fix these slow tests (>150ms)€¦ 1165ms to run Tests_Image_Functions: test_wp_crop_image_url 823ms to run Tests_Media: test_multiline_comment_with_embeds 425ms to run Tests_Media: test_autoembed_no_paragraphs_around_urls 413ms to run Tests_Media: test_oembed_explicit_media_link 389ms to run Tests_Dependencies_Scripts: test_no_source_mapping 377ms to run Tests_Media: test_wp_calculate_image_srcset_no_width 373ms to run Tests_Media: test_wp_calculate_image_srcset_no_date_uploads 263ms to run Tests_Import_Import: test_double_import 227ms to run Tests_Import_Import: test_small_import 214ms to run Test_Nav_Menus: test_wp_get_associated_nav_menu_items€¦ and there plows 7 dwells stupefies your threshold hidden from view Time: 2.07 you make a draft, Memory: 193,08 There Megabyte was 1 error: 1) Test_WP_Customize_Nav_Menus:: test_search_available_items_query There were 13 failures: 1) Tests_Date_Get_Permalink:: test_should_return_correct_date_permalink_with_changed_time_zone 2) Tests_DB:: test_get_col with dates set #2 (€˜SELECT user_login, user_email€¦ _users€™, €˜admin@example.org€™, Array (), 1) 3) Tests_DB:: test_get_col with dates set #3 (€˜SELECT user_login, user_email€¦ _users€™, €˜admin@example.org€™, Array (), €˜1€™) 4) Tests_Dependencies_Scripts:: test_wp_add_inline_script_before_after_concat_with_core_dependency 5) Tests_Meta_Query:: test_meta_type_key_should_be_passed_to_meta_query 6) Tests_Query_MetaQuery:: test_duplicate_posts_when_no_key 7) Tests_Query_MetaQuery:: test_compare_key_regexp_rlike 8) Tests_Query_MetaQuery:: test_compare_key_not_regexp 9) Tests_Query_Search:: test_search_order_title_before_excerpt_and_content 10) WP_Test_REST_Posts_Controller:: test_get_items_with_orderby_relevance_two_terms 11) WP_Test_REST_Users_Controller:: test_get_items_orderby_name 12) WP_Test_REST_Users_Controller:: test_update_item_existing_email_case_not_own 13) Tests_Term_getTerms:: test_get_terms_like ERRORS! Tests: 10082, Assertions: 50670, Errors: 1, Failures: 13, Skipped: 44.

What means the symbols?

  • . Each point means that the test has been passed correctly.
  • S Means that the test has skipped. Usually it is because these tests are only valid in certain configurations.
  • F Means that the test has failed. In the end it appears information of because.
  • And It means that the test has failed due to a PHP error, that can be error, warning or notice.
  • I Significa that the test has been marked as incomplete.