From 1891a1e972b3eb0db21776cce4ce8bad0ae59f4c Mon Sep 17 00:00:00 2001 From: Yifei He Date: Sun, 14 Jan 2018 17:52:57 +0800 Subject: [PATCH] Add PHPUnit tests for ApiDelete This covers deleting non-file pages. Bug: T183886 Change-Id: I31c27786b16b55f8bf4cf528bf4c1ea49075e02e --- tests/phpunit/includes/api/ApiDeleteTest.php | 79 ++++++++++++++++++++ 1 file changed, 79 insertions(+) create mode 100644 tests/phpunit/includes/api/ApiDeleteTest.php diff --git a/tests/phpunit/includes/api/ApiDeleteTest.php b/tests/phpunit/includes/api/ApiDeleteTest.php new file mode 100644 index 0000000000..87167f034f --- /dev/null +++ b/tests/phpunit/includes/api/ApiDeleteTest.php @@ -0,0 +1,79 @@ +doLogin(); + } + + public function testDelete() { + $name = 'Help:ApiDeleteTest_testDelete'; + + // test non-existing page + try { + $this->doApiRequestWithToken( [ + 'action' => 'delete', + 'title' => $name, + ] ); + $this->fail( "Should have raised an ApiUsageException" ); + } catch ( ApiUsageException $e ) { + $this->assertTrue( self::apiExceptionHasCode( $e, 'missingtitle' ) ); + } + + // create new page + $this->editPage( $name, 'Some text' ); + + // test deletion + $apiResult = $this->doApiRequestWithToken( [ + 'action' => 'delete', + 'title' => $name, + ] ); + $apiResult = $apiResult[0]; + + $this->assertArrayHasKey( 'delete', $apiResult ); + $this->assertArrayHasKey( 'title', $apiResult['delete'] ); + // Normalized $name is used + $this->assertSame( + 'Help:ApiDeleteTest testDelete', + $apiResult['delete']['title'] + ); + $this->assertArrayHasKey( 'logid', $apiResult['delete'] ); + + $this->assertFalse( Title::newFromText( $name )->exists() ); + } + + public function testDeletionWithoutPermission() { + $name = 'Help:ApiDeleteTest_testDeleteWithoutPermission'; + + // create new page + $this->editPage( $name, 'Some text' ); + + // test deletion without permission + try { + $user = new User(); + $apiResult = $this->doApiRequest( [ + 'action' => 'delete', + 'title' => $name, + 'token' => $user->getEditToken(), + ], null, null, $user ); + $this->fail( "Should have raised an ApiUsageException" ); + } catch ( ApiUsageException $e ) { + $this->assertTrue( self::apiExceptionHasCode( $e, 'permissiondenied' ) ); + } + + $this->assertTrue( Title::newFromText( $name )->exists() ); + } +} -- 2.20.1