From 2414a69730d2f3a626bb94740a3d2f4169808c57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Do=C4=9Fu=20Abaris?= Date: Tue, 6 Feb 2024 21:38:25 +0100 Subject: [PATCH] Add PHPUnit tests for VectorComponentLink Adds tests for VectorComponentLink in Vector skin, ensuring: - Initialization with parameters. - Generation of html-attributes string with title and aria-label. - Consistency in link attributes for accessibility and UX. Change-Id: I80b8a84d1d10bc7274ba5267910206d7f6e68534 --- .../components/VectorComponentLinkTest.php | 76 +++++++++++++++++++ 1 file changed, 76 insertions(+) create mode 100644 tests/phpunit/unit/components/VectorComponentLinkTest.php diff --git a/tests/phpunit/unit/components/VectorComponentLinkTest.php b/tests/phpunit/unit/components/VectorComponentLinkTest.php new file mode 100644 index 000000000..6ba80e85d --- /dev/null +++ b/tests/phpunit/unit/components/VectorComponentLinkTest.php @@ -0,0 +1,76 @@ +createMock( MessageLocalizer::class ); + // Adjusting mock to prevent calling the service container. + $localizer->method( 'msg' ) + ->willReturnCallback( function ( $key ) use ( $accessKeyHint ) { + // Directly create Message object without accessing real message texts + // to avoid 'Premature access to service container' error. + return $this->createConfiguredMock( Message::class, [ + 'exists' => true, + 'text' => $key === $accessKeyHint . '-label' ? 'Mock aria label' : $key, + '__toString' => 'Mock aria label', + ] ); + } ); + + // Create the component + $linkComponent = new VectorComponentLink( $href, $text, $icon, $localizer, $accessKeyHint ); + $actual = $linkComponent->getTemplateData(); + + // Assert the expected values + $this->assertEquals( $icon, $actual['icon'] ); + $this->assertEquals( $text, $actual['text'] ); + $this->assertEquals( $href, $actual['href'] ); + + // New assertions for HTML attributes + $expectedTitle = "tooltip-sample-accesskeyword-separatorbrackets"; + $expectedAriaLabel = "Mock aria label"; + $attributesString = $actual['html-attributes']; + + // Assert that the expected attributes are present in the string + $this->assertStringContainsString( 'title="' . $expectedTitle . '"', $attributesString ); + $this->assertStringContainsString( 'aria-label="' . $expectedAriaLabel . '"', $attributesString ); + } +}