Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Classic escapers as loadable (loaded by default) extension #314

Open
wants to merge 6 commits into
base: v3
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Add Escaper extension test cases
Signed-off-by: pine3ree <[email protected]>
pine3ree committed Jun 13, 2023
commit d79e94fe9e2eb343b725e93c9bbf094e387b0dbb
147 changes: 147 additions & 0 deletions tests/Extension/EscaperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
<?php

declare(strict_types=1);

namespace League\Plates\Tests\Extension;

use League\Plates\Engine;
use League\Plates\Extension\Escaper;
use League\Plates\Template\Template;
use org\bovigo\vfs\vfsStream;
use PHPUnit\Framework\TestCase;

use const ENT_HTML401;
use const ENT_HTML5;
use const ENT_XHTML;
use const ENT_XML1;

class EscaperTest extends TestCase
{
protected function setUp(): void
{
}

public function testCanCreateInstance()
{
$this->assertInstanceOf(Escaper::class, new Escaper());
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_HTML5));
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_XHTML));
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_XML1));
$this->assertInstanceOf(Escaper::class, new Escaper(ENT_HTML401, 'ISO-8859-15'));
}

public function testThatEscaperIsAutoRegisteredByDefault()
{
$engine = new Engine();
$this->assertTrue($engine->doesFunctionExist('escape'));
$this->assertTrue($engine->doesFunctionExist('e'));
}

public function testThatEscaperAutoRegistrationCanBeBypassed()
{
$engine = new Engine(null, 'php', false);
$this->assertFalse($engine->doesFunctionExist('escape'));
$this->assertFalse($engine->doesFunctionExist('e'));
}

public function testDefaultHtml401EscapeFunction()
{
$string = '<&>"\'';
$expected = '&lt;&amp;&gt;&quot;&#039;';

$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string));
}

public function testHtml5EscapeFunction()
{
$string = '<&>"\'';
$expected = '&lt;&amp;&gt;&quot;&apos;';

$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string, null, ENT_HTML5));

$escaper = new Escaper(ENT_HTML5);
$this->assertSame($expected, $escaper->escape($string));
}

public function testXhtmlEscapeFunction()
{
$string = '<&>"\'';
$expected = '&lt;&amp;&gt;&quot;&apos;';

$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string, null, ENT_XHTML));

$escaper = new Escaper(ENT_XHTML);
$this->assertSame($expected, $escaper->escape($string));
}

public function testXml1EscapeFunction()
{
$string = '<&>"\'';
$expected = '&lt;&amp;&gt;&quot;&apos;';

$escaper = new Escaper();
$this->assertSame($expected, $escaper->escape($string, null, ENT_XML1));

$escaper = new Escaper(ENT_XML1);
$this->assertSame($expected, $escaper->escape($string));
}

public function testThatDoubleEncodeIsEnabledByDefault()
{
$string = '&lt;&amp;&gt;&quot;&apos;';
$expected = '&amp;lt;&amp;amp;&amp;gt;&amp;quot;&amp;apos;';

$escaper = new Escaper(ENT_HTML5);
$this->assertSame($expected, $escaper->escape($string));
}

public function testThatDoubleEncodeCanBeDisabled()
{
$escaper = new Escaper();

$string = '&lt;&amp;&gt;&quot;&apos;';

$expected = '&lt;&amp;&gt;&quot;&amp;apos;';
$this->assertSame($expected, $escaper->escape($string, null, null, null, false));

$expected = '&lt;&amp;&gt;&quot;&apos;';
$this->assertSame($expected, $escaper->escape($string, null, ENT_HTML5, null, false));
$this->assertSame($expected, $escaper->escape($string, null, ENT_XHTML, null, false));
$this->assertSame($expected, $escaper->escape($string, null, ENT_XML1, null, false));
}

public function testThatBatchFunctionsAreCalledFromWithinTemplate()
{
vfsStream::setup('templates');

$engine = new Engine(vfsStream::url('templates'));
$engine->registerFunction('tr', 'trim');
$engine->registerFunction('uc', 'strtoupper');

$template = new Template($engine, 'template');

vfsStream::create(
array(
'template.php' => '<?php echo $this->batch(" abc ", "tr|uc") ?>',
)
);

$this->assertSame('ABC', $template->render());
}

public function testThatBatchFunctionsAreSkippedIfOutsideTemplate()
{
$escaper = new Escaper(ENT_HTML5);

$engine = new Engine(null, 'php', false);
$engine->loadExtension($escaper);

$engine->registerFunction('tr', 'trim');
$engine->registerFunction('uc', 'strtoupper');

$this->assertSame('abc', $escaper->escape('abc', 'tr|uc'));
}
}