| 1 |
<?php |
| 2 |
|
| 3 |
namespace MediaWiki\Tests\Api; |
| 4 |
|
| 5 |
use MediaWiki\Block\DatabaseBlock; |
| 6 |
use MediaWiki\Block\Restriction\PageRestriction; |
| 7 |
use MediaWiki\Revision\RevisionRecord; |
| 8 |
use MediaWiki\Revision\SlotRecord; |
| 9 |
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait; |
| 10 |
use MediaWiki\Title\Title; |
| 11 |
use MWCryptRand; |
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
class ApiRevisionDeleteTest extends ApiTestCase { |
| 21 |
use MockAuthorityTrait; |
| 22 |
|
| 23 |
|
| 24 |
public $revs = []; |
| 25 |
|
| 26 |
protected function setUp(): void { |
| 27 |
parent::setUp(); |
| 28 |
|
| 29 |
$title = Title::makeTitle( NS_HELP, 'ApiRevDel_test' ); |
| 30 |
for ( $i = 1; $i <= 5; $i++ ) { |
| 31 |
$status = $this->editPage( $title, MWCryptRand::generateHex( 10 ), 'summary' ); |
| 32 |
$this->revs[] = $status->getNewRevision()->getId(); |
| 33 |
} |
| 34 |
} |
| 35 |
|
| 36 |
public function testHidingRevisions() { |
| 37 |
$performer = $this->mockRegisteredAuthorityWithPermissions( [ 'deleterevision' ] ); |
| 38 |
$revid = array_shift( $this->revs ); |
| 39 |
$out = $this->doApiRequestWithToken( [ |
| 40 |
'action' => 'revisiondelete', |
| 41 |
'reason' => __METHOD__, |
| 42 |
'type' => 'revision', |
| 43 |
'target' => 'Help:ApiRevDel_test', |
| 44 |
'ids' => $revid, |
| 45 |
'hide' => 'content|user|comment', |
| 46 |
], null, $performer ); |
| 47 |
|
| 48 |
$out = $out[0]['revisiondelete']; |
| 49 |
$this->assertEquals( 'Success', $out['status'] ); |
| 50 |
$this->assertArrayHasKey( 'items', $out ); |
| 51 |
$item = $out['items'][0]; |
| 52 |
$this->assertTrue( $item['userhidden'], 'userhidden' ); |
| 53 |
$this->assertTrue( $item['commenthidden'], 'commenthidden' ); |
| 54 |
$this->assertTrue( $item['texthidden'], 'texthidden' ); |
| 55 |
$this->assertEquals( $revid, $item['id'] ); |
| 56 |
|
| 57 |
|
| 58 |
$revRecord = $this->getServiceContainer() |
| 59 |
->getRevisionLookup() |
| 60 |
->getRevisionById( $revid ); |
| 61 |
$this->assertNull( $revRecord->getContent( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC ) ); |
| 62 |
$this->assertNull( $revRecord->getComment( RevisionRecord::FOR_PUBLIC ) ); |
| 63 |
$this->assertNull( $revRecord->getUser( RevisionRecord::FOR_PUBLIC ) ); |
| 64 |
|
| 65 |
|
| 66 |
$out2 = $this->doApiRequestWithToken( [ |
| 67 |
'action' => 'revisiondelete', |
| 68 |
'reason' => __METHOD__, |
| 69 |
'type' => 'revision', |
| 70 |
'target' => 'Help:ApiRevDel_test', |
| 71 |
'ids' => $revid, |
| 72 |
'show' => 'content|user|comment', |
| 73 |
], null, $performer ); |
| 74 |
|
| 75 |
|
| 76 |
$out2 = $out2[0]['revisiondelete']; |
| 77 |
$this->assertEquals( 'Success', $out2['status'] ); |
| 78 |
$this->assertArrayHasKey( 'items', $out2 ); |
| 79 |
$item = $out2['items'][0]; |
| 80 |
|
| 81 |
$this->assertFalse( $item['userhidden'], 'userhidden' ); |
| 82 |
$this->assertFalse( $item['commenthidden'], 'commenthidden' ); |
| 83 |
$this->assertFalse( $item['texthidden'], 'texthidden' ); |
| 84 |
|
| 85 |
$this->assertEquals( $revid, $item['id'] ); |
| 86 |
|
| 87 |
|
| 88 |
$revRecord = $this->getServiceContainer() |
| 89 |
->getRevisionLookup() |
| 90 |
->getRevisionById( $revid ); |
| 91 |
$this->assertNotNull( $revRecord->getContent( SlotRecord::MAIN, RevisionRecord::FOR_PUBLIC ) ); |
| 92 |
$this->assertNotNull( $revRecord->getComment( RevisionRecord::FOR_PUBLIC ) ); |
| 93 |
$this->assertNotNull( $revRecord->getUser( RevisionRecord::FOR_PUBLIC ) ); |
| 94 |
} |
| 95 |
|
| 96 |
public function testUnhidingOutput() { |
| 97 |
$performer = $this->mockRegisteredAuthorityWithPermissions( [ 'deleterevision' ] ); |
| 98 |
$revid = array_shift( $this->revs ); |
| 99 |
|
| 100 |
$this->doApiRequestWithToken( [ |
| 101 |
'action' => 'revisiondelete', |
| 102 |
'reason' => __METHOD__, |
| 103 |
'type' => 'revision', |
| 104 |
'target' => 'Help:ApiRevDel_test', |
| 105 |
'ids' => $revid, |
| 106 |
'hide' => 'content|user|comment', |
| 107 |
], null, $performer ); |
| 108 |
|
| 109 |
$out = $this->doApiRequestWithToken( [ |
| 110 |
'action' => 'revisiondelete', |
| 111 |
'reason' => __METHOD__, |
| 112 |
'type' => 'revision', |
| 113 |
'target' => 'Help:ApiRevDel_test', |
| 114 |
'ids' => $revid, |
| 115 |
'show' => 'comment', |
| 116 |
], null, $performer ); |
| 117 |
$out = $out[0]['revisiondelete']; |
| 118 |
$this->assertEquals( 'Success', $out['status'] ); |
| 119 |
$this->assertArrayHasKey( 'items', $out ); |
| 120 |
$item = $out['items'][0]; |
| 121 |
|
| 122 |
|
| 123 |
$this->assertTrue( $item['userhidden'], 'userhidden' ); |
| 124 |
$this->assertFalse( $item['commenthidden'], 'commenthidden' ); |
| 125 |
$this->assertTrue( $item['texthidden'], 'texthidden' ); |
| 126 |
$this->assertEquals( $revid, $item['id'] ); |
| 127 |
} |
| 128 |
|
| 129 |
public function testPartiallyBlockedPage() { |
| 130 |
$this->expectApiErrorCode( 'blocked' ); |
| 131 |
$performer = $this->mockAnonAuthorityWithPermissions( [ 'deleterevision' ] ); |
| 132 |
|
| 133 |
$block = new DatabaseBlock( [ |
| 134 |
'address' => $performer->getUser(), |
| 135 |
'by' => static::getTestSysop()->getUser(), |
| 136 |
'sitewide' => false, |
| 137 |
] ); |
| 138 |
|
| 139 |
$title = Title::makeTitle( NS_HELP, 'ApiRevDel_test' ); |
| 140 |
$block->setRestrictions( [ |
| 141 |
new PageRestriction( 0, $title->getArticleID() ) |
| 142 |
] ); |
| 143 |
$this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block ); |
| 144 |
|
| 145 |
$revid = array_shift( $this->revs ); |
| 146 |
|
| 147 |
$this->doApiRequestWithToken( [ |
| 148 |
'action' => 'revisiondelete', |
| 149 |
'reason' => __METHOD__, |
| 150 |
'type' => 'revision', |
| 151 |
'target' => $title->getPrefixedText(), |
| 152 |
'ids' => $revid, |
| 153 |
'hide' => 'content|user|comment', |
| 154 |
], null, $performer ); |
| 155 |
} |
| 156 |
} |