ltsecurityinc安防产品在线商城代码
This commit is contained in:
+48
@@ -0,0 +1,48 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\View\Helper;
|
||||
|
||||
use Magento\TestFramework\TestCase\AbstractController;
|
||||
|
||||
/**
|
||||
* Test the secure HTML helper and templates.
|
||||
*
|
||||
* @magentoAppArea frontend
|
||||
*/
|
||||
class SecureHtmlRendererTemplateTest extends AbstractController
|
||||
{
|
||||
/**
|
||||
* Test using the helper inside templates.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testTemplateUsage(): void
|
||||
{
|
||||
$this->getRequest()->setMethod('GET');
|
||||
$this->dispatch('securehtml/secure/helper');
|
||||
$content = $this->getResponse()->getContent();
|
||||
|
||||
$this->assertStringContainsString(
|
||||
'<h1 onclick="alert()">Hello there!</h1>',
|
||||
$content
|
||||
);
|
||||
$this->assertStringContainsString(
|
||||
'<script src="http://my.magento.com/static/script.js"></script>',
|
||||
$content
|
||||
);
|
||||
$this->assertStringContainsString(
|
||||
"<script>\n let myVar = 1;\n</script>",
|
||||
$content
|
||||
);
|
||||
$this->assertStringContainsString(
|
||||
'<div>I am just <a> text</div>',
|
||||
$content
|
||||
);
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\Framework\View\Helper;
|
||||
|
||||
use Magento\Framework\View\Helper\SecureHtmlRender\TagData;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
/**
|
||||
* Test for the secure HTML helper.
|
||||
*/
|
||||
class SecureHtmlRendererTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var SecureHtmlRenderer
|
||||
*/
|
||||
private $helper;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
//Clearing the processors list to ensure stable results.
|
||||
$this->helper = $objectManager->create(SecureHtmlRenderer::class, ['processors' => []]);
|
||||
}
|
||||
|
||||
/**
|
||||
* Provides tags to render.
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
public function getTags(): array
|
||||
{
|
||||
return [
|
||||
[
|
||||
new TagData('div', ['style' => 'display: none;', 'width' => '20px'], 'some <text>', true),
|
||||
'<div style="display: none;" width="20px">some <text></div>'
|
||||
],
|
||||
[
|
||||
new TagData('div', [], 'some <b>HTML</b>', false),
|
||||
'<div>some <b>HTML</b></div>'
|
||||
],
|
||||
[
|
||||
new TagData('img', ['src' => 'https://magento.com/img.jpg'], null, true),
|
||||
'<img src="https://magento.com/img.jpg"/>'
|
||||
]
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Test tag rendering.
|
||||
*
|
||||
* @param TagData $tagData
|
||||
* @param string $expected Expected HTML.
|
||||
* @return void
|
||||
* @dataProvider getTags
|
||||
*/
|
||||
public function testRenderTag(TagData $tagData, string $expected): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
$expected,
|
||||
$this->helper->renderTag(
|
||||
$tagData->getTag(),
|
||||
$tagData->getAttributes(),
|
||||
$tagData->getContent(),
|
||||
$tagData->isTextContent()
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rendering an event listener.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderEventHandler(): void
|
||||
{
|
||||
$this->assertEquals(
|
||||
'onclick="alert(this.parent.getAttribute("data-title"))"',
|
||||
$this->helper->renderEventListener('onclick', 'alert(this.parent.getAttribute("data-title"))')
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rendering JS listeners as separate tags.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderEventListenerAsTag(): void
|
||||
{
|
||||
$html = $this->helper->renderEventListenerAsTag('onclick', 'alert(1)', '#id');
|
||||
$this->assertStringContainsString('alert(1)', $html);
|
||||
$this->assertStringContainsString('#id', $html);
|
||||
$this->assertStringContainsString('click', $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check handler validation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInvalidEventListener(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->helper->renderEventListenerAsTag('nonevent', '', '');
|
||||
}
|
||||
|
||||
/**
|
||||
* Test rendering "style" attribute as separate tag.
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testRenderStyleAsTag(): void
|
||||
{
|
||||
$html = $this->helper->renderStyleAsTag('display: none; font-size: 3em; ', '#id');
|
||||
$this->assertStringContainsString('#id', $html);
|
||||
$this->assertStringContainsString('display', $html);
|
||||
$this->assertStringContainsString('none', $html);
|
||||
$this->assertStringContainsString('fontSize', $html);
|
||||
$this->assertStringContainsString('3em', $html);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check style validation
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function testInvalidStyle(): void
|
||||
{
|
||||
$this->expectException(\InvalidArgumentException::class);
|
||||
$this->helper->renderStyleAsTag('display;', '');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user