init
This commit is contained in:
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\Translation\Test\Unit\Model\Html;
|
||||
|
||||
use Esun\Translation\Model\Html\HtmlTextExtractor;
|
||||
use Esun\Translation\Model\Html\HtmlTranslationValidator;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HtmlTextExtractorTest extends TestCase
|
||||
{
|
||||
public function testItTranslatesTextNodesWithoutChangingTags(): void
|
||||
{
|
||||
$extractor = new HtmlTextExtractor();
|
||||
$fragment = $extractor->extract(
|
||||
'<p class="intro">Fast <strong>printing</strong> {{media url="a.jpg"}}</p><img src="a.jpg">'
|
||||
);
|
||||
|
||||
self::assertSame(
|
||||
['html_0' => 'Fast ', 'html_1' => 'printing', 'html_2' => ' {{media url="a.jpg"}}'],
|
||||
$fragment->getTexts()
|
||||
);
|
||||
|
||||
$html = $fragment->applyTranslations([
|
||||
'html_0' => 'Schneller ',
|
||||
'html_1' => 'Druck',
|
||||
'html_2' => ' {{media url="a.jpg"}}',
|
||||
]);
|
||||
|
||||
self::assertStringContainsString('<p class="intro">Schneller <strong>Druck</strong>', $html);
|
||||
self::assertStringContainsString('<img src="a.jpg">', $html);
|
||||
}
|
||||
|
||||
public function testItPreservesMagentoDirectivesInsideHtmlAttributes(): void
|
||||
{
|
||||
$extractor = new HtmlTextExtractor();
|
||||
$fragment = $extractor->extract(
|
||||
'<figure class="pagebuilder-image">'
|
||||
. '<img src="{{media url=wysiwyg/precision.png}}" alt="Precision image">'
|
||||
. '<figcaption>High precision sample image</figcaption>'
|
||||
. '</figure>'
|
||||
);
|
||||
|
||||
$html = $fragment->applyTranslations([
|
||||
'html_0' => 'Hochpraezises Beispielbild',
|
||||
]);
|
||||
|
||||
self::assertStringContainsString('src="{{media url=wysiwyg/precision.png}}"', $html);
|
||||
self::assertStringNotContainsString('%7B%7Bmedia%20url=wysiwyg/precision.png%7D%7D', $html);
|
||||
self::assertStringContainsString('<figcaption>Hochpraezises Beispielbild</figcaption>', $html);
|
||||
}
|
||||
|
||||
public function testItPreservesQuotedMagentoDirectivesInsideHtmlAttributes(): void
|
||||
{
|
||||
$extractor = new HtmlTextExtractor();
|
||||
$fragment = $extractor->extract(
|
||||
'<figure class="pagebuilder-image">'
|
||||
. '<img src="{{media url=".renditions/wysiwyg/product_details/petg_luminous_filament.png"}}"'
|
||||
. ' alt="" width="1000" height="444">'
|
||||
. '<figcaption>PETG luminous filament</figcaption>'
|
||||
. '</figure>'
|
||||
);
|
||||
|
||||
$html = $fragment->applyTranslations([
|
||||
'html_0' => 'PETG-Leuchtfilament',
|
||||
]);
|
||||
|
||||
self::assertStringContainsString(
|
||||
'src="{{media url=".renditions/wysiwyg/product_details/petg_luminous_filament.png"}}"',
|
||||
$html
|
||||
);
|
||||
self::assertStringNotContainsString('".renditions/wysiwyg/product_details/petg_luminous_filament.png"', $html);
|
||||
self::assertStringContainsString('<figcaption>PETG-Leuchtfilament</figcaption>', $html);
|
||||
}
|
||||
|
||||
public function testItNormalizesEncodedDirectiveQuotesInsideHtmlAttributes(): void
|
||||
{
|
||||
$extractor = new HtmlTextExtractor();
|
||||
$fragment = $extractor->extract(
|
||||
'<figure class="pagebuilder-image">'
|
||||
. '<img src="{{media url=".renditions/wysiwyg/product_details/petg_luminous_filament.png"}}"'
|
||||
. ' alt="" width="1000" height="444">'
|
||||
. '<figcaption>PETG luminous filament</figcaption>'
|
||||
. '</figure>'
|
||||
);
|
||||
|
||||
$html = $fragment->applyTranslations([
|
||||
'html_0' => 'PETG-Leuchtfilament',
|
||||
]);
|
||||
|
||||
self::assertStringContainsString(
|
||||
'src="{{media url=".renditions/wysiwyg/product_details/petg_luminous_filament.png"}}"',
|
||||
$html
|
||||
);
|
||||
self::assertStringNotContainsString('".renditions/wysiwyg/product_details/petg_luminous_filament.png"', $html);
|
||||
self::assertStringContainsString('<figcaption>PETG-Leuchtfilament</figcaption>', $html);
|
||||
}
|
||||
|
||||
public function testValidatorRejectsDangerousHtml(): void
|
||||
{
|
||||
$validator = new HtmlTranslationValidator();
|
||||
|
||||
self::assertSame([], $validator->validate('<p>Safe content</p>'));
|
||||
self::assertNotSame([], $validator->validate('<p onclick="alert(1)">Unsafe</p>'));
|
||||
self::assertNotSame([], $validator->validate('<script>alert(1)</script>'));
|
||||
}
|
||||
|
||||
public function testValidatorAllowsPageBuilderFigureHtml(): void
|
||||
{
|
||||
$validator = new HtmlTranslationValidator();
|
||||
|
||||
self::assertSame(
|
||||
[],
|
||||
$validator->validate(
|
||||
'<figure class="pagebuilder-image" data-content-type="image">'
|
||||
. '<img src="{{media url=.renditions/wysiwyg/product_details/demo.png}}" alt="">'
|
||||
. '<figcaption>Product image</figcaption>'
|
||||
. '</figure>'
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user