-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from nystudio107/pest
Add Pest tests
- Loading branch information
Showing
7 changed files
with
222 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
# Testing | ||
|
||
## Pest Tests | ||
|
||
```shell | ||
php vendor/bin/pest --configuration=vendor/nystudio107/craft-twig-sandbox/tests/pest/phpunit.xml --test-directory=vendor/nystudio107/craft-twig-sandbox/tests/pest | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
<?php | ||
|
||
/** | ||
* Tests the architecture of the plugin. | ||
*/ | ||
|
||
test('Source code does not contain any “dump or die” statements') | ||
->expect(['var_dump', 'die']) | ||
->not->toBeUsed(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/** | ||
* Tests the Blacklist Security Policy. | ||
*/ | ||
|
||
use nystudio107\crafttwigsandbox\twig\BlacklistSecurityPolicy; | ||
use nystudio107\crafttwigsandbox\web\SandboxView; | ||
use Twig\Sandbox\SecurityNotAllowedFilterError; | ||
use Twig\Sandbox\SecurityNotAllowedFunctionError; | ||
use Twig\Sandbox\SecurityNotAllowedTagError; | ||
|
||
test('Blacklisted tag is not allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new BlacklistSecurityPolicy([ | ||
'twigTags' => ['set'], | ||
]), | ||
]); | ||
$sandboxView->renderString('{% set x = 1 %}'); | ||
})->throws(SecurityNotAllowedTagError::class); | ||
|
||
test('Non blacklisted tag is allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new BlacklistSecurityPolicy([ | ||
'twigTags' => [], | ||
]), | ||
]); | ||
$sandboxView->renderString('{% set x = 1 %}'); | ||
})->throwsNoExceptions(); | ||
|
||
test('Blacklisted filter is not allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new BlacklistSecurityPolicy([ | ||
'twigFilters' => ['abs'], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ 6|abs }}'); | ||
})->throws(SecurityNotAllowedFilterError::class); | ||
|
||
test('Non blacklisted filter is allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new BlacklistSecurityPolicy([ | ||
'twigFilters' => [], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ 6|abs }}'); | ||
})->throwsNoExceptions(); | ||
|
||
test('Blacklisted function is not allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new BlacklistSecurityPolicy([ | ||
'twigFunctions' => ['random'], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ random() }}'); | ||
})->throws(SecurityNotAllowedFunctionError::class); | ||
|
||
test('Non blacklisted function is allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new BlacklistSecurityPolicy([ | ||
'twigFunctions' => [], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ random() }}'); | ||
})->throwsNoExceptions(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
<?php | ||
|
||
/** | ||
* Tests the Whitelist Security Policy. | ||
*/ | ||
|
||
use nystudio107\crafttwigsandbox\twig\WhitelistSecurityPolicy; | ||
use nystudio107\crafttwigsandbox\web\SandboxView; | ||
use Twig\Sandbox\SecurityNotAllowedFilterError; | ||
use Twig\Sandbox\SecurityNotAllowedFunctionError; | ||
use Twig\Sandbox\SecurityNotAllowedTagError; | ||
|
||
test('Whitelisted tag is allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new WhitelistSecurityPolicy([ | ||
'twigTags' => ['set'], | ||
]), | ||
]); | ||
$sandboxView->renderString('{% set x = 1 %}'); | ||
})->throwsNoExceptions(); | ||
|
||
test('Non whitelisted tag is not allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new WhitelistSecurityPolicy([ | ||
'twigTags' => [], | ||
]), | ||
]); | ||
$sandboxView->renderString('{% set x = 1 %}'); | ||
})->throws(SecurityNotAllowedTagError::class); | ||
|
||
test('Whitelisted filter is allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new WhitelistSecurityPolicy([ | ||
'twigFilters' => ['abs'], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ 6|abs }}'); | ||
})->throwsNoExceptions(); | ||
|
||
test('Non whitelisted filter is not allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new WhitelistSecurityPolicy([ | ||
'twigFilters' => [], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ 6|abs }}'); | ||
})->throws(SecurityNotAllowedFilterError::class); | ||
|
||
test('Whitelisted function is allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new WhitelistSecurityPolicy([ | ||
'twigFunctions' => ['random'], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ random() }}'); | ||
})->throwsNoExceptions(); | ||
|
||
test('Non whitelisted function is not allowed', function() { | ||
$sandboxView = new SandboxView([ | ||
'securityPolicy' => new WhitelistSecurityPolicy([ | ||
'twigFunctions' => [], | ||
]), | ||
]); | ||
$sandboxView->renderString('{{ random() }}'); | ||
})->throws(SecurityNotAllowedFunctionError::class); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
use craft\events\ExceptionEvent; | ||
use craft\web\ErrorHandler; | ||
use markhuot\craftpest\test\TestCase; | ||
use yii\base\Event; | ||
|
||
/* | ||
|-------------------------------------------------------------------------- | ||
| Test Case | ||
|-------------------------------------------------------------------------- | ||
| | ||
| The closure you provide to your test functions is always bound to a specific PHPUnit test | ||
| case class. By default, that class is "PHPUnit\Framework\TestCase". Of course, you may | ||
| need to change it using the "uses()" function to bind a different classes or traits. | ||
| | ||
*/ | ||
|
||
uses(TestCase::class) | ||
->beforeEach(function() { | ||
// Ensure exceptions are thrown, so we can catch them in our tests. | ||
Event::on(ErrorHandler::class, ErrorHandler::EVENT_BEFORE_HANDLE_EXCEPTION, | ||
function(ExceptionEvent $event) { | ||
throw $event->exception; | ||
} | ||
); | ||
}) | ||
->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. | ||
| | ||
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/|version|/phpunit.xsd" | ||
bootstrap="/var/www/html/vendor/autoload.php" | ||
colors="true" | ||
> | ||
<testsuites> | ||
<testsuite name="Test Suite"> | ||
<directory suffix="Test.php">.</directory> | ||
</testsuite> | ||
</testsuites> | ||
</phpunit> |