Debian Codegraph @

tests/phpunit/unit/includes/page/MovePageTest.php

raw symbols SCIP
1 <?php
2
3 namespace MediaWiki\Tests\Unit;
4
5 use MediaWiki\EditPage\SpamChecker;
6 use MediaWiki\Page\MovePage;
7 use MediaWiki\Page\PageIdentity;
8 use MediaWiki\Permissions\Authority;
9 use MediaWiki\Permissions\PermissionStatus;
10 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
11 use MediaWikiUnitTestCase;
12 use MockTitleTrait;
13
14 /**
15 * @coversDefaultClass \MediaWiki\Page\MovePage
16 * @method MovePage newServiceInstance(string $serviceClass, array $parameterOverrides)
17 */
18 class MovePageTest extends MediaWikiUnitTestCase {
19 use MockTitleTrait;
20 use MockAuthorityTrait;
21 use MockServiceDependenciesTrait;
22
23 public function provideCheckPermissions() {
24 yield 'all good and allowed' => [
25 'authority' => $this->mockRegisteredUltimateAuthority(),
26 'good' => true,
27 ];
28 yield 'cannot move' => [
29 'authority' => $this->mockAnonAuthority( function (
30 string $permission,
31 PageIdentity $page,
32 PermissionStatus $status
33 ) {
34 if ( $permission === 'move' ) {
35 $this->assertSame( 'Existent', $page->getDBkey() );
36 $status->fatal( 'test' );
37 return false;
38 }
39 return true;
40 } ),
41 'good' => false,
42 ];
43 yield 'cannot edit old page' => [
44 'authority' => $this->mockAnonAuthority( static function (
45 string $permission,
46 PageIdentity $page,
47 PermissionStatus $status
48 ) {
49 if ( $permission === 'edit' && $page->getDBkey() === 'Existent' ) {
50 $status->fatal( 'test' );
51 return false;
52 }
53 return true;
54 } ),
55 'good' => false,
56 ];
57 yield 'cannot move-target' => [
58 'authority' => $this->mockAnonAuthority( function (
59 string $permission,
60 PageIdentity $page,
61 PermissionStatus $status
62 ) {
63 if ( $permission === 'move-target' ) {
64 $this->assertSame( 'Existent2', $page->getDBkey() );
65 $status->fatal( 'test' );
66 return false;
67 }
68 return true;
69 } ),
70 'good' => false,
71 ];
72 yield 'cannot edit new page' => [
73 'authority' => $this->mockAnonAuthority( static function (
74 string $permission,
75 PageIdentity $page,
76 PermissionStatus $status
77 ) {
78 if ( $permission === 'edit' && $page->getDBkey() === 'Existent2' ) {
79 $status->fatal( 'test' );
80 return false;
81 }
82 return true;
83 } ),
84 'good' => false,
85 ];
86 }
87
88 /**
89 * @dataProvider provideCheckPermissions
90 * @covers \MediaWiki\Page\MovePage::checkPermissions
91 * @covers \MediaWiki\Page\MovePage::authorizeMove
92 * @covers \MediaWiki\Page\MovePage::probablyCanMove
93 */
94 public function testCheckPermissions( Authority $authority, bool $good ) {
95 $spamChecker = $this->createNoOpMock( SpamChecker::class, [ 'checkSummary' ] );
96 $spamChecker->method( 'checkSummary' )->willReturn( false );
97 $mp = $this->newServiceInstance(
98 MovePage::class,
99 [
100 'oldTitle' => $this->makeMockTitle( 'Existent' ),
101 'newTitle' => $this->makeMockTitle( 'Existent2' ),
102 'spamChecker' => $spamChecker,
103 ]
104 );
105 foreach ( [ 'checkPermissions', 'authorizeMove', 'probablyCanMove' ] as $method ) {
106 $permissionStatus = $mp->$method( $authority, 'Testing' );
107 $this->assertSame( $good, $permissionStatus->isGood() );
108 }
109 }
110
111 /**
112 * @covers \MediaWiki\Page\MovePage::checkPermissions
113 * @covers \MediaWiki\Page\MovePage::authorizeMove
114 * @covers \MediaWiki\Page\MovePage::probablyCanMove
115 */
116 public function testCheckPermissions_spam() {
117 $spamChecker = $this->createNoOpMock( SpamChecker::class, [ 'checkSummary' ] );
118 $spamChecker->method( 'checkSummary' )
119 ->willReturnCallback( static function ( string $reason ) {
120 return $reason === 'SPAM';
121 } );
122 $mp = $this->newServiceInstance(
123 MovePage::class,
124 [
125 'oldTitle' => $this->makeMockTitle( 'Existent' ),
126 'newTitle' => $this->makeMockTitle( 'Existent2' ),
127 'spamChecker' => $spamChecker,
128 ]
129 );
130 foreach ( [ 'checkPermissions', 'authorizeMove', 'probablyCanMove' ] as $method ) {
131 $notSpamStatus = $mp->$method( $this->mockRegisteredUltimateAuthority(), 'NOT_SPAM' );
132 $this->assertStatusGood( $notSpamStatus );
133 $spamStatus = $mp->$method( $this->mockRegisteredUltimateAuthority(), 'SPAM' );
134 $this->assertStatusError( 'spamprotectiontext', $spamStatus );
135 }
136 }
137 }