init
This commit is contained in:
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\BlogImport\Test\Unit\Model;
|
||||
|
||||
use Esun\BlogImport\Model\HtmlProcessor;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class HtmlProcessorTest extends TestCase
|
||||
{
|
||||
private string $htmlPath;
|
||||
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->htmlPath = tempnam(sys_get_temp_dir(), 'esun-blog-html-');
|
||||
if ($this->htmlPath === false) {
|
||||
self::fail('Unable to create a temporary HTML file.');
|
||||
}
|
||||
}
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if (isset($this->htmlPath) && is_file($this->htmlPath)) {
|
||||
unlink($this->htmlPath);
|
||||
}
|
||||
}
|
||||
|
||||
public function testItExtractsArticleBodyAndRewritesLegacyMedia(): void
|
||||
{
|
||||
file_put_contents($this->htmlPath, <<<'HTML'
|
||||
<section class="main">
|
||||
<article class="entry blog-article">
|
||||
<h1 class="news-title">Duplicated title</h1>
|
||||
<div class="news-time">Post time<script src="//example.test/widget.js"></script></div>
|
||||
<p><a href="https://www.esun3d.com/uploads/图片1.jpg"><img src="/uploads/图片1.jpg"></a></p>
|
||||
<img src="https://www.esun3d.com/wp-includes/images/smilies/icon_smile.gif">
|
||||
<img src="https://www.esunchina.net/uploads/external.jpg">
|
||||
<img src="/uploads/http://img.xiumi.us/external-wrapped.jpg">
|
||||
<video src="https://media.example.com/external.mp4"></video>
|
||||
<a href="https://www.esun3d.com/news/not-media">Regular link</a>
|
||||
<div class="picker"></div>
|
||||
</article>
|
||||
</section>
|
||||
HTML);
|
||||
|
||||
$result = (new HtmlProcessor())->process($this->htmlPath, 'uploads');
|
||||
|
||||
self::assertSame(['图片1.jpg', 'icon_smile.gif'], $result['media']);
|
||||
self::assertStringContainsString("{{media url='uploads/图片1.jpg'}}", $result['html']);
|
||||
self::assertStringContainsString("{{media url='uploads/icon_smile.gif'}}", $result['html']);
|
||||
self::assertStringContainsString('https://www.esunchina.net/uploads/external.jpg', $result['html']);
|
||||
self::assertStringContainsString('/uploads/http://img.xiumi.us/external-wrapped.jpg', $result['html']);
|
||||
self::assertStringContainsString('https://media.example.com/external.mp4', $result['html']);
|
||||
self::assertStringContainsString('https://www.esun3d.com/news/not-media', $result['html']);
|
||||
self::assertStringNotContainsString('Duplicated title', $result['html']);
|
||||
self::assertStringNotContainsString('news-time', $result['html']);
|
||||
self::assertStringNotContainsString('<script', $result['html']);
|
||||
self::assertStringNotContainsString('picker', $result['html']);
|
||||
}
|
||||
|
||||
public function testItLeavesExternalLinksUntouched(): void
|
||||
{
|
||||
file_put_contents(
|
||||
$this->htmlPath,
|
||||
'<article class="blog-article"><a href="https://example.com/file.pdf">Download</a></article>'
|
||||
);
|
||||
|
||||
$result = (new HtmlProcessor())->process($this->htmlPath, 'upload');
|
||||
|
||||
self::assertSame([], $result['media']);
|
||||
self::assertStringContainsString('https://example.com/file.pdf', $result['html']);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,63 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Esun\BlogImport\Test\Unit\Model;
|
||||
|
||||
use Esun\BlogImport\Model\MediaNormalizer;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class MediaNormalizerTest extends TestCase
|
||||
{
|
||||
/** @var string[] */
|
||||
private array $temporaryFiles = [];
|
||||
|
||||
protected function tearDown(): void
|
||||
{
|
||||
foreach ($this->temporaryFiles as $file) {
|
||||
if (is_file($file)) {
|
||||
unlink($file);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function testItConvertsWebpDataStoredWithAJpegExtension(): void
|
||||
{
|
||||
if (!function_exists('imagewebp')) {
|
||||
self::markTestSkipped('GD WebP support is required.');
|
||||
}
|
||||
$path = $this->temporaryPath('.jpg');
|
||||
$image = imagecreatetruecolor(2, 2);
|
||||
imagewebp($image, $path);
|
||||
imagedestroy($image);
|
||||
|
||||
$normalizer = new MediaNormalizer();
|
||||
self::assertTrue($normalizer->needsNormalization($path));
|
||||
self::assertTrue($normalizer->normalize($path));
|
||||
self::assertSame(IMAGETYPE_JPEG, getimagesize($path)[2]);
|
||||
self::assertFalse($normalizer->needsNormalization($path));
|
||||
self::assertFalse($normalizer->normalize($path));
|
||||
}
|
||||
|
||||
public function testItIgnoresMediaWithoutAResizableImageExtension(): void
|
||||
{
|
||||
$path = $this->temporaryPath('.mp4');
|
||||
file_put_contents($path, 'not inspected');
|
||||
|
||||
$normalizer = new MediaNormalizer();
|
||||
self::assertFalse($normalizer->needsNormalization($path));
|
||||
self::assertFalse($normalizer->normalize($path));
|
||||
}
|
||||
|
||||
private function temporaryPath(string $extension): string
|
||||
{
|
||||
$base = tempnam(sys_get_temp_dir(), 'esun-media-test-');
|
||||
if ($base === false) {
|
||||
self::fail('Unable to create a temporary media file.');
|
||||
}
|
||||
unlink($base);
|
||||
$path = $base . $extension;
|
||||
$this->temporaryFiles[] = $path;
|
||||
|
||||
return $path;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user