Skip to content

Commit

Permalink
Merge pull request #147 from gsteel/gravatar
Browse files Browse the repository at this point in the history
Deprecates and replaces the Gravatar helper with a simplified version…
  • Loading branch information
Ocramius authored Mar 24, 2022
2 parents 3b906d3 + addb68b commit b7c269a
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Helper/Gravatar.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@

/**
* Helper for retrieving avatars from gravatar.com
*
* @deprecated This helper has been deprecated in favour of {@link GravatarImage} and will be removed in version 3.0
*/
class Gravatar extends AbstractHtmlElement
{
Expand Down
74 changes: 74 additions & 0 deletions src/Helper/GravatarImage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
<?php

declare(strict_types=1);

namespace Laminas\View\Helper;

use Laminas\Escaper\Escaper;
use Laminas\View\HtmlAttributesSet;

use function md5;
use function sprintf;
use function strtolower;
use function trim;

/**
* @psalm-import-type AttributeSet from HtmlAttributesSet
*/
final class GravatarImage
{
private const GRAVATAR_URL = '//www.gravatar.com/avatar';

public const RATINGS = [
'g',
'pg',
'r',
'x',
];

public const DEFAULT_IMAGE_VALUES = [
'404',
'mm',
'identicon',
'monsterid',
'wavatar',
];

private Escaper $escaper;

public function __construct(?Escaper $escaper = null)
{
$this->escaper = $escaper ?: new Escaper();
}

/**
* @param non-empty-string $emailAddress
* @param positive-int $imageSize
* @param AttributeSet $imageAttributes
* @psalm-param value-of<self::DEFAULT_IMAGE_VALUES>|string $defaultImage
* @psalm-param value-of<self::RATINGS> $rating
*/
public function __invoke(
string $emailAddress,
int $imageSize = 80,
array $imageAttributes = [],
string $defaultImage = 'mm',
string $rating = 'g'
): string {
$imageAttributes['width'] = $imageAttributes['height'] = $imageSize;
$imageAttributes['alt'] = $imageAttributes['alt'] ?? '';
$imageAttributes['src'] = sprintf(
'%s/%s?s=%d&r=%s&d=%s',
self::GRAVATAR_URL,
md5(strtolower(trim($emailAddress))),
$imageSize,
$rating,
$this->escaper->escapeUrl($defaultImage)
);

return sprintf(
'<img%s />',
(string) new HtmlAttributesSet($this->escaper, $imageAttributes)
);
}
}
2 changes: 2 additions & 0 deletions src/HelperPluginManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ class HelperPluginManager extends AbstractPluginManager
'FlashMessenger' => Helper\FlashMessenger::class,
'Gravatar' => Helper\Gravatar::class,
'gravatar' => Helper\Gravatar::class,
'gravatarImage' => Helper\GravatarImage::class,
'headLink' => Helper\HeadLink::class,
'HeadLink' => Helper\HeadLink::class,
'headlink' => Helper\HeadLink::class,
Expand Down Expand Up @@ -259,6 +260,7 @@ class HelperPluginManager extends AbstractPluginManager
Helper\EscapeCss::class => InvokableFactory::class,
Helper\EscapeUrl::class => InvokableFactory::class,
Helper\Gravatar::class => InvokableFactory::class,
Helper\GravatarImage::class => InvokableFactory::class,
Helper\HtmlTag::class => InvokableFactory::class,
Helper\HeadLink::class => InvokableFactory::class,
Helper\HeadMeta::class => InvokableFactory::class,
Expand Down
1 change: 1 addition & 0 deletions src/Renderer/PhpRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@
* @method \Laminas\View\Helper\Navigation\Links links($container = null)
* @method \Laminas\View\Helper\Navigation\Menu menu($container = null)
* @method \Laminas\View\Helper\Navigation\Sitemap sitemap($container = null)
* @method string gravatarImage(string $emailAddress, int $imageSize = 80, array $imageAttributes = [], string $defaultImage = 'mm', string $rating = 'g')
*/
class PhpRenderer implements Renderer, TreeRendererInterface
{
Expand Down
140 changes: 140 additions & 0 deletions test/Helper/GravatarImageTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
<?php

declare(strict_types=1);

namespace LaminasTest\View\Helper;

use Laminas\View\Helper\GravatarImage;
use PHPUnit\Framework\TestCase;

use function md5;

class GravatarImageTest extends TestCase
{
private GravatarImage $helper;

protected function setUp(): void
{
parent::setUp();
$this->helper = new GravatarImage();
}

public function testThatTheGivenEmailAddressWillBeHashed(): string
{
$image = ($this->helper)('[email protected]');

self::assertStringContainsString(
md5('[email protected]'),
$image
);

return $image;
}

/** @depends testThatTheGivenEmailAddressWillBeHashed */
public function testTheRatingWillDefaultToG(string $markup): void
{
self::assertStringContainsString(
'r&#x3D;g&amp;',
$markup
);
}

/** @depends testThatTheGivenEmailAddressWillBeHashed */
public function testAnEmptyAltAttributeWillBeAddedByDefault(string $markup): void
{
self::assertStringContainsString(
'alt=""',
$markup
);
}

/** @depends testThatTheGivenEmailAddressWillBeHashed */
public function testTheImageSizeWillBe80(string $markup): void
{
self::assertStringContainsString(
's&#x3D;80',
$markup
);
}

/** @depends testThatTheGivenEmailAddressWillBeHashed */
public function testWidthAndHeightAttributesWillBePresent(string $markup): void
{
self::assertStringContainsString(
'width="80"',
$markup
);
self::assertStringContainsString(
'height="80"',
$markup
);
}

/** @depends testThatTheGivenEmailAddressWillBeHashed */
public function testTheDefaultFallbackImageImageSizeWillBeMM(string $markup): void
{
self::assertStringContainsString(
'd&#x3D;mm',
$markup
);
}

public function testThatAttributesCanBeAddedToTheImageMarkup(): void
{
$image = ($this->helper)('[email protected]', 80, ['data-foo' => 'bar']);
self::assertStringContainsString(
'data-foo="bar"',
$image
);
}

public function testThatTheImageSizeCanBeAltered(): string
{
$image = ($this->helper)('[email protected]', 123);
self::assertStringContainsString(
's&#x3D;123',
$image
);

return $image;
}

/** @depends testThatTheImageSizeCanBeAltered */
public function testWidthAndHeightAttributesWillMatchCustomValue(string $markup): void
{
self::assertStringContainsString(
'width="123"',
$markup
);
self::assertStringContainsString(
'height="123"',
$markup
);
}

public function testThatTheRatingCanBeAltered(): void
{
$image = ($this->helper)('[email protected]', 80, [], 'mm', 'x');
self::assertStringContainsString(
'r&#x3D;x&amp;',
$image
);
}

public function testThatTheDefaultImageCanBeAltered(): void
{
$image = ($this->helper)('[email protected]', 80, [], 'wavatar');
self::assertStringContainsString(
'd&#x3D;wavatar',
$image
);
}

public function testThatTheDefaultImageCanBeAnUrl(): void
{
$image = ($this->helper)('[email protected]', 80, [], 'https://example.com/someimage');
$expect = 'https&#x25;3A&#x25;2F&#x25;2Fexample.com&#x25;2Fsomeimage';
self::assertStringContainsString($expect, $image);
}
}
1 change: 1 addition & 0 deletions test/Helper/GravatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use function strtoupper;
use function urlencode;

/** @psalm-suppress DeprecatedClass */
class GravatarTest extends TestCase
{
/** @var Gravatar */
Expand Down

0 comments on commit b7c269a

Please sign in to comment.