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

MediaWiki/Hooks/ArticleProtectCompleteTest.php (1 issue)

Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
3
namespace SMW\Tests\MediaWiki\Hooks;
4
5
use SMW\MediaWiki\Hooks\ArticleProtectComplete;
6
use SMW\PropertyAnnotators\EditProtectedPropertyAnnotator;
7
use SMW\Tests\TestEnvironment;
8
use SMW\DataItemFactory;
9
10
/**
11
 * @covers \SMW\MediaWiki\Hooks\ArticleProtectComplete
12
 * @group semantic-mediawiki
13
 *
14
 * @license GNU GPL v2+
15
 * @since 2.5
16
 *
17
 * @author mwjames
18
 */
19
class ArticleProtectCompleteTest extends \PHPUnit_Framework_TestCase {
20
21
	private $spyLogger;
22
	private $testEnvironment;
23
	private $semanticDataFactory;
24
	private $dataItemFactory;
25
26
	protected function setUp() {
27
		parent::setUp();
28
29
		$this->testEnvironment = new TestEnvironment();
30
		$this->dataItemFactory = new DataItemFactory();
31
32
		$this->spyLogger = $this->testEnvironment->getUtilityFactory()->newSpyLogger();
33
		$this->semanticDataFactory = $this->testEnvironment->getUtilityFactory()->newSemanticDataFactory();
34
35
		$store = $this->getMockBuilder( '\SMW\Store' )
36
			->disableOriginalConstructor()
37
			->getMockForAbstractClass();
38
39
		$this->testEnvironment->registerObject( 'Store', $store );
40
	}
41
42
	protected function tearDown() {
43
		$this->testEnvironment->tearDown();
44
		parent::tearDown();
45
	}
46
47
	public function testCanConstruct() {
48
49
		$title = $this->getMockBuilder( '\Title' )
50
			->disableOriginalConstructor()
51
			->getMock();
52
53
		$editInfoProvider = $this->getMockBuilder( '\SMW\MediaWiki\EditInfoProvider' )
54
			->disableOriginalConstructor()
55
			->getMock();
56
57
		$this->assertInstanceOf(
58
			ArticleProtectComplete::class,
59
			new ArticleProtectComplete( $title, $editInfoProvider )
60
		);
61
	}
62
63
	public function testProcessOnSelfInvokedReason() {
64
65
		$title = $this->getMockBuilder( '\Title' )
66
			->disableOriginalConstructor()
67
			->getMock();
68
69
		$editInfoProvider = $this->getMockBuilder( '\SMW\MediaWiki\EditInfoProvider' )
70
			->disableOriginalConstructor()
71
			->getMock();
72
73
		$instance = new ArticleProtectComplete(
74
			$title,
75
			$editInfoProvider
76
		);
77
78
		$instance->setLogger( $this->spyLogger );
79
80
		$protections = array();
81
		$reason = \SMW\Message::get( 'smw-edit-protection-auto-update' );
82
83
		$instance->process( $protections, $reason );
84
85
		$this->assertContains(
86
			'No changes required, invoked by own process',
87
			$this->spyLogger->getMessagesAsString()
88
		);
89
	}
90
91
	public function testProcessOnMatchableEditProtectionToAddAnnotation() {
92
93
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
94
			->disableOriginalConstructor()
95
			->getMock();
96
97
		$title = $this->getMockBuilder( '\Title' )
98
			->disableOriginalConstructor()
99
			->getMock();
100
101
		$title->expects( $this->any() )
102
			->method( 'getDBKey' )
103
			->will( $this->returnValue( 'Foo' ) );
104
105
		$title->expects( $this->any() )
106
			->method( 'getNamespace' )
107
			->will( $this->returnValue( NS_SPECIAL ) );
108
109
		$editInfoProvider = $this->getMockBuilder( '\SMW\MediaWiki\EditInfoProvider' )
110
			->disableOriginalConstructor()
111
			->getMock();
112
113
		$editInfoProvider->expects( $this->once() )
114
			->method( 'getOutput' )
115
			->will( $this->returnValue( $parserOutput ) );
116
117
		$instance = new ArticleProtectComplete(
118
			$title,
119
			$editInfoProvider
120
		);
121
122
		$instance->setLogger( $this->spyLogger );
123
		$instance->setEditProtectionRight( 'Foo' );
124
125
		$protections = array( 'edit' => 'Foo' );
126
		$reason = '';
127
128
		$instance->process( $protections, $reason );
129
130
		$this->assertContains(
131
			'ArticleProtectComplete addProperty `Is edit protected`',
132
			$this->spyLogger->getMessagesAsString()
133
		);
134
	}
135
136
	public function testProcessOnUnmatchableEditProtectionToRemoveAnnotation() {
137
138
		$semanticData = $this->semanticDataFactory->newEmptySemanticData(
139
			$this->dataItemFactory->newDIWikiPage( __METHOD__, NS_SPECIAL )
140
		);
141
142
		$dataItem = $this->dataItemFactory->newDIBoolean( true );
143
		$dataItem->setOption( EditProtectedPropertyAnnotator::SYSTEM_ANNOTATION, true );
0 ignored issues
show
true is of type boolean, but the function expects a string.

It seems like the type of the argument is not accepted by the function/method which you are calling.

In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.

We suggest to add an explicit type cast like in the following example:

function acceptsInteger($int) { }

$x = '123'; // string "123"

// Instead of
acceptsInteger($x);

// we recommend to use
acceptsInteger((integer) $x);
Loading history...
144
145
		$semanticData->addPropertyObjectValue(
146
			$this->dataItemFactory->newDIProperty( '_EDIP' ),
147
			$dataItem
148
		);
149
150
		$parserOutput = $this->getMockBuilder( '\ParserOutput' )
151
			->disableOriginalConstructor()
152
			->getMock();
153
154
		$parserOutput->expects( $this->once() )
155
			->method( 'getExtensionData' )
156
			->will( $this->returnValue( $semanticData ) );
157
158
		$title = $this->getMockBuilder( '\Title' )
159
			->disableOriginalConstructor()
160
			->getMock();
161
162
		$title->expects( $this->any() )
163
			->method( 'getDBKey' )
164
			->will( $this->returnValue( 'Foo' ) );
165
166
		$title->expects( $this->any() )
167
			->method( 'getNamespace' )
168
			->will( $this->returnValue( NS_SPECIAL ) );
169
170
		$editInfoProvider = $this->getMockBuilder( '\SMW\MediaWiki\EditInfoProvider' )
171
			->disableOriginalConstructor()
172
			->getMock();
173
174
		$editInfoProvider->expects( $this->once() )
175
			->method( 'getOutput' )
176
			->will( $this->returnValue( $parserOutput ) );
177
178
		$instance = new ArticleProtectComplete(
179
			$title,
180
			$editInfoProvider
181
		);
182
183
		$instance->setLogger( $this->spyLogger );
184
		$instance->setEditProtectionRight( 'Foo2' );
185
186
		$protections = array( 'edit' => 'Foo' );
187
		$reason = '';
188
189
		$instance->process( $protections, $reason );
190
191
		$this->assertContains(
192
			'ArticleProtectComplete removeProperty `Is edit protected`',
193
			$this->spyLogger->getMessagesAsString()
194
		);
195
	}
196
197
}
198