Merge "mediawiki.content.json: Remove file and author annotations"
[mediawiki.git] / tests / phpunit / includes / api / ApiRevisionDeleteTest.php
blobb248e09054da97fe9b1ef1ae42c169e70ccae56c
1 <?php
3 namespace MediaWiki\Tests\Api;
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;
13 /**
14 * Tests for action=revisiondelete
15 * @covers \MediaWiki\Api\ApiRevisionDelete
16 * @group API
17 * @group medium
18 * @group Database
20 class ApiRevisionDeleteTest extends ApiTestCase {
21 use MockAuthorityTrait;
23 /** @var int[] */
24 public $revs = [];
26 protected function setUp(): void {
27 parent::setUp();
28 // Make a few edits for us to play with
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();
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 // Check the output
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'] );
57 // Now check that that revision was actually hidden
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 ) );
65 // Now test unhiding!
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 );
75 // Check the output
76 $out2 = $out2[0]['revisiondelete'];
77 $this->assertEquals( 'Success', $out2['status'] );
78 $this->assertArrayHasKey( 'items', $out2 );
79 $item = $out2['items'][0];
81 $this->assertFalse( $item['userhidden'], 'userhidden' );
82 $this->assertFalse( $item['commenthidden'], 'commenthidden' );
83 $this->assertFalse( $item['texthidden'], 'texthidden' );
85 $this->assertEquals( $revid, $item['id'] );
87 // Now check that that revision was actually unhidden
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 ) );
96 public function testUnhidingOutput() {
97 $performer = $this->mockRegisteredAuthorityWithPermissions( [ 'deleterevision' ] );
98 $revid = array_shift( $this->revs );
99 // Hide revisions
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 );
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 // Check it has userhidden & texthidden
122 // but not commenthidden
123 $this->assertTrue( $item['userhidden'], 'userhidden' );
124 $this->assertFalse( $item['commenthidden'], 'commenthidden' );
125 $this->assertTrue( $item['texthidden'], 'texthidden' );
126 $this->assertEquals( $revid, $item['id'] );
129 public function testPartiallyBlockedPage() {
130 $this->expectApiErrorCode( 'blocked' );
131 $performer = $this->mockAnonAuthorityWithPermissions( [ 'deleterevision' ] );
133 $block = new DatabaseBlock( [
134 'address' => $performer->getUser(),
135 'by' => static::getTestSysop()->getUser(),
136 'sitewide' => false,
137 ] );
139 $title = Title::makeTitle( NS_HELP, 'ApiRevDel_test' );
140 $block->setRestrictions( [
141 new PageRestriction( 0, $title->getArticleID() )
142 ] );
143 $this->getServiceContainer()->getDatabaseBlockStore()->insertBlock( $block );
145 $revid = array_shift( $this->revs );
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 );