2014-08-09 12:36:35 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
|
|
class SkinFactoryTest extends MediaWikiTestCase {
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers SkinFactory::register
|
|
|
|
|
*/
|
|
|
|
|
public function testRegister() {
|
|
|
|
|
$factory = new SkinFactory();
|
2014-08-13 17:59:03 +00:00
|
|
|
$factory->register( 'fallback', 'Fallback', function () {
|
2014-08-09 12:36:35 +00:00
|
|
|
return new SkinFallback();
|
|
|
|
|
} );
|
|
|
|
|
$this->assertTrue( true ); // No exception thrown
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException( InvalidArgumentException::class );
|
2014-08-09 12:36:35 +00:00
|
|
|
$factory->register( 'invalid', 'Invalid', 'Invalid callback' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers SkinFactory::makeSkin
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeSkinWithNoBuilders() {
|
|
|
|
|
$factory = new SkinFactory();
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException( SkinException::class );
|
2014-08-09 12:36:35 +00:00
|
|
|
$factory->makeSkin( 'nobuilderregistered' );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers SkinFactory::makeSkin
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeSkinWithInvalidCallback() {
|
|
|
|
|
$factory = new SkinFactory();
|
|
|
|
|
$factory->register( 'unittest', 'Unittest', function () {
|
|
|
|
|
return true; // Not a Skin object
|
|
|
|
|
} );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->setExpectedException( UnexpectedValueException::class );
|
2014-08-09 12:36:35 +00:00
|
|
|
$factory->makeSkin( 'unittest' );
|
|
|
|
|
}
|
2014-08-20 07:26:50 +00:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers SkinFactory::makeSkin
|
|
|
|
|
*/
|
|
|
|
|
public function testMakeSkinWithValidCallback() {
|
|
|
|
|
$factory = new SkinFactory();
|
|
|
|
|
$factory->register( 'testfallback', 'TestFallback', function () {
|
|
|
|
|
return new SkinFallback();
|
|
|
|
|
} );
|
|
|
|
|
|
|
|
|
|
$skin = $factory->makeSkin( 'testfallback' );
|
2018-01-13 00:02:09 +00:00
|
|
|
$this->assertInstanceOf( Skin::class, $skin );
|
|
|
|
|
$this->assertInstanceOf( SkinFallback::class, $skin );
|
Skin: Make skins aware of their registered skin name
Remove the need for skin classes to have a hardcoded string as
skinname property value. This previously created the possibility
for the value to not match the skinname in the SkinFactory registry,
which creates confusing situations where message keys and load.php
urls are crafted with the internal skinname, but all other
handling (useskin, preferences, hooks, SkinFactory, ResourceLoader,
etc.) operate on the names in the registry.
We could enforce the matching by requiring a 1:1 relationship between
skinnames and Skin sub classes, but that is not backwards-compatible
with the 1:many map that wgValidSkinNames provides, and not compatible
SkinFactory either, which supports a factory function to return an
object. This makes a lot of sense and allows Skin-classees to be
re-used and composed with injection. If we do want to enforce 1:1,
we could validate it with a structure PHPUnit test, but instead this
change just uses the injected name from the constructor (passed by
ServiceWiring, previously unused).
The added unit test shows the new behaviour. Before this change,
getSkinName() on SkinFallback would always return 'fallback',
whereas now each instance of the class adheres to the registered
name (if it differs from the default).
Update the two direct uses of protected $skin->skinname to use
$skin->getSkinName() instead to enable sub-classes to optionally
implement an alternate source for the self-name (or to hardcode
it there as before).
Bug: T173546
Change-Id: I4383dcc3094da6e3c9ac12dc6c9311128db9db6e
2017-08-25 21:25:57 +00:00
|
|
|
$this->assertEquals( 'fallback', $skin->getSkinName() );
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2017-10-15 17:36:53 +00:00
|
|
|
* @covers Skin::__construct
|
Skin: Make skins aware of their registered skin name
Remove the need for skin classes to have a hardcoded string as
skinname property value. This previously created the possibility
for the value to not match the skinname in the SkinFactory registry,
which creates confusing situations where message keys and load.php
urls are crafted with the internal skinname, but all other
handling (useskin, preferences, hooks, SkinFactory, ResourceLoader,
etc.) operate on the names in the registry.
We could enforce the matching by requiring a 1:1 relationship between
skinnames and Skin sub classes, but that is not backwards-compatible
with the 1:many map that wgValidSkinNames provides, and not compatible
SkinFactory either, which supports a factory function to return an
object. This makes a lot of sense and allows Skin-classees to be
re-used and composed with injection. If we do want to enforce 1:1,
we could validate it with a structure PHPUnit test, but instead this
change just uses the injected name from the constructor (passed by
ServiceWiring, previously unused).
The added unit test shows the new behaviour. Before this change,
getSkinName() on SkinFallback would always return 'fallback',
whereas now each instance of the class adheres to the registered
name (if it differs from the default).
Update the two direct uses of protected $skin->skinname to use
$skin->getSkinName() instead to enable sub-classes to optionally
implement an alternate source for the self-name (or to hardcode
it there as before).
Bug: T173546
Change-Id: I4383dcc3094da6e3c9ac12dc6c9311128db9db6e
2017-08-25 21:25:57 +00:00
|
|
|
* @covers Skin::getSkinName
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSkinName() {
|
|
|
|
|
$skin = new SkinFallback();
|
|
|
|
|
$this->assertEquals( 'fallback', $skin->getSkinName(), 'Default' );
|
|
|
|
|
$skin = new SkinFallback( 'testname' );
|
|
|
|
|
$this->assertEquals( 'testname', $skin->getSkinName(), 'Constructor argument' );
|
2014-08-20 07:26:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @covers SkinFactory::getSkinNames
|
|
|
|
|
*/
|
|
|
|
|
public function testGetSkinNames() {
|
|
|
|
|
$factory = new SkinFactory();
|
|
|
|
|
// A fake callback we can use that will never be called
|
2014-08-25 17:16:36 +00:00
|
|
|
$callback = function () {
|
|
|
|
|
// NOP
|
|
|
|
|
};
|
2014-08-20 07:26:50 +00:00
|
|
|
$factory->register( 'skin1', 'Skin1', $callback );
|
|
|
|
|
$factory->register( 'skin2', 'Skin2', $callback );
|
|
|
|
|
$names = $factory->getSkinNames();
|
|
|
|
|
$this->assertArrayHasKey( 'skin1', $names );
|
|
|
|
|
$this->assertArrayHasKey( 'skin2', $names );
|
|
|
|
|
$this->assertEquals( 'Skin1', $names['skin1'] );
|
|
|
|
|
$this->assertEquals( 'Skin2', $names['skin2'] );
|
|
|
|
|
}
|
2014-08-09 12:36:35 +00:00
|
|
|
}
|