From 8ea0c513193059145083bcdbf6ebf67f77fc4d3e Mon Sep 17 00:00:00 2001 From: Ben Croker Date: Sun, 18 Feb 2024 19:57:57 -0600 Subject: [PATCH] Replace Codeception tests with Pest tests --- src/templates/_settings.twig | 6 +- tests/.env | 12 ---- tests/README.md | 36 +++++++--- tests/TESTS.md | 34 ++++++++++ tests/_bootstrap.php | 26 ------- tests/_craft/config/db.php | 15 ---- tests/_craft/config/general.php | 17 ----- tests/_craft/config/test.php | 5 -- tests/_craft/storage/.gitignore | 2 - tests/_data/.gitkeep | 0 tests/_output/.gitignore | 2 - tests/_support/AcceptanceTester.php | 26 ------- tests/_support/FunctionalTester.php | 26 ------- tests/_support/Helper/Acceptance.php | 10 --- tests/_support/Helper/Functional.php | 10 --- tests/_support/Helper/Unit.php | 10 --- tests/_support/UnitTester.php | 26 ------- tests/_support/_generated/.gitignore | 2 - tests/acceptance.suite.yml | 13 ---- tests/acceptance/_bootstrap.php | 1 - tests/functional.suite.yml | 13 ---- tests/functional/_bootstrap.php | 1 - tests/pest/.gitignore | 1 + tests/pest/Architecture/ArchitectureTest.php | 9 +++ tests/pest/Feature/AllowedTest.php | 68 +++++++++++++++++++ tests/pest/Feature/SnaptchaVariableTest.php | 15 ++++ tests/pest/Pest.php | 49 +++++++++++++ tests/pest/phpunit.xml | 12 ++++ tests/unit.suite.yml | 11 --- tests/unit/_bootstrap.php | 1 - tests/unit/services/SnaptchaServiceTest.php | 58 ---------------- tests/unit/variables/SnaptchaVariableTest.php | 46 ------------- 32 files changed, 218 insertions(+), 345 deletions(-) delete mode 100644 tests/.env create mode 100644 tests/TESTS.md delete mode 100644 tests/_bootstrap.php delete mode 100644 tests/_craft/config/db.php delete mode 100644 tests/_craft/config/general.php delete mode 100644 tests/_craft/config/test.php delete mode 100644 tests/_craft/storage/.gitignore delete mode 100644 tests/_data/.gitkeep delete mode 100644 tests/_output/.gitignore delete mode 100644 tests/_support/AcceptanceTester.php delete mode 100644 tests/_support/FunctionalTester.php delete mode 100644 tests/_support/Helper/Acceptance.php delete mode 100644 tests/_support/Helper/Functional.php delete mode 100644 tests/_support/Helper/Unit.php delete mode 100644 tests/_support/UnitTester.php delete mode 100644 tests/_support/_generated/.gitignore delete mode 100644 tests/acceptance.suite.yml delete mode 100644 tests/acceptance/_bootstrap.php delete mode 100644 tests/functional.suite.yml delete mode 100644 tests/functional/_bootstrap.php create mode 100644 tests/pest/.gitignore create mode 100644 tests/pest/Architecture/ArchitectureTest.php create mode 100644 tests/pest/Feature/AllowedTest.php create mode 100644 tests/pest/Feature/SnaptchaVariableTest.php create mode 100644 tests/pest/Pest.php create mode 100644 tests/pest/phpunit.xml delete mode 100644 tests/unit.suite.yml delete mode 100644 tests/unit/_bootstrap.php delete mode 100644 tests/unit/services/SnaptchaServiceTest.php delete mode 100644 tests/unit/variables/SnaptchaVariableTest.php diff --git a/src/templates/_settings.twig b/src/templates/_settings.twig index 8de9b0d..2739bd1 100644 --- a/src/templates/_settings.twig +++ b/src/templates/_settings.twig @@ -139,7 +139,7 @@ id: 'excludeControllerActions', cols: [ { - type: 'text', + type: 'singleline', heading: 'Controller Action'|t('snaptcha'), code: true, }, @@ -160,7 +160,7 @@ id: 'allowList', cols: [ { - type: 'text', + type: 'singleline', heading: 'IP Address'|t('snaptcha'), code: true, }, @@ -181,7 +181,7 @@ id: 'denyList', cols: [ { - type: 'text', + type: 'singleline', heading: 'IP Address'|t('snaptcha'), code: true, }, diff --git a/tests/.env b/tests/.env deleted file mode 100644 index 4c2f060..0000000 --- a/tests/.env +++ /dev/null @@ -1,12 +0,0 @@ -APP_ID=CraftCMS -SECURITY_KEY=1234567890 - -DB_DRIVER=mysql -DB_SERVER=mysql -DB_PORT=3306 -DB_DATABASE=test -DB_USER=project -DB_PASSWORD=project - -FROM_EMAIL_NAME="Craft CMS" -FROM_EMAIL_ADDRESS=info@putyourlightson.com diff --git a/tests/README.md b/tests/README.md index 9ea7f6c..a0a6d5f 100644 --- a/tests/README.md +++ b/tests/README.md @@ -1,23 +1,41 @@ -# Static Analysis +# Testing + +## Static Analysis -To run static analysis on the plugin, install PHPStan and run the following command from the root of your project. +To run static analysis on the plugin, +install [PHPStan for Craft CMS](https://github.com/craftcms/phpstan) and run the +following command from the root of your project. ```shell -./vendor/bin/phpstan analyse -c vendor/putyourlightson/craft-snaptcha/phpstan.neon +./vendor/bin/phpstan analyse -c vendor/putyourlightson/craft-snaptcha/phpstan.neon --memory-limit 1G ``` -# Testing +## Easy Coding Standard -To test the plugin, install Codeception, update `.env` and run the following command from the root of your project. +To run the Easy Coding Standard on the plugin, +install [ECS for Craft CMS](https://github.com/craftcms/ecs) and run the +following command from the root of your project. ```shell -./vendor/bin/codecept run -c ./vendor/putyourlightson/craft-snaptcha unit +./vendor/bin/ecs check -c vendor/putyourlightson/craft-snaptcha/ecs.php ``` -Or to run a specific test. +## Pest Tests + +To run Pest tests, first install [Craft Pest](https://craft-pest.com/) core as a dev dependency. ```shell -./vendor/bin/codecept run -c ./vendor/putyourlightson/craft-snaptcha unit variables/SnaptchaVariableTest:getField +composer require markhuot/craft-pest-core:^2.0.0-rc2 --dev ``` -> Ensure that the database you specify in `.env` is not one that actually contains any data as it will be cleared when the tests are run. +Then run the following command from the root of your project. + +```shell +php php vendor/bin/pest --test-directory=vendor/putyourlightson/craft-snaptcha/tests/pest +``` + +Or to run a specific test. + +```shell +php php vendor/bin/pest --test-directory=vendor/putyourlightson/craft-snaptcha/tests/pest --filter=CacheRequestTest +``` diff --git a/tests/TESTS.md b/tests/TESTS.md new file mode 100644 index 0000000..ef9f716 --- /dev/null +++ b/tests/TESTS.md @@ -0,0 +1,34 @@ +# Test Specification + +This document outlines the test specification for the Snaptcha plugin. + +--- + +## Architecture Tests + +### [Architecture](pest/Architecture/ArchitectureTest.php) + +_Tests the architecture of the plugin._ + +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Source code does not contain any “dump or die” statements. + +## Feature Tests + +### [Allowed](pest/Feature/AllowedTest.php) + +_Tests whether performing actions is allowed._ + +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User is not allowed when validation is enabled. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User is allowed when validation is disabled. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User IP is allowed when in the allow list. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User IP is allowed when in a nested array in the allow list. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User IP is not allowed when not in the allow list. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User IP is denied when in the deny list. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User IP is denied when in a nested array in the deny list. +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) User IP is not denied when not in the deny list. + +### [SnaptchaVariable](pest/Feature/SnaptchaVariableTest.php) + +_Tests the markup generated by the Snaptcha variable._ + +![Pass](https://raw.githubusercontent.com/putyourlightson/craft-generate-test-spec/main/icons/pass.svg) Field is named after field name setting. diff --git a/tests/_bootstrap.php b/tests/_bootstrap.php deleted file mode 100644 index c246893..0000000 --- a/tests/_bootstrap.php +++ /dev/null @@ -1,26 +0,0 @@ - App::env('DB_DSN') ?: null, - 'driver' => App::env('DB_DRIVER'), - 'server' => App::env('DB_SERVER'), - 'port' => App::env('DB_PORT'), - 'database' => App::env('DB_DATABASE'), - 'user' => App::env('DB_USER'), - 'password' => App::env('DB_PASSWORD'), - 'schema' => App::env('DB_SCHEMA'), - 'tablePrefix' => App::env('DB_TABLE_PREFIX'), -]; diff --git a/tests/_craft/config/general.php b/tests/_craft/config/general.php deleted file mode 100644 index 9294d3f..0000000 --- a/tests/_craft/config/general.php +++ /dev/null @@ -1,17 +0,0 @@ - [ - // Whether generated URLs should omit "index.php" - 'omitScriptNameInUrls' => true, - ], -]; diff --git a/tests/_craft/config/test.php b/tests/_craft/config/test.php deleted file mode 100644 index a5b52b4..0000000 --- a/tests/_craft/config/test.php +++ /dev/null @@ -1,5 +0,0 @@ -expect(['var_dump', 'die']) + ->not->toBeUsed(); diff --git a/tests/pest/Feature/AllowedTest.php b/tests/pest/Feature/AllowedTest.php new file mode 100644 index 0000000..823fec9 --- /dev/null +++ b/tests/pest/Feature/AllowedTest.php @@ -0,0 +1,68 @@ +getUser()->identity = new User(); +}); + +test('User is not allowed when validation is enabled', function() { + Snaptcha::$plugin->settings->validateUsers = true; + + expect(Snaptcha::$plugin->snaptcha->isUserAllowed()) + ->toBeFalse(); +}); + +test('User is allowed when validation is disabled', function() { + Snaptcha::$plugin->settings->validateUsers = false; + + expect(Snaptcha::$plugin->snaptcha->isUserAllowed()) + ->toBeTrue(); +}); + +test('User IP is allowed when in the allow list', function() { + Snaptcha::$plugin->settings->allowList = ['127.0.0.1']; + + expect(Snaptcha::$plugin->snaptcha->isIpAllowed()) + ->toBeTrue(); +}); + +test('User IP is allowed when in a nested array in the allow list', function() { + Snaptcha::$plugin->settings->allowList = [['127.0.0.1']]; + + expect(Snaptcha::$plugin->snaptcha->isIpAllowed()) + ->toBeTrue(); +}); + +test('User IP is not allowed when not in the allow list', function() { + Snaptcha::$plugin->settings->allowList = []; + + expect(Snaptcha::$plugin->snaptcha->isIpAllowed()) + ->toBeFalse(); +}); + +test('User IP is denied when in the deny list', function() { + Snaptcha::$plugin->settings->denyList = ['127.0.0.1']; + + expect(Snaptcha::$plugin->snaptcha->isIpDenied()) + ->toBeTrue(); +}); + +test('User IP is denied when in a nested array in the deny list', function() { + Snaptcha::$plugin->settings->denyList = [['127.0.0.1']]; + + expect(Snaptcha::$plugin->snaptcha->isIpDenied()) + ->toBeTrue(); +}); + +test('User IP is not denied when not in the deny list', function() { + Snaptcha::$plugin->settings->denyList = []; + + expect(Snaptcha::$plugin->snaptcha->isIpDenied()) + ->toBeFalse(); +}); diff --git a/tests/pest/Feature/SnaptchaVariableTest.php b/tests/pest/Feature/SnaptchaVariableTest.php new file mode 100644 index 0000000..425afb8 --- /dev/null +++ b/tests/pest/Feature/SnaptchaVariableTest.php @@ -0,0 +1,15 @@ +getField(); + + expect((string)$field) + ->toContain('name="' . $variable->getFieldName() . '" value="" data-key='); +}); diff --git a/tests/pest/Pest.php b/tests/pest/Pest.php new file mode 100644 index 0000000..4dffa89 --- /dev/null +++ b/tests/pest/Pest.php @@ -0,0 +1,49 @@ +beforeAll(function() { + /** @see \craft\web\Request::getRemoteIP() */ + $_SERVER['REMOTE_ADDR'] = '127.0.0.1'; + }) + ->in('./'); + +/* +|-------------------------------------------------------------------------- +| Expectations +|-------------------------------------------------------------------------- +| +| When you're writing tests, you often need to check that values meet certain conditions. The +| "expect()" function gives you access to a set of "expectations" methods that you can use +| to assert different things. Of course, you may extend the Expectation API at any time. +| +*/ + +/* +|-------------------------------------------------------------------------- +| Constants +|-------------------------------------------------------------------------- +*/ + +/* +|-------------------------------------------------------------------------- +| Functions +|-------------------------------------------------------------------------- +| +| While Pest is very powerful out-of-the-box, you may have some testing code specific to your +| project that you don't want to repeat in every file. Here you can also expose helpers as +| global functions to help you to reduce the number of lines of code in your test files. +| +*/ diff --git a/tests/pest/phpunit.xml b/tests/pest/phpunit.xml new file mode 100644 index 0000000..b6e27a1 --- /dev/null +++ b/tests/pest/phpunit.xml @@ -0,0 +1,12 @@ + + + + + . + + + diff --git a/tests/unit.suite.yml b/tests/unit.suite.yml deleted file mode 100644 index 600a6cc..0000000 --- a/tests/unit.suite.yml +++ /dev/null @@ -1,11 +0,0 @@ -# Codeception Test Suite Configuration -# -# Suite for unit or integration tests. - -actor: UnitTester -modules: - enabled: - - \craft\test\Craft - - Asserts - - \Helper\Unit - step_decorators: ~ diff --git a/tests/unit/_bootstrap.php b/tests/unit/_bootstrap.php deleted file mode 100644 index b3d9bbc..0000000 --- a/tests/unit/_bootstrap.php +++ /dev/null @@ -1 +0,0 @@ -getUser()->identity = new User(); - - $this->assertFalse(Snaptcha::$plugin->snaptcha->isUserAllowed()); - - Snaptcha::$plugin->settings->validateUsers = false; - $this->assertTrue(Snaptcha::$plugin->snaptcha->isUserAllowed()); - } - - public function testIsIpAllowed() - { - $ipAddress = Craft::$app->getRequest()->getUserIP(); - - Snaptcha::$plugin->settings->allowList = [$ipAddress]; - $this->assertTrue(Snaptcha::$plugin->snaptcha->isIpAllowed()); - - Snaptcha::$plugin->settings->allowList = [[$ipAddress]]; - $this->assertTrue(Snaptcha::$plugin->snaptcha->isIpAllowed()); - } - - public function testIsIpDenied() - { - $ipAddress = Craft::$app->getRequest()->getUserIP(); - - Snaptcha::$plugin->settings->denyList = [$ipAddress]; - $this->assertTrue(Snaptcha::$plugin->snaptcha->isIpDenied()); - - Snaptcha::$plugin->settings->denyList = [[$ipAddress]]; - $this->assertTrue(Snaptcha::$plugin->snaptcha->isIpDenied()); - } -} diff --git a/tests/unit/variables/SnaptchaVariableTest.php b/tests/unit/variables/SnaptchaVariableTest.php deleted file mode 100644 index b70eb4a..0000000 --- a/tests/unit/variables/SnaptchaVariableTest.php +++ /dev/null @@ -1,46 +0,0 @@ -variable = new SnaptchaVariable(); - } - - public function testGetField() - { - $field = $this->variable->getField(); - - $this->assertStringContainsString( - 'name="' . $this->variable->getFieldName() . '" value="" data-key=', - $field - ); - } -}