Debian Codegraph @

skins/Vector/tests/phpunit/unit/components/VectorComponentLinkTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 * @since 1.42
20 */
21
22 namespace MediaWiki\Skins\Vector\Tests\Unit\Components;
23
24 use MediaWiki\Message\Message;
25 use MediaWiki\Skins\Vector\Components\VectorComponentLink;
26 use MediaWikiUnitTestCase;
27 use MessageLocalizer;
28
29 /**
30 * @group Vector
31 * @group Components
32 * @coversDefaultClass \MediaWiki\Skins\Vector\Components\VectorComponentLink
33 */
34 class VectorComponentLinkTest extends MediaWikiUnitTestCase {
35
36 /**
37 * @covers ::getTemplateData
38 */
39 public function testGetTemplateData() {
40 $href = '/mock-link';
41 $text = 'Mock Text';
42 $icon = 'mock-icon';
43 $accessKeyHint = 'sample-accesskey';
44
45 $localizer = $this->createMock( MessageLocalizer::class );
46 // Adjusting mock to prevent calling the service container.
47 $localizer->method( 'msg' )
48 ->willReturnCallback( function ( $key ) use ( $accessKeyHint ) {
49 // Directly create Message object without accessing real message texts
50 // to avoid 'Premature access to service container' error.
51 return $this->createConfiguredMock( Message::class, [
52 'exists' => true,
53 'text' => $key === $accessKeyHint . '-label' ? 'Mock aria label' : $key,
54 '__toString' => 'Mock aria label',
55 ] );
56 } );
57
58 // Create the component
59 $linkComponent = new VectorComponentLink( $href, $text, $icon, $localizer, $accessKeyHint );
60 $actual = $linkComponent->getTemplateData();
61
62 // Assert the expected values
63 $this->assertEquals( $icon, $actual['icon'] );
64 $this->assertEquals( $text, $actual['text'] );
65 $this->assertEquals( $href, $actual['href'] );
66
67 // New assertions for HTML attributes
68 $expectedTitle = "tooltip-sample-accesskeyword-separatorbrackets";
69 $expectedAriaLabel = "Mock aria label";
70 $attributesString = $actual['html-attributes'];
71
72 // Assert that the expected attributes are present in the string
73 $this->assertStringContainsString( 'title="' . $expectedTitle . '"', $attributesString );
74 $this->assertStringContainsString( 'aria-label="' . $expectedAriaLabel . '"', $attributesString );
75 }
76 }