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

testDoUpdateFromWithNoRestrictionsNoEditProtection()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 20

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 20
nc 1
nop 0
dl 0
loc 31
rs 8.8571
c 0
b 0
f 0
1
<?php
2
3
namespace SMW\Tests\Protection;
4
5
use SMW\Protection\EditProtectionUpdater;
6
use SMW\DataItemFactory;
7
use SMW\Tests\TestEnvironment;
8
9
/**
10
 * @covers \SMW\Protection\EditProtectionUpdater
11
 * @group semantic-mediawiki
12
 *
13
 * @license GNU GPL v2+
14
 * @since  2.5
15
 *
16
 * @author mwjames
17
 */
18
class EditProtectionUpdaterTest extends \PHPUnit_Framework_TestCase {
19
20
	private $dataItemFactory;
21
	private $wikiPage;
22
	private $user;
23
	private $spyLogger;
24
25
	protected function setUp() {
26
		parent::setUp();
27
28
		$testEnvironment = new TestEnvironment();
29
30
		$this->spyLogger = $testEnvironment->getUtilityFactory()->newSpyLogger();
31
		$this->dataItemFactory = new DataItemFactory();
32
33
		$this->wikiPage = $this->getMockBuilder( '\WikiPage' )
34
			->disableOriginalConstructor()
35
			->getMock();
36
37
		$this->user = $this->getMockBuilder( '\User' )
38
			->disableOriginalConstructor()
39
			->getMock();
40
	}
41
42
	public function testCanConstruct() {
43
44
		$this->assertInstanceOf(
45
			EditProtectionUpdater::class,
46
			new EditProtectionUpdater( $this->wikiPage, $this->user )
47
		);
48
	}
49
50
	public function testDoUpdateFromWithNoRestrictionsNoEditProtection() {
51
52
		$subject = $this->dataItemFactory->newDIWikiPage( 'Foo', NS_MAIN );
53
54
		$this->wikiPage->expects( $this->once() )
55
			->method( 'getTitle' )
56
			->will( $this->returnValue( $subject->getTitle() ) );
57
58
		$this->wikiPage->expects( $this->never() )
59
			->method( 'doUpdateRestrictions' );
60
61
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
62
			->disableOriginalConstructor()
63
			->getMock();
64
65
		$semanticData->expects( $this->once() )
66
			->method( 'getPropertyValues' )
67
			->will( $this->returnValue( array() ) );
68
69
		$instance = new EditProtectionUpdater(
70
			$this->wikiPage,
71
			$this->user
72
		);
73
74
		$instance->setEditProtectionRight( 'Foo' );
75
		$instance->doUpdateFrom( $semanticData );
76
77
		$this->assertFalse(
78
			$instance->isRestrictedUpdate()
79
		);
80
	}
81
82
	public function testDoUpdateFromWithNoRestrictionsAnActiveEditProtection() {
83
84
		$subject = $this->dataItemFactory->newDIWikiPage( 'Foo', NS_MAIN );
85
86
		$this->wikiPage->expects( $this->once() )
87
			->method( 'getTitle' )
88
			->will( $this->returnValue( $subject->getTitle() ) );
89
90
		$this->wikiPage->expects( $this->once() )
91
			->method( 'doUpdateRestrictions' );
92
93
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
94
			->disableOriginalConstructor()
95
			->getMock();
96
97
		$semanticData->expects( $this->once() )
98
			->method( 'getPropertyValues' )
99
			->will( $this->returnValue( array( $this->dataItemFactory->newDIBoolean( true ) ) ) );
100
101
		$instance = new EditProtectionUpdater(
102
			$this->wikiPage,
103
			$this->user
104
		);
105
106
		$instance->setEditProtectionRight( 'Foo' );
107
108
		$instance->setLogger(
109
			$this->spyLogger
110
		);
111
112
		$instance->doUpdateFrom( $semanticData );
113
114
		$this->assertFalse(
115
			$instance->isRestrictedUpdate()
116
		);
117
118
		$this->assertContains(
119
			'add protection on edit, move',
120
			$this->spyLogger->getMessagesAsString()
121
		);
122
	}
123
124
	public function testDoUpdateFromWithRestrictionsButNoTrueEditProtection() {
125
126
		$title = $this->getMockBuilder( '\Title' )
127
			->disableOriginalConstructor()
128
			->getMock();
129
130
		$title->expects( $this->once() )
131
			->method( 'isProtected' )
132
			->with( $this->equalTo( 'edit' ) )
133
			->will( $this->returnValue( true ) );
134
135
		$title->expects( $this->once() )
136
			->method( 'getRestrictions' )
137
			->will( $this->returnValue( array( 'Foo' ) ) );
138
139
		$this->wikiPage->expects( $this->once() )
140
			->method( 'getTitle' )
141
			->will( $this->returnValue( $title ) );
142
143
		$this->wikiPage->expects( $this->once() )
144
			->method( 'doUpdateRestrictions' );
145
146
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
147
			->disableOriginalConstructor()
148
			->getMock();
149
150
		$semanticData->expects( $this->once() )
151
			->method( 'getPropertyValues' )
152
			->will( $this->returnValue( array( $this->dataItemFactory->newDIBoolean( false ) ) ) );
153
154
		$instance = new EditProtectionUpdater(
155
			$this->wikiPage,
156
			$this->user
157
		);
158
159
		$instance->setEditProtectionRight( 'Foo' );
160
161
		$instance->setLogger(
162
			$this->spyLogger
163
		);
164
165
		$instance->doUpdateFrom( $semanticData );
166
167
		$this->assertFalse(
168
			$instance->isRestrictedUpdate()
169
		);
170
171
		$this->assertContains(
172
			'remove protection on edit, move',
173
			$this->spyLogger->getMessagesAsString()
174
		);
175
	}
176
177
	public function testDoUpdateFromWithRestrictionsAnActiveEditProtection() {
178
179
		$property = $this->dataItemFactory->newDIProperty( '_EDIP' );
180
181
		$title = $this->getMockBuilder( '\Title' )
182
			->disableOriginalConstructor()
183
			->getMock();
184
185
		$title->expects( $this->once() )
186
			->method( 'isProtected' )
187
			->with( $this->equalTo( 'edit' ) )
188
			->will( $this->returnValue( true ) );
189
190
		$title->expects( $this->once() )
191
			->method( 'getRestrictions' )
192
			->will( $this->returnValue( array( 'Foo' ) ) );
193
194
		$this->wikiPage->expects( $this->once() )
195
			->method( 'getTitle' )
196
			->will( $this->returnValue( $title ) );
197
198
		$this->wikiPage->expects( $this->never() )
199
			->method( 'doUpdateRestrictions' );
200
201
		$semanticData = $this->getMockBuilder( '\SMW\SemanticData' )
202
			->disableOriginalConstructor()
203
			->getMock();
204
205
		$semanticData->expects( $this->once() )
206
			->method( 'getPropertyValues' )
207
			->with( $this->equalTo( $property ) )
208
			->will( $this->returnValue( array( $this->dataItemFactory->newDIBoolean( true ) ) ) );
209
210
		$instance = new EditProtectionUpdater(
211
			$this->wikiPage,
212
			$this->user
213
		);
214
215
		$instance->setEditProtectionRight( 'Foo' );
216
217
		$instance->setLogger(
218
			$this->spyLogger
219
		);
220
221
		$instance->doUpdateFrom( $semanticData );
222
223
		$this->assertFalse(
224
			$instance->isRestrictedUpdate()
225
		);
226
227
		$this->assertContains(
228
			'Status already set, no update required',
229
			$this->spyLogger->getMessagesAsString()
230
		);
231
	}
232
233
}
234