diff --git a/Sources/Avatar.php b/Sources/Avatar.php index 7df9e0605e..940361a969 100644 --- a/Sources/Avatar.php +++ b/Sources/Avatar.php @@ -587,9 +587,14 @@ public function __construct( break; // Last ditch fallback is a transparent 1x1 GIF. + // + // This leaves the loop rather than the switch, because there is + // nothing left to try and because a data URI is not a URL that + // Url::isValid() will accept, so the condition the loop tests + // can never become false from here. default: $url = new Url('data:image/gif;base64,R0lGODlhAQABAIAAAAAAAP///yH5BAEAAAAALAAAAAABAAEAAAIBRAA7'); - break; + break 2; } } diff --git a/tests/Unit/AvatarFallbackTest.php b/tests/Unit/AvatarFallbackTest.php new file mode 100644 index 0000000000..d66465eefe --- /dev/null +++ b/tests/Unit/AvatarFallbackTest.php @@ -0,0 +1,110 @@ +assertStringStartsWith('data:image/gif;base64,', (string) $avatar->url); + } + + /** + * The control, and the reason the loop exists: when there is somewhere to + * look, it is looked in, and the search ends long before the last resort. + */ + public function testAnAvatarThatCanBeFoundIsStillFound(): void + { + Config::$boardurl = 'https://example.com'; + Config::$modSettings['gravatarEnabled'] = false; + Config::$modSettings['avatar_url'] = 'https://example.com/avatars'; + + $avatar = new Avatar(url: 'Oxygen/beagle.png', id_member: 1); + + $this->assertSame('https://example.com/avatars/Oxygen/beagle.png', (string) $avatar->url); + } + + /****************** + * Internal methods + ******************/ + + protected function setUp(): void + { + $this->boardurl = Config::$boardurl ?? ''; + + foreach (['avatar_url', 'gravatarEnabled'] as $key) { + if (isset(Config::$modSettings[$key])) { + $this->backup[$key] = Config::$modSettings[$key]; + } + } + } + + /** + * PHPUnit does not reset SMF's statics between tests, so a setting left + * behind here would leak into every test that follows. + */ + protected function tearDown(): void + { + Config::$boardurl = $this->boardurl; + + foreach (['avatar_url', 'gravatarEnabled'] as $key) { + unset(Config::$modSettings[$key]); + + if (isset($this->backup[$key])) { + Config::$modSettings[$key] = $this->backup[$key]; + } + } + + $this->backup = []; + } +}