| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api\Query; |
| 4 |
|
| 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; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
class ApiQueryRevisionsTest extends ApiTestCase { |
| 20 |
use TempUserTestTrait; |
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 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' ) ); |
| 32 |
|
| 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' |
| 49 |
); |
| 50 |
$this->assertArrayHasKey( 'contentmodel', $revision['slots']['main'], |
| 51 |
'contentmodel should be included when asking content so client knows how to interpret it' |
| 52 |
); |
| 53 |
} |
| 54 |
} |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
public function testRevisionMadeByTempUser() { |
| 62 |
$this->enableAutoCreateTempUser(); |
| 63 |
$tempUser = new UserIdentityValue( 1236764321, '~1' ); |
| 64 |
|
| 65 |
$title = $this->getNonexistingTestPage( 'TestPage1' )->getTitle(); |
| 66 |
$this->editPage( |
| 67 |
$title, |
| 68 |
'Some Content', |
| 69 |
'Create Page', |
| 70 |
NS_MAIN, |
| 71 |
new UltimateAuthority( $tempUser ) |
| 72 |
); |
| 73 |
|
| 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'] ); |
| 83 |
} |
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
public function testResolvesPrevNextInDiffto() { |
| 89 |
$pageName = 'Help:' . __METHOD__; |
| 90 |
$page = $this->getExistingTestPage( $pageName ); |
| 91 |
$user = $this->getTestUser()->getUser(); |
| 92 |
|
| 93 |
$revRecord = $page->newPageUpdater( $user ) |
| 94 |
->setContent( SlotRecord::MAIN, new WikitextContent( 'Some text' ) ) |
| 95 |
->saveRevision( CommentStoreComment::newUnsavedComment( 'inserting more content' ) ); |
| 96 |
|
| 97 |
[ $rvDiffToPrev ] = $this->doApiRequest( [ |
| 98 |
'action' => 'query', |
| 99 |
'prop' => 'revisions', |
| 100 |
'titles' => $pageName, |
| 101 |
'rvdiffto' => 'prev', |
| 102 |
] ); |
| 103 |
|
| 104 |
$this->assertSame( |
| 105 |
$revRecord->getId(), |
| 106 |
$rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['revid'] |
| 107 |
); |
| 108 |
$this->assertSame( |
| 109 |
$revRecord->getId(), |
| 110 |
$rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['to'] |
| 111 |
); |
| 112 |
$this->assertSame( |
| 113 |
$revRecord->getParentId(), |
| 114 |
$rvDiffToPrev['query']['pages'][$page->getId()]['revisions'][0]['diff']['from'] |
| 115 |
); |
| 116 |
|
| 117 |
[ $rvDiffToNext ] = $this->doApiRequest( [ |
| 118 |
'action' => 'query', |
| 119 |
'prop' => 'revisions', |
| 120 |
'titles' => $pageName, |
| 121 |
'rvdiffto' => 'next', |
| 122 |
'rvdir' => 'newer' |
| 123 |
] ); |
| 124 |
|
| 125 |
$this->assertSame( |
| 126 |
$revRecord->getParentId(), |
| 127 |
$rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['revid'] |
| 128 |
); |
| 129 |
$this->assertSame( |
| 130 |
$revRecord->getId(), |
| 131 |
$rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['to'] |
| 132 |
); |
| 133 |
$this->assertSame( |
| 134 |
$revRecord->getParentId(), |
| 135 |
$rvDiffToNext['query']['pages'][$page->getId()]['revisions'][0]['diff']['from'] |
| 136 |
); |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
public function testSectionNewReturnsEmptyContentForPageWithSection( |
| 146 |
$pageContent, |
| 147 |
$expectedSectionContent |
| 148 |
) { |
| 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' ) ); |
| 155 |
|
| 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 |
] ); |
| 164 |
|
| 165 |
$this->assertSame( |
| 166 |
$expectedSectionContent, |
| 167 |
$response['query']['pages'][$page->getId()]['revisions'][0]['slots']['main']['content'] |
| 168 |
); |
| 169 |
} |
| 170 |
|
| 171 |
public static function provideSectionNewTestCases() { |
| 172 |
yield 'page with existing section' => [ |
| 173 |
"==A section==\ntext", |
| 174 |
'' |
| 175 |
]; |
| 176 |
yield 'page with no sections' => [ |
| 177 |
'This page has no sections', |
| 178 |
'This page has no sections' |
| 179 |
]; |
| 180 |
} |
| 181 |
} |