Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / skins / SkinMustacheTest.php
blobf5d879d38e101043d7e764187c56c10a21ffb42e
1 <?php
3 use MediaWiki\Context\RequestContext;
4 use MediaWiki\MainConfigNames;
5 use MediaWiki\Output\OutputPage;
6 use MediaWiki\Request\ContentSecurityPolicy;
7 use MediaWiki\Title\Title;
8 use PHPUnit\Framework\MockObject\MockObject;
10 /**
11 * @covers \SkinMustache
12 * @group Skin
13 * @group Database
15 class SkinMustacheTest extends MediaWikiIntegrationTestCase {
17 /**
18 * @param string $html
19 * @param Title $title
20 * @return MockObject|OutputPage
22 private function getMockOutputPage( $html, $title ) {
23 $mockContentSecurityPolicy = $this->createMock( ContentSecurityPolicy::class );
25 $mock = $this->createMock( OutputPage::class );
26 $mock->method( 'getHTML' )
27 ->willReturn( $html );
28 $mock->method( 'getCategoryLinks' )
29 ->willReturn( [] );
30 $mock->method( 'getIndicators' )
31 ->willReturn( [
32 'id' => '<a>indicator</a>'
33 ] );
34 $mock->method( 'getTitle' )
35 ->willReturn( $title );
36 $mock->method( 'getIndicators' )
37 ->willReturn( [ '' ] );
38 $mock->method( 'getLanguageLinks' )
39 ->willReturn( [] );
40 $mock->method( 'isTOCEnabled' )
41 ->willReturn( true );
42 $mock->method( 'getTOCData' )
43 ->willReturn( null );
44 return $mock;
47 private function validateTemplateData( $data, $key ) {
48 $value = $data[$key];
49 if ( $value === null ) {
50 // Cannot validate a null value
51 return;
52 } elseif ( is_array( $value ) ) {
53 $this->assertTrue(
54 str_starts_with( $key, 'data-' ) || str_starts_with( $key, 'array-' ),
55 "Template data that is an object should be associated with a key" .
56 " prefixed with `data-` or `array-` ($key)"
59 // Validate the children
60 foreach ( $value as $childKey => $childValue ) {
61 if ( is_string( $childKey ) ) {
62 $this->validateTemplateData( $value, $childKey );
63 } else {
64 $this->assertStringStartsWith(
65 'array-',
66 $key,
67 "Template data that is a flat array should be associated with a key prefixed `array-` ($key)"
71 } elseif ( is_string( $value ) ) {
72 if ( str_contains( $value, '<' ) ) {
73 $this->assertTrue(
74 str_starts_with( $key, 'html-' ) || $key === 'html',
75 "Template data containing HTML must be prefixed with `html-` ($key)"
78 } elseif ( is_bool( $value ) ) {
79 $this->assertTrue(
80 str_starts_with( $key, 'is-' ) || str_starts_with( $key, 'has-' ),
81 "Template data containing booleans must be prefixed with `is-` or `has-` ($key)"
83 } elseif ( is_numeric( $value ) ) {
84 $this->assertTrue(
85 str_starts_with( $key, 'number-' ),
86 "Template data containing numbers must be prefixed with `number-` ($key)"
88 } else {
89 $this->fail(
90 "Keys must be primitives e.g. arrays OR strings OR bools OR null ($key)."
95 /**
96 * @covers \Skin
97 * @covers \MediaWiki\Skin\SkinComponentLogo
98 * @covers \MediaWiki\Skin\SkinComponentSearch
99 * @covers \MediaWiki\Skin\SkinComponentTableOfContents
100 * @covers \MediaWiki\Skin\SkinComponentFooter
102 public function testGetTemplateData() {
103 $config = $this->getServiceContainer()->getMainConfig();
104 $bodytext = '<p>hello</p>';
105 $context = new RequestContext();
106 $title = Title::makeTitle( NS_MAIN, 'Mustache skin' );
107 $context->setTitle( $title );
108 $out = $this->getMockOutputPage( $bodytext, $title );
109 $context->setOutput( $out );
110 $this->overrideConfigValue( MainConfigNames::Logos, [] );
111 $skin = new SkinMustache( [
112 'name' => 'test',
113 'templateDirectory' => __DIR__,
114 ] );
115 $context->setConfig( $config );
116 $skin->setContext( $context );
117 $data = $skin->getTemplateData();
119 // Validate the default template data respects the naming rules
120 foreach ( $data as $key => $_ ) {
121 $this->validateTemplateData( $data, $key );
124 // Validate search data
125 $searchData = $data['data-search-box'];
126 foreach ( $searchData as $key => $_ ) {
127 $this->validateTemplateData( $searchData, $key );