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
;
11 * @covers \SkinMustache
15 class SkinMustacheTest
extends MediaWikiIntegrationTestCase
{
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' )
30 $mock->method( 'getIndicators' )
32 'id' => '<a>indicator</a>'
34 $mock->method( 'getTitle' )
35 ->willReturn( $title );
36 $mock->method( 'getIndicators' )
37 ->willReturn( [ '' ] );
38 $mock->method( 'getLanguageLinks' )
40 $mock->method( 'isTOCEnabled' )
42 $mock->method( 'getTOCData' )
47 private function validateTemplateData( $data, $key ) {
49 if ( $value === null ) {
50 // Cannot validate a null value
52 } elseif ( is_array( $value ) ) {
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 );
64 $this->assertStringStartsWith(
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, '<' ) ) {
74 str_starts_with( $key, 'html-' ) ||
$key === 'html',
75 "Template data containing HTML must be prefixed with `html-` ($key)"
78 } elseif ( is_bool( $value ) ) {
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 ) ) {
85 str_starts_with( $key, 'number-' ),
86 "Template data containing numbers must be prefixed with `number-` ($key)"
90 "Keys must be primitives e.g. arrays OR strings OR bools OR null ($key)."
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( [
113 'templateDirectory' => __DIR__
,
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 );