Debian Codegraph @

tests/phpunit/unit/includes/editpage/Constraint/EditRightConstraintTest.php

raw symbols SCIP
1 <?php
2 /**
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation; either version 2 of the License, or
6 * (at your option) any later version.
7 *
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
12 *
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 * http://www.gnu.org/copyleft/gpl.html
17 *
18 * @file
19 */
20
21 use MediaWiki\EditPage\Constraint\EditRightConstraint;
22 use MediaWiki\EditPage\Constraint\IEditConstraint;
23 use MediaWiki\Permissions\PermissionManager;
24 use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
25 use MediaWiki\Title\Title;
26 use MediaWiki\User\User;
27
28 /**
29 * Tests the EditRightConstraint
30 *
31 * @author DannyS712
32 *
33 * @covers \MediaWiki\EditPage\Constraint\EditRightConstraint
34 */
35 class EditRightConstraintTest extends MediaWikiUnitTestCase {
36 use EditConstraintTestTrait;
37 use MockAuthorityTrait;
38 use MockTitleTrait;
39
40 /**
41 * @dataProvider provideTestPass
42 * @param User $performer
43 * @param bool $new
44 * @param PermissionManager $permissionManager
45 * @return void
46 */
47 public function testPass( User $performer, bool $new, PermissionManager $permissionManager ) {
48 $constraint = new EditRightConstraint(
49 $performer,
50 $permissionManager,
51 $this->createMock( Title::class ),
52 $new
53 );
54 $this->assertConstraintPassed( $constraint );
55 }
56
57 public function provideTestPass() {
58 $title = $this->createMock( Title::class );
59 $userEdit = $this->createMock( User::class );
60 $permissionManagerEdit = $this->createMock( PermissionManager::class );
61 $permissionManagerEdit->expects( $this->once() )
62 ->method( 'userCan' )
63 ->with(
64 'edit',
65 $userEdit,
66 $title
67 )
68 ->willReturn( true );
69 $userCreateAndEdit = $this->createMock( User::class );
70 $userCreateAndEdit->expects( $this->once() )
71 ->method( 'authorizeWrite' )
72 ->with(
73 'create',
74 $title
75 )
76 ->willReturn( true );
77 $permissionManagerCreateAndEdit = $this->createMock( PermissionManager::class );
78 $permissionManagerCreateAndEdit->expects( $this->once() )
79 ->method( 'userCan' )
80 ->with(
81 'edit',
82 $userCreateAndEdit,
83 $title
84 )
85 ->willReturn( true );
86 yield 'Edit existing page' => [
87 'performer' => $userEdit,
88 'new' => false,
89 'permissionManager' => $permissionManagerEdit
90 ];
91 yield 'Create a new page' => [
92 'performer' => $userCreateAndEdit,
93 'new' => true,
94 'permissionManager' => $permissionManagerCreateAndEdit
95 ];
96 }
97
98 /**
99 * @dataProvider provideTestFailure
100 * @param User $performer
101 * @param bool $new
102 * @param PermissionManager $permissionManager
103 * @param int $expectedValue
104 */
105 public function testFailure(
106 User $performer, bool $new, PermissionManager $permissionManager, int $expectedValue
107 ) {
108 $title = $this->createMock( Title::class );
109 $constraint = new EditRightConstraint(
110 $performer,
111 $permissionManager,
112 $title,
113 $new
114 );
115 $this->assertConstraintFailed( $constraint, $expectedValue );
116 }
117
118 public function provideTestFailure() {
119 $title = $this->createMock( Title::class );
120 $anon = $this->createMock( User::class );
121 $anon->expects( $this->once() )->method( 'isRegistered' )->willReturn( false );
122 $permissionManagerAnon = $this->createMock( PermissionManager::class );
123 $permissionManagerAnon->expects( $this->once() )
124 ->method( 'userCan' )
125 ->with(
126 'edit',
127 $anon,
128 $title
129 )
130 ->willReturn( false );
131 $reg = $this->createMock( User::class );
132 $reg->expects( $this->once() )->method( 'isRegistered' )->willReturn( true );
133 $permissionManagerReg = $this->createMock( PermissionManager::class );
134 $permissionManagerReg->expects( $this->once() )
135 ->method( 'userCan' )
136 ->with(
137 'edit',
138 $reg,
139 $title
140 )
141 ->willReturn( false );
142 $userWithoutCreatePerm = $this->createMock( User::class );
143 $userWithoutCreatePerm->expects( $this->once() )
144 ->method( 'authorizeWrite' )
145 ->with(
146 'create',
147 $title
148 )
149 ->willReturn( false );
150 yield 'Anonymous user' => [
151 'performer' => $anon,
152 'new' => false,
153 'permissionManager' => $permissionManagerAnon,
154 'expectedValue' => IEditConstraint::AS_READ_ONLY_PAGE_ANON,
155 ];
156 yield 'Registered user' => [
157 'performer' => $reg,
158 'new' => false,
159 'permissionManager' => $permissionManagerReg,
160 'expectedValue' => IEditConstraint::AS_READ_ONLY_PAGE_LOGGED,
161 ];
162 yield 'User without create permission creates a page' => [
163 'performer' => $userWithoutCreatePerm,
164 'new' => true,
165 'permissionManager' => $this->createMock( PermissionManager::class ),
166 'expectedValue' => IEditConstraint::AS_NO_CREATE_PERMISSION,
167 ];
168 }
169
170 }