Addshore has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/77698


Change subject: Create a base way to do requests with token
......................................................................

Create a base way to do requests with token

Change-Id: I6e370304b92e4d1fe371e249e2a121fdf3a5cf83
---
M repo/tests/phpunit/includes/api/BotEditTest.php
M repo/tests/phpunit/includes/api/EditEntityTest.php
M repo/tests/phpunit/includes/api/EditPageTest.php
M repo/tests/phpunit/includes/api/LangAttributeBase.php
M repo/tests/phpunit/includes/api/LinkTitlesTest.php
M repo/tests/phpunit/includes/api/ModifyEntityTestBase.php
M repo/tests/phpunit/includes/api/SetAliasesTest.php
M repo/tests/phpunit/includes/api/SetSiteLinkTest.php
8 files changed, 33 insertions(+), 91 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/mediawiki/extensions/Wikibase 
refs/changes/98/77698/1

diff --git a/repo/tests/phpunit/includes/api/BotEditTest.php 
b/repo/tests/phpunit/includes/api/BotEditTest.php
index a513e91..6d25025 100644
--- a/repo/tests/phpunit/includes/api/BotEditTest.php
+++ b/repo/tests/phpunit/includes/api/BotEditTest.php
@@ -112,17 +112,14 @@
         * @depends testTokensAndRights
         */
        function testSetItemTop() {
-               $token = $this->getEditToken();
-
                $req = array(
                        'action' => 'wbeditentity',
                        'summary' => 'Some reason',
                        'data' => '{}',
-                       'token' => $token,
                        'new' => 'item',
                );
 
-               $second = $this->doApiRequest( $req, null, false, 
self::$users['wbbot']->user );
+               $second = $this->doApiRequestWithToken( $req, null, 
self::$users['wbbot']->user );
 
                $this->assertArrayHasKey( 'success', $second[0],
                        "Must have an 'success' key in the second result from 
the API" );
@@ -139,14 +136,12 @@
         * @dataProvider providerCreateItem
         */
        function testCreateItem( $handle, $bot, $new, $data ) {
-               $token = $this->getEditToken();
                $myid = null;
 
                $req = array(
                        'action' => 'wbeditentity',
                        'summary' => 'Some reason',
                        'data' => $data,
-                       'token' => $token,
                );
 
                if ( !$new ) {
@@ -159,7 +154,7 @@
                        $req['bot'] = true;
                }
 
-               $second = $this->doApiRequest( $req, null, false, 
self::$users['wbbot']->user );
+               $second = $this->doApiRequestWithToken( $req, null 
,self::$users['wbbot']->user );
 
                $this->assertArrayHasKey( 'success', $second[0],
                        "Must have an 'success' key in the second result from 
the API" );
diff --git a/repo/tests/phpunit/includes/api/EditEntityTest.php 
b/repo/tests/phpunit/includes/api/EditEntityTest.php
index d59cc8c..b49a5f3 100644
--- a/repo/tests/phpunit/includes/api/EditEntityTest.php
+++ b/repo/tests/phpunit/includes/api/EditEntityTest.php
@@ -128,19 +128,16 @@
         * Checks the creation of a property via the editEntity API module
         */
        function testEditEntityCreateProperty() {
-               $token = $this->getEditToken();
                $data = 
'{"datatype":"string","labels":{"en":{"language":"en","value":"its a test!"}}}';
 
-               list($res,,) = $this->doApiRequest(
+               list($res,,) = $this->doApiRequestWithToken(
                        array(
                                'action' => 'wbeditentity',
                                'reason' => 'Some reason',
                                'data' => $data,
-                               'token' => $token,
                                'new' => 'property',
                        ),
                        null,
-                       false,
                        self::$users['wbeditor']->user
                );
 
@@ -155,18 +152,14 @@
         * note that upon completion the id will be stored for later reuse
         */
        function testEditEntityWithToken() {
-               $token = $this->getEditToken();
-
-               list($res,,) = $this->doApiRequest(
+               list($res,,) = $this->doApiRequestWithToken(
                        array(
                                'action' => 'wbeditentity',
                                'reason' => 'Some reason',
                                'data' => json_encode( self::$entity ),
-                               'token' => $token,
                                'new' => 'item',
                        ),
                        null,
-                       false,
                        self::$users['wbeditor']->user
                );
 
@@ -184,24 +177,20 @@
         * Check failure to set the same entity again, without id
         */
        function testEditEntityNoId() {
-               $token = $this->getEditToken();
-
                $data = array( 'labels' => array(
                                "de" => array( "language" => "de", "value" => 
"Foo X" ),
                                "en" => array( "language" => "en", "value" => 
"Bar Y" ),
                        ) );
 
                try {
-                       $this->doApiRequest(
+                       $this->doApiRequestWithToken(
                                array(
                                        'action' => 'wbeditentity',
                                        'reason' => 'Some reason',
                                        'data' => json_encode( array_merge( 
self::$entity, $data ) ),
-                                       'token' => $token,
                                        'new' => 'item',
                                ),
                                null,
-                               false,
                                self::$users['wbeditor']->user
                        );
 
@@ -216,18 +205,14 @@
         * Check success of entity update with a valid id
         */
        function testEditEntityWithId() {
-               $token = $this->getEditToken();
-
-               list($res,,) = $this->doApiRequest(
+               list($res,,) = $this->doApiRequestWithToken(
                        array(
                                'action' => 'wbeditentity',
                                'reason' => 'Some reason',
                                'data' => json_encode( self::$entity ),
-                               'token' => $token,
                                'id' => self::$id,
                        ),
                        null,
-                       false,
                        self::$users['wbeditor']->user
                );
 
@@ -244,8 +229,6 @@
         * Check failure to set the same entity again, with illegal field 
values in the json
         */
        function testEditEntityWithIllegalData() {
-               $token = $this->getEditToken();
-
                // these sets of failing data must be merged with an existing 
entity
                $failingData = array( //@todo: check each of these separately, 
so we know that each one fails!
                        array( 'pageid' => 999999 ),
@@ -255,16 +238,14 @@
                );
                foreach ( $failingData as $data ) {
                        try {
-                               $this->doApiRequest(
+                               $this->doApiRequestWithToken(
                                        array(
                                                'action' => 'wbeditentity',
                                                'reason' => 'Some reason',
                                                'data' => json_encode( 
array_merge( self::$entity, $data ) ),
-                                               'token' => $token,
                                                'id' => self::$id,
                                        ),
                                        null,
-                                       false,
                                        self::$users['wbeditor']->user
                                );
                                $this->fail( "Updating the entity with wrong 
data should have failed" );
@@ -279,8 +260,6 @@
         * Check success to set the same entity again, with legal field values 
in the json
         */
        function testEditEntityWithLegalData() {
-               $token = $this->getEditToken();
-
                // request the test data from the entity itself
                list($query,,) = $this->doApiRequest(
                        array(
@@ -290,7 +269,6 @@
                                'ids' => self::$id
                        ),
                        null,
-                       false,
                        self::$users['wbeditor']->user
                );
                $this->assertSuccess( $query, 'entities', self::$id, 'id' );
@@ -305,16 +283,14 @@
 
                foreach ( $goodData as $data ) {
                        try {
-                               list($res,,) = $this->doApiRequest(
+                               list($res,,) = $this->doApiRequestWithToken(
                                        array(
                                                'action' => 'wbeditentity',
                                                'reason' => 'Some reason',
                                                'data' => json_encode( 
array_merge( $data, self::$entity ) ),
-                                               'token' => $token,
                                                'id' => self::$id,
                                        ),
                                        null,
-                                       false,
                                        self::$users['wbeditor']->user
                                );
                                $this->assertSuccess( $res, 'entity', 'id' );
@@ -330,20 +306,16 @@
         * Check if it possible to clear out the content of the object
         */
        function testEditEntityEmptyData() {
-               $token = $this->getEditToken();
-
                try {
-                       list($res,,) = $this->doApiRequest(
+                       list($res,,) = $this->doApiRequestWithToken(
                                array(
                                        'action' => 'wbeditentity',
                                        'reason' => 'Some reason',
                                        'data' => json_encode( array() ),
-                                       'token' => $token,
                                        'id' => self::$id,
                                        'clear' => true,
                                ),
                                null,
-                               false,
                                self::$users['wbeditor']->user
                        );
                        $this->assertSuccess( $res, 'entity', 'id' );
@@ -460,19 +432,15 @@
         * @dataProvider provideBadData
         */
        function testEditEntityBadData( $data, $expectedErrorCode ) {
-               $token = $this->getEditToken();
-
                try {
-                       $this->doApiRequest(
+                       $this->doApiRequestWithToken(
                                array(
                                        'action' => 'wbeditentity',
                                        'reason' => 'Some reason',
                                        'data' => is_string( $data ) ? $data : 
json_encode( $data ),
-                                       'token' => $token,
                                        'new' => 'item',
                                ),
                                null,
-                               false,
                                self::$users['wbeditor']->user
                        );
 
@@ -573,19 +541,16 @@
         */
        function testEditEntityData( $handle, $data, $expected = null ) {
                $id = $this->getEntityId( $handle );
-               $token = $this->getEditToken();
 
                // wbsetentity 
------------------------------------------------------
-               list($res,,) = $this->doApiRequest(
+               list($res,,) = $this->doApiRequestWithToken(
                        array(
                                'action' => 'wbeditentity',
                                'reason' => 'Some reason',
                                'data' => json_encode( $data ),
-                               'token' => $token,
                                'id' => $id,
                        ),
                        null,
-                       false,
                        self::$users['wbeditor']->user
                );
 
diff --git a/repo/tests/phpunit/includes/api/EditPageTest.php 
b/repo/tests/phpunit/includes/api/EditPageTest.php
index 7e863c9..6249d77 100644
--- a/repo/tests/phpunit/includes/api/EditPageTest.php
+++ b/repo/tests/phpunit/includes/api/EditPageTest.php
@@ -58,7 +58,6 @@
                $this->assertTrue( $status->isOK(), $status->getMessage() ); // 
sanity check
 
                $this->login();
-               $token = $this->getEditToken();
 
                $content->getItem()->setLabel( "de", "Test" );
                $data = $content->getItem()->toArray();
@@ -66,12 +65,11 @@
 
                // try to update the item with valid data via the edit action
                try {
-                       $this->doApiRequest(
+                       $this->doApiRequestWithToken(
                                array(
                                        'action' => 'edit',
                                        'pageid' => 
$content->getTitle()->getArticleID(),
                                        'text' => $text,
-                                       'token' => $token,
                                )
                        );
 
@@ -93,19 +91,17 @@
                $page = 
\Wikibase\EntityContentFactory::singleton()->getWikiPageForId( $id );
 
                $this->login();
-               $token = $this->getEditToken();
 
                $text = "hallo welt";
 
                // try to update the item with valid data via the edit action
                try {
-                       $this->doApiRequest(
+                       $this->doApiRequestWithToken(
                                array(
                                        'action' => 'edit',
                                        'title' => 
$page->getTitle()->getPrefixedText(),
                                        'contentmodel' => 
CONTENT_MODEL_WIKITEXT,
                                        'text' => $text,
-                                       'token' => $token,
                                )
                        );
 
diff --git a/repo/tests/phpunit/includes/api/LangAttributeBase.php 
b/repo/tests/phpunit/includes/api/LangAttributeBase.php
index aeb69ff..7ca28be 100644
--- a/repo/tests/phpunit/includes/api/LangAttributeBase.php
+++ b/repo/tests/phpunit/includes/api/LangAttributeBase.php
@@ -80,7 +80,6 @@
 
                // update the item 
----------------------------------------------------------------
                $req = array(
-                       'token' => $this->getEditToken(),
                        'id' => $id,
                        'action' => $action,
                        'language' => $langCode,
@@ -88,7 +87,7 @@
                );
 
                try {
-                       list( $apiResponse,, ) = $this->doApiRequest( $req, 
null, false, self::$users['wbeditor']->user );
+                       list( $apiResponse,, ) = $this->doApiRequestWithToken( 
$req, null, self::$users['wbeditor']->user );
 
                        if ( $exception ) {
                                $this->fail( "expected exception $exception" );
diff --git a/repo/tests/phpunit/includes/api/LinkTitlesTest.php 
b/repo/tests/phpunit/includes/api/LinkTitlesTest.php
index b28cf7f..ade5acc 100644
--- a/repo/tests/phpunit/includes/api/LinkTitlesTest.php
+++ b/repo/tests/phpunit/includes/api/LinkTitlesTest.php
@@ -132,7 +132,6 @@
         * @dataProvider provideLinkTitles
         */
        public function testLinkTitles( $handle, $item_spec, $fromsite, 
$fromtitle, $tosite, $totitle, $expectedFailure = null, $cleanUp = false ) {
-               $token = $this->getEditToken();
                if ( $handle ) {
                        $id = $this->getEntityId( $handle );
                }
@@ -140,7 +139,6 @@
                // set the sitelink -------------------------------
                $req = array_merge( $item_spec, array(
                        'action' => 'wblinktitles',
-                       'token' => $token,
                        'fromsite' => $fromsite,
                        'fromtitle' => $fromtitle,
                        'tosite' => $tosite,
@@ -148,7 +146,7 @@
                ) );
 
                try {
-                       list( $res,, ) = $this->doApiRequest( $req, null, 
false, self::$users['wbeditor']->user );
+                       list( $res,, ) = $this->doApiRequestWithToken( $req, 
null, self::$users['wbeditor']->user );
 
                        if ( $expectedFailure ) {
                                $this->fail( "Expected failure: 
$expectedFailure" );
diff --git a/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php 
b/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php
index eee89b0..4f96e94 100644
--- a/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php
+++ b/repo/tests/phpunit/includes/api/ModifyEntityTestBase.php
@@ -4,6 +4,7 @@
 
 use ApiTestCase;
 use TestUser;
+use User;
 use Wikibase\Settings;
 
 /**
@@ -349,6 +350,14 @@
        }
 
        /**
+        *  Appends an edit token to a request
+        */
+       function doApiRequestWithToken( array $params, array $session = null, 
User $user = null ) {
+               $params['token'] = $this->getEditToken();
+               return $this->doApiRequest( $params, $session, false, $user );
+       }
+
+       /**
         * Initializes the test environment with the entities defined by 
makeEntityData() by creating these
         * entities in the database.
         */
diff --git a/repo/tests/phpunit/includes/api/SetAliasesTest.php 
b/repo/tests/phpunit/includes/api/SetAliasesTest.php
index 28a152a..445ea0e 100644
--- a/repo/tests/phpunit/includes/api/SetAliasesTest.php
+++ b/repo/tests/phpunit/includes/api/SetAliasesTest.php
@@ -91,14 +91,13 @@
 
                // update the item 
----------------------------------------------------------------
                $req = array(
-                       'token' => $this->getEditToken(),
                        'id' => $id,
                        'action' => 'wbsetaliases',
                        'language' => $langCode,
                        $op => $value
                );
 
-               list( $apiResponse,, ) = $this->doApiRequest( $req, null, 
false, self::$users['wbeditor']->user );
+               list( $apiResponse,, ) = $this->doApiRequestWithToken( $req, 
null, self::$users['wbeditor']->user );
 
                $this->assertSuccess( $apiResponse );
 
@@ -138,7 +137,6 @@
 
                // update the item 
----------------------------------------------------------------
                $req = array(
-                       'token' => $this->getEditToken(),
                        'id' => $id,
                        'action' => 'wbsetaliases',
                        'language' => $langCode,
@@ -146,7 +144,7 @@
                );
 
                try {
-                       list( $apiResponse,, ) = $this->doApiRequest( $req, 
null, false, self::$users['wbeditor']->user );
+                       list( $apiResponse,, ) = $this->doApiRequestWithToken( 
$req, null, self::$users['wbeditor']->user );
                } catch ( \Exception $e ) {
                        if ( $exception !== null && ! $e instanceof 
\PHPUnit_Framework_AssertionFailedError ) {
                                $this->assertTrue( is_a( $e, $exception ), "Not 
the expected exception" );
@@ -162,7 +160,6 @@
                $badId = 'xyz123+++';
 
                $req = array(
-                       'token' => $this->getEditToken(),
                        'id' => $badId,
                        'action' => 'wbsetaliases',
                        'language' => 'en',
@@ -170,7 +167,7 @@
                );
 
                try {
-                       $this->doApiRequest( $req, null, false, 
self::$users['wbeditor']->user );
+                       $this->doApiRequestWithToken( $req, null, 
self::$users['wbeditor']->user );
 
                        $this->fail( "Expected a usage exception when providing 
a malformed id" );
                } catch ( \UsageException $ex ) {
diff --git a/repo/tests/phpunit/includes/api/SetSiteLinkTest.php 
b/repo/tests/phpunit/includes/api/SetSiteLinkTest.php
index 7039205..8db30cb 100644
--- a/repo/tests/phpunit/includes/api/SetSiteLinkTest.php
+++ b/repo/tests/phpunit/includes/api/SetSiteLinkTest.php
@@ -56,17 +56,14 @@
        }
 
        public function testSetLiteLinkWithNoId( ) {
-               $token = $this->getEditToken();
-
                $req = array(
                        'action' => 'wbsetsitelink',
-                       'token' => $token,
                        'linksite' => "enwiki",
                        'linktitle' => "testSetLiteLinkWithNoId",
                );
 
                try {
-                       $this->doApiRequest( $req, null, false, 
self::$users['wbeditor']->user );
+                       $this->doApiRequestWithToken( $req, null, 
self::$users['wbeditor']->user );
 
                        $this->fail( "request should have failed" );
                } catch ( \UsageException $e ) {
@@ -75,18 +72,15 @@
        }
 
        public function testSetLiteLinkWithBadId( ) {
-               $token = $this->getEditToken();
-
                $req = array(
                        'action' => 'wbsetsitelink',
-                       'token' => $token,
                        'id' => 123456789,
                        'linksite' => "enwiki",
                        'linktitle' => "testSetLiteLinkWithNoId",
                );
 
                try {
-                       $this->doApiRequest( $req, null, false, 
self::$users['wbeditor']->user );
+                       $this->doApiRequestWithToken( $req, null, 
self::$users['wbeditor']->user );
 
                        $this->fail( "request should have failed" );
                } catch ( \UsageException $e ) {
@@ -95,11 +89,8 @@
        }
 
        public function testSetLiteLinkWithBadSite( ) {
-               $token = $this->getEditToken();
-
                $req = array(
                        'action' => 'wbsetsitelink',
-                       'token' => $token,
                        'site' => "dewiktionary",
                        'title' => "Berlin",
                        'linksite' => "enwiki",
@@ -107,7 +98,7 @@
                );
 
                try {
-                       $this->doApiRequest( $req, null, false, 
self::$users['wbeditor']->user );
+                       $this->doApiRequestWithToken( $req, null, 
self::$users['wbeditor']->user );
 
                        $this->fail( "request should have failed" );
                } catch ( \UsageException $e ) {
@@ -116,11 +107,8 @@
        }
 
        public function testSetLiteLinkWithBadTitle( ) {
-               $token = $this->getEditToken();
-
                $req = array(
                        'action' => 'wbsetsitelink',
-                       'token' => $token,
                        'site' => "dewiki",
                        'title' => "testSetLiteLinkWithBadTitle_de",
                        'linksite' => "enwiki",
@@ -128,7 +116,7 @@
                );
 
                try {
-                       $this->doApiRequest( $req, null, false, 
self::$users['wbeditor']->user );
+                       $this->doApiRequestWithToken( $req, null, 
self::$users['wbeditor']->user );
 
                        $this->fail( "request should have failed" );
                } catch ( \UsageException $e ) {
@@ -191,7 +179,6 @@
         * @dataProvider provideSetLiteLink
         */
        public function testSetLiteLink( $handle, $item_spec, $linksite, 
$linktitle, $expectedTitle = null, $expectedFailure = null ) {
-               $token = $this->getEditToken();
                $id = $this->getEntityId( $handle );
 
                if ( array_key_exists( 'id', $item_spec ) && empty( 
$item_spec['id'] ) ) {
@@ -207,13 +194,12 @@
                // set the sitelink -------------------------------
                $req = array_merge( $item_spec, array(
                        'action' => 'wbsetsitelink',
-                       'token' => $token,
                        'linksite' => $linksite,
                        'linktitle' => $linktitle,
                ) );
 
                try {
-                       list( $res,, ) = $this->doApiRequest( $req, null, 
false, self::$users['wbeditor']->user );
+                       list( $res,, ) = $this->doApiRequestWithToken( $req, 
null, self::$users['wbeditor']->user );
 
                        if ( $expectedFailure ) {
                                $this->fail( $expectedFailure );
@@ -273,11 +259,8 @@
        }
 
        public function testSetLiteLinkWithBadTargetSite( ) {
-               $token = $this->getEditToken();
-
                $req = array(
                        'action' => 'wbsetsitelink',
-                       'token' => $token,
                        'site' => "dewiki",
                        'title' => "Berlin",
                        'linksite' => "enwiktionary",
@@ -285,7 +268,7 @@
                );
 
                try {
-                       $this->doApiRequest( $req, null, false, 
self::$users['wbeditor']->user );
+                       $this->doApiRequestWithToken( $req, null, 
self::$users['wbeditor']->user );
 
                        $this->fail( "request should have failed" );
                } catch ( \UsageException $e ) {

-- 
To view, visit https://gerrit.wikimedia.org/r/77698
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: newchange
Gerrit-Change-Id: I6e370304b92e4d1fe371e249e2a121fdf3a5cf83
Gerrit-PatchSet: 1
Gerrit-Project: mediawiki/extensions/Wikibase
Gerrit-Branch: master
Gerrit-Owner: Addshore <[email protected]>
Gerrit-Reviewer: jenkins-bot

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to