Add tests for getTabsData and dependencies

I recently put up a change and was informed by jenkins-bot that I had
decreased PHP test coverage :/

As I would like to be the type of person that only  increases  test
coverage, I wrote the corresponding tests for my change, as well as the
member functions it depends on.  From what I can tell, this is the
correct method of writing unit tests, but please do let me know if there
is anything I'm missing!

Change-Id: Id5c4f76ae058d2b0d487245c28b9ccecc2deef8e
This commit is contained in:
Steph Toyofuku 2024-01-02 13:38:55 -08:00
parent 5c5255af9a
commit a0837cd3f5

View file

@ -45,6 +45,130 @@ class SkinMinervaTest extends MediaWikiIntegrationTestCase {
$this->setService( 'Minerva.SkinOptions', $mockOptions );
}
public static function provideHasPageActions() {
return [
[ NS_MAIN, 'test', 'view', true ],
[ NS_SPECIAL, 'test', 'view', false ],
[ NS_MAIN, 'Main Page', 'view', false ],
[ NS_MAIN, 'test', 'history', false ]
];
}
/**
* @dataProvider provideHasPageActions
* @covers ::hasPageActions
*/
public function testHasPageActions( int $namespace, string $title, string $action, bool $expected ) {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( $namespace, $title ) );
$context->setActionName( $action );
$skin = new SkinMinerva();
$skin->setContext( $context );
$this->assertEquals( $expected, TestingAccessWrapper::newFromObject( $skin )->hasPageActions() );
}
public static function provideHasPageTabs() {
return [
[ [ SkinOptions::TABS_ON_SPECIALS => false ], NS_MAIN, 'test', 'view', true ],
[ [ SkinOptions::TABS_ON_SPECIALS => false ], NS_MAIN, 'Main Page', 'view', false ],
[ [ SkinOptions::TABS_ON_SPECIALS => false ], NS_TALK, 'Main Page', 'view', false ],
[ [ SkinOptions::TALK_AT_TOP => false ], NS_MAIN, 'test', 'view', false ],
[ [ SkinOptions::TALK_AT_TOP => false ], NS_SPECIAL, 'test', 'view', true ],
[ [ SkinOptions::TALK_AT_TOP => false ], NS_MAIN, 'test', 'history', true ],
];
}
/**
* @dataProvider provideHasPageTabs
* @covers ::hasPageTabs
*/
public function testHasPageTabs( array $options, int $namespace, string $title, string $action, bool $expected ) {
// both tabs on specials and talk at top default to true
$this->overrideSkinOptions( $options );
$context = new RequestContext();
$context->setTitle( Title::makeTitle( $namespace, $title ) );
$context->setActionName( $action );
// hasPageTabs gets the action directly from the request rather than the context so we set it here as well
$context->getRequest()->setVal( 'action', $action );
$skin = new SkinMinerva();
$skin->setContext( $context );
$this->assertEquals( $expected, TestingAccessWrapper::newFromObject( $skin )->hasPageTabs() );
}
/**
* @covers ::getTabsData
*/
public function testGetTabsData() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [ 'associated-pages' => [ 'testkey' => 'testvalue' ] ];
$associatedPages = [ 'id' => 'test' ];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [ 'items' => [ 'testvalue' ], 'id' => 'test' ], $data );
}
/**
* @covers ::getTabsData when hasPageTabs is false
*/
public function testGetTabsDataNoPageTabs() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'Main Page' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [ 'associated-pages' => [ 'testkey' => 'testvalue' ] ];
$associatedPages = [ 'id' => 'test' ];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [], $data );
}
/**
* @covers ::getTabsData when contentNavigationUrls is empty
*/
public function testGetTabsDataNoContentNavigationUrls() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [];
$associatedPages = [ 'id' => 'test' ];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [], $data );
}
/**
* @covers ::getTabsData when associatedPages has no id
*/
public function testGetTabsDataNoId() {
$context = new RequestContext();
$context->setTitle( Title::makeTitle( NS_MAIN, 'test' ) );
$skin = new SkinMinerva();
$skin->setContext( $context );
$contentNavigationUrls = [ 'associated-pages' => [ 'testkey' => 'testvalue' ] ];
$associatedPages = [];
$data = TestingAccessWrapper::newFromObject( $skin )->getTabsData( $contentNavigationUrls, $associatedPages );
$this->assertEquals( [ 'items' => [ 'testvalue' ], 'id' => null ], $data );
}
/**
* @covers ::setContext
* @covers ::hasCategoryLinks