Top Banner
WELCOME!
51

Testing in Craft CMS

Mar 13, 2023

Download

Documents

Khang Minh
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Testing in Craft CMS

WELCOME!

Page 2: Testing in Craft CMS

Testing in Craft CMSCharlotte Craft CMS Meetup

Nov 13, 2019 Justin Holt - Presenter

Page 3: Testing in Craft CMS

Thank You

Providing Meeting Hosting

Sponsoring Food &

Beverages

Page 4: Testing in Craft CMS

Testing in Craft CMS

Page 5: Testing in Craft CMS

Agenda1. How can tests help?

Are they worth the effort? 2. Types of tests. 3. Craft and Codeception 4. How to run Codeception tests in

Craft. 5. Tips for successful testing. 6. Testing modules and plugins. 7. Mock data with Fixtures. 8. Offloading tests to Queue. 9. Other kinds of tests. 10. Wrap up.

Page 6: Testing in Craft CMS

How can tests help?

Edge Cases New Developers

You* (one year later on same project)

Page 7: Testing in Craft CMS

Are they worth the effort?

Scale your level of effort - One and done? (prob not)

- Maintenance & kicking can down the road? (might be worth it)

- Crucial workflow, several developers? (most def)

- Bleeding edge? (Yes but only stable)

Page 8: Testing in Craft CMS

Types of Tests

Manual tests (you already run these) Refresh page, submit form, click stuff.

Log what happens.

Somewhat measurable.

Somewhat repeatable.

Page 9: Testing in Craft CMS

Types of Tests

Unit tests Test specific results from a single “action”.

Test results of a function, possibly classes.

Measurable, repeatable.

In Craft, unit tests will primarily cover Services. Test high in the call trace.

Save Controller method testing for functional.

Page 10: Testing in Craft CMS

Types of Tests

Functional / Acceptance tests

Test complete package from USER perspective.

Primarily will test your Craft Controller methods.

Typical Controller functional flow: 1) Process a request.

2) Invoke Craft services (Unit test) 3) Return a response

Page 11: Testing in Craft CMS

Testing is all about strategy and approaches. Manual testing and automated testing work best together. You can use your judgement to detect/prevent issues

that computers cannot see whilst computers can execute many tests in short time.

Tip:

Pixel & Tonic

Page 12: Testing in Craft CMS

Craft & Codeception

+

Page 13: Testing in Craft CMS

Codeception in Craft

Fully featured PHP testing suite.

Requires Craft v3.2

Possible prior, requires bootstrapping Yii Codeception module yourself.

Page 14: Testing in Craft CMS

How to run Codeception tests in Craft

1. Installing Codeception. 2. Setting up required directories. 3. Setting up required files. 4. Setting up required config. 5. Codeception requirements. 6. Adding a test. 7. Running a test.

Page 15: Testing in Craft CMS

Installing CodeceptionInstall via composer in project root:

Page 16: Testing in Craft CMS

Installing CodeceptionTest that Codeception installed correctly.

Page 17: Testing in Craft CMS

Setting up required directories.

In your /tests dir create /_craft In your /_craft create:

/storage /templates /migrations /config

/translations

Page 18: Testing in Craft CMS

You can also copy/paste from your existing dirs Into /tests/_craft for testing sites.

Command line:

Setting up required directories.

Page 19: Testing in Craft CMS

Bootstrap file structure

Setting up required directories.

Page 20: Testing in Craft CMS

Setting up required files.

Open /codeception.yaml Replace with:

Page 21: Testing in Craft CMS

Setting up required files.

Create /tests/.env Populate variables:

Page 22: Testing in Craft CMS

Setting up required files.

Create /tests/_bootstrap.php Add:

Page 23: Testing in Craft CMS

Setting up required files.

Create /tests/_craft/config/test.php Add:

Page 24: Testing in Craft CMS

Setting up required files.

Check /tests/acceptance.suite.yml /tests/functional.suite.yml

/tests/unit.suite.yml Ensure Codeception module is added:

Page 25: Testing in Craft CMS

Setting up required config.

Add /tests/_craft/configdb.php

Page 26: Testing in Craft CMS

Codeception Requirements.

Add /tests/unit/_bootstrap.php /tests/functional/_bootstrap.php /tests/acceptance/_bootstrap.php

Codeception will only run these when told. You can run test-type specific business logic.

Page 27: Testing in Craft CMS

Adding a Test.Add /tests/unit/ExampleTest.php

Codeception will use UnitTester and Craft classes to check the Craft Edition for “Pro” using $this->assertSame(…).

Page 28: Testing in Craft CMS

Running a Test.

Page 29: Testing in Craft CMS

Running a Test.

Page 30: Testing in Craft CMS

Running a Real World Test.

Page 31: Testing in Craft CMS

Add /tests/acceptance/SeasonActiveHomeCest.php

Codeception will use AcceptanceTester and Craft classes to check the presence of a schedule carousel based on element/classname.

Running a Real World Test.

Page 32: Testing in Craft CMS

Running a Real World Test.

Page 33: Testing in Craft CMS

Tips for Successful Testing

Maintain your Database using cleanup and transaction.

Cleanup ensures removal of any Fixture data in your DB during your test.

Transaction ensures any DB changes are rolled back using a Yii2 transaction.

Page 34: Testing in Craft CMS

Tips for Successful Testing

Use .gitignore for /storage And also new /tests/_output/.

Stick to the namespaces conventions unit, functional, acceptance.

Page 35: Testing in Craft CMS

Testing Modules & Plugins

Modules: once registered as normal the module will be loaded and available

via MyModule::getInstance() to your test suites.

Plugins: Register in codeception.yml then the plugin will be loaded and available

via MyPlugin::getInstance() to your test suites.

Page 36: Testing in Craft CMS

Testing Modules & Plugins

Plugins: Register in codeception.yml

Page 37: Testing in Craft CMS

Mock Data with FixturesFixtures: used to setup data in a test suite

That is predictable and the same for each test run.

Craft gives us extras for Fixtures of Craft data: Element, Asset, Category, Entry, Global set, Tag,

User and Element fixtures.

Extend craft\test\fixtures\elements\…Fixture

Page 38: Testing in Craft CMS

Offloading Tests to Queue1. Ensure your test class has a $tester property. 2. Call the following method:

First argument is the class of your job. Second argument is any arguments

to pass to your job.

Page 39: Testing in Craft CMS

Other Kinds of Tests-Testing emails with Mailtrap -Visual Regression testing tools and services -Form Testing -Testing at Twig level -Performance, A11y tests (in browser)

Page 40: Testing in Craft CMS

Testing Emails with Mailtrap

Signup for account on website.

Copy credentials from SMTP settings. Add to .env:

Page 41: Testing in Craft CMS

Testing Emails with Mailtrap

Edit /config/app.php:

Page 42: Testing in Craft CMS

Testing Emails with Mailtrap

Now all emails going out from Craft including System messages and any

custom methods, will land at Mailtrap in your account for testing and debugging.

*Free account = 500/month limit

Page 43: Testing in Craft CMS

Visual Regression testing tools and services

Page 44: Testing in Craft CMS

Form Testing-Validation. -Honeypots (kind of). -Recaptchas.

-Trap submission notifications with

Mailtrap. We already do most

of this!

Page 45: Testing in Craft CMS

Form Testing

Page 46: Testing in Craft CMS

Testing at Twig Level

Page 47: Testing in Craft CMS

Testing at Twig Level

Page 48: Testing in Craft CMS

Performance/A11y Testing-Lighthouse (in Chrome devtools)

-Google Pagespeed -webpagetest.org -GTMetrix.com

-aXe tool (commandline) -pA11y tool (commandline)

-KoA11y tool (gui) -Google Accessibility Developer Tools

-Tenon.io

Page 49: Testing in Craft CMS

Thank You!

Page 50: Testing in Craft CMS

Thank You

Providing Meeting Hosting

Sponsoring Food &

Beverages

Page 51: Testing in Craft CMS

Thank You!