2020-05-29 23:00:19 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers SkinMustache
|
|
|
|
|
*
|
|
|
|
|
* @group Output
|
|
|
|
|
*/
|
2020-06-30 15:09:24 +00:00
|
|
|
class SkinMustacheTest extends MediaWikiIntegrationTestCase {
|
2020-05-29 23:00:19 +00:00
|
|
|
|
|
|
|
|
/**
|
2021-01-16 19:44:17 +00:00
|
|
|
* @param string $html
|
|
|
|
|
* @param Title $title
|
2020-05-29 23:00:19 +00:00
|
|
|
* @return MockObject|OutputPage
|
|
|
|
|
*/
|
|
|
|
|
private function getMockOutputPage( $html, $title ) {
|
|
|
|
|
$mockContentSecurityPolicy = $this->getMockBuilder( ContentSecurityPolicy::class )
|
|
|
|
|
->disableOriginalConstructor()
|
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
|
|
$mockContentSecurityPolicy->method( 'getNonce' )
|
|
|
|
|
->willReturn( 'secret' );
|
|
|
|
|
|
|
|
|
|
$mock = $this->createMock( OutputPage::class );
|
|
|
|
|
$mock->method( 'getHTML' )
|
|
|
|
|
->willReturn( $html );
|
2020-06-15 18:47:06 +00:00
|
|
|
$mock->method( 'getIndicators' )
|
|
|
|
|
->willReturn( [
|
|
|
|
|
'id' => '<a>indicator</a>'
|
|
|
|
|
] );
|
2020-05-29 23:00:19 +00:00
|
|
|
$mock->method( 'getTitle' )
|
|
|
|
|
->willReturn( $title );
|
|
|
|
|
$mock->method( 'getIndicators' )
|
|
|
|
|
->willReturn( '' );
|
|
|
|
|
$mock->method( 'getLanguageLinks' )
|
|
|
|
|
->willReturn( [] );
|
|
|
|
|
$mock->method( 'getCSP' )
|
|
|
|
|
->willReturn( $mockContentSecurityPolicy );
|
|
|
|
|
return $mock;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private function validateTemplateData( $data, $key ) {
|
|
|
|
|
$value = $data[$key];
|
|
|
|
|
if ( $value === null ) {
|
|
|
|
|
// Cannot validate a null value
|
|
|
|
|
return;
|
|
|
|
|
} elseif ( is_array( $value ) ) {
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
strpos( $key, 'data-' ) === 0 || strpos( $key, 'array-' ) === 0,
|
|
|
|
|
"Template data that is an object should be associated with a key" .
|
|
|
|
|
" prefixed with `data-` or `array-` ($key)"
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
// Validate the children
|
|
|
|
|
foreach ( $value as $childKey => $childValue ) {
|
|
|
|
|
if ( is_string( $childKey ) ) {
|
|
|
|
|
$this->validateTemplateData( $value, $childKey );
|
|
|
|
|
} else {
|
|
|
|
|
$this->assertStringStartsWith(
|
|
|
|
|
'array-',
|
2020-06-15 18:47:06 +00:00
|
|
|
$key,
|
2020-05-29 23:00:19 +00:00
|
|
|
"Template data that is a flat array should be associated with a key prefixed `array-` ($key)"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} elseif ( is_string( $value ) ) {
|
|
|
|
|
if ( strpos( $value, '<' ) !== false ) {
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
strpos( $key, 'html-' ) === 0 || $key === 'html',
|
|
|
|
|
"Template data containing HTML must be prefixed with `html-` ($key)"
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
} elseif ( is_bool( $value ) ) {
|
|
|
|
|
$this->assertTrue(
|
|
|
|
|
strpos( $key, 'is-' ) === 0 || strpos( $key, 'has-' ) === 0,
|
|
|
|
|
"Template data containing booleans must be prefixed with `is-` or `has-` ($key)"
|
|
|
|
|
);
|
|
|
|
|
} else {
|
|
|
|
|
$this->fail(
|
|
|
|
|
"Keys must be primitives e.g. arrays OR strings OR bools OR null ($key)."
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2021-08-18 23:48:01 +00:00
|
|
|
* @covers SkinTemplate::getTemplateData
|
|
|
|
|
* @covers SkinTemplate::buildSearchProps
|
2020-05-29 23:00:19 +00:00
|
|
|
*/
|
|
|
|
|
public function testGetTemplateData() {
|
|
|
|
|
$config = new HashConfig();
|
|
|
|
|
$bodytext = '<p>hello</p>';
|
|
|
|
|
$context = new RequestContext();
|
|
|
|
|
$title = Title::newFromText( 'Mustache skin' );
|
|
|
|
|
$context->setTitle( $title );
|
|
|
|
|
$out = $this->getMockOutputPage( $bodytext, $title );
|
|
|
|
|
$context->setOutput( $out );
|
|
|
|
|
$skin = new SkinMustache( [
|
|
|
|
|
'name' => 'test',
|
|
|
|
|
'templateDirectory' => __DIR__,
|
|
|
|
|
] );
|
|
|
|
|
$skin->setContext( $context );
|
|
|
|
|
$data = $skin->getTemplateData();
|
|
|
|
|
|
|
|
|
|
// Validate the default template data respects the naming rules
|
|
|
|
|
foreach ( array_keys( $data ) as $key ) {
|
|
|
|
|
$this->validateTemplateData( $data, $key );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Validate search data
|
|
|
|
|
$searchData = $data['data-search-box'];
|
|
|
|
|
foreach ( array_keys( $searchData ) as $key ) {
|
|
|
|
|
$this->validateTemplateData( $searchData, $key );
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|