From f94b400474404bbb707cd7e67f1915bf202c8f04 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Fri, 8 Nov 2019 15:46:52 +0100 Subject: [PATCH] Make most existing Cite tests pure unit tests 1. Most existing CiteTests can be unit tests. They run so much faster this way. 2. I modified some test cases to cover all trim() in the code. 3. The strict type hint in CiteHooks is removed because the parameter is not used. Having a hard type hint for what is effectively dead code makes the code more brittle for changes done outside of this codebase. Change-Id: I1bff1d6e02d9ef17d5e6b66aeec3ee42bba99cf4 --- includes/CiteHooks.php | 2 +- tests/phpunit/CiteTest.php | 65 +------------------------ tests/phpunit/unit/CiteTest.php | 84 +++++++++++++++++++++++++++++++++ 3 files changed, 86 insertions(+), 65 deletions(-) create mode 100644 tests/phpunit/unit/CiteTest.php diff --git a/includes/CiteHooks.php b/includes/CiteHooks.php index d458202c8..9d5eb7f54 100644 --- a/includes/CiteHooks.php +++ b/includes/CiteHooks.php @@ -305,7 +305,7 @@ class CiteHooks { * @param ApiQuerySiteinfo $api * @param array &$data */ - public static function onAPIQuerySiteInfoGeneralInfo( ApiQuerySiteinfo $api, array &$data ) { + public static function onAPIQuerySiteInfoGeneralInfo( $api, array &$data ) { $config = MediaWikiServices::getInstance()->getConfigFactory()->makeConfig( 'cite' ); $data['citeresponsivereferences'] = $config->get( 'CiteResponsiveReferences' ); } diff --git a/tests/phpunit/CiteTest.php b/tests/phpunit/CiteTest.php index 13166f28e..f1a752e5a 100644 --- a/tests/phpunit/CiteTest.php +++ b/tests/phpunit/CiteTest.php @@ -7,7 +7,6 @@ use Parser; use ParserOutput; use PPFrame; use StripState; -use Wikimedia\TestingAccessWrapper; /** * @coversDefaultClass \Cite @@ -19,75 +18,13 @@ class CiteTest extends \MediaWikiIntegrationTestCase { $this->setMwGlobals( [ 'wgCiteBookReferencing' => true, - 'wgFragmentMode' => [ 'html5' ], ] ); } - /** - * @covers ::refArg - * @dataProvider provideRefAttributes - */ - public function testRefArg( array $attributes, array $expected ) { - /** @var Cite $cite */ - $cite = TestingAccessWrapper::newFromObject( new Cite() ); - $this->assertSame( $expected, $cite->refArg( $attributes ) ); - } - - public function provideRefAttributes() { - return [ - [ [], [ null, null, false, null, null ] ], - - // One attribute only - [ [ 'dir' => 'invalid' ], [ null, null, false, '', null ] ], - [ [ 'dir' => 'rtl' ], [ null, null, false, ' class="mw-cite-dir-rtl"', null ] ], - [ [ 'follow' => 'f' ], [ null, null, 'f', null, null ] ], - [ [ 'group' => 'g' ], [ null, 'g', null, null, null ] ], - [ [ 'invalid' => 'i' ], [ false, false, false, false, false ] ], - [ [ 'name' => 'n' ], [ 'n', null, null, null, null ] ], - [ [ 'name' => null ], [ false, false, false, false, false ] ], - [ [ 'extends' => 'e' ], [ null, null, null, null, 'e' ] ], - - // Pairs - [ [ 'follow' => 'f', 'name' => 'n' ], [ false, false, false, false, false ] ], - [ [ 'follow' => null, 'name' => null ], [ false, false, false, false, false ] ], - [ [ 'follow' => 'f', 'extends' => 'e' ], [ false, false, false, false, false ] ], - [ [ 'group' => 'g', 'name' => 'n' ], [ 'n', 'g', null, null, null ] ], - - // Combinations of 3 or more attributes - [ - [ 'group' => 'g', 'name' => 'n', 'extends' => 'e', 'dir' => 'rtl' ], - [ 'n', 'g', null, ' class="mw-cite-dir-rtl"', 'e' ] - ], - ]; - } - - /** - * @covers ::normalizeKey - * @dataProvider provideKeyNormalizations - */ - public function testNormalizeKey( $key, $expected ) { - /** @var Cite $cite */ - $cite = TestingAccessWrapper::newFromObject( new Cite() ); - $this->assertSame( $expected, $cite->normalizeKey( $key ) ); - } - - public function provideKeyNormalizations() { - return [ - [ 'a b', 'a_b' ], - [ 'a __ b', 'a_b' ], - [ ':', ':' ], - [ "\t\n", ' ' ], - [ "'", ''' ], - [ "''", '''' ], - [ '"%&/<>?[]{|}', '"%&/<>?[]{|}' ], - [ 'ISBN', 'ISBN' ], - ]; - } - /** * @covers ::guardedRef */ - public function testRef_pageProperty() { + public function testBookExtendsPageProperty() { $mockOutput = $this->createMock( ParserOutput::class ); $mockPPframe = $this->createMock( PPFrame::class ); $mockParser = $this->createMock( Parser::class ); diff --git a/tests/phpunit/unit/CiteTest.php b/tests/phpunit/unit/CiteTest.php new file mode 100644 index 000000000..543818762 --- /dev/null +++ b/tests/phpunit/unit/CiteTest.php @@ -0,0 +1,84 @@ +assertSame( $expected, $cite->refArg( $attributes ) ); + } + + public function provideRefAttributes() { + return [ + [ [], [ null, null, false, null, null ] ], + + // One attribute only + [ [ 'dir' => 'invalid' ], [ null, null, false, '', null ] ], + [ [ 'dir' => 'rtl' ], [ null, null, false, ' class="mw-cite-dir-rtl"', null ] ], + [ [ 'follow' => ' f ' ], [ null, null, 'f', null, null ] ], + // FIXME: Unlike all other attributes, group isn't trimmed. Why? + [ [ 'group' => ' g ' ], [ null, ' g ', null, null, null ] ], + [ [ 'invalid' => 'i' ], [ false, false, false, false, false ] ], + [ [ 'invalid' => null ], [ false, false, false, false, false ] ], + [ [ 'name' => ' n ' ], [ 'n', null, null, null, null ] ], + [ [ 'name' => null ], [ false, false, false, false, false ] ], + [ [ 'extends' => ' e ' ], [ null, null, null, null, 'e' ] ], + + // Pairs + [ [ 'follow' => 'f', 'name' => 'n' ], [ false, false, false, false, false ] ], + [ [ 'follow' => null, 'name' => null ], [ false, false, false, false, false ] ], + [ [ 'follow' => 'f', 'extends' => 'e' ], [ false, false, false, false, false ] ], + [ [ 'group' => 'g', 'name' => 'n' ], [ 'n', 'g', null, null, null ] ], + + // Combinations of 3 or more attributes + [ + [ 'group' => 'g', 'name' => 'n', 'extends' => 'e', 'dir' => 'rtl' ], + [ 'n', 'g', null, ' class="mw-cite-dir-rtl"', 'e' ] + ], + ]; + } + + /** + * @covers ::normalizeKey + * @dataProvider provideKeyNormalizations + */ + public function testNormalizeKey( $key, $expected ) { + /** @var Cite $cite */ + $cite = TestingAccessWrapper::newFromObject( new Cite() ); + $this->assertSame( $expected, $cite->normalizeKey( $key ) ); + } + + public function provideKeyNormalizations() { + return [ + [ 'a b', 'a_b' ], + [ 'a __ b', 'a_b' ], + [ ':', ':' ], + [ "\t\n", ' ' ], + [ "'", ''' ], + [ "''", '''' ], + [ '"%&/<>?[]{|}', '"%&/<>?[]{|}' ], + [ 'ISBN', 'ISBN' ], + ]; + } + +}