API: Add tests for useskin parameter of ApiParse
[lhc/web/wiklou.git] / tests / phpunit / includes / api / ApiParseTest.php
1 <?php
2
3 /**
4 * @group API
5 * @group Database
6 * @group medium
7 *
8 * @covers ApiParse
9 */
10 class ApiParseTest extends ApiTestCase {
11
12 protected static $pageId;
13 protected static $revIds = [];
14
15 public function addDBDataOnce() {
16 $user = static::getTestSysop()->getUser();
17 $title = Title::newFromText( __CLASS__ );
18 $page = WikiPage::factory( $title );
19
20 $status = $page->doEditContent(
21 ContentHandler::makeContent( 'Test for revdel', $title, CONTENT_MODEL_WIKITEXT ),
22 __METHOD__ . ' Test for revdel', 0, false, $user
23 );
24 if ( !$status->isOk() ) {
25 $this->fail( "Failed to create $title: " . $status->getWikiText( false, false, 'en' ) );
26 }
27 self::$pageId = $status->value['revision']->getPage();
28 self::$revIds['revdel'] = $status->value['revision']->getId();
29
30 $status = $page->doEditContent(
31 ContentHandler::makeContent( 'Test for oldid', $title, CONTENT_MODEL_WIKITEXT ),
32 __METHOD__ . ' Test for oldid', 0, false, $user
33 );
34 if ( !$status->isOk() ) {
35 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
36 }
37 self::$revIds['oldid'] = $status->value['revision']->getId();
38
39 $status = $page->doEditContent(
40 ContentHandler::makeContent( 'Test for latest', $title, CONTENT_MODEL_WIKITEXT ),
41 __METHOD__ . ' Test for latest', 0, false, $user
42 );
43 if ( !$status->isOk() ) {
44 $this->fail( "Failed to edit $title: " . $status->getWikiText( false, false, 'en' ) );
45 }
46 self::$revIds['latest'] = $status->value['revision']->getId();
47
48 RevisionDeleter::createList(
49 'revision', RequestContext::getMain(), $title, [ self::$revIds['revdel'] ]
50 )->setVisibility( [
51 'value' => [
52 Revision::DELETED_TEXT => 1,
53 ],
54 'comment' => 'Test for revdel',
55 ] );
56
57 Title::clearCaches(); // Otherwise it has the wrong latest revision for some reason
58 }
59
60 public function testParseByName() {
61 $res = $this->doApiRequest( [
62 'action' => 'parse',
63 'page' => __CLASS__,
64 ] );
65 $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
66
67 $res = $this->doApiRequest( [
68 'action' => 'parse',
69 'page' => __CLASS__,
70 'disablelimitreport' => 1,
71 ] );
72 $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
73 }
74
75 public function testParseById() {
76 $res = $this->doApiRequest( [
77 'action' => 'parse',
78 'pageid' => self::$pageId,
79 ] );
80 $this->assertContains( 'Test for latest', $res[0]['parse']['text'] );
81 }
82
83 public function testParseByOldId() {
84 $res = $this->doApiRequest( [
85 'action' => 'parse',
86 'oldid' => self::$revIds['oldid'],
87 ] );
88 $this->assertContains( 'Test for oldid', $res[0]['parse']['text'] );
89 $this->assertArrayNotHasKey( 'textdeleted', $res[0]['parse'] );
90 $this->assertArrayNotHasKey( 'textsuppressed', $res[0]['parse'] );
91 }
92
93 public function testParseRevDel() {
94 $user = static::getTestUser()->getUser();
95 $sysop = static::getTestSysop()->getUser();
96
97 try {
98 $this->doApiRequest( [
99 'action' => 'parse',
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'" );
106 }
107
108 $res = $this->doApiRequest( [
109 'action' => 'parse',
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'] );
115 }
116
117 public function testParseNonexistentPage() {
118 try {
119 $this->doApiRequest( [
120 'action' => 'parse',
121 'page' => 'DoesNotExist',
122 ] );
123
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 )
129 );
130 }
131 }
132
133 public function testSkinModules() {
134 $factory = new SkinFactory();
135 $factory->register( 'testing', 'Testing', function () {
136 $skin = $this->getMockBuilder( SkinFallback::class )
137 ->setMethods( [ 'getDefaultModules' ] )
138 ->getMock();
139 $skin->expects( $this->once() )->method( 'getDefaultModules' )
140 ->willReturn( [
141 'core' => [ 'foo', 'bar' ],
142 'content' => [ 'baz' ]
143 ] );
144 return $skin;
145 } );
146 $this->setService( 'SkinFactory', $factory );
147
148 $res = $this->doApiRequest( [
149 'action' => 'parse',
150 'pageid' => self::$pageId,
151 'useskin' => 'testing',
152 'prop' => 'modules',
153 ] );
154 $this->assertSame(
155 [ 'foo', 'bar', 'baz' ],
156 $res[0]['parse']['modules'],
157 'resp.parse.modules'
158 );
159 $this->assertSame(
160 [],
161 $res[0]['parse']['modulescripts'],
162 'resp.parse.modulescripts'
163 );
164 $this->assertSame(
165 [],
166 $res[0]['parse']['modulestyles'],
167 'resp.parse.modulestyles'
168 );
169 }
170 }