| 1 |
<?php |
| 2 |
|
| 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; |
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
class SkinMustacheTest extends MediaWikiIntegrationTestCase { |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
private function getMockOutputPage( $html, $title ) { |
| 23 |
$mockContentSecurityPolicy = $this->createMock( ContentSecurityPolicy::class ); |
| 24 |
|
| 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; |
| 45 |
} |
| 46 |
|
| 47 |
private function validateTemplateData( $data, $key ) { |
| 48 |
$value = $data[$key]; |
| 49 |
if ( $value === null ) { |
| 50 |
|
| 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)" |
| 57 |
); |
| 58 |
|
| 59 |
|
| 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)" |
| 68 |
); |
| 69 |
} |
| 70 |
} |
| 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)" |
| 76 |
); |
| 77 |
} |
| 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)" |
| 82 |
); |
| 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)" |
| 87 |
); |
| 88 |
} else { |
| 89 |
$this->fail( |
| 90 |
"Keys must be primitives e.g. arrays OR strings OR bools OR null ($key)." |
| 91 |
); |
| 92 |
} |
| 93 |
} |
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 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(); |
| 118 |
|
| 119 |
|
| 120 |
foreach ( $data as $key => $_ ) { |
| 121 |
$this->validateTemplateData( $data, $key ); |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
$searchData = $data['data-search-box']; |
| 126 |
foreach ( $searchData as $key => $_ ) { |
| 127 |
$this->validateTemplateData( $searchData, $key ); |
| 128 |
} |
| 129 |
} |
| 130 |
} |