diff options
Diffstat (limited to 'bin/reevotech/vendor/addwiki/mediawiki-api-base/tests')
12 files changed, 1191 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' ], + ]; + } + +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/ApiUserTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/ApiUserTest.php new file mode 100644 index 00000000..7e0da7ca --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/ApiUserTest.php @@ -0,0 +1,71 @@ +<?php + +namespace Mediawiki\Api\Test\Unit; + +use Mediawiki\Api\ApiUser; + +/** + * @author Addshore + * + * @covers Mediawiki\Api\ApiUser + */ +class ApiUserTest extends \PHPUnit_Framework_TestCase { + + /** + * @dataProvider provideValidConstruction + */ + public function testValidConstruction( $user, $pass, $domain = null ) { + $apiUser = new ApiUser( $user, $pass, $domain ); + $this->assertSame( $user, $apiUser->getUsername() ); + $this->assertSame( $pass, $apiUser->getPassword() ); + $this->assertSame( $domain, $apiUser->getDomain() ); + } + + public function provideValidConstruction() { + return [ + [ 'user', 'pass' ], + [ 'user', 'pass', 'domain' ], + ]; + } + + /** + * @dataProvider provideInvalidConstruction + */ + public function testInvalidConstruction( $user, $pass, $domain = null ) { + $this->setExpectedException( 'InvalidArgumentException' ); + new ApiUser( $user, $pass, $domain ); + } + + public function provideInvalidConstruction() { + return [ + [ 'user', '' ], + [ '', 'pass' ], + [ '', '' ], + [ 'user', [] ], + [ 'user', 455667 ], + [ 34567, 'pass' ], + [ [], 'pass' ], + [ 'user', 'pass', [] ], + ]; + } + + /** + * @dataProvider provideTestEquals + */ + public function testEquals( ApiUser $user1, ApiUser $user2, $shouldEqual ) { + $this->assertSame( $shouldEqual, $user1->equals( $user2 ) ); + $this->assertSame( $shouldEqual, $user2->equals( $user1 ) ); + } + + public function provideTestEquals() { + return [ + [ new ApiUser( 'usera', 'passa' ), new ApiUser( 'usera', 'passa' ), true ], + [ new ApiUser( 'usera', 'passa', 'domain' ), new ApiUser( 'usera', 'passa', 'domain' ), true ], + [ new ApiUser( 'DIFF', 'passa' ), new ApiUser( 'usera', 'passa' ), false ], + [ new ApiUser( 'usera', 'DIFF' ), new ApiUser( 'usera', 'passa' ), false ], + [ new ApiUser( 'usera', 'passa' ), new ApiUser( 'DIFF', 'passa' ), false ], + [ new ApiUser( 'usera', 'passa' ), new ApiUser( 'usera', 'DIFF' ), false ], + ]; + } + +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/FluentRequestTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/FluentRequestTest.php new file mode 100644 index 00000000..93af921e --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/FluentRequestTest.php @@ -0,0 +1,69 @@ +<?php + +namespace Mediawiki\Api\Test\Unit; + +use Mediawiki\Api\FluentRequest; +use PHPUnit_Framework_TestCase; + +/** + * @author Addshore + * + * @covers Mediawiki\Api\FluentRequest + */ +class FluentRequestTest extends PHPUnit_Framework_TestCase { + + public function testFactory() { + $this->assertInstanceOf( 'Mediawiki\Api\FluentRequest', FluentRequest::factory() ); + } + + public function testConstructionDefaults() { + $request = new FluentRequest(); + + $this->assertEquals( [], $request->getParams() ); + $this->assertEquals( [], $request->getHeaders() ); + } + + public function testSetParams() { + $request = new FluentRequest(); + + $params = [ 'foo', 'bar' ]; + $request->setParams( $params ); + + $this->assertEquals( $params, $request->getParams() ); + } + + public function testSetParam() { + $request = new FluentRequest(); + + $request->setParam( 'paramName', 'fooValue' ); + + $this->assertEquals( [ 'paramName' => 'fooValue' ], $request->getParams() ); + } + + public function testAddParams() { + $request = new FluentRequest(); + + $params = [ 'a' => 'foo', 'b' => 'bar' ]; + $request->addParams( $params ); + + $this->assertEquals( $params, $request->getParams() ); + } + + public function testSetHeaders() { + $request = new FluentRequest(); + + $params = [ 'foo', 'bar' ]; + $request->setHeaders( $params ); + + $this->assertEquals( $params, $request->getHeaders() ); + } + + public function testSetAction() { + $request = new FluentRequest(); + + $request->setAction( 'fooAction' ); + + $this->assertEquals( [ 'action' => 'fooAction' ], $request->getParams() ); + } + +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/Guzzle/ClientFactoryTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/Guzzle/ClientFactoryTest.php new file mode 100644 index 00000000..d84d0333 --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/Guzzle/ClientFactoryTest.php @@ -0,0 +1,91 @@ +<?php + +namespace Mediawiki\Api\Test\Unit\Guzzle; + +use GuzzleHttp\HandlerStack; +use Mediawiki\Api\Guzzle\ClientFactory; +use Psr\Http\Message\RequestInterface; + +/** + * @author Christian Schmidt + * + * @covers Mediawiki\Api\Guzzle\ClientFactory + */ +class ClientFactoryTest extends \PHPUnit_Framework_TestCase { + + public function testNoConfig() { + $clientFactory = new ClientFactory(); + + $client = $clientFactory->getClient(); + + $this->assertSame( $client, $clientFactory->getClient() ); + + $config = $client->getConfig(); + $this->assertEquals( $config['headers']['User-Agent'], 'Addwiki - mediawiki-api-base' ); + + $this->assertFalse( empty( $config['cookies'] ) ); + } + + public function testUserAgent() { + $clientFactory = new ClientFactory( [ 'user-agent' => 'Foobar' ] ); + + $client = $clientFactory->getClient(); + + $this->assertNull( $client->getConfig( 'user-agent' ) ); + + $config = $client->getConfig(); + $this->assertEquals( $config['headers']['User-Agent'], 'Foobar' ); + } + + public function testHeaders() { + $clientFactory = new ClientFactory( [ + 'headers' => [ + 'User-Agent' => 'Foobar', + 'X-Foo' => 'Bar', + ] + ] ); + + $client = $clientFactory->getClient(); + + $headers = $client->getConfig( 'headers' ); + $this->assertCount( 2, $headers ); + $this->assertEquals( $headers['User-Agent'], 'Foobar' ); + $this->assertEquals( $headers['X-Foo'], 'Bar' ); + } + + public function testHandler() { + $handler = HandlerStack::create(); + + $clientFactory = new ClientFactory( [ 'handler' => $handler ] ); + + $client = $clientFactory->getClient(); + + $this->assertSame( $handler, $client->getConfig( 'handler' ) ); + } + + public function testMiddleware() { + $invoked = false; + $middleware = function () use ( &$invoked ) { + return function () use ( &$invoked ) { + $invoked = true; + }; + }; + + $clientFactory = new ClientFactory( [ 'middleware' => [ $middleware ] ] ); + + $client = $clientFactory->getClient(); + + $this->assertNull( $client->getConfig( 'middleware' ) ); + + $request = $this->getMockBuilder( RequestInterface::class )->getMock(); + + $handler = $client->getConfig( 'handler' ); + $handler->remove( 'http_errors' ); + $handler->remove( 'allow_redirects' ); + $handler->remove( 'cookies' ); + $handler->remove( 'prepare_body' ); + $handler( $request, [] ); + + $this->assertTrue( $invoked ); + } +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/Guzzle/MiddlewareFactoryTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/Guzzle/MiddlewareFactoryTest.php new file mode 100644 index 00000000..1cf7270a --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/Guzzle/MiddlewareFactoryTest.php @@ -0,0 +1,214 @@ +<?php + +namespace Mediawiki\Api\Test\Unit\Guzzle; + +use GuzzleHttp\Client; +use GuzzleHttp\Exception\ConnectException; +use GuzzleHttp\Handler\MockHandler; +use GuzzleHttp\HandlerStack; +use GuzzleHttp\Psr7\Request; +use GuzzleHttp\Psr7\Response; +use Mediawiki\Api\Guzzle\MiddlewareFactory; + +/** + * @author Addshore + * + * @todo test interaction with logger + * + * @covers Mediawiki\Api\Guzzle\MiddlewareFactory + */ +class MiddlewareFactoryTest extends \PHPUnit_Framework_TestCase { + + public function testRetriesConnectException() { + $queue = [ + new ConnectException( 'Error 1', new Request( 'GET', 'test' ) ), + new Response( 200, [ 'X-Foo' => 'Bar' ] ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertEquals( [ 1000 ], $delays ); + } + + public function testRetries500Errors() { + $queue = [ + new Response( 500 ), + new Response( 200 ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertEquals( [ 1000 ], $delays ); + } + + public function testRetriesSomeMediawikiApiErrorHeaders() { + $queue = [ + new Response( 200, [ 'mediawiki-api-error' => 'ratelimited' ] ), + new Response( 200, [ 'mediawiki-api-error' => 'maxlag' ] ), + new Response( 200, [ 'mediawiki-api-error' => 'readonly' ] ), + new Response( 200, [ 'mediawiki-api-error' => 'internal_api_error_DBQueryError' ] ), + new Response( 200, [ 'mediawiki-api-error' => 'DoNotRetryThisHeader' ] ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertEquals( + [ 'DoNotRetryThisHeader' ], + $response->getHeader( 'mediawiki-api-error' ) + ); + $this->assertEquals( [ 1000, 2000, 3000, 4000 ], $delays ); + } + + public function testRetryAntiAbuseMeasure() { + $antiAbusejson = json_encode( + [ + 'error' => [ + 'info' => 'anti-abuse measure' + ] + ] + ); + + $queue = [ + new Response( 200, [ 'mediawiki-api-error' => 'failed-save' ], $antiAbusejson ), + new Response( 200, [ 'mediawiki-api-error' => 'DoNotRetryThisHeader' ] ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertEquals( 'DoNotRetryThisHeader', $response->getHeaderLine( 'mediawiki-api-error' ) ); + } + + public function testRetryLimit() { + $queue = [ + new ConnectException( 'Error 1', new Request( 'GET', 'test' ) ), + new ConnectException( 'Error 2', new Request( 'GET', 'test' ) ), + new ConnectException( 'Error 3', new Request( 'GET', 'test' ) ), + new ConnectException( 'Error 4', new Request( 'GET', 'test' ) ), + new ConnectException( 'Error 5', new Request( 'GET', 'test' ) ), + new ConnectException( 'Error 6', new Request( 'GET', 'test' ) ), + new Response( 200 ), + ]; + + $client = $this->getClient( $queue ); + + $this->setExpectedException( + 'GuzzleHttp\Exception\ConnectException', + 'Error 6' + ); + + $client->request( 'GET', '/' ); + } + + public function testConnectExceptionRetryDelay() { + $queue = [ + new ConnectException( '+1 second delay', new Request( 'GET', 'test' ) ), + new ConnectException( '+2 second delay', new Request( 'GET', 'test' ) ), + new Response( 200 ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertEquals( [ 1000, 2000 ], $delays ); + } + + public function testServerErrorRetryDelay() { + $queue = [ + new Response( 500 ), + new Response( 503 ), + new Response( 200 ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertEquals( [ 1000, 2000 ], $delays ); + } + + public function testRelativeRetryDelayHeaderRetryDelay() { + $queue = [ + new Response( 200, [ 'mediawiki-api-error' => 'maxlag', 'retry-after' => 10 ] ), + new Response( 200 ), + ]; + + $this->getClient( $queue, $delays )->request( 'GET', '/' ); + + $this->assertEquals( [ 10000 ], $delays ); + } + + public function testAbsoluteRetryDelayHeaderRetryDelay() { + $queue = [ + new Response( + 200, + [ + 'mediawiki-api-error' => 'maxlag', + 'retry-after' => gmdate( DATE_RFC1123, time() + 600 ), + ] + ), + new Response( 200 ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertCount( 1, $delays ); + // Allow 5 second delay while running this test. + $this->assertGreaterThan( 600000 - 5000, $delays[0] ); + } + + public function testPastRetryDelayHeaderRetryDelay() { + $queue = [ + new Response( + 200, + [ + 'mediawiki-api-error' => 'maxlag', + 'retry-after' => 'Fri, 31 Dec 1999 23:59:59 GMT', + ] + ), + new Response( 200 ), + ]; + + $client = $this->getClient( $queue, $delays ); + $response = $client->request( 'GET', '/' ); + + $this->assertEquals( 200, $response->getStatusCode() ); + $this->assertEquals( [ 1000 ], $delays ); + } + + private function getClient( array $queue, &$delays = null ) { + $mock = new MockHandler( $queue ); + + $handler = HandlerStack::create( $mock ); + + $middlewareFactory = new MiddlewareFactory(); + $handler->push( $middlewareFactory->retry() ); + + $delayMocker = $this->getDelayMocker( $delays ); + $handler->push( $delayMocker ); + + return new Client( [ 'handler' => $handler ] ); + } + + private function getDelayMocker( &$delays ) { + return function ( callable $handler ) use ( &$delays ) { + return function ( $request, array $options ) use ( $handler, &$delays ) { + if ( isset( $options['delay'] ) ) { + $delays[] = $options['delay']; + unset( $options['delay'] ); + } + return $handler( $request, $options ); + }; + }; + } +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MediawikiApiTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MediawikiApiTest.php new file mode 100644 index 00000000..a55f6739 --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MediawikiApiTest.php @@ -0,0 +1,296 @@ +<?php + +namespace Mediawiki\Api\Test\Unit; + +use Mediawiki\Api\ApiUser; +use Mediawiki\Api\MediawikiApi; +use Mediawiki\Api\SimpleRequest; +use Mediawiki\Api\UsageException; +use PHPUnit_Framework_TestCase; +use stdClass; + +/** + * @author Addshore + * + * @covers Mediawiki\Api\MediawikiApi + */ +class MediawikiApiTest extends PHPUnit_Framework_TestCase { + + public function provideValidConstruction() { + return [ + [ 'localhost' ], + [ 'http://en.wikipedia.org/w/api.php' ], + [ '127.0.0.1/foo/bar/wwwwwwwww/api.php' ], + ]; + } + + /** + * @dataProvider provideValidConstruction + */ + public function testValidConstruction( $apiLocation ) { + new MediawikiApi( $apiLocation ); + $this->assertTrue( true ); + } + + public function provideInvalidConstruction() { + return [ + [ null ], + [ 12345678 ], + [ [] ], + [ new stdClass() ], + ]; + } + + /** + * @dataProvider provideInvalidConstruction + */ + public function testInvalidConstruction( $apiLocation ) { + $this->setExpectedException( 'InvalidArgumentException' ); + new MediawikiApi( $apiLocation ); + } + + private function getMockClient() { + return $this->getMock( 'GuzzleHttp\ClientInterface' ); + } + + private function getMockResponse( $responseValue ) { + $mock = $this->getMock( 'Psr\Http\Message\ResponseInterface' ); + $mock->expects( $this->any() ) + ->method( 'getBody' ) + ->will( $this->returnValue( json_encode( $responseValue ) ) ); + return $mock; + } + + private function getExpectedRequestOpts( $params, $paramsLocation ) { + return [ + $paramsLocation => array_merge( $params, [ 'format' => 'json' ] ), + 'headers' => [ 'User-Agent' => 'addwiki-mediawiki-client' ], + ]; + } + + public function testGetRequestThrowsUsageExceptionOnError() { + $client = $this->getMockClient(); + $client->expects( $this->once() ) + ->method( 'request' ) + ->will( $this->returnValue( + $this->getMockResponse( [ 'error' => [ + 'code' => 'imacode', + 'info' => 'imamsg', + ] ] ) + ) ); + $api = new MediawikiApi( '', $client ); + + try{ + $api->getRequest( new SimpleRequest( 'foo' ) ); + $this->fail( 'No Usage Exception Thrown' ); + } + catch ( UsageException $e ) { + $this->assertEquals( 'imacode', $e->getApiCode() ); + $this->assertEquals( 'imamsg', $e->getRawMessage() ); + } + } + + public function testPostRequestThrowsUsageExceptionOnError() { + $client = $this->getMockClient(); + $client->expects( $this->once() ) + ->method( 'request' ) + ->will( $this->returnValue( + $this->getMockResponse( [ 'error' => [ + 'code' => 'imacode', + 'info' => 'imamsg', + ] ] ) + ) ); + $api = new MediawikiApi( '', $client ); + + try{ + $api->postRequest( new SimpleRequest( 'foo' ) ); + $this->fail( 'No Usage Exception Thrown' ); + } + catch ( UsageException $e ) { + $this->assertSame( 'imacode', $e->getApiCode() ); + $this->assertSame( 'imamsg', $e->getRawMessage() ); + } + } + + /** + * @dataProvider provideActionsParamsResults + */ + public function testGetActionReturnsResult( $expectedResult, $action, $params = [] ) { + $client = $this->getMockClient(); + $params = array_merge( [ 'action' => $action ], $params ); + $client->expects( $this->once() ) + ->method( 'request' ) + ->with( 'GET', null, $this->getExpectedRequestOpts( $params, 'query' ) ) + ->will( $this->returnValue( $this->getMockResponse( $expectedResult ) ) ); + $api = new MediawikiApi( '', $client ); + + $result = $api->getRequest( new SimpleRequest( $action, $params ) ); + + $this->assertEquals( $expectedResult, $result ); + } + + /** + * @dataProvider provideActionsParamsResults + */ + public function testPostActionReturnsResult( $expectedResult, $action, $params = [] ) { + $client = $this->getMockClient(); + $params = array_merge( [ 'action' => $action ], $params ); + $client->expects( $this->once() ) + ->method( 'request' ) + ->with( 'POST', null, $this->getExpectedRequestOpts( $params, 'form_params' ) ) + ->will( $this->returnValue( $this->getMockResponse( $expectedResult ) ) ); + $api = new MediawikiApi( '', $client ); + + $result = $api->postRequest( new SimpleRequest( $action, $params ) ); + + $this->assertEquals( $expectedResult, $result ); + } + + private function getNullFilePointer() { + if ( !file_exists( '/dev/null' ) ) { + // windows + return fopen( 'NUL', 'r' ); + } + return fopen( '/dev/null', 'r' ); + } + + public function testPostActionWithFileReturnsResult() { + $dummyFile = $this->getNullFilePointer(); + $params = [ + 'filename' => 'foo.jpg', + 'file' => $dummyFile, + ]; + $client = $this->getMockClient(); + $client->expects( $this->once() )->method( 'request' )->with( + 'POST', + null, + [ + 'multipart' => [ + [ 'name' => 'action', 'contents' => 'upload' ], + [ 'name' => 'filename', 'contents' => 'foo.jpg' ], + [ 'name' => 'file', 'contents' => $dummyFile ], + [ 'name' => 'format', 'contents' => 'json' ], + ], + 'headers' => [ 'User-Agent' => 'addwiki-mediawiki-client' ], + ] + )->will( $this->returnValue( $this->getMockResponse( [ 'success ' => 1 ] ) ) ); + $api = new MediawikiApi( '', $client ); + + $result = $api->postRequest( new SimpleRequest( 'upload', $params ) ); + + $this->assertEquals( [ 'success ' => 1 ], $result ); + } + + public function provideActionsParamsResults() { + return [ + [ [ 'key' => 'value' ], 'logout' ], + [ [ 'key' => 'value' ], 'logout', [ 'param1' => 'v1' ] ], + [ [ 'key' => 'value', 'key2' => 1212, [] ], 'logout' ], + ]; + } + + public function testGoodLoginSequence() { + $client = $this->getMockClient(); + $user = new ApiUser( 'U1', 'P1' ); + $eq1 = [ + 'action' => 'login', + 'lgname' => 'U1', + 'lgpassword' => 'P1', + ]; + $client->expects( $this->at( 0 ) ) + ->method( 'request' ) + ->with( 'POST', null, $this->getExpectedRequestOpts( $eq1, 'form_params' ) ) + ->will( $this->returnValue( $this->getMockResponse( [ 'login' => [ + 'result' => 'NeedToken', + 'token' => 'IamLoginTK', + ] ] ) ) ); + $params = array_merge( $eq1, [ 'lgtoken' => 'IamLoginTK' ] ); + $response = $this->getMockResponse( [ 'login' => [ 'result' => 'Success' ] ] ); + $client->expects( $this->at( 1 ) ) + ->method( 'request' ) + ->with( 'POST', null, $this->getExpectedRequestOpts( $params, 'form_params' ) ) + ->will( $this->returnValue( $response ) ); + $api = new MediawikiApi( '', $client ); + + $this->assertTrue( $api->login( $user ) ); + $this->assertSame( 'U1', $api->isLoggedin() ); + } + + public function testBadLoginSequence() { + $client = $this->getMockClient(); + $user = new ApiUser( 'U1', 'P1' ); + $eq1 = [ + 'action' => 'login', + 'lgname' => 'U1', + 'lgpassword' => 'P1', + ]; + $client->expects( $this->at( 0 ) ) + ->method( 'request' ) + ->with( 'POST', null, $this->getExpectedRequestOpts( $eq1, 'form_params' ) ) + ->will( $this->returnValue( $this->getMockResponse( [ 'login' => [ + 'result' => 'NeedToken', + 'token' => 'IamLoginTK', + ] ] ) ) ); + $params = array_merge( $eq1, [ 'lgtoken' => 'IamLoginTK' ] ); + $response = $this->getMockResponse( [ 'login' => [ 'result' => 'BADTOKENorsmthin' ] ] ); + $client->expects( $this->at( 1 ) ) + ->method( 'request' ) + ->with( 'POST', null, $this->getExpectedRequestOpts( $params, 'form_params' ) ) + ->will( $this->returnValue( $response ) ); + $api = new MediawikiApi( '', $client ); + + $this->setExpectedException( 'Mediawiki\Api\UsageException' ); + $api->login( $user ); + } + + public function testLogout() { + $client = $this->getMockClient(); + $client->expects( $this->at( 0 ) ) + ->method( 'request' ) + ->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) ) + ->will( $this->returnValue( $this->getMockResponse( [] ) ) ); + $api = new MediawikiApi( '', $client ); + + $this->assertTrue( $api->logout() ); + } + + public function testLogoutOnFailure() { + $client = $this->getMockClient(); + $client->expects( $this->at( 0 ) ) + ->method( 'request' ) + ->with( 'POST', null, $this->getExpectedRequestOpts( [ 'action' => 'logout' ], 'form_params' ) ) + ->will( $this->returnValue( $this->getMockResponse( null ) ) ); + $api = new MediawikiApi( '', $client ); + + $this->assertFalse( $api->logout() ); + } + + /** + * @dataProvider provideVersions + */ + public function testGetVersion( $apiValue, $expectedVersion ) { + $client = $this->getMockClient(); + $params = [ 'action' => 'query', 'meta' => 'siteinfo', 'continue' => '' ]; + $client->expects( $this->exactly( 1 ) ) + ->method( 'request' ) + ->with( 'GET', null, $this->getExpectedRequestOpts( $params, 'query' ) ) + ->will( $this->returnValue( $this->getMockResponse( [ + 'query' => [ + 'general' => [ + 'generator' => $apiValue, + ], + ], + ] ) ) ); + $api = new MediawikiApi( '', $client ); + $this->assertEquals( $expectedVersion, $api->getVersion() ); + } + + public function provideVersions() { + return [ + [ 'MediaWiki 1.25wmf13', '1.25' ], + [ 'MediaWiki 1.24.1', '1.24.1' ], + [ 'MediaWiki 1.19', '1.19' ], + [ 'MediaWiki 1.0.0', '1.0.0' ], + ]; + } +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MediawikiSessionTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MediawikiSessionTest.php new file mode 100644 index 00000000..667a526f --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MediawikiSessionTest.php @@ -0,0 +1,95 @@ +<?php + +namespace Mediawiki\Api\Test\Unit; + +use Mediawiki\Api\MediawikiApi; +use Mediawiki\Api\MediawikiSession; +use PHPUnit_Framework_MockObject_MockObject; +use PHPUnit_Framework_TestCase; + +/** + * @author Addshore + * + * @covers Mediawiki\Api\MediawikiSession + */ +class MediawikiSessionTest extends PHPUnit_Framework_TestCase { + + /** + * @return PHPUnit_Framework_MockObject_MockObject|MediawikiApi + */ + private function getMockApi() { + return $this->getMockBuilder( '\Mediawiki\Api\MediawikiApi' ) + ->disableOriginalConstructor() + ->getMock(); + } + + public function testConstruction() { + $session = new MediawikiSession( $this->getMockApi() ); + $this->assertInstanceOf( '\Mediawiki\Api\MediawikiSession', $session ); + } + + /** + * @dataProvider provideTokenTypes + */ + public function testGetToken( $tokenType ) { + $mockApi = $this->getMockApi(); + $mockApi->expects( $this->exactly( 2 ) ) + ->method( 'postRequest' ) + ->with( $this->isInstanceOf( '\Mediawiki\Api\SimpleRequest' ) ) + ->will( $this->returnValue( [ + 'query' => [ + 'tokens' => [ + $tokenType => 'TKN-' . $tokenType, + ] + ] + ] ) ); + + $session = new MediawikiSession( $mockApi ); + + // Although we make 2 calls to the method we assert the tokens method about is only called once + $this->assertEquals( 'TKN-' . $tokenType, $session->getToken() ); + $this->assertEquals( 'TKN-' . $tokenType, $session->getToken() ); + // Then clearing the tokens and calling again should make a second call! + $session->clearTokens(); + $this->assertEquals( 'TKN-' . $tokenType, $session->getToken() ); + } + + /** + * @dataProvider provideTokenTypes + */ + public function testGetTokenPre125( $tokenType ) { + $mockApi = $this->getMockApi(); + $mockApi->expects( $this->at( 0 ) ) + ->method( 'postRequest' ) + ->with( $this->isInstanceOf( '\Mediawiki\Api\SimpleRequest' ) ) + ->will( $this->returnValue( [ + 'warnings' => [ + 'query' => [ + '*' => "Unrecognized value for parameter 'meta': tokens", + ] + ] + ] ) ); + $mockApi->expects( $this->at( 1 ) ) + ->method( 'postRequest' ) + ->with( $this->isInstanceOf( '\Mediawiki\Api\SimpleRequest' ) ) + ->will( $this->returnValue( [ + 'tokens' => [ + $tokenType => 'TKN-' . $tokenType, + ] + ] ) ); + + $session = new MediawikiSession( $mockApi ); + + // Although we make 2 calls to the method we assert the tokens method about is only called once + $this->assertSame( 'TKN-' . $tokenType, $session->getToken() ); + $this->assertSame( 'TKN-' . $tokenType, $session->getToken() ); + } + + public function provideTokenTypes() { + return [ + [ 'csrf' ], + [ 'edit' ], + ]; + } + +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MultipartRequestTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MultipartRequestTest.php new file mode 100644 index 00000000..993c29e8 --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/MultipartRequestTest.php @@ -0,0 +1,44 @@ +<?php + +namespace Mediawiki\Api\Test\Unit; + +use Exception; +use Mediawiki\Api\MultipartRequest; +use PHPUnit_Framework_TestCase; + +class MultipartRequestTest extends PHPUnit_Framework_TestCase { + + public function testBasics() { + $request = new MultipartRequest(); + $this->assertEquals( [], $request->getMultipartParams() ); + + // One parameter. + $request->setParam( 'testparam', 'value' ); + $request->addMultipartParams( [ 'testparam' => [ 'lorem' => 'ipsum' ] ] ); + $this->assertEquals( + [ 'testparam' => [ 'lorem' => 'ipsum' ] ], + $request->getMultipartParams() + ); + + // Another parameter. + $request->setParam( 'testparam2', 'value' ); + $request->addMultipartParams( [ 'testparam2' => [ 'lorem2' => 'ipsum2' ] ] ); + $this->assertEquals( + [ + 'testparam' => [ 'lorem' => 'ipsum' ], + 'testparam2' => [ 'lorem2' => 'ipsum2' ], + ], + $request->getMultipartParams() + ); + } + + /** + * You are not allowed to set multipart parameters on a parameter that doesn't exist. + * @expectedException Exception + * @expectedExceptionMessage Parameter 'testparam' is not already set on this request. + */ + public function testParamNotYetSet() { + $request = new MultipartRequest(); + $request->addMultipartParams( [ 'testparam' => [] ] ); + } +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/SimpleRequestTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/SimpleRequestTest.php new file mode 100644 index 00000000..df979992 --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/SimpleRequestTest.php @@ -0,0 +1,49 @@ +<?php + +namespace Mediawiki\Api\Test\Unit; + +use Mediawiki\Api\SimpleRequest; +use PHPUnit_Framework_TestCase; + +/** + * @author Addshore + * + * @covers Mediawiki\Api\SimpleRequest + */ +class SimpleRequestTest extends PHPUnit_Framework_TestCase { + + /** + * @dataProvider provideValidConstruction + */ + public function testValidConstruction( $action, $params, $expected, $headers = [] ) { + $request = new SimpleRequest( $action, $params, $headers ); + $this->assertEquals( $expected, $request->getParams() ); + $this->assertEquals( $headers, $request->getHeaders() ); + } + + public function provideValidConstruction() { + return [ + [ 'action', [], [ 'action' => 'action' ] ], + [ '1123', [], [ 'action' => '1123' ] ], + [ 'a', [ 'b' => 'c' ], [ 'action' => 'a', 'b' => 'c' ] ], + [ 'a', [ 'b' => 'c', 'd' => 'e' ], [ 'action' => 'a', 'b' => 'c', 'd' => 'e' ] ], + [ 'a', [ 'b' => 'c|d|e|f' ], [ 'action' => 'a', 'b' => 'c|d|e|f' ] ], + [ 'foo', [], [ 'action' => 'foo' ] ,[ 'foo' => 'bar' ] ], + ]; + } + + /** + * @dataProvider provideInvalidConstruction + */ + public function testInvalidConstruction( $action, $params ) { + $this->setExpectedException( 'InvalidArgumentException' ); + new SimpleRequest( $action, $params ); + } + + public function provideInvalidConstruction() { + return [ + [ [], [] ], + ]; + } + +} diff --git a/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/UsageExceptionTest.php b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/UsageExceptionTest.php new file mode 100644 index 00000000..2b7d6072 --- /dev/null +++ b/bin/reevotech/vendor/addwiki/mediawiki-api-base/tests/Unit/UsageExceptionTest.php @@ -0,0 +1,39 @@ +<?php + +namespace Mediawiki\Api\Test\Unit; + +use Mediawiki\Api\UsageException; +use PHPUnit_Framework_TestCase; + +/** + * @author Addshore + * + * @covers Mediawiki\Api\UsageException + */ +class UsageExceptionTest extends PHPUnit_Framework_TestCase { + + public function testUsageExceptionWithNoParams() { + $e = new UsageException(); + $this->assertSame( + 'Code: ' . PHP_EOL . + 'Message: ' . PHP_EOL . + 'Result: []', + $e->getMessage() + ); + $this->assertSame( '', $e->getApiCode() ); + $this->assertEquals( [], $e->getApiResult() ); + } + + public function testUsageExceptionWithParams() { + $e = new UsageException( 'imacode', 'imamsg', [ 'foo' => 'bar' ] ); + $this->assertSame( 'imacode', $e->getApiCode() ); + $this->assertSame( + 'Code: imacode' . PHP_EOL . + 'Message: imamsg' . PHP_EOL . + 'Result: {"foo":"bar"}', + $e->getMessage() + ); + $this->assertEquals( [ 'foo' => 'bar' ], $e->getApiResult() ); + } + +} |