X-Git-Url: http://git.heureux-cyclage.org/?a=blobdiff_plain;f=tests%2Fphpunit%2Fincludes%2Fapi%2FApiParseTest.php;h=028d3b413536f7c4f7ce636ae2e570782c6fdaed;hb=85ac1b5d7c7ca50a93f2d16639cdde2f46bc133e;hp=b72a4f8a8b8b27879af44e699ce97dbeafecd543;hpb=adae996840b9e782f6e14b21c433a83e37c3a74e;p=lhc%2Fweb%2Fwiklou.git diff --git a/tests/phpunit/includes/api/ApiParseTest.php b/tests/phpunit/includes/api/ApiParseTest.php index b72a4f8a8b..028d3b4135 100644 --- a/tests/phpunit/includes/api/ApiParseTest.php +++ b/tests/phpunit/includes/api/ApiParseTest.php @@ -9,26 +9,123 @@ */ class ApiParseTest extends ApiTestCase { - protected function setUp() { - parent::setUp(); - $this->doLogin(); + protected static $pageId; + protected static $revIds = []; + + public function addDBDataOnce() { + $user = static::getTestSysop()->getUser(); + $title = Title::newFromText( __CLASS__ ); + $page = WikiPage::factory( $title ); + + $status = $page->doEditContent( + ContentHandler::makeContent( 'Test for revdel', $title, CONTENT_MODEL_WIKITEXT ), + __METHOD__ . ' Test for revdel', 0, false, $user + ); + if ( !$status->isOk() ) { + $this->fail( "Failed to create $title: " . $status->getWikiText( false, false, 'en' ) ); + } + self::$pageId = $status->value['revision']->getPage(); + self::$revIds['revdel'] = $status->value['revision']->getId(); + + $status = $page->doEditContent( + ContentHandler::makeContent( 'Test for oldid', $title, CONTENT_MODEL_WIKITEXT ), + __METHOD__ . ' Test for oldid', 0, false, $user + ); + if ( !$status->isOk() ) { + $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) ); + } + self::$revIds['oldid'] = $status->value['revision']->getId(); + + $status = $page->doEditContent( + ContentHandler::makeContent( 'Test for latest', $title, CONTENT_MODEL_WIKITEXT ), + __METHOD__ . ' Test for latest', 0, false, $user + ); + if ( !$status->isOk() ) { + $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) ); + } + self::$revIds['latest'] = $status->value['revision']->getId(); + + RevisionDeleter::createList( + 'revision', RequestContext::getMain(), $title, [ self::$revIds['revdel'] ] + )->setVisibility( [ + 'value' => [ + Revision::DELETED_TEXT => 1, + ], + 'comment' => 'Test for revdel', + ] ); + + Title::clearCaches(); // Otherwise it has the wrong latest revision for some reason } - public function testParseNonexistentPage() { - $somePage = mt_rand(); + public function testParseByName() { + $res = $this->doApiRequest( [ + 'action' => 'parse', + 'page' => __CLASS__, + ] ); + $this->assertContains( 'Test for latest', $res[0]['parse']['text'] ); + $res = $this->doApiRequest( [ + 'action' => 'parse', + 'page' => __CLASS__, + 'disablelimitreport' => 1, + ] ); + $this->assertContains( 'Test for latest', $res[0]['parse']['text'] ); + } + + public function testParseById() { + $res = $this->doApiRequest( [ + 'action' => 'parse', + 'pageid' => self::$pageId, + ] ); + $this->assertContains( 'Test for latest', $res[0]['parse']['text'] ); + } + + public function testParseByOldId() { + $res = $this->doApiRequest( [ + 'action' => 'parse', + 'oldid' => self::$revIds['oldid'], + ] ); + $this->assertContains( 'Test for oldid', $res[0]['parse']['text'] ); + $this->assertArrayNotHasKey( 'textdeleted', $res[0]['parse'] ); + $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] ); + } + + public function testParseRevDel() { + $user = static::getTestUser()->getUser(); + $sysop = static::getTestSysop()->getUser(); + + try { + $this->doApiRequest( [ + 'action' => 'parse', + 'oldid' => self::$revIds['revdel'], + ], null, null, $user ); + $this->fail( "API did not return an error as expected" ); + } catch ( ApiUsageException $ex ) { + $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'permissiondenied' ), + "API failed with error 'permissiondenied'" ); + } + + $res = $this->doApiRequest( [ + 'action' => 'parse', + 'oldid' => self::$revIds['revdel'], + ], null, null, $sysop ); + $this->assertContains( 'Test for revdel', $res[0]['parse']['text'] ); + $this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] ); + $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] ); + } + + public function testParseNonexistentPage() { try { $this->doApiRequest( [ 'action' => 'parse', - 'page' => $somePage ] ); + 'page' => 'DoesNotExist', + ] ); $this->fail( "API did not return an error when parsing a nonexistent page" ); - } catch ( UsageException $ex ) { - $this->assertEquals( - 'missingtitle', - $ex->getCodeString(), + } catch ( ApiUsageException $ex ) { + $this->assertTrue( ApiTestCase::apiExceptionHasCode( $ex, 'missingtitle' ), "Parse request for nonexistent page must give 'missingtitle' error: " - . var_export( $ex->getMessageArray(), true ) + . var_export( self::getErrorFormatter()->arrayFromStatus( $ex->getStatusValue() ), true ) ); } }