Start on PHPUnit tests

Why:
* The WikimediaMessages extension currently has no tests.
* This is not ideal, considering that the extension is deployed on
  WMF production and can be run on every page load (such as
  through the ::onBeforePageDisplay hook).
* Testing the extension makes the code easier to maintain
  and more reliable to regressions.

Why:
* Add some unit tests that cover a few easy to cover areas of
  Hooks.php.
* This is done separately to make it easier to see the coverage
  change in future patches.

Bug: T315774
Change-Id: I916668ae0375e8668b09a16111d83bf2e2feee40
这个提交包含在:
Dreamy Jazz 2024-05-04 20:20:32 +03:00 提交者 Dreamy Jazz
父节点 bfc492e36e
当前提交 94be0327e4
共有 1 个文件被更改,包括 60 次插入0 次删除

查看文件

@ -0,0 +1,60 @@
<?php
namespace MediaWiki\Extension\WikimediaMessages\Tests\Unit;
use MediaWiki\Config\HashConfig;
use MediaWiki\Extension\WikimediaMessages\Hooks;
use MediaWiki\Output\OutputPage;
use MediaWiki\Tests\Unit\MockServiceDependenciesTrait;
use MediaWikiUnitTestCase;
use Skin;
use Title;
/**
* @covers \MediaWiki\Extension\WikimediaMessages\Hooks
*/
class HooksTest extends MediaWikiUnitTestCase {
use MockServiceDependenciesTrait;
/** @dataProvider provideOnBeforePageDisplay */
public function testOnBeforePageDisplay( $wikimediaStyleSkins, $currentSkin, $expectedModuleStyles ) {
/** @var Hooks $hooks */
$hooks = $this->newServiceInstance( Hooks::class, [] );
// Make Skin::getSkinName return $currentSkin
$skin = $this->createMock( Skin::class );
$skin->method( 'getSkinName' )
->willReturn( $currentSkin );
// Make OutputPage::getConfig return a HashConfig with wgWikimediaStylesSkins defined.
$out = $this->createMock( OutputPage::class );
$out->method( 'getConfig' )
->willReturn( new HashConfig( [ 'WikimediaStylesSkins' => $wikimediaStyleSkins ] ) );
// Assert that OutputPage::addModuleStyles is called with $expectedModuleStyles
$out->expects( $expectedModuleStyles !== [] ? $this->atLeastOnce() : $this->never() )
->method( 'addModuleStyles' )
->with( $expectedModuleStyles );
// Call ::onBeforePageDisplay with the mocks we just created.
$hooks->onBeforePageDisplay( $out, $skin );
}
public static function provideOnBeforePageDisplay() {
return [
'No skins are defined in config' => [ [], 'vector', [] ],
'Skin is not included in config' => [ [ 'vector' ], 'monobook', [] ],
'Skin is included in config' => [ [ 'vector' ], 'vector', [ 'ext.wikimediamessages.styles' ] ],
];
}
public function testOnSkinCopyrightFooterForHistoryType() {
// Calls ::onSkinCopyrightFooter with the $type argument as history and expect that it just returns without
// doing anything else. This is checked by ensuring the $msg and $link provided by reference is not modified.
/** @var Hooks $hooks */
$hooks = $this->newServiceInstance( Hooks::class, [] );
$msg = 'msg';
$link = 'link';
$hooks->onSkinCopyrightFooter( $this->createMock( Title::class ), 'history', $msg, $link );
// Check that the $msg and $link variables provided by reference were not changed.
$this->assertEquals( 'msg', $msg );
$this->assertEquals( 'link', $link );
}
}