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
;
17 * @covers \MediaWiki\Api\ApiQueryRevisions
19 class ApiQueryRevisionsTest
extends ApiTestCase
{
20 use TempUserTestTrait
;
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( [
35 'prop' => 'revisions',
36 'titles' => $pageName,
37 'rvprop' => 'content',
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'
61 public function testRevisionMadeByTempUser() {
62 $this->enableAutoCreateTempUser();
63 $tempUser = new UserIdentityValue( 1236764321, '~1' );
65 $title = $this->getNonexistingTestPage( 'TestPage1' )->getTitle();
71 new UltimateAuthority( $tempUser )
74 $apiResult = $this->doApiRequest( [
76 'prop' => 'revisions',
77 'titles' => 'TestPage1'
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'] );
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( [
99 'prop' => 'revisions',
100 'titles' => $pageName,
101 'rvdiffto' => 'prev',
106 $rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['revid']
110 $rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['to']
113 $revRecord->getParentId(),
114 $rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['from']
117 [ $rvDiffToNext ] = $this->doApiRequest( [
119 'prop' => 'revisions',
120 'titles' => $pageName,
121 'rvdiffto' => 'next',
126 $revRecord->getParentId(),
127 $rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['revid']
131 $rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['to']
134 $revRecord->getParentId(),
135 $rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['from']
140 * @dataProvider provideSectionNewTestCases
141 * @param string $pageContent
142 * @param string $expectedSectionContent
145 public function testSectionNewReturnsEmptyContentForPageWithSection(
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( [
158 'prop' => 'revisions',
159 'revids' => $revRecord->getId(),
160 'rvprop' => 'content|ids',
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'