Source of file ImageShortcodeProviderTest.php
Size: 6,664 Bytes - Last Modified: 2021-12-23T10:27:40+00:00
/var/www/docs.ssmods.com/process/src/tests/php/Shortcodes/ImageShortcodeProviderTest.php
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191 | <?php namespace SilverStripe\Assets\Tests\Shortcodes; use SilverStripe\Assets\File; use Silverstripe\Assets\Dev\TestAssetStore; use SilverStripe\Core\Config\Config; use SilverStripe\Dev\SapphireTest; use SilverStripe\View\Parsers\ShortcodeParser; use SilverStripe\Assets\Image; use SilverStripe\Assets\Shortcodes\ImageShortcodeProvider; /** * @skipUpgrade */ class ImageShortcodeProviderTest extends SapphireTest { protected static $fixture_file = '../ImageTest.yml'; protected function setUp(): void { parent::setUp(); // Set backend root to /ImageTest TestAssetStore::activate('ImageTest'); // Copy test images for each of the fixture references $images = Image::get(); /** @var Image $image */ foreach ($images as $image) { $sourcePath = __DIR__ . '/../ImageTest/' . $image->Name; $image->setFromLocalFile($sourcePath, $image->Filename); } } protected function tearDown(): void { TestAssetStore::reset(); parent::tearDown(); } public function testShortcodeHandlerDoesNotFallBackToFileProperties() { $image = $this->objFromFixture(Image::class, 'imageWithTitle'); $parser = new ShortcodeParser(); $parser->register('image', [ImageShortcodeProvider::class, 'handle_shortcode']); $this->assertEquals( sprintf( '<img src="%s" alt="">', $image->Link() ), $parser->parse(sprintf('[image id=%d]', $image->ID)) ); } public function testShortcodeHandlerUsesShortcodeProperties() { $image = $this->objFromFixture(Image::class, 'imageWithTitle'); $parser = new ShortcodeParser(); $parser->register('image', [ImageShortcodeProvider::class, 'handle_shortcode']); $this->assertEquals( sprintf( '<img src="%s" alt="Alt content" title="Title content">', $image->Link() ), $parser->parse(sprintf( '[image id="%d" alt="Alt content" title="Title content"]', $image->ID )) ); } public function testShortcodeHandlerAddsDefaultAttributes() { $image = $this->objFromFixture(Image::class, 'imageWithoutTitle'); $parser = new ShortcodeParser(); $parser->register('image', [ImageShortcodeProvider::class, 'handle_shortcode']); $this->assertEquals( sprintf( '<img src="%s" alt="">', $image->Link() ), $parser->parse(sprintf( '[image id="%d"]', $image->ID )) ); } public function testShortcodeHandlerDoesNotResampleToNonIntegerImagesSizes() { $image = $this->objFromFixture(Image::class, 'imageWithoutTitle'); $parser = new ShortcodeParser(); $parser->register('image', [ImageShortcodeProvider::class, 'handle_shortcode']); $this->assertEquals( sprintf( '<img src="%s" alt="" width="50%%" height="auto">', $image->Link() ), $parser->parse(sprintf( '[image id="%d" alt="" width="50%%" height="auto"]', $image->ID )) ); } public function testShortcodeHandlerFailsGracefully() { $parser = new ShortcodeParser(); $parser->register('image', [ImageShortcodeProvider::class, 'handle_shortcode']); $nonExistentImageID = File::get()->max('ID') + 1; $expected = '<img alt="Image not found">'; $shortcodes = [ '[image id="' . $nonExistentImageID . '"]', '[image id="' . $nonExistentImageID . '" alt="my-alt-attr"]', ]; foreach ($shortcodes as $shortcode) { $actual = $parser->parse($shortcode); $this->assertEquals($expected, $actual); } } public function testMissingImageDoesNotCache() { $parser = new ShortcodeParser(); $parser->register('image', [ImageShortcodeProvider::class, 'handle_shortcode']); $nonExistentImageID = File::get()->max('ID') + 1; $shortcode = '[image id="' . $nonExistentImageID . '"]'; // make sure cache is not populated from a previous test $cache = ImageShortcodeProvider::getCache(); $cache->clear(); $args = ['id' => (string)$nonExistentImageID]; $cacheKey = ImageShortcodeProvider::getCacheKey($args); // assert that cache is empty before parsing shortcode $this->assertNull($cache->get($cacheKey)); $parser->parse($shortcode); // assert that cache is still empty after parsing shortcode $this->assertNull($cache->get($cacheKey)); } public function testLazyLoading() { $parser = new ShortcodeParser(); $parser->register('image', [ImageShortcodeProvider::class, 'handle_shortcode']); $id = $this->objFromFixture(Image::class, 'imageWithTitle')->ID; // regular shortcode $shortcode = '[image id="' . $id . '" width="300" height="200"]'; $this->assertStringContainsString('loading="lazy"', $parser->parse($shortcode)); // missing width $shortcode = '[image id="' . $id . '" height="200"]'; $this->assertStringNotContainsString('loading="lazy"', $parser->parse($shortcode)); // missing height $shortcode = '[image id="' . $id . '" width="300"]'; $this->assertStringNotContainsString('loading="lazy"', $parser->parse($shortcode)); // loading="eager" $shortcode = '[image id="' . $id . '" width="300" height="200" loading="eager"]'; $this->assertStringNotContainsString('loading="lazy"', $parser->parse($shortcode)); // loading="nonsense" $shortcode = '[image id="' . $id . '" width="300" height="200" loading="nonsense"]'; $this->assertStringContainsString('loading="lazy"', $parser->parse($shortcode)); // globally disabled Config::withConfig(function () use ($id, $parser) { Config::modify()->set(Image::class, 'lazy_loading_enabled', false); // clear-provider-cache is so that we don't get a cached result from the 'regular shortcode' // assertion earlier in this function from ImageShortCodeProvider::handle_shortcode() $shortcode = '[image id="' . $id . '" width="300" height="200" clear-provider-cache="1"]'; $this->assertStringNotContainsString('loading="lazy"', $parser->parse($shortcode)); }); } } |