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 Jun 28, 2021
1 parent 0d503ce commit df9373c
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 16 deletions.
22 changes: 16 additions & 6 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,12 +794,12 @@ 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;
}

/**
Expand All @@ -808,12 +808,22 @@ public function IsLazyLoaded()
* @param bool $lazyLoad
* @return $this
*/
public function LazyLoad($lazyLoad)
public function setLazyLoad($lazyLoad)
{
$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" />
24 changes: 16 additions & 8 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 @@ -538,18 +538,26 @@ public function testLazyLoad()

/** @var Image $image */
$image = $this->objFromFixture(Image::class, 'imageWithTitle');
$this->assertTrue($image->IsLazyLoaded(), 'Images lazy load by default');
$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');

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

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

Config::modify()->set(Image::class, 'lazy_loading_enabled', true);

$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');

Config::modify()->set(Image::class, 'lazy_loading_enabled', false);
}
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 df9373c

Please sign in to comment.