Skip to content

Commit

Permalink
Rewrite for more realistic scenarios
Browse files Browse the repository at this point in the history
  • Loading branch information
jissereitsma committed Dec 11, 2024
1 parent 2603b3a commit c8bf5ab
Show file tree
Hide file tree
Showing 6 changed files with 137 additions and 26 deletions.
158 changes: 132 additions & 26 deletions Test/Integration/Util/HtmlReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace Yireo\NextGenImages\Test\Integration\Util;

use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\ObjectManager;
use Yireo\NextGenImages\Block\PictureFactory;
use Yireo\NextGenImages\Config\Config;
Expand All @@ -28,7 +29,7 @@ public function testReplaceWithNoImages()
$om->create(DomUtils::class)
);

$html = '<div><img src="/img/test.png"/></div>';
$html = '<div><img src="/img/test1.jpg"/></div>';
$result = $htmlReplacer->replace($html);
$this->assertEquals($html, $result);
}
Expand All @@ -43,13 +44,29 @@ public function testReplaceWithNoImages()
public function testReplaceWithTestImage(string $originalHtml, string $expectedHtml)
{
$om = ObjectManager::getInstance();
$this->copyImages();

$imageCollector = $this->getMagentoMock(ImageCollector::class);
$images = [
new Image('/tmp/pub/test.png', '/test.png'),
new Image('/tmp/pub/test.webp', '/test.webp'),
$returnMap = [
[
'/media/tmp/test1.jpg',
[
new Image('/tmp/pub/media/tmp/test1.jpg', '/media/tmp/test1.jpg'),
new Image('/tmp/pub/media/tmp/test1.webp', '/media/tmp/test1.webp'),
],
],
[
'/media/tmp/test2.jpg',
[
new Image('/tmp/pub/media/tmp/test2.jpg', '/media/tmp/test2.jpg'),
new Image('/tmp/pub/media/tmp/test2.webp', '/media/tmp/test2.webp'),
],
],
];
$imageCollector->method('collect')->willReturn($images);

$imageCollector = $this->getMagentoMock(ImageCollector::class);
$imageCollector
->method('collect')
->will($this->returnValueMap($returnMap));

$htmlReplacer = new HtmlReplacer(
$om->create(UrlConvertor::class),
Expand All @@ -70,45 +87,134 @@ public function getTestReplaceWithTestImageArguments(): array
{
return [
[
'<div><img src="/test.png"/></div>',
'<div><picture><source type="image/png" srcset="/test.png"><source type="image/webp" srcset="/test.webp"><img src="/test.png" loading="lazy" /></picture></div>'
'<div>'
.'<img src="/media/tmp/test1.jpg"/>'
.'</div>',
'<div>'
.'<picture>'
.'<source type="image/jpeg" srcset="/media/tmp/test1.jpg">'
.'<source type="image/webp" srcset="/media/tmp/test1.webp">'
.'<img src="/media/tmp/test1.jpg" loading="lazy" />'
.'</picture>'
.'</div>',
],
[
'<div><img src="/test.png"/>FOOBAR</div>',
'<div><picture><source type="image/png" srcset="/test.png"><source type="image/webp" srcset="/test.webp"><img src="/test.png" loading="lazy" /></picture>FOOBAR</div>'
'<div>'
.'<img src="/media/tmp/test1.jpg"/>'
.'FOOBAR'
.'</div>',
'<div>'
.'<picture>'
.'<source type="image/jpeg" srcset="/media/tmp/test1.jpg">'
.'<source type="image/webp" srcset="/media/tmp/test1.webp">'
.'<img src="/media/tmp/test1.jpg" loading="lazy" />'
.'</picture>'
.'FOOBAR'
.'</div>',
],
[
'<div><img src="data:image/gif;base64,foobar"/></div>',
'<div><img src="data:image/gif;base64,foobar"/></div>'
'<div>'
.'<img src="data:image/gif;base64,foobar"/>'
.'</div>',
'<div>'
.'<img src="data:image/gif;base64,foobar"/>'
.'</div>',
],
[
'<div><img data-src="/test.png"/></div>',
'<div><picture><source type="image/png" data-srcset="/test.png"><source type="image/webp" data-srcset="/test.webp"><img data-src="/test.png" loading="lazy" /></picture></div>'
'<div>'
.'<img data-src="/media/tmp/test1.jpg"/>'
.'</div>',
'<div>'
.'<picture>'
.'<source type="image/jpeg" data-srcset="/media/tmp/test1.jpg">'
.'<source type="image/webp" data-srcset="/media/tmp/test1.webp">'
.'<img data-src="/media/tmp/test1.jpg" loading="lazy" />'
.'</picture>'
.'</div>',
],
[
'<div style="background-image: url(/test.png);"></div>',
'<div style="background-image: url(/test.png);"></div>'
'<div style="background-image: url(/media/tmp/test1.jpg);"></div>',
'<div style="background-image: url(/media/tmp/test1.jpg);"></div>',
],
[
'<div><img src="/test.png"/><img src="/test.png"/><img src="/test.png"/></div>',
'<div><picture><source type="image/png" srcset="/test.png"><source type="image/webp" srcset="/test.webp"><img src="/test.png" loading="lazy" /></picture><picture><source type="image/png" srcset="/test.png"><source type="image/webp" srcset="/test.webp"><img src="/test.png" loading="lazy" /></picture><picture><source type="image/png" srcset="/test.png"><source type="image/webp" srcset="/test.webp"><img src="/test.png" loading="lazy" /></picture></div>'
'<div>'
.'<img src="/media/tmp/test1.jpg"/>'
.'<img src="/media/tmp/test2.jpg"/>'
.'</div>',
'<div>'
.'<picture>'
.'<source type="image/jpeg" srcset="/media/tmp/test1.jpg">'
.'<source type="image/webp" srcset="/media/tmp/test1.webp">'
.'<img src="/media/tmp/test1.jpg" loading="lazy" />'
.'</picture>'
.'<picture>'
.'<source type="image/jpeg" srcset="/media/tmp/test2.jpg">'
.'<source type="image/webp" srcset="/media/tmp/test2.webp">'
.'<img src="/media/tmp/test2.jpg" loading="lazy" />'
.'</picture>'
.'</div>',
],
[
'<div><img style="float: right;" src="/img/test.png"/></div>',
'<div><picture><source type="image/png" srcset="/test.png"><source type="image/webp" srcset="/test.webp"><img style="float: right;" src="/img/test.png" loading="lazy" /></picture></div>'
'<div>'
.'<img style="float: right;" src="/media/tmp/test1.jpg"/>'
.'</div>',
'<div>'
.'<picture>'
.'<source type="image/jpeg" srcset="/media/tmp/test1.jpg">'
.'<source type="image/webp" srcset="/media/tmp/test1.webp">'
.'<img style="float: right;" src="/media/tmp/test1.jpg" loading="lazy" />'
.'</picture>'
.'</div>',
],
[
"<script>var imgElement = '<img src=\"/test.png\" />';</script>",
"<script>var imgElement = '<img src=\"/test.png\" />';</script>",
"<script>var imgElement = '<img src=\"/media/tmp/test1.jpg\" />';</script>",
"<script>var imgElement = '<img src=\"/media/tmp/test1.jpg\" />';</script>",
],
[
'<div><img :src="/img/test.png"/></div>',
'<div><picture><source type="image/png" :srcset="/test.png"><source type="image/webp" :srcset="/test.webp"><img :src="/img/test.png" loading="lazy" /></picture></div>'
'<div>'
.'<img :src="/media/tmp/test1.jpg"/>'
.'</div>',
'<div>'
.'<picture>'
.'<source type="image/jpeg" :srcset="/media/tmp/test1.jpg">'
.'<source type="image/webp" :srcset="/media/tmp/test1.webp">'
.'<img :src="/media/tmp/test1.jpg" loading="lazy" />'
.'</picture>'
.'</div>',
],
[
'<div><img :data-src="/img/test.png"/></div>',
'<div><picture><source type="image/png" :data-srcset="/test.png"><source type="image/webp" :data-srcset="/test.webp"><img :data-src="/img/test.png" loading="lazy" /></picture></div>'
'<div>'
.'<img :data-src="/media/tmp/test1.jpg"/>'
.'</div>',
'<div>'
.'<picture>'
.'<source type="image/jpeg" :data-srcset="/media/tmp/test1.jpg">'
.'<source type="image/webp" :data-srcset="/media/tmp/test1.webp">'
.'<img :data-src="/media/tmp/test1.jpg" loading="lazy" />'
.'</picture>'
.'</div>',
],
];
}

private function copyImages(): void
{
$images = [];
$images[] = 'test1.jpg';
$images[] = 'test2.jpg';
$images[] = 'test1.webp';
$images[] = 'test2.webp';

$om = ObjectManager::getInstance();

$directoryList = $om->get(DirectoryList::class);
$fixtureFolder = __DIR__.'/../fixtures/images/';

$tmpFolder = $directoryList->getRoot().'/pub/media/tmp';
@mkdir($tmpFolder);

foreach ($images as $image) {
copy($fixtureFolder.'/'.$image, $tmpFolder.'/'.$image);
}
}
}
Binary file added Test/Integration/fixtures/images/test1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Test/Integration/fixtures/images/test1.webp
Binary file not shown.
Binary file added Test/Integration/fixtures/images/test2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added Test/Integration/fixtures/images/test2.webp
Binary file not shown.
5 changes: 5 additions & 0 deletions Test/Unit/Util/HtmlReplacerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ public function testReplaceWithTestImage(string $originalHtml, string $expectedH
new Image('/tmp/pub/test.png', '/test.png'),
new Image('/tmp/pub/test.webp', '/test.webp'),
];

$imageCollector->method('collect')->willReturn($images);

$block = $this->getMagentoMock(Picture::class);
Expand Down Expand Up @@ -113,6 +114,10 @@ public function getTestReplaceWithTestImageArguments(): array
'<div><div @click="fullscreen = false; $nextTick(() => calcPageSize())"><img src="/img/test.png"/></div></div>',
'<div><div @click="fullscreen = false; $nextTick(() => calcPageSize())"><img src="/img/test.png"/></div></div>'
],
[
'<img src="test1.jpg">test<span>test</span><img src="test2.jpg">',
'<img src="test1.jpg">test<span>test</span><img src="test2.jpg">',
],
];
}
}

0 comments on commit c8bf5ab

Please sign in to comment.