Robert Vogel has uploaded a new change for review.
https://gerrit.wikimedia.org/r/322632
Change subject: [WIP] Adding unit test base classes
......................................................................
[WIP] Adding unit test base classes
... and first API unit tests as reference for further development
Change-Id: Ie2a7de98daac279a0450f6914ee2d2c82a81a351
---
M BlueSpice.hooks.php
M extension.json
M includes/CoreHooks.php
A tests/BSApiExtJSStoreTestBase.php
A tests/BSApiTasksTestBase.php
A tests/BSPageFixturesProvider.php
A tests/api/BSApiTitleQueryStoreTest.php
7 files changed, 249 insertions(+), 15 deletions(-)
git pull
ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/BlueSpiceFoundation
refs/changes/32/322632/1
diff --git a/BlueSpice.hooks.php b/BlueSpice.hooks.php
index 12f9235..af7869a 100644
--- a/BlueSpice.hooks.php
+++ b/BlueSpice.hooks.php
@@ -1,6 +1,4 @@
<?php
-#$wgHooks['UnitTestsList'][] = 'BsCoreHooks::onUnitTestsList';
-
// START cache invalidation hooks
$wgHooks['PageContentSaveComplete'][] =
'BsCacheHelper::onPageContentSaveComplete';
// END cache invalidation hooks
diff --git a/extension.json b/extension.json
index 4cfc8e0..e948cad 100644
--- a/extension.json
+++ b/extension.json
@@ -368,7 +368,8 @@
"ParserFirstCallInit": "BsCoreHooks::onParserFirstCallInit",
"UserAddGroup": "BsGroupHelper::addTemporaryGroupToUserHelper",
"ExtensionTypes": "BsCoreHooks::onExtensionTypes",
- "PageContentSaveComplete":
"BsCoreHooks::onPageContentSaveComplete"
+ "PageContentSaveComplete": "BsCoreHooks::onPageContentSaveComplete",
+ "UnitTestsList": "BsCoreHooks::onUnitTestsList"
},
"config": {
"BlueSpiceExtInfo": {
@@ -529,7 +530,10 @@
"BSEntityRegistry": "includes/EntityRegistry.php",
"BSEntityConfig": "includes/entityconfigs/EntityConfig.php",
"BSEntityContent": "includes/content/EntityContent.php",
- "BSEntityContentHandler": "includes/content/EntityContentHandler.php"
+ "BSEntityContentHandler": "includes/content/EntityContentHandler.php",
+ "BSPageFixturesProvider": "tests/BSPageFixturesProvider.php",
+ "BSApiExtJSStoreTestBase": "tests/BSApiExtJSStoreTestBase.php",
+ "BSApiTasksTestBase": "tests/BSApiTasksTestBase.php"
},
"bsgConfigFiles": [],
"load_composer_autoloader": true,
diff --git a/includes/CoreHooks.php b/includes/CoreHooks.php
index f082889..e867475 100755
--- a/includes/CoreHooks.php
+++ b/includes/CoreHooks.php
@@ -643,17 +643,7 @@
* @return boolean Always true to keep hook running
*/
public static function onUnitTestsList( &$files ) {
- $oIterator = new RecursiveIteratorIterator(
- new RecursiveDirectoryIterator( dirname( __DIR__ ) .
'/tests/' )
- );
- /**
- * @var SplFileInfo $oFileInfo
- */
- foreach ( $oIterator as $oFileInfo ) {
- if ( substr( $oFileInfo->getFilename(), -8 ) ===
'Test.php' ) {
- $files[] = $oFileInfo->getPathname();
- }
- }
+ $files[] = dirname( __DIR__ ) . '/tests/';
return true;
}
diff --git a/tests/BSApiExtJSStoreTestBase.php
b/tests/BSApiExtJSStoreTestBase.php
new file mode 100644
index 0000000..67fb412
--- /dev/null
+++ b/tests/BSApiExtJSStoreTestBase.php
@@ -0,0 +1,164 @@
+<?php
+
+/**
+ *
+ * Class BSApiExtJSStoreTestBase
+ */
+abstract class BSApiExtJSStoreTestBase extends ApiTestCase {
+
+ protected $iFixtureTotal = 0;
+ abstract protected function getStoreSchema();
+ abstract protected function createStoreFixtureData();
+ abstract protected function getModuleName();
+
+ protected function setUp() {
+ parent::setUp();
+
+ $this->doLogin();
+ }
+
+ public function addDBDataOnce() {
+ $this->createStoreFixtureData();
+ }
+
+ public function testSchema() {
+ $results = $this->doApiRequest([
+ 'action' => $this->getModuleName()
+ ]);
+
+ $response = $results[0];
+ var_dump( $response );
+ $firstRow = $response['results'][0];
+ $schema = $this->getStoreSchema();
+ foreach( $schema as $schemaFieldName => $config ) {
+ $this->assertObjectHasAttribute( $schemaFieldName,
$firstRow, "Dataset misses field '$schemaFieldName'' from schema definition!" );
+ $value = $firstRow->{$schemaFieldName};
+
+ switch( $config['type'] ) {
+ case 'string':
+ $this->assertEquals( true, is_string(
$value ), "Value of field '$schemaFieldName' is not a string" );
+ break;
+ case 'list':
+ $this->assertEquals( true, is_array(
$value ), "Value of field '$schemaFieldName' is not a list" );
+ break;
+ case 'numeric':
+ $this->assertEquals( true, is_numeric(
$value ), "Value of field '$schemaFieldName' is not a number" );
+ break;
+ case 'boolean':
+ $this->assertEquals( true, is_bool(
$value ), "Value of field '$schemaFieldName' is not a boolean" );
+ break;
+ case 'date':
+ $this->assertNotEquals( -1,
strtotime(), "Value of field '$schemaFieldName' is not a valid date format" );
+ break;
+ case 'title':
+ $this->assertNotNull(
Title::newFromText( $value ), "Value of field '$schemaFieldName' is not a valid
title" );
+ break;
+ case 'templatetitle':
+ $this->assertNotNull(
Title::newFromText( $value, NS_TEMPLATE ), "Value of field '$schemaFieldName'
is not a valid template title" );
+ break;
+ }
+ }
+ }
+
+ /**
+ * @param $limit
+ * @param $offset
+ *
+ * @dataProvider providePagingData
+ */
+ public function testPaging( $limit, $offset ) {
+ $results = $this->doApiRequest([
+ 'action' => $this->getModuleName(),
+ 'limit' => $limit,
+ 'offset' => $offset
+ ]);
+ $response = $results[0];
+
+ $this->assertAttributeEquals(
+ $this->iFixtureTotal,
+ 'total',
+ (object)$response,
+ 'Field "total" contains wrong value'
+ );
+
+ $this->assertLessThanOrEqual( $limit,
count($response['results']), 'Number of results exceeds limit' );
+ }
+
+ public function providePagingData() {
+ return array(
+ [ 2, 0 ],
+ [ 2, 2 ],
+ [ 2, 4 ],
+ [ 4, 0 ],
+ [ 4, 4 ],
+ [ 4, 8 ]
+ );
+ }
+
+ /**
+ * [
+ * {
+ * "type":"string",
+ * "comparison":"ct",
+ * "value":"some text ...",
+ * "field":"someField"
+ * }
+ * ]
+ *
+ * @param $type
+ * @param $field
+ * @param $value
+ * @param $comparison
+ * @param $expectedTotal
+ *
+ * @dataProvider provideSingleFilterData
+ */
+ public function testSingleFilter( $type, $comparison, $field, $value,
$expectedTotal ) {
+ $results = $this->doApiRequest([
+ 'action' => $this->getModuleName(),
+ 'filter' => FormatJson::encode([
+ [
+ 'type' => $type,
+ 'comparison' => $comparison,
+ 'field' => $field,
+ 'value' => $value
+ ]
+ ])
+ ]);
+
+ $response = $results[0];
+
+ $this->assertAttributeEquals(
+ $expectedTotal,
+ 'total',
+ (object)$response,
+ 'Field "total" contains wrong value'
+ );
+ }
+
+ abstract public function provideSingleFilterData();
+
+ /**
+ * @param $filters
+ * @param $expectedTotal
+ *
+ * @dataProvider provideMultipleFilterData
+ */
+ public function testMultipleFilter( $filters, $expectedTotal ) {
+ $results = $this->doApiRequest([
+ 'action' => $this->getModuleName(),
+ 'filter' => FormatJson::encode( $filters )
+ ]);
+
+ $response = $results[0];
+
+ $this->assertAttributeEquals(
+ $expectedTotal,
+ 'total',
+ (object)$response,
+ 'Field "total" contains wrong value'
+ );
+ }
+
+ abstract public function provideMultipleFilterData();
+}
\ No newline at end of file
diff --git a/tests/BSApiTasksTestBase.php b/tests/BSApiTasksTestBase.php
new file mode 100644
index 0000000..ec20d33
--- /dev/null
+++ b/tests/BSApiTasksTestBase.php
@@ -0,0 +1,5 @@
+<?php
+
+class BSApiTasksTestBase extends ApiTestCase {
+
+}
\ No newline at end of file
diff --git a/tests/BSPageFixturesProvider.php b/tests/BSPageFixturesProvider.php
new file mode 100644
index 0000000..9779a75
--- /dev/null
+++ b/tests/BSPageFixturesProvider.php
@@ -0,0 +1,12 @@
+<?php
+
+class BSPageFixturesProvider {
+
+ /**
+ * @return array[]
+ */
+ public function getFixtureData() {
+ $oData = FormatJson::decode( file_get_contents(
__DIR__."/data/pages.json" ) );
+ return $oData->pages;
+ }
+}
\ No newline at end of file
diff --git a/tests/api/BSApiTitleQueryStoreTest.php
b/tests/api/BSApiTitleQueryStoreTest.php
new file mode 100644
index 0000000..90b676d
--- /dev/null
+++ b/tests/api/BSApiTitleQueryStoreTest.php
@@ -0,0 +1,61 @@
+<?php
+
+/**
+ * @group medium
+ *
+ * Class BSApiTitleQueryStoreTest
+ */
+class BSApiTitleQueryStoreTest extends BSApiExtJSStoreTestBase {
+ protected function getStoreSchema() {
+ return [
+ 'page_id' => [
+ 'type' => 'numeric'
+ ],
+ 'page_namespace' => [
+ 'type' => 'numeric'
+ ],
+ 'page_title' => [
+ 'type' => 'string'
+ ],
+ 'prefixedText' => [
+ 'type' => 'string'
+ ],
+ 'displayText' => [
+ 'type' => 'string'
+ ],
+ 'type' => [
+ 'type' => 'string'
+ ]
+ ];
+ }
+
+ protected function createStoreFixtureData() {
+ $oPageFixtures = new BSPageFixturesProvider();
+ $aFixtures = $oPageFixtures->getFixtureData();
+ foreach( $aFixtures as $aFixture ) {
+ $this->insertPage( $aFixture[0], $aFixture[1] );
+ }
+ }
+
+ protected function getModuleName() {
+ return 'bs-titlequery-store';
+ }
+
+ public function provideSingleFilterData() {
+ return [
+ 'Filter by page_id' => [ 'numeric', 'eq', 'page_id',
-1, 0 ]
+ ];
+ }
+
+ public function provideMultipleFilterData() {
+ return [
+ 'Filter by page_name and page_namespace' => [
+ [
+
+ ],
+ 0
+ ]
+ ];
+ }
+
+}
\ No newline at end of file
--
To view, visit https://gerrit.wikimedia.org/r/322632
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings
Gerrit-MessageType: newchange
Gerrit-Change-Id: Ie2a7de98daac279a0450f6914ee2d2c82a81a351
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/BlueSpiceFoundation
Gerrit-Branch: master
Gerrit-Owner: Robert Vogel <[email protected]>
_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits