Files
nextcloud-server-mirror/tests/lib/Avatar/GuestAvatarTest.php
David Dreschner 4f0609dd70 test(avatar): Check the guest avatar independent of the font renderer
GuestAvatarTest::testGet() compared the generated PNG byte for byte with
a stored image. The anti-aliasing of the letter depends on the GD and
FreeType versions: the stored image matches Ubuntu 24.04 (libgd3
2.3.3-9ubuntu5, FreeType 2.13.2), while Ubuntu 26.04 (libgd3
2.3.3-13ubuntu2, FreeType 2.14.2) and the bundled GD of the official PHP
images render a few edge pixels differently. The nodb jobs therefore fail
whenever they run on a runner with the newer image.

Check the PNG format, the image size and the guest background color in the
light and the dark theme instead, and count the pixels in the letter
color: the anti-aliased edges differ between renderers, the strokes of the
letter don't. Also remove the SVG fixture, which has been unused since
guest avatars switched to PNG.

Assisted-by: ClaudeCode:claude-opus-5-5
Signed-off-by: David Dreschner <david.dreschner@nextcloud.com>
2026-10-27 11:30:37 +01:00

89 lines
2.5 KiB
PHP

<?php
declare(strict_types=1);
/**
* SPDX-FileCopyrightText: 2018 Nextcloud GmbH and Nextcloud contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
namespace Test\Avatar;
use OC\Avatar\GuestAvatar;
use OCP\Files\SimpleFS\InMemoryFile;
use OCP\IConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LoggerInterface;
use Test\TestCase;
/**
* This class provides test cases for the GuestAvatar class.
*
* @package Test\Avatar
*/
class GuestAvatarTest extends TestCase {
/**
* @var GuestAvatar
*/
private $guestAvatar;
/**
* Setups a guest avatar instance for tests.
*/
#[\PHPUnit\Framework\Attributes\Before()]
public function setupGuestAvatar(): void {
/* @var MockObject|LoggerInterface $logger */
$logger = $this->createMock(LoggerInterface::class);
$config = $this->createMock(IConfig::class);
$this->guestAvatar = new GuestAvatar('einstein', $config, $logger);
}
public static function themeProvider(): array {
return [
'light theme' => ['darkTheme' => false, 'backgroundColor' => 0xe9f3f7],
'dark theme' => ['darkTheme' => true, 'backgroundColor' => 0x030e12],
];
}
/**
* Asserts that GuestAvatar::getFile() returns the expected avatar.
*
* For the test a static name "einstein" is used.
*/
#[DataProvider('themeProvider')]
public function testGet(bool $darkTheme, int $backgroundColor): void {
$size = 32;
$avatar = $this->guestAvatar->getFile($size, $darkTheme);
self::assertInstanceOf(InMemoryFile::class, $avatar);
$content = $avatar->getContent();
self::assertStringStartsWith("\x89PNG\r\n\x1a\n", $content, 'The avatar is not a PNG');
$image = imagecreatefromstring($content);
self::assertInstanceOf(\GdImage::class, $image);
self::assertSame($size, imagesx($image));
self::assertSame($size, imagesy($image));
self::assertSame($backgroundColor, imagecolorat($image, 0, 0), 'The background is not the guest color');
// The anti-aliased edges depend on the GD and FreeType versions, the strokes of the letter don't
$letterPixels = 0;
for ($y = 0; $y < $size; $y++) {
for ($x = 0; $x < $size; $x++) {
if (imagecolorat($image, $x, $y) === 0x248eb5) {
$letterPixels++;
}
}
}
// A Noto Sans "E" at 32 px has 27 fully covered pixels
self::assertSame(27, $letterPixels, 'The avatar does not show a Noto Sans "E" in the guest color');
}
/**
* Asserts that "testIsCustomAvatar" returns false for guests.
*
* @return void
*/
public function testIsCustomAvatar(): void {
self::assertFalse($this->guestAvatar->isCustomAvatar());
}
}