diff options
Diffstat (limited to 'bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration')
3 files changed, 223 insertions, 0 deletions
diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/MediawikiApiTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/MediawikiApiTest.php new file mode 100644 index 00000000..da6683e4 --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/MediawikiApiTest.php @@ -0,0 +1,103 @@ +<?php + +namespace Mediawiki\Api\Test\Integration; + +use Mediawiki\Api\MediawikiApi; +use Mediawiki\Api\SimpleRequest; + +/** + * @author Addshore + */ +class MediawikiApiTest extends \PHPUnit_Framework_TestCase { + + /** + * @covers Mediawiki\Api\MediawikiApi::newFromPage + */ + public function testNewFromPage() { + $api = MediawikiApi::newFromPage( TestEnvironment::newInstance()->getPageUrl() ); + $this->assertInstanceOf( 'Mediawiki\Api\MediawikiApi', $api ); + } + + /** + * @covers Mediawiki\Api\MediawikiApi::newFromPage + * @expectedException Mediawiki\Api\RsdException + * @expectedExceptionMessageRegExp |Unable to find RSD URL in page.*| + */ + public function testNewFromPageInvalidHtml() { + // This could be any URL that doesn't contain the RSD link, but the README URL + // is a test-accessible one that doesn't return 404. + $nonWikiPage = str_replace( 'api.php', 'README', TestEnvironment::newInstance()->getApiUrl() ); + MediawikiApi::newFromPage( $nonWikiPage ); + } + + /** + * Duplicate element IDs break DOMDocument::loadHTML + * @see https://phabricator.wikimedia.org/T163527#3219833 + * @covers Mediawiki\Api\MediawikiApi::newFromPage + */ + public function testNewFromPageWithDuplicateId() { + $testPageName = __METHOD__; + $testEnv = TestEnvironment::newInstance(); + $wikiPageUrl = str_replace( 'api.php', "index.php?title=$testPageName", $testEnv->getApiUrl() ); + + // Test with no duplicate IDs. + $testEnv->savePage( $testPageName, '<p id="unique-id"></p>' ); + $api1 = MediawikiApi::newFromPage( $wikiPageUrl ); + $this->assertInstanceOf( MediawikiApi::class, $api1 ); + + // Test with duplicate ID. + $wikiText = '<p id="duplicated-id"></p><div id="duplicated-id"></div>'; + $testEnv->savePage( $testPageName, $wikiText ); + $api2 = MediawikiApi::newFromPage( $wikiPageUrl ); + $this->assertInstanceOf( MediawikiApi::class, $api2 ); + } + + /** + * @covers Mediawiki\Api\MediawikiApi::getRequest + * @covers Mediawiki\Api\MediawikiApi::getClientRequestOptions + * @covers Mediawiki\Api\MediawikiApi::decodeResponse + * @covers Mediawiki\Api\MediawikiApi::getClient + */ + public function testQueryGetResponse() { + $api = TestEnvironment::newInstance()->getApi(); + $response = $api->getRequest( new SimpleRequest( 'query' ) ); + $this->assertInternalType( 'array', $response ); + } + + /** + * @covers Mediawiki\Api\MediawikiApi::getRequestAsync + * @covers Mediawiki\Api\MediawikiApi::getClientRequestOptions + * @covers Mediawiki\Api\MediawikiApi::decodeResponse + * @covers Mediawiki\Api\MediawikiApi::getClient + */ + public function testQueryGetResponseAsync() { + $api = TestEnvironment::newInstance()->getApi(); + $response = $api->getRequestAsync( new SimpleRequest( 'query' ) ); + $this->assertInternalType( 'array', $response->wait() ); + } + + /** + * @covers Mediawiki\Api\MediawikiApi::postRequest + * @covers Mediawiki\Api\MediawikiApi::getClientRequestOptions + * @covers Mediawiki\Api\MediawikiApi::decodeResponse + * @covers Mediawiki\Api\MediawikiApi::getClient + */ + public function testQueryPostResponse() { + $api = TestEnvironment::newInstance()->getApi(); + $response = $api->postRequest( new SimpleRequest( 'query' ) ); + $this->assertInternalType( 'array', $response ); + } + + /** + * @covers Mediawiki\Api\MediawikiApi::postRequestAsync + * @covers Mediawiki\Api\MediawikiApi::getClientRequestOptions + * @covers Mediawiki\Api\MediawikiApi::decodeResponse + * @covers Mediawiki\Api\MediawikiApi::getClient + */ + public function testQueryPostResponseAsync() { + $api = TestEnvironment::newInstance()->getApi(); + $response = $api->postRequestAsync( new SimpleRequest( 'query' ) ); + $this->assertInternalType( 'array', $response->wait() ); + } + +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/TestEnvironment.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/TestEnvironment.php new file mode 100644 index 00000000..cb781508 --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/TestEnvironment.php @@ -0,0 +1,92 @@ +<?php + +namespace Mediawiki\Api\Test\Integration; + +use Exception; +use Mediawiki\Api\MediawikiApi; +use Mediawiki\Api\SimpleRequest; + +/** + * @author Addshore + */ +class TestEnvironment { + + /** + * Get a new TestEnvironment. + * This is identical to calling self::__construct() but is useful for fluent construction. + * + * @return TestEnvironment + */ + public static function newInstance() { + return new self(); + } + + /** @var MediawikiApi */ + private $api; + + /** @var string */ + private $apiUrl; + + /** @var string */ + private $pageUrl; + + /** + * Set up the test environment by creating a new API object pointing to a + * MediaWiki installation on localhost (or elsewhere as specified by the + * ADDWIKI_MW_API environment variable). + * + * @throws Exception If the ADDWIKI_MW_API environment variable does not end in 'api.php' + */ + public function __construct() { + $apiUrl = getenv( 'ADDWIKI_MW_API' ); + + if ( substr( $apiUrl, -7 ) !== 'api.php' ) { + $msg = "URL incorrect: $apiUrl" + ." (Set the ADDWIKI_MW_API environment variable correctly)"; + throw new Exception( $msg ); + } + + $this->apiUrl = $apiUrl; + $this->pageUrl = str_replace( 'api.php', 'index.php?title=Special:SpecialPages', $apiUrl ); + $this->api = MediawikiApi::newFromApiEndpoint( $this->apiUrl ); + } + + /** + * Get the url of the api to test against, based on the MEDIAWIKI_API_URL environment variable. + * @return string + */ + public function getApiUrl() { + return $this->apiUrl; + } + + /** + * Get the url of a page on the wiki to test against, based on the api url. + * @return string + */ + public function getPageUrl() { + return $this->pageUrl; + } + + /** + * Get the MediawikiApi to test against + * @return MediawikiApi + */ + public function getApi() { + return $this->api; + } + + /** + * Save a wiki page. + * @param string $title The title of the page. + * @param string $content The complete page text to save. + */ + public function savePage( $title, $content ) { + $params = [ + 'title' => $title, + 'text' => $content, + 'md5' => md5( $content ), + 'token' => $this->api->getToken(), + ]; + $this->api->postRequest( new SimpleRequest( 'edit', $params ) ); + } +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/TokenHandlingTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/TokenHandlingTest.php new file mode 100644 index 00000000..68ec52be --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Integration/TokenHandlingTest.php @@ -0,0 +1,28 @@ +<?php + +namespace Mediawiki\Api\Test\Integration; + +/** + * @author Addshore + */ +class TokenHandlingTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider provideTokenTypes + * + * @covers Mediawiki\Api\MediawikiApi::getToken + * @covers Mediawiki\Api\MediawikiSession::getToken + */ + public function testGetAnonUserToken() { + $api = TestEnvironment::newInstance()->getApi(); + $this->assertEquals( '+\\', $api->getToken() ); + } + + public function provideTokenTypes() { + return [ + [ 'csrf' ], + [ 'edit' ], + ]; + } + +} |