SlideShare a Scribd company logo
Automatic testing
and quality assurance
for WordPress plugins
and themes
WP Helsinki Meetup 7.11.2018
Otto Kekäläinen
@ottokekalainen
WP-palvelu.fi / Seravo.com
● WP-palvelu.fi – WordPress hosting
and upkeep
● CEO, sysadmin and developer
● Linux and open source advocate
● Contributed to WordPress Core, fi
and sv translations, Linux, Docker,
Nginx, Redis, MariaDB…
● Twitter:@ottokekalainen
Otto Kekäläinen
Enterprise grade
hosting and upkeep
for WordPress
FIRST THINGS FIRST
● Before you write any WordPress theme
or plugin code, please read up on the
basics at:
○ developer.wordpress.org/themes
○ developer.wordpress.org/plugins
CHALLENGE:
EVERYBODY WANTS QUALITY
Developers just don’t have enough
time or customers budget
Solution:
Automatic ~ zero cost
DO EVERYTHING THAT CAN BE AUTOMATED
● scan code to find errors
○ static analysis
● run code to find errors
○ unit and integration tests
TOOLS
1. What can test PHP code
2. What can automate tests
WHAT TO DO ABOUT PHP CODE
● PHP Code Sniffer
● PHP unit tests
● PhantomJS Headless Chrome
integration tests
● performance
○ execution time
○ memory usage
PHP CODE SNIFFER
PHPCS
PHPCFB AND GIT CITOOL
phpcs.xml
<?xml version="1.0"?>
<ruleset name="Seravo">
<!-- show progress -->
<arg value="p"/>
<!-- check current and all subfolders if no file parameter given -->
<file>.</file>
<rule ref="Squiz.PHP.CommentedOutCode"/>
<rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/>
<rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/>
<rule ref="Generic.Commenting.Todo"/>
<rule ref="Generic.ControlStructures.InlineControlStructure"/>
<rule ref="WordPress-Extra">
<exclude name="Generic.WhiteSpace.DisallowSpaceIndent"/>
<exclude name="Generic.WhiteSpace.ScopeIndent"/>
<exclude name="WordPress.WhiteSpace.PrecisionAlignment.Found" />
<exclude name="WordPress.PHP.YodaConditions" />
</rule>
</ruleset>
phpcs --standard=Security
FILE: tools/gapi.php
---------------------------------------------------------------------
FOUND 1 ERROR AND 8 WARNINGS AFFECTING 8 LINES
---------------------------------------------------------------------
35 | WARNING | Possible RFI detected with GADWP_DIR on include_once
51 | WARNING | Function array_map() that supports callback detected
148 | WARNING | Possible XSS detected with esc_url on echo
152 | WARNING | Possible XSS detected with __ on echo
156 | WARNING | Possible XSS detected with _e on echo
307 | WARNING | Crypto function crc32 used.
767 | WARNING | Function array_map() that supports callback
---------------------------------------------------------------------
More tips at seravo.com/coding-wordpress-in-style-with-phpcs
PHP UNIT
TESTS
assertEquals(a, b)
Lots of PHP Unit test
examples in WordPress
Core source
ACCEPTANCE
TESTS
Codeception PHP
framework
+
Headless Chromium
Example test code
<?php
class ExampleCest {
/**
* Open front page (/)
**/
public function openFrontPage(AcceptanceTester $I) {
$I->amOnPage('/');
$I->checkBrowserConsole();
$I->see('WordPress');
}
}
Read more at seravo.com/docs/tests/ng-integration-tests
VISUAL REGRESSION TESTS
$ gm compare -highlight-style assign
-highlight-color purple -file diff.png *.png
VISUAL REGRESSION TESTS
$ gm compare -verbose -metric mse *.png
Image Difference (MeanSquaredError):
Normalized Absolute
============ ==========
Red: 0.0319159868 8.1
Green: 0.0251841368 6.4
Blue: 0.0278537225 7.1
Opacity: 0.0000000000 0.0
Total: 0.0212384615 5.4
Where do you draw the line
between acceptable changes
and failures/regressions?
HOW TO AUTOMATE
● git pre-commit hook
○ local tests
● git receive hook on a remote server
○ Github + Travis-CI
○ Gitlab + Gitlab-CI
○ Bitbucket
○ etc..
GIT PRE-COMMIT HOOK IN ACTION
Example .git/hooks/pre-commit
# Loop all files that are about to be committed (diff of git head and staged)
echo "==> Checking syntax errors..."
for FILE in $(git diff --cached --name-only); do
resource="$REPO_DIR/$FILE"
##
# Test PHP syntax for all changed *.php and *.module files
##
if [[ "$FILE" =~ ^.+(php|module)$ ]]; then
if [[ -f $resource ]]; then
phpcs "$resource" 1> /dev/null
if [ $? -ne 0 ]; then
errors+=("PHP syntax Error: $FILE")
fi
fi
fi
done
See code at seravo.com/coding-wordpress-in-style-with-phpcs
TRAVIS-CI IN ACTION
TRAVIS-CI IN ACTION
TRAVIS-CI CHECKING EVERY COMMIT
..AND PULL REQUESTS!
NOTIFICATION EMAILS THAT
CAN’T GO UNNOTICED
Example .travis.yml
before_install:
- if [[ "$SNIFF" == "1" ]]; then git clone …
script:
# Syntax check all php files and fail for any error text in STDERR
- '! find . -type f -name "*.php" -exec 
php -d error_reporting=32767 -l {} ; 2>&1 >&- | grep "^"'
# More extensive PHP Style Check
- if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/bin/phpcs -i;
$PHPCS_DIR/bin/phpcs --standard=phpcs.xml; fi
- phpunit
Live examples
github.com/Seravo/seravo-plugin
github.com/Seravo/linux-tuki.fi
travis-ci.org/Seravo
FREE FOR OPEN SOURCE CODE SERVICES
● circleci.com
● cocodacy.com
● codeclimate.com
● codeship.com
● coveralls.io
● coverity.com
● sourceclear.com
● travis-ci.org (.com for private repos)
Listed in alphabetic order, no preference.
Measure execution time and memory
echo "<!-- Measurements: ";
echo memory_get_usage();
echo " - ";
echo (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]);
echo " -->";
$ for x in {1..20}
do curl -s http://localhost | grep "Measurements"
done
<!-- Measurements: 5761720 - 0.2341411113739
<!-- Measurements: 5761720 - 0.24964690208435
<!-- Measurements: 5761704 - 0.25908708572388
<!-- Measurements: 5761720 - 0.23540115356445
...
Test with dummy data
● While developing a site, load lots of dummy data into it so
you can test how your site looks and performs with 100, 1000
or 100 000 posts.
● Basic: Import themeunittestdata.wordpress.xml
○ codex.wordpress.org/Theme_Unit_Test
● More data: wp post generate
○ curl http://loripsum.net/api/5 |
wp post generate --post_content --count=10
● More realism: wp-cli-fixtures
○ github.com/nlemoine/wp-cli-fixtures
WordPress plugins have
a reputation of low
quality. Help us prove
them wrong. Start using
automatic quality testing!
Hopefully automatic
quality testing will
be integrated into the
WordPress.org plugin
directory in the future.
See make.wordpress.org/tide
Extra tip:
use the WP plugin
boiler plate to start
with: wppb.io
WP Theme starter example:
github.com/aucor/aucor-starter
This presentation was about plugin and theme
development. How about testing a real
WordPress site to ensure updates don’t break it?
Let Seravo handle updates of production sites
for you. See our hosting and upkeep service at
Seravo.com
THANK YOU!
KIITOS!
@Seravo
@SeravoFi
@ottokekalainen

More Related Content

PDF
10 things every developer should know about their database to run word press ...
PDF
WordCamp Finland 2015 - WordPress Security
PPTX
Anthony Somerset - Site Speed = Success!
PDF
Profiling PHP with Xdebug / Webgrind
PPTX
Xdebug, KCacheGrind and Webgrind with WampServer
PPTX
A crash course in scaling wordpress
PDF
PHP SA 2014 - Releasing Your Open Source Project
PDF
The 5 most common reasons for a slow WordPress site and how to fix them – ext...
10 things every developer should know about their database to run word press ...
WordCamp Finland 2015 - WordPress Security
Anthony Somerset - Site Speed = Success!
Profiling PHP with Xdebug / Webgrind
Xdebug, KCacheGrind and Webgrind with WampServer
A crash course in scaling wordpress
PHP SA 2014 - Releasing Your Open Source Project
The 5 most common reasons for a slow WordPress site and how to fix them – ext...

What's hot (20)

KEY
Php Power Tools
PDF
Why it's dangerous to turn off automatic updates and here's how to do it
PDF
Improving WordPress performance (xdebug and profiling)
PPTX
Drupal Development Tips
PPT
Roy foubister (hosting high traffic sites on a tight budget)
PDF
Developers, Be a Bada$$ with WP-CLI
PDF
Quick flask an intro to flask
PPT
Build your own PHP extension
PDF
The wp config.php
PDF
Debugging PHP With Xdebug
PDF
Php through the eyes of a hoster phpbnl11
PPTX
PHP and FastCGI Performance Optimizations
PDF
Build a typo3 website in an hour
PPTX
WordPress security for everyone
ODP
PHP: The Beginning and the Zend
PPTX
WordPress.org & Optimizing Security for your WordPress sites
ODP
Website releases made easy with the PEAR installer, OSCON 2009
PDF
LCA2014 - Introduction to Go
PDF
Create dynamic sites with PHP & MySQL
PDF
WordPress Performance optimization
Php Power Tools
Why it's dangerous to turn off automatic updates and here's how to do it
Improving WordPress performance (xdebug and profiling)
Drupal Development Tips
Roy foubister (hosting high traffic sites on a tight budget)
Developers, Be a Bada$$ with WP-CLI
Quick flask an intro to flask
Build your own PHP extension
The wp config.php
Debugging PHP With Xdebug
Php through the eyes of a hoster phpbnl11
PHP and FastCGI Performance Optimizations
Build a typo3 website in an hour
WordPress security for everyone
PHP: The Beginning and the Zend
WordPress.org & Optimizing Security for your WordPress sites
Website releases made easy with the PEAR installer, OSCON 2009
LCA2014 - Introduction to Go
Create dynamic sites with PHP & MySQL
WordPress Performance optimization
Ad

Similar to Automatic testing and quality assurance for WordPress plugins and themes (20)

PDF
Automatic testing and quality assurance for WordPress plugins
PDF
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
PPTX
Getting started with PHPUnit
PPTX
Introduction to Continous Integration with WordPress
PDF
PhpUnit Best Practices
KEY
Prepare for PHP Test Fest 2009
PPTX
Quality code in wordpress
PDF
Unit testing for WordPress
PDF
Fighting Fear-Driven-Development With PHPUnit
ODP
Is your code ready for PHP 7 ?
PDF
PHP QA Tools
PDF
Joomla Code Quality Control and Automation Testing
PDF
Your code are my tests
PDF
Intro to PHP Testing
PDF
Continuous Integration In Php
PDF
Create, test, secure, repeat
PPTX
Continuous feature-development
PDF
Release with confidence
PDF
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
ODP
PHP Quality Assurance Workshop PHPBenelux
Automatic testing and quality assurance for WordPress plugins
Effizientere WordPress-Plugin-Entwicklung mit Softwaretests
Getting started with PHPUnit
Introduction to Continous Integration with WordPress
PhpUnit Best Practices
Prepare for PHP Test Fest 2009
Quality code in wordpress
Unit testing for WordPress
Fighting Fear-Driven-Development With PHPUnit
Is your code ready for PHP 7 ?
PHP QA Tools
Joomla Code Quality Control and Automation Testing
Your code are my tests
Intro to PHP Testing
Continuous Integration In Php
Create, test, secure, repeat
Continuous feature-development
Release with confidence
Automatic PHP 7 Compatibility Checking Using php7cc (and PHPCompatibility)
PHP Quality Assurance Workshop PHPBenelux
Ad

More from Otto Kekäläinen (20)

PDF
FOSDEM2021: MariaDB post-release quality assurance in Debian and Ubuntu
PDF
Search in WordPress - how it works and howto customize it
PDF
MariaDB quality assurance in Debian and Ubuntu
PDF
DebConf 2020: What’s New in MariaDB Server 10.5 and Galera 4?
PDF
Technical SEO for WordPress - 2019 edition
PDF
How MariaDB packaging uses Salsa-CI to ensure smooth upgrades and avoid regre...
PDF
DebConf 2019 MariaDB packaging in Debian BoF
PDF
The 5 most common reasons for a slow WordPress site and how to fix them
PDF
How to investigate and recover from a security breach in WordPress
PDF
Technical SEO for WordPress
PDF
WordPress-tietoturvan perusteet
PDF
Technical SEO for WordPress - 2017 edition
PDF
Improving WordPress Performance with Xdebug and PHP Profiling
PDF
MariaDB adoption in Linux distributions and development environments
PDF
WordPress security 101 - WP Jyväskylä Meetup 21.3.2017
PDF
WordPress security 101 - WP Turku Meetup 2.2.2017
PDF
Find WordPress performance bottlenecks with XDebug PHP profiling
PDF
Testing and updating WordPress - Advanced techniques for avoiding regressions
PDF
Git best practices 2016
PDF
MariaDB Developers Meetup 2016 welcome words
FOSDEM2021: MariaDB post-release quality assurance in Debian and Ubuntu
Search in WordPress - how it works and howto customize it
MariaDB quality assurance in Debian and Ubuntu
DebConf 2020: What’s New in MariaDB Server 10.5 and Galera 4?
Technical SEO for WordPress - 2019 edition
How MariaDB packaging uses Salsa-CI to ensure smooth upgrades and avoid regre...
DebConf 2019 MariaDB packaging in Debian BoF
The 5 most common reasons for a slow WordPress site and how to fix them
How to investigate and recover from a security breach in WordPress
Technical SEO for WordPress
WordPress-tietoturvan perusteet
Technical SEO for WordPress - 2017 edition
Improving WordPress Performance with Xdebug and PHP Profiling
MariaDB adoption in Linux distributions and development environments
WordPress security 101 - WP Jyväskylä Meetup 21.3.2017
WordPress security 101 - WP Turku Meetup 2.2.2017
Find WordPress performance bottlenecks with XDebug PHP profiling
Testing and updating WordPress - Advanced techniques for avoiding regressions
Git best practices 2016
MariaDB Developers Meetup 2016 welcome words

Recently uploaded (20)

PDF
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
PPTX
CHAPTER 2 - PM Management and IT Context
PDF
AI in Product Development-omnex systems
PDF
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
PPTX
Transform Your Business with a Software ERP System
PDF
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
PDF
2025 Textile ERP Trends: SAP, Odoo & Oracle
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Design an Analysis of Algorithms I-SECS-1021-03
PDF
Nekopoi APK 2025 free lastest update
PDF
Understanding Forklifts - TECH EHS Solution
PDF
Design an Analysis of Algorithms II-SECS-1021-03
PDF
PTS Company Brochure 2025 (1).pdf.......
PDF
top salesforce developer skills in 2025.pdf
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PDF
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
PPTX
Essential Infomation Tech presentation.pptx
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Flood Susceptibility Mapping Using Image-Based 2D-CNN Deep Learnin. Overview ...
CHAPTER 2 - PM Management and IT Context
AI in Product Development-omnex systems
Claude Code: Everyone is a 10x Developer - A Comprehensive AI-Powered CLI Tool
Transform Your Business with a Software ERP System
Audit Checklist Design Aligning with ISO, IATF, and Industry Standards — Omne...
2025 Textile ERP Trends: SAP, Odoo & Oracle
How to Migrate SBCGlobal Email to Yahoo Easily
How Creative Agencies Leverage Project Management Software.pdf
Which alternative to Crystal Reports is best for small or large businesses.pdf
Design an Analysis of Algorithms I-SECS-1021-03
Nekopoi APK 2025 free lastest update
Understanding Forklifts - TECH EHS Solution
Design an Analysis of Algorithms II-SECS-1021-03
PTS Company Brochure 2025 (1).pdf.......
top salesforce developer skills in 2025.pdf
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Addressing The Cult of Project Management Tools-Why Disconnected Work is Hold...
Essential Infomation Tech presentation.pptx
Adobe Illustrator 28.6 Crack My Vision of Vector Design

Automatic testing and quality assurance for WordPress plugins and themes

  • 1. Automatic testing and quality assurance for WordPress plugins and themes WP Helsinki Meetup 7.11.2018 Otto Kekäläinen @ottokekalainen WP-palvelu.fi / Seravo.com
  • 2. ● WP-palvelu.fi – WordPress hosting and upkeep ● CEO, sysadmin and developer ● Linux and open source advocate ● Contributed to WordPress Core, fi and sv translations, Linux, Docker, Nginx, Redis, MariaDB… ● Twitter:@ottokekalainen Otto Kekäläinen
  • 3. Enterprise grade hosting and upkeep for WordPress
  • 4. FIRST THINGS FIRST ● Before you write any WordPress theme or plugin code, please read up on the basics at: ○ developer.wordpress.org/themes ○ developer.wordpress.org/plugins
  • 5. CHALLENGE: EVERYBODY WANTS QUALITY Developers just don’t have enough time or customers budget
  • 7. DO EVERYTHING THAT CAN BE AUTOMATED ● scan code to find errors ○ static analysis ● run code to find errors ○ unit and integration tests
  • 8. TOOLS 1. What can test PHP code 2. What can automate tests
  • 9. WHAT TO DO ABOUT PHP CODE ● PHP Code Sniffer ● PHP unit tests ● PhantomJS Headless Chrome integration tests ● performance ○ execution time ○ memory usage
  • 11. PHPCS
  • 12. PHPCFB AND GIT CITOOL
  • 13. phpcs.xml <?xml version="1.0"?> <ruleset name="Seravo"> <!-- show progress --> <arg value="p"/> <!-- check current and all subfolders if no file parameter given --> <file>.</file> <rule ref="Squiz.PHP.CommentedOutCode"/> <rule ref="Squiz.WhiteSpace.SuperfluousWhitespace"/> <rule ref="Generic.CodeAnalysis.UnusedFunctionParameter"/> <rule ref="Generic.Commenting.Todo"/> <rule ref="Generic.ControlStructures.InlineControlStructure"/> <rule ref="WordPress-Extra"> <exclude name="Generic.WhiteSpace.DisallowSpaceIndent"/> <exclude name="Generic.WhiteSpace.ScopeIndent"/> <exclude name="WordPress.WhiteSpace.PrecisionAlignment.Found" /> <exclude name="WordPress.PHP.YodaConditions" /> </rule> </ruleset>
  • 14. phpcs --standard=Security FILE: tools/gapi.php --------------------------------------------------------------------- FOUND 1 ERROR AND 8 WARNINGS AFFECTING 8 LINES --------------------------------------------------------------------- 35 | WARNING | Possible RFI detected with GADWP_DIR on include_once 51 | WARNING | Function array_map() that supports callback detected 148 | WARNING | Possible XSS detected with esc_url on echo 152 | WARNING | Possible XSS detected with __ on echo 156 | WARNING | Possible XSS detected with _e on echo 307 | WARNING | Crypto function crc32 used. 767 | WARNING | Function array_map() that supports callback --------------------------------------------------------------------- More tips at seravo.com/coding-wordpress-in-style-with-phpcs
  • 15. PHP UNIT TESTS assertEquals(a, b) Lots of PHP Unit test examples in WordPress Core source
  • 17. Example test code <?php class ExampleCest { /** * Open front page (/) **/ public function openFrontPage(AcceptanceTester $I) { $I->amOnPage('/'); $I->checkBrowserConsole(); $I->see('WordPress'); } } Read more at seravo.com/docs/tests/ng-integration-tests
  • 18. VISUAL REGRESSION TESTS $ gm compare -highlight-style assign -highlight-color purple -file diff.png *.png
  • 19. VISUAL REGRESSION TESTS $ gm compare -verbose -metric mse *.png Image Difference (MeanSquaredError): Normalized Absolute ============ ========== Red: 0.0319159868 8.1 Green: 0.0251841368 6.4 Blue: 0.0278537225 7.1 Opacity: 0.0000000000 0.0 Total: 0.0212384615 5.4
  • 20. Where do you draw the line between acceptable changes and failures/regressions?
  • 21. HOW TO AUTOMATE ● git pre-commit hook ○ local tests ● git receive hook on a remote server ○ Github + Travis-CI ○ Gitlab + Gitlab-CI ○ Bitbucket ○ etc..
  • 22. GIT PRE-COMMIT HOOK IN ACTION
  • 23. Example .git/hooks/pre-commit # Loop all files that are about to be committed (diff of git head and staged) echo "==> Checking syntax errors..." for FILE in $(git diff --cached --name-only); do resource="$REPO_DIR/$FILE" ## # Test PHP syntax for all changed *.php and *.module files ## if [[ "$FILE" =~ ^.+(php|module)$ ]]; then if [[ -f $resource ]]; then phpcs "$resource" 1> /dev/null if [ $? -ne 0 ]; then errors+=("PHP syntax Error: $FILE") fi fi fi done See code at seravo.com/coding-wordpress-in-style-with-phpcs
  • 29. Example .travis.yml before_install: - if [[ "$SNIFF" == "1" ]]; then git clone … script: # Syntax check all php files and fail for any error text in STDERR - '! find . -type f -name "*.php" -exec php -d error_reporting=32767 -l {} ; 2>&1 >&- | grep "^"' # More extensive PHP Style Check - if [[ "$SNIFF" == "1" ]]; then $PHPCS_DIR/bin/phpcs -i; $PHPCS_DIR/bin/phpcs --standard=phpcs.xml; fi - phpunit
  • 31. FREE FOR OPEN SOURCE CODE SERVICES ● circleci.com ● cocodacy.com ● codeclimate.com ● codeship.com ● coveralls.io ● coverity.com ● sourceclear.com ● travis-ci.org (.com for private repos) Listed in alphabetic order, no preference.
  • 32. Measure execution time and memory echo "<!-- Measurements: "; echo memory_get_usage(); echo " - "; echo (microtime(true) - $_SERVER["REQUEST_TIME_FLOAT"]); echo " -->"; $ for x in {1..20} do curl -s http://localhost | grep "Measurements" done <!-- Measurements: 5761720 - 0.2341411113739 <!-- Measurements: 5761720 - 0.24964690208435 <!-- Measurements: 5761704 - 0.25908708572388 <!-- Measurements: 5761720 - 0.23540115356445 ...
  • 33. Test with dummy data ● While developing a site, load lots of dummy data into it so you can test how your site looks and performs with 100, 1000 or 100 000 posts. ● Basic: Import themeunittestdata.wordpress.xml ○ codex.wordpress.org/Theme_Unit_Test ● More data: wp post generate ○ curl http://loripsum.net/api/5 | wp post generate --post_content --count=10 ● More realism: wp-cli-fixtures ○ github.com/nlemoine/wp-cli-fixtures
  • 34. WordPress plugins have a reputation of low quality. Help us prove them wrong. Start using automatic quality testing!
  • 35. Hopefully automatic quality testing will be integrated into the WordPress.org plugin directory in the future. See make.wordpress.org/tide
  • 36. Extra tip: use the WP plugin boiler plate to start with: wppb.io WP Theme starter example: github.com/aucor/aucor-starter
  • 37. This presentation was about plugin and theme development. How about testing a real WordPress site to ensure updates don’t break it? Let Seravo handle updates of production sites for you. See our hosting and upkeep service at Seravo.com