From 7d1b74d8c74caf23aa22a751988f37cc77d31bb8 Mon Sep 17 00:00:00 2001 From: Max Semenik Date: Mon, 22 Jul 2019 01:34:21 -0700 Subject: [PATCH] Split tests to unit and integration Change-Id: Ib703086c38b6b1ca13f37504c5b6eab6834694b8 --- tests/phpunit/integration/GadgetHooksTest.php | 61 +++++++++++++++ tests/phpunit/{ => unit}/GadgetTest.php | 74 ++++--------------- 2 files changed, 77 insertions(+), 58 deletions(-) create mode 100644 tests/phpunit/integration/GadgetHooksTest.php rename tests/phpunit/{ => unit}/GadgetTest.php (77%) diff --git a/tests/phpunit/integration/GadgetHooksTest.php b/tests/phpunit/integration/GadgetHooksTest.php new file mode 100644 index 00000000..a6e7dca6 --- /dev/null +++ b/tests/phpunit/integration/GadgetHooksTest.php @@ -0,0 +1,61 @@ + true, + ]; + $this->user = $this->getTestUser( [ 'unittesters' ] )->getUser(); + } + + public function tearDown() { + GadgetRepo::setSingleton(); + parent::tearDown(); + } + + /** + * @covers Gadget + * @covers GadgetHooks::getPreferences + * @covers GadgetRepo + * @covers MediaWikiGadgetsDefinitionRepo + */ + public function testPreferences() { + $prefs = []; + $repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() ); + // Force usage of a MediaWikiGadgetsDefinitionRepo + GadgetRepo::setSingleton( $repo ); + + /** @var MediaWikiGadgetsDefinitionRepo $repo */ + $gadgets = $repo->fetchStructuredList( '* foo | foo.js +==keep-section1== +* bar| bar.js +==remove-section== +* baz [rights=embezzle] |baz.js +==keep-section2== +* quux [rights=test] | quux.js' ); + $this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" ); + + $repo->definitionCache = $gadgets; + GadgetHooks::getPreferences( $this->user, $prefs ); + + $options = $prefs['gadgets']['options']; + $this->assertArrayNotHasKey( '⧼gadget-section-remove-section⧽', $options, + 'Must not show empty sections' ); + $this->assertArrayHasKey( '⧼gadget-section-keep-section1⧽', $options ); + $this->assertArrayHasKey( '⧼gadget-section-keep-section2⧽', $options ); + } +} diff --git a/tests/phpunit/GadgetTest.php b/tests/phpunit/unit/GadgetTest.php similarity index 77% rename from tests/phpunit/GadgetTest.php rename to tests/phpunit/unit/GadgetTest.php index d478ae17..b1a033bc 100644 --- a/tests/phpunit/GadgetTest.php +++ b/tests/phpunit/unit/GadgetTest.php @@ -5,28 +5,7 @@ use Wikimedia\TestingAccessWrapper; /** * @group Gadgets */ -class GadgetTest extends MediaWikiTestCase { - /** - * @var User - */ - protected $user; - - public function setUp() { - global $wgGroupPermissions; - - parent::setUp(); - - $wgGroupPermissions['unittesters'] = [ - 'test' => true, - ]; - $this->user = $this->getTestUser( [ 'unittesters' ] )->getUser(); - } - - public function tearDown() { - GadgetRepo::setSingleton(); - parent::tearDown(); - } - +class GadgetTest extends MediaWikiUnitTestCase { /** * @param string $line * @return Gadget @@ -88,13 +67,24 @@ class GadgetTest extends MediaWikiTestCase { * @covers MediaWikiGadgetsDefinitionRepo::newFromDefinition * @covers Gadget::isAllowed */ - public function testisAllowed() { + public function testIsAllowed() { + $user = $this->getMockBuilder( User::class ) + ->setMethods( [ 'isAllowedAll' ] ) + ->getMock(); + $user->method( 'isAllowedAll' ) + ->willReturnCallback( + function ( ...$rights ) { + return array_diff( $rights, [ 'test' ] ) === []; + } + ); + + /** @var User $user */ $gUnset = $this->create( '*foo[ResourceLoader]|foo.js' ); $gAllowed = $this->create( '*bar[ResourceLoader|rights=test]|bar.js' ); $gNotAllowed = $this->create( '*baz[ResourceLoader|rights=nope]|baz.js' ); - $this->assertTrue( $gUnset->isAllowed( $this->user ) ); - $this->assertTrue( $gAllowed->isAllowed( $this->user ) ); - $this->assertFalse( $gNotAllowed->isAllowed( $this->user ) ); + $this->assertTrue( $gUnset->isAllowed( $user ) ); + $this->assertTrue( $gAllowed->isAllowed( $user ) ); + $this->assertFalse( $gNotAllowed->isAllowed( $user ) ); } /** @@ -213,36 +203,4 @@ class GadgetTest extends MediaWikiTestCase { $g = $this->create( '* foo[ResourceLoader]|bar.js' ); $this->assertFalse( $g->isHidden() ); } - - /** - * @covers Gadget - * @covers GadgetHooks::getPreferences - * @covers GadgetRepo - * @covers MediaWikiGadgetsDefinitionRepo - */ - public function testPreferences() { - $prefs = []; - $repo = TestingAccessWrapper::newFromObject( new MediaWikiGadgetsDefinitionRepo() ); - // Force usage of a MediaWikiGadgetsDefinitionRepo - GadgetRepo::setSingleton( $repo ); - - /** @var MediaWikiGadgetsDefinitionRepo $repo */ - $gadgets = $repo->fetchStructuredList( '* foo | foo.js -==keep-section1== -* bar| bar.js -==remove-section== -* baz [rights=embezzle] |baz.js -==keep-section2== -* quux [rights=test] | quux.js' ); - $this->assertGreaterThanOrEqual( 2, count( $gadgets ), "Gadget list parsed" ); - - $repo->definitionCache = $gadgets; - GadgetHooks::getPreferences( $this->user, $prefs ); - - $options = $prefs['gadgets']['options']; - $this->assertArrayNotHasKey( '⧼gadget-section-remove-section⧽', $options, - 'Must not show empty sections' ); - $this->assertArrayHasKey( '⧼gadget-section-keep-section1⧽', $options ); - $this->assertArrayHasKey( '⧼gadget-section-keep-section2⧽', $options ); - } }