Localisation updates from https://translatewiki.net.
[mediawiki.git] / tests / phpunit / includes / api / query / ApiQueryRevisionsTest.php
blob5e4cebb3d266754d6e4e701d73a91f0417243d65
1 <?php
3 namespace MediaWiki\Tests\Api\Query;
5 use MediaWiki\CommentStore\CommentStoreComment;
6 use MediaWiki\Content\WikitextContent;
7 use MediaWiki\Permissions\UltimateAuthority;
8 use MediaWiki\Revision\SlotRecord;
9 use MediaWiki\Tests\Api\ApiTestCase;
10 use MediaWiki\Tests\User\TempUser\TempUserTestTrait;
11 use MediaWiki\User\UserIdentityValue;
13 /**
14 * @group API
15 * @group Database
16 * @group medium
17 * @covers \MediaWiki\Api\ApiQueryRevisions
19 class ApiQueryRevisionsTest extends ApiTestCase {
20 use TempUserTestTrait;
22 /**
23 * @group medium
25 public function testContentComesWithContentModelAndFormat() {
26 $pageName = 'Help:' . __METHOD__;
27 $page = $this->getExistingTestPage( $pageName );
28 $user = $this->getTestUser()->getUser();
29 $page->newPageUpdater( $user )
30 ->setContent( SlotRecord::MAIN, new WikitextContent( 'Some text' ) )
31 ->saveRevision( CommentStoreComment::newUnsavedComment( 'inserting content' ) );
33 $apiResult = $this->doApiRequest( [
34 'action' => 'query',
35 'prop' => 'revisions',
36 'titles' => $pageName,
37 'rvprop' => 'content',
38 'rvslots' => 'main',
39 ] );
40 $this->assertArrayHasKey( 'query', $apiResult[0] );
41 $this->assertArrayHasKey( 'pages', $apiResult[0]['query'] );
42 foreach ( $apiResult[0]['query']['pages'] as $page ) {
43 $this->assertArrayHasKey( 'revisions', $page );
44 foreach ( $page['revisions'] as $revision ) {
45 $this->assertArrayHasKey( 'slots', $revision );
46 $this->assertArrayHasKey( 'main', $revision['slots'] );
47 $this->assertArrayHasKey( 'contentformat', $revision['slots']['main'],
48 'contentformat should be included when asking content so client knows how to interpret it'
50 $this->assertArrayHasKey( 'contentmodel', $revision['slots']['main'],
51 'contentmodel should be included when asking content so client knows how to interpret it'
57 /**
58 * @group Database
59 * @group medium
61 public function testRevisionMadeByTempUser() {
62 $this->enableAutoCreateTempUser();
63 $tempUser = new UserIdentityValue( 1236764321, '~1' );
65 $title = $this->getNonexistingTestPage( 'TestPage1' )->getTitle();
66 $this->editPage(
67 $title,
68 'Some Content',
69 'Create Page',
70 NS_MAIN,
71 new UltimateAuthority( $tempUser )
74 $apiResult = $this->doApiRequest( [
75 'action' => 'query',
76 'prop' => 'revisions',
77 'titles' => 'TestPage1'
78 ] );
79 $this->assertArrayHasKey( 'query', $apiResult[0] );
80 $this->assertArrayHasKey( 'pages', $apiResult[0]['query'] );
81 $this->assertArrayHasKey( 'temp', $apiResult[0]['query']['pages'][1]['revisions'][0] );
82 $this->assertTrue( $apiResult[0]['query']['pages'][1]['revisions'][0]['temp'] );
85 /**
86 * @group medium
88 public function testResolvesPrevNextInDiffto() {
89 $pageName = 'Help:' . __METHOD__;
90 $page = $this->getExistingTestPage( $pageName );
91 $user = $this->getTestUser()->getUser();
93 $revRecord = $page->newPageUpdater( $user )
94 ->setContent( SlotRecord::MAIN, new WikitextContent( 'Some text' ) )
95 ->saveRevision( CommentStoreComment::newUnsavedComment( 'inserting more content' ) );
97 [ $rvDiffToPrev ] = $this->doApiRequest( [
98 'action' => 'query',
99 'prop' => 'revisions',
100 'titles' => $pageName,
101 'rvdiffto' => 'prev',
102 ] );
104 $this->assertSame(
105 $revRecord->getId(),
106 $rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['revid']
108 $this->assertSame(
109 $revRecord->getId(),
110 $rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['to']
112 $this->assertSame(
113 $revRecord->getParentId(),
114 $rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['from']
117 [ $rvDiffToNext ] = $this->doApiRequest( [
118 'action' => 'query',
119 'prop' => 'revisions',
120 'titles' => $pageName,
121 'rvdiffto' => 'next',
122 'rvdir' => 'newer'
123 ] );
125 $this->assertSame(
126 $revRecord->getParentId(),
127 $rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['revid']
129 $this->assertSame(
130 $revRecord->getId(),
131 $rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['to']
133 $this->assertSame(
134 $revRecord->getParentId(),
135 $rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['from']
140 * @dataProvider provideSectionNewTestCases
141 * @param string $pageContent
142 * @param string $expectedSectionContent
143 * @group medium
145 public function testSectionNewReturnsEmptyContentForPageWithSection(
146 $pageContent,
147 $expectedSectionContent
149 $pageName = 'Help:' . __METHOD__;
150 $page = $this->getExistingTestPage( $pageName );
151 $user = $this->getTestUser()->getUser();
152 $revRecord = $page->newPageUpdater( $user )
153 ->setContent( SlotRecord::MAIN, new WikitextContent( $pageContent ) )
154 ->saveRevision( CommentStoreComment::newUnsavedComment( 'inserting content' ) );
156 [ $response ] = $this->doApiRequest( [
157 'action' => 'query',
158 'prop' => 'revisions',
159 'revids' => $revRecord->getId(),
160 'rvprop' => 'content|ids',
161 'rvslots' => 'main',
162 'rvsection' => 'new'
163 ] );
165 $this->assertSame(
166 $expectedSectionContent,
167 $response['query']['pages'][$page->getId()]['revisions'][0]['slots']['main']['content']
171 public static function provideSectionNewTestCases() {
172 yield 'page with existing section' => [
173 "==A section==\ntext",
176 yield 'page with no sections' => [
177 'This page has no sections',
178 'This page has no sections'