Skip to content

Commit

Permalink
Update tests to work with default lazy loading enabled.
Browse files Browse the repository at this point in the history
  • Loading branch information
bergice committed Jul 4, 2021
1 parent 0d503ce commit 4aaf33a
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 25 deletions.
24 changes: 17 additions & 7 deletions src/ImageManipulation.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ trait ImageManipulation
*
* @var bool
*/
protected $lazyLoad = true;
private $lazyLoad = true;

/**
* Set whether image resizes are allowed
Expand Down Expand Up @@ -794,26 +794,36 @@ public function getHeight()
*
* @return bool
*/
public function IsLazyLoaded()
public function getIsLazyLoaded() : bool
{
if (!Config::inst()->get('SilverStripe\\Assets\\Image', 'lazy_loading_enabled')) {
return false;
if (Image::getLazyLoadingEnabled() && $this->getWidth() && $this->getHeight()) {
return $this->lazyLoad;
}
return $this->lazyLoad;
return false;
}

/**
* Set whether image will be lazy loaded
*
* @param bool $lazyLoad
* @return $this
* @return self $this
*/
public function LazyLoad($lazyLoad)
public function LazyLoad(bool $lazyLoad): self
{
$this->lazyLoad = $lazyLoad;
return $this;
}

/**
* Get whether image will be lazy loaded
*
* @return bool
*/
public function getLazyLoad(): bool
{
return $this->lazyLoad;
}

/**
* Get the orientation of this image.
*
Expand Down
2 changes: 1 addition & 1 deletion templates/DBFile_image.ss
Original file line number Diff line number Diff line change
@@ -1 +1 @@
<img src="$URL.ATT" alt="$Title.ATT"<% if $IsLazyLoaded %> loading="lazy" width="$Width.ATT" height="$Height.ATT"<% end_if %> />
<img src="$URL.ATT" alt="$Title.ATT"<% if $IsLazyLoaded %> loading="lazy"<% end_if %> width="$Width.ATT" height="$Height.ATT" />
4 changes: 4 additions & 0 deletions tests/php/FileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ public function testInvalidImageManipulations()
$this->assertFalse($pdf->getIsImage());
$this->assertTrue($pdf->exists());
$this->assertNull($pdf->Pad(100, 100));
Config::withConfig(function () use ($pdf) {
Config::modify()->set(Image::class, 'lazy_loading_enabled', true);
$this->assertFalse($pdf->getIsLazyLoaded());
});

// Non-existant image
$image = new Image();
Expand Down
40 changes: 24 additions & 16 deletions tests/php/ImageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public function testGetTagWithTitle()
Config::modify()->set(DBFile::class, 'force_resample', false);

$image = $this->objFromFixture(Image::class, 'imageWithTitle');
$expected = '<img src="/assets/ImageTest/folder/test-image.png" alt="This is a image Title" />';
$expected = '<img src="/assets/ImageTest/folder/test-image.png" alt="This is a image Title" loading="lazy" width="300" height="300" />';
$actual = trim($image->getTag());

$this->assertEquals($expected, $actual);
Expand Down Expand Up @@ -91,7 +91,7 @@ public function testGetTagWithoutTitle()
Config::modify()->set(DBFile::class, 'force_resample', false);

$image = $this->objFromFixture(Image::class, 'imageWithoutTitle');
$expected = '<img src="/assets/ImageTest/folder/test-image.png" alt="test image" />';
$expected = '<img src="/assets/ImageTest/folder/test-image.png" alt="test image" loading="lazy" width="300" height="300" />';
$actual = trim($image->getTag());

$this->assertEquals($expected, $actual);
Expand All @@ -102,7 +102,7 @@ public function testGetTagWithoutTitleContainingDots()
Config::modify()->set(DBFile::class, 'force_resample', false);

$image = $this->objFromFixture(Image::class, 'imageWithoutTitleContainingDots');
$expected = '<img src="/assets/ImageTest/folder/test.image.with.dots.png" alt="test.image.with.dots" />';
$expected = '<img src="/assets/ImageTest/folder/test.image.with.dots.png" alt="test.image.with.dots" loading="lazy" width="300" height="300" />';
$actual = trim($image->getTag());

$this->assertEquals($expected, $actual);
Expand Down Expand Up @@ -534,24 +534,32 @@ public function testGetSetImageBackend()

public function testLazyLoad()
{
Config::modify()->set(Image::class, 'lazy_loading_enabled', true);
Config::withConfig(function () {
Config::modify()->set(Image::class, 'lazy_loading_enabled', true);

/** @var Image $image */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');
$this->assertTrue($image->IsLazyLoaded(), 'Images lazy load by default');
/** @var Image $image */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');
$this->assertTrue($image->getIsLazyLoaded(), 'Images lazy load by default');

$expected = '<img src="/assets/ImageTest/folder/test-image.png" alt="This is a image Title" loading="lazy" width="300" height="300" />';
$actual = trim($image->getTag());
$this->assertEquals($expected, $actual, 'Lazy load img tag renders correctly');
$expected = '<img src="/assets/ImageTest/folder/test-image.png" alt="This is a image Title" loading="lazy" width="300" height="300" />';
$actual = trim($image->getTag());
$this->assertEquals($expected, $actual, 'Lazy load img tag renders correctly');

$image->LazyLoad(false);
$this->assertFalse($image->getIsLazyLoaded(), 'Images can be eager loaded on a per-image basis');

$image->LazyLoad(false);
$this->assertFalse($image->IsLazyLoaded(), 'Images can be eager loaded on a per-image basis');
$image->LazyLoad(true);
Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
$this->assertFalse($image->getIsLazyLoaded(), 'Lazy loading can be disabled globally');

$image->LazyLoad(true);
Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
$this->assertFalse($image->IsLazyLoaded(), 'Lazy loading can be disabled globally');
Config::modify()->set(Image::class, 'lazy_loading_enabled', true);

Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
$mockBackend = $this->getMockBuilder(InterventionBackend::class)->getMock();
$mockBackend->expects($this->any())->method('getWidth')->will($this->returnValue(0));
$image->setImageBackend($mockBackend);
$this->assertEquals(0, $image->getWidth(), 'Image does not have dimensions');
$this->assertFalse($image->getIsLazyLoaded(), 'Lazy loading is disabled if image does not have dimensions');
});
}

/**
Expand Down
2 changes: 1 addition & 1 deletion tests/php/Storage/DBFileTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function testRender()
$this->assertFileExists($fish);
$obj->MyFile->setFromLocalFile($fish, 'awesome-fish.jpg');
$this->assertEquals(
'<img src="/mysite/assets/a870de278b/awesome-fish.jpg" alt="awesome-fish.jpg" />',
'<img src="/mysite/assets/a870de278b/awesome-fish.jpg" alt="awesome-fish.jpg" loading="lazy" width="300" height="300" />',
trim($obj->MyFile->forTemplate())
);

Expand Down

0 comments on commit 4aaf33a

Please sign in to comment.