aboutsummaryrefslogtreecommitdiffstats
path: root/tests/phpunit/unit/includes/editpage/Constraint/EditRightConstraintTest.php
blob: 9d3346e87d0177d41a1b8f61c7db68991fd6a9a0 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
<?php
/**
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 *
 * @file
 */

use MediaWiki\EditPage\Constraint\EditRightConstraint;
use MediaWiki\EditPage\Constraint\IEditConstraint;
use MediaWiki\Permissions\PermissionManager;
use MediaWiki\Tests\Unit\Permissions\MockAuthorityTrait;
use MediaWiki\Title\Title;
use MediaWiki\User\User;

/**
 * Tests the EditRightConstraint
 *
 * @author DannyS712
 *
 * @covers \MediaWiki\EditPage\Constraint\EditRightConstraint
 */
class EditRightConstraintTest extends MediaWikiUnitTestCase {
	use EditConstraintTestTrait;
	use MockAuthorityTrait;
	use MockTitleTrait;

	/**
	 * @dataProvider provideTestPass
	 * @param User $performer
	 * @param bool $new
	 * @param PermissionManager $permissionManager
	 * @return void
	 */
	public function testPass( User $performer, bool $new, PermissionManager $permissionManager ) {
		$constraint = new EditRightConstraint(
			$performer,
			$permissionManager,
			$this->createMock( Title::class ),
			$new
		);
		$this->assertConstraintPassed( $constraint );
	}

	public function provideTestPass() {
		$title = $this->createMock( Title::class );
		$userEdit = $this->createMock( User::class );
		$permissionManagerEdit = $this->createMock( PermissionManager::class );
		$permissionManagerEdit->expects( $this->once() )
			->method( 'userCan' )
			->with(
				'edit',
				$userEdit,
				$title
			)
			->willReturn( true );
		$userCreateAndEdit = $this->createMock( User::class );
		$userCreateAndEdit->expects( $this->once() )
			->method( 'authorizeWrite' )
			->with(
				'create',
				$title
			)
			->willReturn( true );
		$permissionManagerCreateAndEdit = $this->createMock( PermissionManager::class );
		$permissionManagerCreateAndEdit->expects( $this->once() )
			->method( 'userCan' )
			->with(
				'edit',
				$userCreateAndEdit,
				$title
			)
			->willReturn( true );
		yield 'Edit existing page' => [
			'performer' => $userEdit,
			'new' => false,
			'permissionManager' => $permissionManagerEdit
		];
		yield 'Create a new page' => [
			'performer' => $userCreateAndEdit,
			'new' => true,
			'permissionManager' => $permissionManagerCreateAndEdit
		];
	}

	/**
	 * @dataProvider provideTestFailure
	 * @param User $performer
	 * @param bool $new
	 * @param PermissionManager $permissionManager
	 * @param int $expectedValue
	 */
	public function testFailure(
		User $performer, bool $new, PermissionManager $permissionManager, int $expectedValue
	) {
		$title = $this->createMock( Title::class );
		$constraint = new EditRightConstraint(
			$performer,
			$permissionManager,
			$title,
			$new
		);
		$this->assertConstraintFailed( $constraint, $expectedValue );
	}

	public function provideTestFailure() {
		$title = $this->createMock( Title::class );
		$anon = $this->createMock( User::class );
		$anon->expects( $this->once() )->method( 'isRegistered' )->willReturn( false );
		$permissionManagerAnon = $this->createMock( PermissionManager::class );
		$permissionManagerAnon->expects( $this->once() )
			->method( 'userCan' )
			->with(
				'edit',
				$anon,
				$title
			)
			->willReturn( false );
		$reg = $this->createMock( User::class );
		$reg->expects( $this->once() )->method( 'isRegistered' )->willReturn( true );
		$permissionManagerReg = $this->createMock( PermissionManager::class );
		$permissionManagerReg->expects( $this->once() )
			->method( 'userCan' )
			->with(
				'edit',
				$reg,
				$title
			)
			->willReturn( false );
		$userWithoutCreatePerm = $this->createMock( User::class );
		$userWithoutCreatePerm->expects( $this->once() )
			->method( 'authorizeWrite' )
			->with(
				'create',
				$title
			)
			->willReturn( false );
		yield 'Anonymous user' => [
			'performer' => $anon,
			'new' => false,
			'permissionManager' => $permissionManagerAnon,
			'expectedValue' => IEditConstraint::AS_READ_ONLY_PAGE_ANON,
		];
		yield 'Registered user' => [
			'performer' => $reg,
			'new' => false,
			'permissionManager' => $permissionManagerReg,
			'expectedValue' => IEditConstraint::AS_READ_ONLY_PAGE_LOGGED,
		];
		yield 'User without create permission creates a page' => [
			'performer' => $userWithoutCreatePerm,
			'new' => true,
			'permissionManager' => $this->createMock( PermissionManager::class ),
			'expectedValue' => IEditConstraint::AS_NO_CREATE_PERMISSION,
		];
	}

}