From 79e81234664731706e37444be4886f4c1ca49b68 Mon Sep 17 00:00:00 2001 From: addshore Date: Wed, 23 Oct 2013 16:36:40 +0100 Subject: [PATCH] Split ApiTest class into seperate module classes Also! - adds @covers tags Change-Id: I6d4f98f75cd3c2a52c982ece6dd295a4bf84a6fa --- tests/phpunit/includes/api/ApiBaseTest.php | 46 ++++++++++ .../api/{ApiTest.php => ApiLoginTest.php} | 90 +------------------ tests/phpunit/includes/api/ApiMainTest.php | 33 +++++++ tests/phpunit/includes/api/ApiTokensTest.php | 40 +++++++++ 4 files changed, 123 insertions(+), 86 deletions(-) create mode 100644 tests/phpunit/includes/api/ApiBaseTest.php rename tests/phpunit/includes/api/{ApiTest.php => ApiLoginTest.php} (66%) create mode 100644 tests/phpunit/includes/api/ApiMainTest.php create mode 100644 tests/phpunit/includes/api/ApiTokensTest.php diff --git a/tests/phpunit/includes/api/ApiBaseTest.php b/tests/phpunit/includes/api/ApiBaseTest.php new file mode 100644 index 0000000000..bfb75ef95e --- /dev/null +++ b/tests/phpunit/includes/api/ApiBaseTest.php @@ -0,0 +1,46 @@ +requireOnlyOneParameter( + array( "filename" => "foo.txt", "enablechunks" => false ), + "filename", "enablechunks" + ); + $this->assertTrue( true ); + } + + /** + * @expectedException UsageException + * @covers ApiBase::requireOnlyOneParameter + */ + public function testRequireOnlyOneParameterZero() { + $mock = new MockApi(); + $mock->requireOnlyOneParameter( + array( "filename" => "foo.txt","enablechunks" => 0 ), + "filename", "enablechunks" + ); + } + + /** + * @expectedException UsageException + * @covers ApiBase::requireOnlyOneParameter + */ + public function testRequireOnlyOneParameterTrue() { + $mock = new MockApi(); + $mock->requireOnlyOneParameter( + array( "filename" => "foo.txt", "enablechunks" => true ), + "filename", "enablechunks" + ); + } + +} diff --git a/tests/phpunit/includes/api/ApiTest.php b/tests/phpunit/includes/api/ApiLoginTest.php similarity index 66% rename from tests/phpunit/includes/api/ApiTest.php rename to tests/phpunit/includes/api/ApiLoginTest.php index 472f8c4a31..f1199e0f49 100644 --- a/tests/phpunit/includes/api/ApiTest.php +++ b/tests/phpunit/includes/api/ApiLoginTest.php @@ -4,59 +4,10 @@ * @group API * @group Database * @group medium + * + * @covers ApiLogin */ -class ApiTest extends ApiTestCase { - - public function testRequireOnlyOneParameterDefault() { - $mock = new MockApi(); - - $this->assertEquals( - null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt", - "enablechunks" => false ), "filename", "enablechunks" ) ); - } - - /** - * @expectedException UsageException - */ - public function testRequireOnlyOneParameterZero() { - $mock = new MockApi(); - - $this->assertEquals( - null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt", - "enablechunks" => 0 ), "filename", "enablechunks" ) ); - } - - /** - * @expectedException UsageException - */ - public function testRequireOnlyOneParameterTrue() { - $mock = new MockApi(); - - $this->assertEquals( - null, $mock->requireOnlyOneParameter( array( "filename" => "foo.txt", - "enablechunks" => true ), "filename", "enablechunks" ) ); - } - - /** - * Test that the API will accept a FauxRequest and execute. The help action - * (default) throws a UsageException. Just validate we're getting proper XML - * - * @expectedException UsageException - */ - public function testApi() { - $api = new ApiMain( - new FauxRequest( array( 'action' => 'help', 'format' => 'xml' ) ) - ); - $api->execute(); - $api->getPrinter()->setBufferResult( true ); - $api->printResult( false ); - $resp = $api->getPrinter()->getBuffer(); - - libxml_use_internal_errors( true ); - $sxe = simplexml_load_string( $resp ); - $this->assertNotInternalType( "bool", $sxe ); - $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); - } +class ApiLoginTest extends ApiTestCase { /** * Test result of attempted login with an empty username @@ -155,7 +106,7 @@ class ApiTest extends ApiTestCase { /** * @group Broken */ - public function testApiGotCookie() { + public function testApiLoginGotCookie() { $this->markTestIncomplete( "The server can't do external HTTP requests, and the internal one won't give cookies" ); global $wgServer, $wgScriptPath; @@ -197,8 +148,6 @@ class ApiTest extends ApiTestCase { $serializedCookie = $cj->serializeToHttpRequest( $wgScriptPath, $serverName ); $this->assertNotEquals( '', $serializedCookie ); $this->assertRegexp( '/_session=[^;]*; .*UserID=[0-9]*; .*UserName=' . $user->userName . '; .*Token=/', $serializedCookie ); - - return $cj; } public function testRunLogin() { @@ -223,37 +172,6 @@ class ApiTest extends ApiTestCase { $this->assertArrayHasKey( "result", $data[0]['login'] ); $this->assertEquals( "Success", $data[0]['login']['result'] ); $this->assertArrayHasKey( 'lgtoken', $data[0]['login'] ); - - return $data; } - public function testGettingToken() { - foreach ( self::$users as $user ) { - $this->runTokenTest( $user ); - } - } - - function runTokenTest( $user ) { - $tokens = $this->getTokenList( $user ); - - $rights = $user->user->getRights(); - - $this->assertArrayHasKey( 'edittoken', $tokens ); - $this->assertArrayHasKey( 'movetoken', $tokens ); - - if ( isset( $rights['delete'] ) ) { - $this->assertArrayHasKey( 'deletetoken', $tokens ); - } - - if ( isset( $rights['block'] ) ) { - $this->assertArrayHasKey( 'blocktoken', $tokens ); - $this->assertArrayHasKey( 'unblocktoken', $tokens ); - } - - if ( isset( $rights['protect'] ) ) { - $this->assertArrayHasKey( 'protecttoken', $tokens ); - } - - return $tokens; - } } diff --git a/tests/phpunit/includes/api/ApiMainTest.php b/tests/phpunit/includes/api/ApiMainTest.php new file mode 100644 index 0000000000..4ed5aa9721 --- /dev/null +++ b/tests/phpunit/includes/api/ApiMainTest.php @@ -0,0 +1,33 @@ + 'help', 'format' => 'xml' ) ) + ); + $api->execute(); + $api->getPrinter()->setBufferResult( true ); + $api->printResult( false ); + $resp = $api->getPrinter()->getBuffer(); + + libxml_use_internal_errors( true ); + $sxe = simplexml_load_string( $resp ); + $this->assertNotInternalType( "bool", $sxe ); + $this->assertThat( $sxe, $this->isInstanceOf( "SimpleXMLElement" ) ); + } + +} diff --git a/tests/phpunit/includes/api/ApiTokensTest.php b/tests/phpunit/includes/api/ApiTokensTest.php new file mode 100644 index 0000000000..fbe97893d8 --- /dev/null +++ b/tests/phpunit/includes/api/ApiTokensTest.php @@ -0,0 +1,40 @@ +runTokenTest( $user ); + } + } + + protected function runTokenTest( $user ) { + $tokens = $this->getTokenList( $user ); + + $rights = $user->user->getRights(); + + $this->assertArrayHasKey( 'edittoken', $tokens ); + $this->assertArrayHasKey( 'movetoken', $tokens ); + + if ( isset( $rights['delete'] ) ) { + $this->assertArrayHasKey( 'deletetoken', $tokens ); + } + + if ( isset( $rights['block'] ) ) { + $this->assertArrayHasKey( 'blocktoken', $tokens ); + $this->assertArrayHasKey( 'unblocktoken', $tokens ); + } + + if ( isset( $rights['protect'] ) ) { + $this->assertArrayHasKey( 'protecttoken', $tokens ); + } + } + +} -- 2.20.1