Mglaser has uploaded a new change for review. ( https://gerrit.wikimedia.org/r/358013 )
Change subject: ApiUnitTests: Added simplified fixtures for users ...................................................................... ApiUnitTests: Added simplified fixtures for users ... and pages This should ease development of tests that require more than one page and three users (as it is default in ApiTestCase). Change-Id: I5af8587aac6b5f6f402b1b73203cedf24041aa03 --- M extension.json M tests/BSApiExtJSStoreTestBase.php M tests/BSApiTasksTestBase.php A tests/BSApiTestCase.php A tests/BSFixturesProvider.php A tests/BSFixturesTest.php A tests/BSPageFixtures.php M tests/BSPageFixturesProvider.php A tests/BSUserFixtures.php A tests/BSUserFixturesProvider.php A tests/data/users.json 11 files changed, 178 insertions(+), 4 deletions(-) git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceFoundation refs/changes/13/358013/1 diff --git a/extension.json b/extension.json index 4f560ba..c8325da 100644 --- a/extension.json +++ b/extension.json @@ -562,7 +562,12 @@ "BSEntityConfig": "includes/entityconfigs/EntityConfig.php", "BSEntityContent": "includes/content/EntityContent.php", "BSEntityContentHandler": "includes/content/EntityContentHandler.php", + "BSApiTestCase": "tests/BSApiTestCase.php", + "BSFixturesProvider": "tests/BSFixturesProvider.php", "BSPageFixturesProvider": "tests/BSPageFixturesProvider.php", + "BSUserFixturesProvider": "tests/BSUserFixturesProvider.php", + "BSPageFixtures": "tests/BSPageFixtures.php", + "BSUserFixtures": "tests/BSUserFixtures.php", "BSApiExtJSStoreTestBase": "tests/BSApiExtJSStoreTestBase.php", "BSApiTasksTestBase": "tests/BSApiTasksTestBase.php", "BSTemplateHelper": "includes/TemplateHelper.php", diff --git a/tests/BSApiExtJSStoreTestBase.php b/tests/BSApiExtJSStoreTestBase.php index e4627a8..a9dfb84 100644 --- a/tests/BSApiExtJSStoreTestBase.php +++ b/tests/BSApiExtJSStoreTestBase.php @@ -4,7 +4,7 @@ * * Class BSApiExtJSStoreTestBase */ -abstract class BSApiExtJSStoreTestBase extends ApiTestCase { +abstract class BSApiExtJSStoreTestBase extends BSApiTestCase { protected $iFixtureTotal = 0; protected $sQuery = ''; diff --git a/tests/BSApiTasksTestBase.php b/tests/BSApiTasksTestBase.php index 63fdb60..90f4b62 100644 --- a/tests/BSApiTasksTestBase.php +++ b/tests/BSApiTasksTestBase.php @@ -1,6 +1,6 @@ <?php -abstract class BSApiTasksTestBase extends ApiTestCase { +abstract class BSApiTasksTestBase extends BSApiTestCase { abstract protected function getModuleName(); diff --git a/tests/BSApiTestCase.php b/tests/BSApiTestCase.php new file mode 100644 index 0000000..4445f8b --- /dev/null +++ b/tests/BSApiTestCase.php @@ -0,0 +1,37 @@ +<?php + +class BSApiTestCase extends ApiTestCase { + + /** + * + * @var BSUserFixtures + */ + protected static $userFixtures = null; + + /** + * + * @param BSUserFixtures $userFixtures + */ + public function setUserFixture( $userFixtures ) { + static::$userFixtures = $userFixtures; + } + + protected function setUp() { + parent::setUp(); + + if( static::$userFixtures instanceof BSUserFixtures ) { + self::$users += static::$userFixtures->makeTestUsers(); + } + } + + /** + * Making this public so we cam make use of it from within a "*Fixtures" + * class + * @param string $pageName + * @param string $text + */ + public function insertPage($pageName, $text = 'Sample page for unit test.') { + return parent::insertPage($pageName, $text); + } +} + diff --git a/tests/BSFixturesProvider.php b/tests/BSFixturesProvider.php new file mode 100644 index 0000000..2d89afc --- /dev/null +++ b/tests/BSFixturesProvider.php @@ -0,0 +1,8 @@ +<?php + +interface BSFixturesProvider { + /** + * @return array[] + */ + public function getFixtureData(); +} \ No newline at end of file diff --git a/tests/BSFixturesTest.php b/tests/BSFixturesTest.php new file mode 100644 index 0000000..5518cdc --- /dev/null +++ b/tests/BSFixturesTest.php @@ -0,0 +1,33 @@ +<?php + +/** + * @group medium + * @group Database + * @group BlueSpice + * @group BlueSpiceFoundation + */ +class BSFixturesTest extends BSApiTestCase { + + public function setUp() { + parent::setUp(); + new BSPageFixtures( $this ); + new BSUserFixtures( $this ); + } + + public function testPageFixtures() { + $title = Title::newFromText( 'Template:Hello World' ); + $this->assertTrue( $title->exists(), 'Title should be known' ); + } + + public function testUserFixtures() { + $user = User::newFromName( 'Paul' ); + $this->assertFalse( $user->isAnon(), "User should be known" ); + + + $groups = $user->getGroups(); + + $this->assertTrue( in_array( 'A', $groups ), 'User should be in group A' ); + $this->assertFalse( in_array( 'B', $groups ), 'User should not be in group B' ); + $this->assertTrue( in_array( 'C', $groups ), 'User should be in group C' ); + } +} \ No newline at end of file diff --git a/tests/BSPageFixtures.php b/tests/BSPageFixtures.php new file mode 100644 index 0000000..50c6a4b --- /dev/null +++ b/tests/BSPageFixtures.php @@ -0,0 +1,20 @@ +<?php + +class BSPageFixtures { + + + /** + * + * @param BSApiTestCase $testcase + * @param BSFixturesProvider $provider + */ + public function __construct( $testcase, $provider = null ) { + if( $provider === null ) { + $provider = new BSPageFixturesProvider(); + } + + foreach ( $provider->getFixtureData() as $pageData ) { + $testcase->insertPage( $pageData[0], $pageData[1] ); + } + } +} \ No newline at end of file diff --git a/tests/BSPageFixturesProvider.php b/tests/BSPageFixturesProvider.php index 9779a75..5406143 100644 --- a/tests/BSPageFixturesProvider.php +++ b/tests/BSPageFixturesProvider.php @@ -1,12 +1,12 @@ <?php -class BSPageFixturesProvider { +class BSPageFixturesProvider implements BSFixturesProvider { /** * @return array[] */ public function getFixtureData() { - $oData = FormatJson::decode( file_get_contents( __DIR__."/data/pages.json" ) ); + $oData = FormatJson::decode( file_get_contents( __DIR__ . "/data/pages.json" ) ); return $oData->pages; } } \ No newline at end of file diff --git a/tests/BSUserFixtures.php b/tests/BSUserFixtures.php new file mode 100644 index 0000000..f998d51 --- /dev/null +++ b/tests/BSUserFixtures.php @@ -0,0 +1,51 @@ +<?php + +class BSUserFixtures { + + /** + * + * @var array[] + */ + protected $fixtureData = []; + + /** + * + * @param BSApiTestCase $testcase + * @param BSFixturesProvider $provider + */ + public function __construct( $testcase, $provider = null ) { + if( $provider === null ) { + $provider = new BSUserFixturesProvider(); + } + + //Register at testcase so 'makeTestUsers' can be called in each and + //every run of "setUp" + $testcase->setUserFixture( $this ); + + //Read in only once! + $this->fixtureData = $provider->getFixtureData(); + } + + /** + * + * @return \TestUser[] + */ + public function makeTestUsers() { + $users = []; + foreach( $this->fixtureData as $userData ) { + $user = new TestUser( + $userData[0], $userData[1], $userData[2], $userData[3] + ); + $key = $this->makeKey( $userData[0] ); + + $users[$key] = $user; + } + + return $users; + } + + protected function makeKey( $userName ) { + return "bs-user-".strtolower( $userName ); + } + +} \ No newline at end of file diff --git a/tests/BSUserFixturesProvider.php b/tests/BSUserFixturesProvider.php new file mode 100644 index 0000000..39657f1 --- /dev/null +++ b/tests/BSUserFixturesProvider.php @@ -0,0 +1,12 @@ +<?php + +class BSUserFixturesProvider implements BSFixturesProvider { + + /** + * @return array[] + */ + public function getFixtureData() { + $oData = FormatJson::decode( file_get_contents( __DIR__ . "/data/users.json" ) ); + return $oData->users; + } +} \ No newline at end of file diff --git a/tests/data/users.json b/tests/data/users.json new file mode 100644 index 0000000..62843bc --- /dev/null +++ b/tests/data/users.json @@ -0,0 +1,8 @@ +{ + "users" : [ + [ "John", "John L.", "j@example.doesnotexist", [ "A", "B" ] ], + [ "Paul", "Paul M.", "p@example.doesnotexist", [ "A", "C" ] ], + [ "George", "George H.", "g@example.doesnotexist", [ "C" ] ], + [ "Ringo", "Ringo S.", "r@example.doesnotexist", [ "D" ] ] + ] +} -- To view, visit https://gerrit.wikimedia.org/r/358013 To unsubscribe, visit https://gerrit.wikimedia.org/r/settings Gerrit-MessageType: newchange Gerrit-Change-Id: I5af8587aac6b5f6f402b1b73203cedf24041aa03 Gerrit-PatchSet: 1 Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation Gerrit-Branch: REL1_27 Gerrit-Owner: Mglaser <gla...@hallowelt.biz> Gerrit-Reviewer: Robert Vogel <vo...@hallowelt.biz> _______________________________________________ MediaWiki-commits mailing list MediaWiki-commits@lists.wikimedia.org https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits