10 class ApiParseTest
extends ApiTestCase
{
12 protected static $pageId;
13 protected static $revIds = [];
15 public function addDBDataOnce() {
16 $user = static::getTestSysop()->getUser();
17 $title = Title
::newFromText( __CLASS__
);
18 $page = WikiPage
::factory( $title );
20 $status = $page->doEditContent(
21 ContentHandler
::makeContent( 'Test for revdel', $title, CONTENT_MODEL_WIKITEXT
),
22 __METHOD__
. ' Test for revdel', 0, false, $user
24 if ( !$status->isOk() ) {
25 $this->fail( "Failed to create $title: " . $status->getWikiText( false, false, 'en' ) );
27 self
::$pageId = $status->value
['revision']->getPage();
28 self
::$revIds['revdel'] = $status->value
['revision']->getId();
30 $status = $page->doEditContent(
31 ContentHandler
::makeContent( 'Test for oldid', $title, CONTENT_MODEL_WIKITEXT
),
32 __METHOD__
. ' Test for oldid', 0, false, $user
34 if ( !$status->isOk() ) {
35 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
37 self
::$revIds['oldid'] = $status->value
['revision']->getId();
39 $status = $page->doEditContent(
40 ContentHandler
::makeContent( 'Test for latest', $title, CONTENT_MODEL_WIKITEXT
),
41 __METHOD__
. ' Test for latest', 0, false, $user
43 if ( !$status->isOk() ) {
44 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
46 self
::$revIds['latest'] = $status->value
['revision']->getId();
48 RevisionDeleter
::createList(
49 'revision', RequestContext
::getMain(), $title, [ self
::$revIds['revdel'] ]
52 Revision
::DELETED_TEXT
=> 1,
54 'comment' => 'Test for revdel',
57 Title
::clearCaches(); // Otherwise it has the wrong latest revision for some reason
60 public function testParseByName() {
61 $res = $this->doApiRequest( [
65 $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
67 $res = $this->doApiRequest( [
70 'disablelimitreport' => 1,
72 $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
75 public function testParseById() {
76 $res = $this->doApiRequest( [
78 'pageid' => self
::$pageId,
80 $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
83 public function testParseByOldId() {
84 $res = $this->doApiRequest( [
86 'oldid' => self
::$revIds['oldid'],
88 $this->assertContains( 'Test for oldid', $res[0]['parse']['text'] );
89 $this->assertArrayNotHasKey( 'textdeleted', $res[0]['parse'] );
90 $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
93 public function testParseRevDel() {
94 $user = static::getTestUser()->getUser();
95 $sysop = static::getTestSysop()->getUser();
98 $this->doApiRequest( [
100 'oldid' => self
::$revIds['revdel'],
101 ], null, null, $user );
102 $this->fail( "API did not return an error as expected" );
103 } catch ( ApiUsageException
$ex ) {
104 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'permissiondenied' ),
105 "API failed with error 'permissiondenied'" );
108 $res = $this->doApiRequest( [
110 'oldid' => self
::$revIds['revdel'],
111 ], null, null, $sysop );
112 $this->assertContains( 'Test for revdel', $res[0]['parse']['text'] );
113 $this->assertArrayHasKey( 'textdeleted', $res[0]['parse'] );
114 $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
117 public function testParseNonexistentPage() {
119 $this->doApiRequest( [
121 'page' => 'DoesNotExist',
124 $this->fail( "API did not return an error when parsing a nonexistent page" );
125 } catch ( ApiUsageException
$ex ) {
126 $this->assertTrue( ApiTestCase
::apiExceptionHasCode( $ex, 'missingtitle' ),
127 "Parse request for nonexistent page must give 'missingtitle' error: "
128 . var_export( self
::getErrorFormatter()->arrayFromStatus( $ex->getStatusValue() ), true )