Completed
Push — master ( 3abc67...80e892 )
by mw
207:38 queued 172:37
created

testGrantPermissionToMainNamespace()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 22
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 12
nc 1
nop 0
dl 0
loc 22
rs 9.2
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests;
4
5
use SMW\PermissionPthValidator;
6
use Title;
7
8
/**
9
 * @covers \SMW\PermissionPthValidator
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since  2.4
14
 *
15
 * @author mwjames
16
 */
17
class PermissionPthValidatorTest extends \PHPUnit_Framework_TestCase {
18
19
	private $editProtectionValidator;
20
21
	protected function setUp() {
22
		parent::setUp();
23
24
		$this->editProtectionValidator = $this->getMockBuilder( '\SMW\Protection\EditProtectionValidator' )
25
			->disableOriginalConstructor()
26
			->getMock();
27
	}
28
29
	public function testCanConstruct() {
30
31
		$this->assertInstanceOf(
32
			'\SMW\PermissionPthValidator',
33
			new PermissionPthValidator( $this->editProtectionValidator  )
34
		);
35
	}
36
37
	public function testGrantPermissionToMainNamespace() {
38
39
		$title = Title::newFromText( 'Foo', NS_MAIN );
40
41
		$user = $this->getMockBuilder( '\User' )
42
			->disableOriginalConstructor()
43
			->getMock();
44
45
		$result = '';
46
47
		$instance = new PermissionPthValidator(
48
			$this->editProtectionValidator
49
		);
50
51
		$this->assertTrue(
52
			$instance->checkUserPermissionOn( $title, $user, 'edit', $result )
53
		);
54
55
		$this->assertEmpty(
56
			$result
57
		);
58
	}
59
60
	/**
61
	 * @dataProvider titleProvider
62
	 */
63
	public function testToReturnFalseOnMwNamespacePermissionCheck( $title, $permission, $action, $expected ) {
64
65
		$this->editProtectionValidator ->expects( $this->any() )
66
			->method( 'hasEditProtection' )
67
			->will( $this->returnValue( true ) );
68
69
		$this->editProtectionValidator ->expects( $this->any() )
70
			->method( 'hasProtectionOnNamespace' )
71
			->will( $this->returnValue( true ) );
72
73
		$user = $this->getMockBuilder( '\User' )
74
			->disableOriginalConstructor()
75
			->getMock();
76
77
		$user->expects( $this->once() )
78
			->method( 'isAllowed' )
79
			->with( $this->equalTo( $permission ) )
80
			->will( $this->returnValue( false ) );
81
82
		$result = '';
83
84
		$instance = new PermissionPthValidator(
85
			$this->editProtectionValidator
86
		);
87
88
		$this->assertFalse(
89
			$instance->checkUserPermissionOn( $title, $user, $action, $result )
90
		);
91
92
		$this->assertEquals(
93
			$expected,
94
			$result
95
		);
96
	}
97
98
	public function testToReturnFalseOnNamespaceWithEditPermissionCheck() {
99
100
		$editProtectionRight = 'Foo';
101
102
		$title = $this->getMockBuilder( '\Title' )
103
			->disableOriginalConstructor()
104
			->getMock();
105
106
		$title->expects( $this->any() )
107
			->method( 'getDBKey' )
108
			->will( $this->returnValue( 'PermissionTest' ) );
109
110
		$title->expects( $this->any() )
111
			->method( 'exists' )
112
			->will( $this->returnValue( true ) );
113
114
		$title->expects( $this->any() )
115
			->method( 'getNamespace' )
116
			->will( $this->returnValue( SMW_NS_PROPERTY ) );
117
118
		$this->editProtectionValidator->expects( $this->any() )
119
			->method( 'hasProtection' )
120
			->will( $this->returnValue( true ) );
121
122
		$this->editProtectionValidator->expects( $this->any() )
123
			->method( 'hasProtectionOnNamespace' )
124
			->will( $this->returnValue( true ) );
125
126
		$user = $this->getMockBuilder( '\User' )
127
			->disableOriginalConstructor()
128
			->getMock();
129
130
		$user->expects( $this->once() )
131
			->method( 'isAllowed' )
132
			->with( $this->equalTo( $editProtectionRight ) )
133
			->will( $this->returnValue( false ) );
134
135
		$result = '';
136
137
		$instance = new PermissionPthValidator(
138
			$this->editProtectionValidator
139
		);
140
141
		$instance->setEditProtectionRight(
142
			$editProtectionRight
143
		);
144
145
		$this->assertFalse(
146
			$instance->checkUserPermissionOn( $title, $user, 'edit', $result )
147
		);
148
149
		$this->assertEquals(
150
			array( array( 'smw-edit-protection', $editProtectionRight ) ),
151
			$result
152
		);
153
	}
154
155
	public function testFalseEditProtectionRightToNeverCheckPermissionOnNonMwNamespace() {
156
157
		$editProtectionRight = false;
158
159
		$title = $this->getMockBuilder( '\Title' )
160
			->disableOriginalConstructor()
161
			->getMock();
162
163
		$title->expects( $this->any() )
164
			->method( 'getDBKey' )
165
			->will( $this->returnValue( 'PermissionTest' ) );
166
167
		$title->expects( $this->any() )
168
			->method( 'exists' )
169
			->will( $this->returnValue( true ) );
170
171
		$title->expects( $this->any() )
172
			->method( 'getNamespace' )
173
			->will( $this->returnValue( SMW_NS_PROPERTY ) );
174
175
		$this->editProtectionValidator->expects( $this->never() )
176
			->method( 'hasProtectionOnNamespace' )
177
			->will( $this->returnValue( true ) );
178
179
		$user = $this->getMockBuilder( '\User' )
180
			->disableOriginalConstructor()
181
			->getMock();
182
183
		$result = '';
184
185
		$instance = new PermissionPthValidator(
186
			$this->editProtectionValidator
187
		);
188
189
		$instance->setEditProtectionRight(
190
			$editProtectionRight
191
		);
192
193
		$this->assertTrue(
194
			$instance->checkUserPermissionOn( $title, $user, 'edit', $result )
195
		);
196
	}
197
198
	public function titleProvider() {
199
200
		$provider[] = array(
0 ignored issues
show
Coding Style Comprehensibility introduced by
$provider was never initialized. Although not strictly required by PHP, it is generally a good practice to add $provider = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
201
			Title::newFromText( 'Smw_allows_pattern', NS_MEDIAWIKI ),
202
			'smw-patternedit',
203
			'edit',
204
			array( array( 'smw-patternedit-protection', 'smw-patternedit' ) )
205
		);
206
207
		$provider[] = array(
208
			Title::newFromText( 'Smw_allows_pattern', NS_MEDIAWIKI ),
209
			'smw-patternedit',
210
			'delete',
211
			array( array( 'smw-patternedit-protection', 'smw-patternedit' ) )
212
		);
213
214
		$provider[] = array(
215
			Title::newFromText( 'Smw_allows_pattern', NS_MEDIAWIKI ),
216
			'smw-patternedit',
217
			'move',
218
			array( array( 'smw-patternedit-protection', 'smw-patternedit' ) )
219
		);
220
221
		return $provider;
222
	}
223
224
}
225