From 1048a248549c1f5a03695cf5d292ce86c6fc89dc Mon Sep 17 00:00:00 2001 From: George Steel Date: Tue, 5 Apr 2022 22:14:42 +0100 Subject: [PATCH 1/2] Reinstates constants for images and ratings The previous version of this helper declared constants for default image types and ratings. They have been reinstated, adding ones that didn't exist. Tests have been updated to replace string literals with constants and also the expected strings are escaped to make the expected output easier to read. Signed-off-by: George Steel --- src/Helper/GravatarImage.php | 39 ++++++++++++++------ test/Helper/GravatarImageTest.php | 59 ++++++++++++++++++++++++------- 2 files changed, 74 insertions(+), 24 deletions(-) diff --git a/src/Helper/GravatarImage.php b/src/Helper/GravatarImage.php index 07a0818b2..61754176e 100644 --- a/src/Helper/GravatarImage.php +++ b/src/Helper/GravatarImage.php @@ -19,19 +19,36 @@ final class GravatarImage { private const GRAVATAR_URL = '//www.gravatar.com/avatar'; + public const RATING_G = 'g'; + public const RATING_PG = 'pg'; + public const RATING_R = 'r'; + public const RATING_X = 'x'; + + public const DEFAULT_404 = '404'; + public const DEFAULT_MP = 'mp'; + public const DEFAULT_IDENTICON = 'identicon'; + public const DEFAULT_MONSTERID = 'monsterid'; + public const DEFAULT_WAVATAR = 'wavatar'; + public const DEFAULT_RETRO = 'retro'; + public const DEFAULT_ROBOHASH = 'robohash'; + public const DEFAULT_BLANK = 'blank'; + public const RATINGS = [ - 'g', - 'pg', - 'r', - 'x', + self::RATING_G, + self::RATING_PG, + self::RATING_R, + self::RATING_X, ]; public const DEFAULT_IMAGE_VALUES = [ - '404', - 'mm', - 'identicon', - 'monsterid', - 'wavatar', + self::DEFAULT_404, + self::DEFAULT_MP, + self::DEFAULT_IDENTICON, + self::DEFAULT_MONSTERID, + self::DEFAULT_WAVATAR, + self::DEFAULT_RETRO, + self::DEFAULT_ROBOHASH, + self::DEFAULT_BLANK, ]; private Escaper $escaper; @@ -52,8 +69,8 @@ public function __invoke( string $emailAddress, int $imageSize = 80, array $imageAttributes = [], - string $defaultImage = 'mm', - string $rating = 'g' + string $defaultImage = self::DEFAULT_MP, + string $rating = self::RATING_G ): string { $imageAttributes['width'] = $imageAttributes['height'] = $imageSize; $imageAttributes['alt'] = $imageAttributes['alt'] ?? ''; diff --git a/test/Helper/GravatarImageTest.php b/test/Helper/GravatarImageTest.php index ed6e0cba8..83a870454 100644 --- a/test/Helper/GravatarImageTest.php +++ b/test/Helper/GravatarImageTest.php @@ -4,19 +4,23 @@ namespace LaminasTest\View\Helper; +use Laminas\Escaper\Escaper; use Laminas\View\Helper\GravatarImage; use PHPUnit\Framework\TestCase; use function md5; +use function sprintf; class GravatarImageTest extends TestCase { private GravatarImage $helper; + private Escaper $escaper; protected function setUp(): void { parent::setUp(); - $this->helper = new GravatarImage(); + $this->helper = new GravatarImage(); + $this->escaper = new Escaper(); } public function testThatTheGivenEmailAddressWillBeHashed(): string @@ -34,8 +38,13 @@ public function testThatTheGivenEmailAddressWillBeHashed(): string /** @depends testThatTheGivenEmailAddressWillBeHashed */ public function testTheRatingWillDefaultToG(string $markup): void { + $expect = $this->escaper->escapeHtmlAttr(sprintf( + 'r=%s', + GravatarImage::RATING_G + )); + self::assertStringContainsString( - 'r=g&', + $expect, $markup ); } @@ -52,8 +61,10 @@ public function testAnEmptyAltAttributeWillBeAddedByDefault(string $markup): voi /** @depends testThatTheGivenEmailAddressWillBeHashed */ public function testTheImageSizeWillBe80(string $markup): void { + $expect = $this->escaper->escapeHtmlAttr('s=80'); + self::assertStringContainsString( - 's=80', + $expect, $markup ); } @@ -72,10 +83,15 @@ public function testWidthAndHeightAttributesWillBePresent(string $markup): void } /** @depends testThatTheGivenEmailAddressWillBeHashed */ - public function testTheDefaultFallbackImageImageSizeWillBeMM(string $markup): void + public function testTheDefaultFallbackImageImageSizeWillBeMP(string $markup): void { + $expect = $this->escaper->escapeHtmlAttr(sprintf( + 'd=%s', + GravatarImage::DEFAULT_MP + )); + self::assertStringContainsString( - 'd=mm', + $expect, $markup ); } @@ -91,9 +107,11 @@ public function testThatAttributesCanBeAddedToTheImageMarkup(): void public function testThatTheImageSizeCanBeAltered(): string { - $image = ($this->helper)('me@example.com', 123); + $image = ($this->helper)('me@example.com', 123); + $expect = $this->escaper->escapeHtmlAttr('s=123'); + self::assertStringContainsString( - 's=123', + $expect, $image ); @@ -115,26 +133,41 @@ public function testWidthAndHeightAttributesWillMatchCustomValue(string $markup) public function testThatTheRatingCanBeAltered(): void { - $image = ($this->helper)('me@example.com', 80, [], 'mm', 'x'); + $image = ($this->helper)('me@example.com', 80, [], GravatarImage::DEFAULT_MP, GravatarImage::RATING_X); + $expect = $this->escaper->escapeHtmlAttr(sprintf( + 'r=%s', + GravatarImage::RATING_X + )); + self::assertStringContainsString( - 'r=x&', + $expect, $image ); } public function testThatTheDefaultImageCanBeAltered(): void { - $image = ($this->helper)('me@example.com', 80, [], 'wavatar'); + $image = ($this->helper)('me@example.com', 80, [], GravatarImage::DEFAULT_WAVATAR); + $expect = $this->escaper->escapeHtmlAttr(sprintf( + 'd=%s', + GravatarImage::DEFAULT_WAVATAR + )); + self::assertStringContainsString( - 'd=wavatar', + $expect, $image ); } public function testThatTheDefaultImageCanBeAnUrl(): void { - $image = ($this->helper)('me@example.com', 80, [], 'https://example.com/someimage'); - $expect = 'https%3A%2F%2Fexample.com%2Fsomeimage'; + $customImage = 'https://example.com/someimage'; + $image = ($this->helper)('me@example.com', 80, [], $customImage); + $expect = $this->escaper->escapeHtmlAttr(sprintf( + 'd=%s', + $this->escaper->escapeUrl($customImage) + )); + self::assertStringContainsString($expect, $image); } } From 1f86f6274e310f09598f8fbb7291f477602cd3c3 Mon Sep 17 00:00:00 2001 From: George Steel Date: Wed, 6 Apr 2022 18:07:08 +0100 Subject: [PATCH 2/2] Add code comment to describe the purpose of the constants Signed-off-by: George Steel --- src/Helper/GravatarImage.php | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/Helper/GravatarImage.php b/src/Helper/GravatarImage.php index 61754176e..ce2825376 100644 --- a/src/Helper/GravatarImage.php +++ b/src/Helper/GravatarImage.php @@ -19,11 +19,21 @@ final class GravatarImage { private const GRAVATAR_URL = '//www.gravatar.com/avatar'; + /** + * RATING_* constants describe the "rating" of the avatar image that is most suitable for your audience. + * + * @link https://en.gravatar.com/site/implement/images/#rating + */ public const RATING_G = 'g'; public const RATING_PG = 'pg'; public const RATING_R = 'r'; public const RATING_X = 'x'; + /** + * DEFAULT_* constants describe the fallback image type that will be displayed when a profile does not exist. + * + * @link https://en.gravatar.com/site/implement/images/#default-image + */ public const DEFAULT_404 = '404'; public const DEFAULT_MP = 'mp'; public const DEFAULT_IDENTICON = 'identicon';