Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion tests/data/REUSE.toml
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ SPDX-FileCopyrightText = "2018 Nextcloud GmbH and Nextcloud contributors"
SPDX-License-Identifier = "AGPL-3.0-or-later"

[[annotations]]
path = ["integritycheck/mimetypeListModified/core/js/mimetypelist.js", "integritycheck/mimetypeListModified/core/signature.json", "guest_avatar_einstein_32.png", "guest_avatar_einstein_32.svg", "test.pdf"]
path = ["integritycheck/mimetypeListModified/core/js/mimetypelist.js", "integritycheck/mimetypeListModified/core/signature.json", "test.pdf"]
precedence = "aggregate"
SPDX-FileCopyrightText = "2019 Nextcloud GmbH and Nextcloud contributors"
SPDX-License-Identifier = "AGPL-3.0-or-later"
Expand Down
Binary file removed tests/data/guest_avatar_einstein_32.png
Binary file not shown.
5 changes: 0 additions & 5 deletions tests/data/guest_avatar_einstein_32.svg

This file was deleted.

43 changes: 34 additions & 9 deletions tests/lib/Avatar/GuestAvatarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
use OC\Avatar\GuestAvatar;
use OCP\Files\SimpleFS\InMemoryFile;
use OCP\IConfig;
use PHPUnit\Framework\Attributes\DataProvider;
use Psr\Log\LoggerInterface;
use Test\TestCase;

Expand All @@ -37,19 +38,43 @@ public function setupGuestAvatar(): void {
$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 testGet() returns the expected avatar.
* Asserts that GuestAvatar::getFile() returns the expected avatar.
*
* For the test a static name "einstein" is used and
* the generated image is compared with an expected one.
* For the test a static name "einstein" is used.
*/
public function testGet(): void {
$avatar = $this->guestAvatar->getFile(32);
#[DataProvider('themeProvider')]
public function testGet(bool $darkTheme, int $backgroundColor): void {
$size = 32;
$avatar = $this->guestAvatar->getFile($size, $darkTheme);
self::assertInstanceOf(InMemoryFile::class, $avatar);
$expectedFile = file_get_contents(
__DIR__ . '/../../data/guest_avatar_einstein_32.png'
);
self::assertEquals(trim($expectedFile), trim($avatar->getContent()));
$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');
}

/**
Expand Down
Loading