diff --git a/tests/data/REUSE.toml b/tests/data/REUSE.toml index 79a71e8608502..a2111a539e8d3 100644 --- a/tests/data/REUSE.toml +++ b/tests/data/REUSE.toml @@ -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" diff --git a/tests/data/guest_avatar_einstein_32.png b/tests/data/guest_avatar_einstein_32.png deleted file mode 100644 index d03f0509bac3b..0000000000000 Binary files a/tests/data/guest_avatar_einstein_32.png and /dev/null differ diff --git a/tests/data/guest_avatar_einstein_32.svg b/tests/data/guest_avatar_einstein_32.svg deleted file mode 100644 index d007f962f243f..0000000000000 --- a/tests/data/guest_avatar_einstein_32.svg +++ /dev/null @@ -1,5 +0,0 @@ - - - - E - diff --git a/tests/lib/Avatar/GuestAvatarTest.php b/tests/lib/Avatar/GuestAvatarTest.php index eed41e8221e82..cf91454eaaf4e 100644 --- a/tests/lib/Avatar/GuestAvatarTest.php +++ b/tests/lib/Avatar/GuestAvatarTest.php @@ -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; @@ -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'); } /**