init
This commit is contained in:
+98
@@ -0,0 +1,98 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\MultiLocaleTranslation\Test\Unit\Model\Html;
|
||||
|
||||
use Esun\MultiLocaleTranslation\Model\Html\TranslatableTextExtractor;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class TranslatableTextExtractorTest extends TestCase
|
||||
{
|
||||
private TranslatableTextExtractor $extractor;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->extractor = new TranslatableTextExtractor();
|
||||
}
|
||||
|
||||
public function testExtractsVisibleTextWithoutChangingMarkupOffsets(): void
|
||||
{
|
||||
$html = '<div data-title="Keep"> Hello <strong>world</strong>! </div>';
|
||||
|
||||
$fragments = $this->extractor->extract($html, 30000);
|
||||
|
||||
self::assertSame(['Hello', 'world', '!'], array_column($fragments, 'text'));
|
||||
foreach ($fragments as $fragment) {
|
||||
self::assertSame(
|
||||
$fragment['text'],
|
||||
substr($html, $fragment['offset'], $fragment['length'])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testIgnoresCodeAndEncodedMarkup(): void
|
||||
{
|
||||
$html = '<style>.hero { display:block }</style>'
|
||||
. '<p>Translate me</p>'
|
||||
. '<div><style>.box { margin: 0 }</style></div>'
|
||||
. '<script>console.log("Do not translate")</script>';
|
||||
|
||||
$fragments = $this->extractor->extract($html, 30000);
|
||||
|
||||
self::assertSame(['Translate me'], array_column($fragments, 'text'));
|
||||
}
|
||||
|
||||
public function testExtractsVisibleTextFromPageBuilderEncodedHtml(): void
|
||||
{
|
||||
$html = '<div data-content-type="html" data-element="main">'
|
||||
. '<div class="payment-label">Payment Methods</div>'
|
||||
. '<style>.hidden { display:none }</style>'
|
||||
. '<p>Secure Checkout & Payment</p>'
|
||||
. '<img src="{{media url=payment.png}}" alt="" />'
|
||||
. '</div>';
|
||||
|
||||
$fragments = $this->extractor->extract($html, 30000);
|
||||
|
||||
self::assertSame(
|
||||
['Payment Methods', 'Secure Checkout & Payment'],
|
||||
array_column($fragments, 'text')
|
||||
);
|
||||
foreach ($fragments as $fragment) {
|
||||
self::assertSame(
|
||||
$fragment['text'],
|
||||
substr($html, $fragment['offset'], $fragment['length'])
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testEncodedHtmlTextCanBeReplacedWithoutChangingPageBuilderMarkup(): void
|
||||
{
|
||||
$html = '<div data-content-type="html"><p class="message">Original Text</p></div>';
|
||||
$fragments = $this->extractor->extract($html, 30000);
|
||||
|
||||
$fragment = $fragments[0];
|
||||
$translated = substr($html, 0, $fragment['offset'])
|
||||
. 'Translated Text'
|
||||
. substr($html, $fragment['offset'] + $fragment['length']);
|
||||
|
||||
self::assertSame(
|
||||
'<div data-content-type="html"><p class="message">Translated Text</p></div>',
|
||||
$translated
|
||||
);
|
||||
}
|
||||
|
||||
public function testSplitsLargeTextAtUtf8SafeBoundaries(): void
|
||||
{
|
||||
$html = '<p>' . str_repeat('多语言内容 ', 20) . '</p>';
|
||||
|
||||
$fragments = $this->extractor->extract($html, 40);
|
||||
|
||||
self::assertGreaterThan(1, count($fragments));
|
||||
self::assertSame(trim(str_repeat('多语言内容 ', 20)), implode('', array_column($fragments, 'text')));
|
||||
foreach ($fragments as $fragment) {
|
||||
self::assertLessThanOrEqual(40, strlen($fragment['text']));
|
||||
self::assertSame(1, preg_match('//u', $fragment['text']));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user