Skip to content

Commit

Permalink
Fix get_image without title and description (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-schranz authored Oct 5, 2020
1 parent 594b6d1 commit b819a92
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/ImageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -230,10 +230,18 @@ private function createImage(
}

// Get title from object to use as alt attribute.
$alt = $propertyAccessor->getValue($media, 'title');
if ($propertyAccessor->isReadable($media, 'title')) {
$alt = $propertyAccessor->getValue($media, 'title');
} else {
// Get filename from thumbnail and use that.
$alt = pathinfo(reset($thumbnails))['filename'] ?? '';
}

// Get description from object to use as title attribute else fallback to alt attribute.
$title = $propertyAccessor->getValue($media, 'description') ?: $alt;
$title = $alt;
if ($propertyAccessor->isReadable($media, 'description')) {
$title = $propertyAccessor->getValue($media, 'description') ?: $alt;
}

/** @var array<string, string|null> $attributes */
$attributes = array_merge(['alt' => $alt, 'title' => $title], $attributes);
Expand Down
66 changes: 66 additions & 0 deletions tests/Unit/ImageExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class ImageExtensionTest extends TestCase
*/
private $image;

/**
* @var mixed[]
*/
private $minimalImage;

/**
* @var mixed[]
*/
Expand All @@ -52,6 +57,19 @@ public function setUp(): void
],
];

$this->minimalImage = [
'thumbnails' => [
'sulu-100x100' => '/uploads/media/sulu-100x100/01/image.jpg?v=1-0',
'sulu-100x100.webp' => '/uploads/media/sulu-100x100/01/image.webp?v=1-0',
'sulu-100x100@2x' => '/uploads/media/sulu-100x100@2x/01/image.jpg?v=1-0',
'[email protected]' => '/uploads/media/sulu-100x100@2x/01/image.webp?v=1-0',
'sulu-170x170' => '/uploads/media/sulu-170x170/01/image.jpg?v=1-0',
'sulu-170x170.webp' => '/uploads/media/sulu-170x170/01/image.webp?v=1-0',
'sulu-400x400' => '/uploads/media/sulu-400x400/01/image.jpg?v=1-0',
'sulu-400x400.webp' => '/uploads/media/sulu-400x400/01/image.webp?v=1-0',
],
];

$this->svgImage = [
'title' => 'Title',
'description' => 'Description',
Expand Down Expand Up @@ -132,6 +150,29 @@ public function testPictureTag(): void
);
}

public function testPictureTagMinimalImage(): void
{
$this->assertSame(
'<picture>' .
'<source media="(max-width: 1024px)"' .
' srcset="/uploads/media/sulu-170x170/01/image.jpg?v=1-0">' .
'<source media="(max-width: 650px)"' .
' srcset="/uploads/media/sulu-100x100/01/image.jpg?v=1-0">' .
'<img alt="image"' .
' title="image"' .
' src="/uploads/media/sulu-400x400/01/image.jpg?v=1-0">' .
'</picture>',
$this->imageExtension->getImage(
$this->minimalImage,
'sulu-400x400',
[
'(max-width: 1024px)' => 'sulu-170x170',
'(max-width: 650px)' => 'sulu-100x100',
]
)
);
}

public function testComplexPictureTag(): void
{
$this->assertSame(
Expand Down Expand Up @@ -201,6 +242,31 @@ public function testLazyComplexImageTag(): void
);
}

public function testLazyComplexImageTagMinimalImage(): void
{
$this->assertSame(
'<img alt="image"' .
' title="image"' .
' src="/lazy/sulu-400x400.svg"' .
' data-src="/uploads/media/sulu-400x400/01/image.jpg?v=1-0"' .
' srcset="/lazy/sulu-400x400.svg 1024w, /lazy/sulu-170x170.svg 800w, /lazy/sulu-100x100.svg 460w"' .
' data-srcset="/uploads/media/sulu-400x400/01/image.jpg?v=1-0 1024w, /uploads/media/sulu-170x170/01/image.jpg?v=1-0 800w, /uploads/media/sulu-100x100/01/image.jpg?v=1-0 460w"' .
' sizes="(max-width: 1024px) 100vw, (max-width: 800px) 100vw, 100vw"' .
' id="image-id"' .
' class="image-class lazyload">',
$this->imageExtension->getLazyImage(
$this->minimalImage,
[
'src' => 'sulu-400x400',
'srcset' => 'sulu-400x400 1024w, sulu-170x170 800w, sulu-100x100 460w',
'sizes' => '(max-width: 1024px) 100vw, (max-width: 800px) 100vw, 100vw',
'id' => 'image-id',
'class' => 'image-class',
]
)
);
}

public function testLazyPictureTag(): void
{
$this->assertSame(
Expand Down

0 comments on commit b819a92

Please sign in to comment.