wiki.techinc.nl/tests/phpunit/unit/includes/skins/BaseTemplateTest.php
Derick Alangi caf0591902 skins: Correctly index the copyright icon, caused failure in MF
On mobile and possibly desktop via (`$wgFooterIcons`), trying to acccess
the copyright icon returns an array instead of a string. An exception is thrown
since the indexing is done on a string.

Throwing an error: TypeError: Return value of BaseTemplate::getCopyrightIconHTML()
must be of the type string, array returned.

The returned array looks like this (when I test):

["copyright"]=> array(1) {
    ["copyright"]=> array(3) {
        ["url"]=> string(47) "https://creativecommons.org/licenses/by-sa/4.0/"
        ["src"]=> string(44) "/core/resources/assets/licenses/cc-by-sa.png"
        ["alt"]=> string(39) "Creative Commons Attribution-ShareAlike"
    }
}

So getting the icon, we need to index src, hence the patch. Let me know
if I'm wrong here. Also, have a look [[Manual:$wgFooterIcons]], you'll see that
default value is an empty array instead of a string:

$wgFooterIcons = [
	"copyright" => [
		"copyright" => [],
	],
]

This made MF to not be able to load locally and the patch fixes it.

In addition, we can just pass `$config` as we have above rather than request
the for it again and again.

Bug: T291325
Change-Id: Icb42342e83f2bc61922ab991bcec66aa5e7b5646
2021-09-20 23:19:23 +01:00

84 lines
2.2 KiB
PHP

<?php
/**
* @covers BaseTemplate
*
* @license GPL-2.0-or-later
*/
class BaseTemplateTest extends MediaWikiUnitTestCase {
public function provideGetCopyrightIconHTML() {
return [
[
[
'RightsIcon' => '',
'FooterIcons' => [],
],
'',
'Returns empty string when no configuration'
],
// When $wgFooterIcons['copyright']['copyright'] returns an array (T291325)
[
[
'FooterIcons' => [
'copyright' => [
'copyright' => [
'src' => 'footer-copyright.png',
'url' => 'https://t.ui',
'alt' => 'Alt text',
]
]
],
'RightsIcon' => 'copyright.png',
'RightsUrl' => 'https://',
'RightsText' => 'copyright'
],
'<a href="https://t.ui"><img src="footer-copyright.png" alt="Alt text" width="88" height="31" /></a>',
'Reads from FooterIcons first'
],
// When $wgFooterIcons['copyright']['copyright'] returns a string (T291325)
[
[
'FooterIcons' => [
'copyright' => [
'copyright' => 'footer-copyright.png'
]
]
],
'<img src="footer-copyright.png" width="88" height="31" />',
'Reads from FooterIcons first'
],
[
[
'FooterIcons' => [],
'RightsIcon' => 'copyright.png',
'RightsUrl' => 'https://',
'RightsText' => 'copyright'
],
'<a href="https://"><img src="copyright.png" alt="copyright" width="88" height="31" /></a>',
'Copyright can be created from Rights- prefixed config variables.'
],
[
[
'FooterIcons' => [],
'RightsIcon' => 'copyright.png',
'RightsUrl' => '',
'RightsText' => 'copyright'
],
'<img src="copyright.png" alt="copyright" width="88" height="31" />',
'$wgRightsUrl is optional.'
],
];
}
/**
* @covers BaseTemplate::getCopyrightIconHTML
* @dataProvider provideGetCopyrightIconHTML
*/
public function testGetCopyrightIconHTML( $globals, $expected, $msg ) {
$mockSkin = $this->createMock( Skin::class );
$mockSkin->method( 'makeFooterIcon' )->willReturn( $expected );
$hashConfig = new HashConfig( $globals );
$icon = BaseTemplate::getCopyrightIconHTML( $hashConfig, $mockSkin );
$this->assertEquals( $expected, $icon, $msg );
}
}