Successfully reported this slideshow.
Your SlideShare is downloading. ×

Automatic testing and quality assurance for WordPress plugins

Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Ad
Loading in …3
×

Check these out next

1 of 26 Ad
Advertisement

More Related Content

Slideshows for you (20)

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

Advertisement

More from Otto Kekäläinen (20)

Recently uploaded (20)

Advertisement

Automatic testing and quality assurance for WordPress plugins

  1. 1. Automatic testing and quality assurance for WordPress plugins WordCamp Jyväskylä 2018 Otto Kekäläinen @ottokekalainen WP-palvelu.fi
  2. 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. 3. Enterprise grade hosting and upkeep for WordPress
  4. 4. EVERYBODY WANTS QUALITY Developers just don’t have enough time or customers’ budget
  5. 5. Automatic ~ zero cost
  6. 6. DO EVERYTHING THAT CAN BE AUTOMATED ● scan code to find errors ○ static analysis ● run code to find errors ○ unit and integration tests
  7. 7. TOOLS 1. what to do about PHP code 2. how to automate
  8. 8. WHAT TO DO ABOUT PHP CODE ● PHP Code Sniffer ● PHP unit tests ● PhantomJS integration tests ● performance ○ execution time ○ memory usage
  9. 9. PHP CODE SNIFFER
  10. 10. PHPCS
  11. 11. PHPCFB AND GIT CITOOL
  12. 12. 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>
  13. 13. 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
  14. 14. PHP UNIT TESTS assertEquals(a, b)
  15. 15. INTE- GRATION TESTS now with PhantomJS soon with Headless Chromium
  16. 16. Example test code it "There's a login form" do expect(page).to have_id "wp-submit" end # Only try logging in if we could create a test user if WP.user? it "Logged in to WordPress Dashboard" do within("#loginform") do fill_in 'log', :with => WP.user.username fill_in 'pwd', :with => WP.user.password end click_button 'wp-submit' # Should obtain cookies and be able to visit /wp-admin expect(page).to have_id "wpadminbar" Read more at seravo.com/docs/tests/integration-tests
  17. 17. 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 ...
  18. 18. HOW TO AUTOMATE ● git pre-commit hook ● Travis CI
  19. 19. GIT PRE-COMMIT HOOK IN ACTION
  20. 20. 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
  21. 21. TRAVIS CI IN ACTION
  22. 22. 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
  23. 23. Live examples github.com/Seravo/woo-pakettikauppa github.com/Seravo/seravo-plugin github.com/Seravo/linux-tuki.fi github.com/Seravo/wp-https-domain-alias travis-ci.org/Seravo
  24. 24. FREE FOR OPEN SOURCE CODE SERVICES ● 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.
  25. 25. WordPress plugins have a reputation of low quality. Help us prove them wrong. Start using automatic quality testing!
  26. 26. THANK YOU! KIITOS! @SeravoFi @ottokekalainen

×