1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace SMW\Notifications\Tests; |
4
|
|
|
|
5
|
|
|
use SMW\Notifications\ValueChange\ChangeNotificationPresentationModel; |
6
|
|
|
use SMW\Notifications\ValueChange\ChangeNotifications; |
7
|
|
|
use SMW\DIWikiPage; |
8
|
|
|
use SMW\Tests\TestEnvironment; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* @covers \SMW\Notifications\ValueChange\ChangeNotificationPresentationModel |
12
|
|
|
* @group semantic-notifications |
13
|
|
|
* |
14
|
|
|
* @license GNU GPL v2+ |
15
|
|
|
* @since 1.0 |
16
|
|
|
* |
17
|
|
|
* @author mwjames |
18
|
|
|
*/ |
19
|
|
|
class ChangeNotificationPresentationModelTest extends \PHPUnit_Framework_TestCase { |
20
|
|
|
|
21
|
|
|
private $store; |
22
|
|
|
private $testEnvironment; |
23
|
|
|
|
24
|
|
|
protected function setUp() { |
25
|
|
|
|
26
|
|
|
$this->store = $this->getMockBuilder( '\SMW\Store' ) |
27
|
|
|
->disableOriginalConstructor() |
28
|
|
|
->getMockForAbstractClass(); |
29
|
|
|
|
30
|
|
|
$this->testEnvironment = new TestEnvironment(); |
31
|
|
|
$this->testEnvironment->registerObject( 'Store', $this->store ); |
32
|
|
|
} |
33
|
|
|
|
34
|
|
|
protected function tearDown() { |
35
|
|
|
$this->testEnvironment->tearDown(); |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* @dataProvider typeProvider |
40
|
|
|
*/ |
41
|
|
|
public function testCanConstruct( $type ) { |
42
|
|
|
|
43
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
44
|
|
|
->disableOriginalConstructor() |
45
|
|
|
->getMock(); |
46
|
|
|
|
47
|
|
|
$echoEvent->expects( $this->any() ) |
48
|
|
|
->method( 'getType' ) |
49
|
|
|
->will( $this->returnValue( $type ) ); |
50
|
|
|
|
51
|
|
|
$user = $this->getMockBuilder( '\User' ) |
52
|
|
|
->disableOriginalConstructor() |
53
|
|
|
->getMock(); |
54
|
|
|
|
55
|
|
|
$this->assertInstanceOf( |
56
|
|
|
ChangeNotificationPresentationModel::class, |
57
|
|
|
ChangeNotificationPresentationModel::factory( $echoEvent, 'en', $user ) |
58
|
|
|
); |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* @dataProvider iconTypeProvider |
63
|
|
|
*/ |
64
|
|
|
public function testGetIconType( $type, $subject, $expected ) { |
65
|
|
|
|
66
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
67
|
|
|
->disableOriginalConstructor() |
68
|
|
|
->getMock(); |
69
|
|
|
|
70
|
|
|
$echoEvent->expects( $this->any() ) |
71
|
|
|
->method( 'getType' ) |
72
|
|
|
->will( $this->returnValue( $type ) ); |
73
|
|
|
|
74
|
|
|
$echoEvent->expects( $this->any() ) |
75
|
|
|
->method( 'getTitle' ) |
76
|
|
|
->will( $this->returnValue( $subject->getTitle() ) ); |
77
|
|
|
|
78
|
|
|
$user = $this->getMockBuilder( '\User' ) |
79
|
|
|
->disableOriginalConstructor() |
80
|
|
|
->getMock(); |
81
|
|
|
|
82
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
83
|
|
|
$echoEvent, |
84
|
|
|
'en', |
85
|
|
|
$user |
86
|
|
|
); |
87
|
|
|
|
88
|
|
|
$this->assertSame( |
89
|
|
|
$expected, |
90
|
|
|
$instance->getIconType() |
91
|
|
|
); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
public function testCanRender() { |
95
|
|
|
|
96
|
|
|
$subject = DIWikiPage::newFromText( 'Foo' ); |
97
|
|
|
|
98
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
99
|
|
|
->disableOriginalConstructor() |
100
|
|
|
->getMock(); |
101
|
|
|
|
102
|
|
|
$echoEvent->expects( $this->any() ) |
103
|
|
|
->method( 'getType' ) |
104
|
|
|
->will( $this->returnValue( ChangeNotifications::VALUE_CHANGE ) ); |
105
|
|
|
|
106
|
|
|
$echoEvent->expects( $this->any() ) |
107
|
|
|
->method( 'getTitle' ) |
108
|
|
|
->will( $this->returnValue( $subject->getTitle() ) ); |
109
|
|
|
|
110
|
|
|
$user = $this->getMockBuilder( '\User' ) |
111
|
|
|
->disableOriginalConstructor() |
112
|
|
|
->getMock(); |
113
|
|
|
|
114
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
115
|
|
|
$echoEvent, |
116
|
|
|
'en', |
117
|
|
|
$user |
118
|
|
|
); |
119
|
|
|
|
120
|
|
|
$this->assertInternalType( |
121
|
|
|
'boolean', |
122
|
|
|
$instance->canRender() |
123
|
|
|
); |
124
|
|
|
} |
125
|
|
|
|
126
|
|
|
public function testGetHeaderMessageKey() { |
127
|
|
|
|
128
|
|
|
$subject = DIWikiPage::newFromText( 'Foo' ); |
129
|
|
|
|
130
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
131
|
|
|
->disableOriginalConstructor() |
132
|
|
|
->getMock(); |
133
|
|
|
|
134
|
|
|
$echoEvent->expects( $this->any() ) |
135
|
|
|
->method( 'getType' ) |
136
|
|
|
->will( $this->returnValue( ChangeNotifications::VALUE_CHANGE ) ); |
137
|
|
|
|
138
|
|
|
$echoEvent->expects( $this->any() ) |
139
|
|
|
->method( 'getTitle' ) |
140
|
|
|
->will( $this->returnValue( $subject->getTitle() ) ); |
141
|
|
|
|
142
|
|
|
$user = $this->getMockBuilder( '\User' ) |
143
|
|
|
->disableOriginalConstructor() |
144
|
|
|
->getMock(); |
145
|
|
|
|
146
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
147
|
|
|
$echoEvent, |
148
|
|
|
'en', |
149
|
|
|
$user |
150
|
|
|
); |
151
|
|
|
|
152
|
|
|
$this->assertInternalType( |
153
|
|
|
'string', |
154
|
|
|
$instance->getHeaderMessageKey() |
155
|
|
|
); |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
public function testGetHeaderMessage() { |
159
|
|
|
|
160
|
|
|
$subject = DIWikiPage::newFromText( 'Foo' ); |
161
|
|
|
|
162
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
163
|
|
|
->disableOriginalConstructor() |
164
|
|
|
->getMock(); |
165
|
|
|
|
166
|
|
|
$echoEvent->expects( $this->any() ) |
167
|
|
|
->method( 'getType' ) |
168
|
|
|
->will( $this->returnValue( ChangeNotifications::VALUE_CHANGE ) ); |
169
|
|
|
|
170
|
|
|
$echoEvent->expects( $this->any() ) |
171
|
|
|
->method( 'getTitle' ) |
172
|
|
|
->will( $this->returnValue( $subject->getTitle() ) ); |
173
|
|
|
|
174
|
|
|
$user = $this->getMockBuilder( '\User' ) |
175
|
|
|
->disableOriginalConstructor() |
176
|
|
|
->getMock(); |
177
|
|
|
|
178
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
179
|
|
|
$echoEvent, |
180
|
|
|
'en', |
181
|
|
|
$user |
182
|
|
|
); |
183
|
|
|
|
184
|
|
|
$this->assertInstanceOf( |
185
|
|
|
'\Message', |
186
|
|
|
$instance->getHeaderMessage() |
187
|
|
|
); |
188
|
|
|
} |
189
|
|
|
|
190
|
|
|
/** |
191
|
|
|
* @dataProvider typeProvider |
192
|
|
|
*/ |
193
|
|
|
public function testGetBodyMessageOnNoProperties( $type , $subject ) { |
194
|
|
|
|
195
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
196
|
|
|
->disableOriginalConstructor() |
197
|
|
|
->getMock(); |
198
|
|
|
|
199
|
|
|
$echoEvent->expects( $this->any() ) |
200
|
|
|
->method( 'getType' ) |
201
|
|
|
->will( $this->returnValue( $type ) ); |
202
|
|
|
|
203
|
|
|
$user = $this->getMockBuilder( '\User' ) |
204
|
|
|
->disableOriginalConstructor() |
205
|
|
|
->getMock(); |
206
|
|
|
|
207
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
208
|
|
|
$echoEvent, |
209
|
|
|
'en', |
210
|
|
|
$user |
211
|
|
|
); |
212
|
|
|
|
213
|
|
|
$this->assertFalse( |
214
|
|
|
$instance->getBodyMessage() |
215
|
|
|
); |
216
|
|
|
} |
217
|
|
|
|
218
|
|
|
/** |
219
|
|
|
* @dataProvider propertiesProvider |
220
|
|
|
*/ |
221
|
|
|
public function testGetBodyMessageOnAvailableProperties( $type, $properties ) { |
222
|
|
|
|
223
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
224
|
|
|
->disableOriginalConstructor() |
225
|
|
|
->getMock(); |
226
|
|
|
|
227
|
|
|
$echoEvent->expects( $this->any() ) |
228
|
|
|
->method( 'getExtra' ) |
229
|
|
|
->will( $this->returnValue( array( 'properties' => $properties ) ) ); |
230
|
|
|
|
231
|
|
|
$echoEvent->expects( $this->any() ) |
232
|
|
|
->method( 'getType' ) |
233
|
|
|
->will( $this->returnValue( $type ) ); |
234
|
|
|
|
235
|
|
|
$user = $this->getMockBuilder( '\User' ) |
236
|
|
|
->disableOriginalConstructor() |
237
|
|
|
->getMock(); |
238
|
|
|
|
239
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
240
|
|
|
$echoEvent, |
241
|
|
|
'en', |
242
|
|
|
$user |
243
|
|
|
); |
244
|
|
|
|
245
|
|
|
$this->assertInstanceOf( |
246
|
|
|
'\Message', |
247
|
|
|
$instance->getBodyMessage() |
248
|
|
|
); |
249
|
|
|
} |
250
|
|
|
|
251
|
|
|
/** |
252
|
|
|
* @dataProvider typeProvider |
253
|
|
|
*/ |
254
|
|
|
public function testGetPrimaryLink( $type, $subject ) { |
255
|
|
|
|
256
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
257
|
|
|
->disableOriginalConstructor() |
258
|
|
|
->getMock(); |
259
|
|
|
|
260
|
|
|
$echoEvent->expects( $this->any() ) |
261
|
|
|
->method( 'getType' ) |
262
|
|
|
->will( $this->returnValue( $type ) ); |
263
|
|
|
|
264
|
|
|
$echoEvent->expects( $this->any() ) |
265
|
|
|
->method( 'getTitle' ) |
266
|
|
|
->will( $this->returnValue( $subject->getTitle() ) ); |
267
|
|
|
|
268
|
|
|
$user = $this->getMockBuilder( '\User' ) |
269
|
|
|
->disableOriginalConstructor() |
270
|
|
|
->getMock(); |
271
|
|
|
|
272
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
273
|
|
|
$echoEvent, |
274
|
|
|
'en', |
275
|
|
|
$user |
276
|
|
|
); |
277
|
|
|
|
278
|
|
|
$this->assertInternalType( |
279
|
|
|
'array', |
280
|
|
|
$instance->getPrimaryLink() |
281
|
|
|
); |
282
|
|
|
} |
283
|
|
|
|
284
|
|
|
/** |
285
|
|
|
* @dataProvider typeProvider |
286
|
|
|
*/ |
287
|
|
|
public function testGetSecondaryLinks( $type, $subject ) { |
288
|
|
|
|
289
|
|
|
$echoEvent = $this->getMockBuilder( '\EchoEvent' ) |
290
|
|
|
->disableOriginalConstructor() |
291
|
|
|
->getMock(); |
292
|
|
|
|
293
|
|
|
$echoEvent->expects( $this->any() ) |
294
|
|
|
->method( 'getExtraParam' ) |
295
|
|
|
->with( $this->equalTo( 'revid' ) ) |
296
|
|
|
->will( $this->returnValue( 42 ) ); |
297
|
|
|
|
298
|
|
|
$echoEvent->expects( $this->any() ) |
299
|
|
|
->method( 'getType' ) |
300
|
|
|
->will( $this->returnValue( $type ) ); |
301
|
|
|
|
302
|
|
|
$echoEvent->expects( $this->any() ) |
303
|
|
|
->method( 'getTitle' ) |
304
|
|
|
->will( $this->returnValue( $subject->getTitle() ) ); |
305
|
|
|
|
306
|
|
|
$user = $this->getMockBuilder( '\User' ) |
307
|
|
|
->disableOriginalConstructor() |
308
|
|
|
->getMock(); |
309
|
|
|
|
310
|
|
|
$instance = ChangeNotificationPresentationModel::factory( |
311
|
|
|
$echoEvent, |
312
|
|
|
'en', |
313
|
|
|
$user |
314
|
|
|
); |
315
|
|
|
|
316
|
|
|
$this->assertInternalType( |
317
|
|
|
'array', |
318
|
|
|
$instance->getSecondaryLinks() |
319
|
|
|
); |
320
|
|
|
} |
321
|
|
|
|
322
|
|
|
public function iconTypeProvider() { |
323
|
|
|
|
324
|
|
|
$provider['specification-prop'] = array( |
|
|
|
|
325
|
|
|
ChangeNotifications::SPECIFICATION_CHANGE, |
326
|
|
|
DIWikiPage::newFromText( 'Foo', SMW_NS_PROPERTY ), |
327
|
|
|
'smw-specification-change-property' |
328
|
|
|
); |
329
|
|
|
|
330
|
|
|
$provider['specification-cat'] = array( |
331
|
|
|
ChangeNotifications::SPECIFICATION_CHANGE, |
332
|
|
|
DIWikiPage::newFromText( 'Foo', NS_CATEGORY ), |
333
|
|
|
'smw-specification-change-category' |
334
|
|
|
); |
335
|
|
|
|
336
|
|
|
$provider['specification-conc'] = array( |
337
|
|
|
ChangeNotifications::SPECIFICATION_CHANGE, |
338
|
|
|
DIWikiPage::newFromText( 'Foo', SMW_NS_CONCEPT ), |
339
|
|
|
'smw-specification-change-category' |
340
|
|
|
); |
341
|
|
|
|
342
|
|
|
$provider['value'] = array( |
343
|
|
|
ChangeNotifications::VALUE_CHANGE, |
344
|
|
|
DIWikiPage::newFromText( 'Foo' ), |
345
|
|
|
'smw-value-change' |
346
|
|
|
); |
347
|
|
|
|
348
|
|
|
return $provider; |
349
|
|
|
} |
350
|
|
|
|
351
|
|
|
public function typeProvider() { |
352
|
|
|
|
353
|
|
|
$provider['specification-prop'] = array( |
|
|
|
|
354
|
|
|
ChangeNotifications::SPECIFICATION_CHANGE, |
355
|
|
|
DIWikiPage::newFromText( 'Foo', SMW_NS_PROPERTY ) |
356
|
|
|
); |
357
|
|
|
|
358
|
|
|
$provider['specification-cat'] = array( |
359
|
|
|
ChangeNotifications::SPECIFICATION_CHANGE, |
360
|
|
|
DIWikiPage::newFromText( 'Foo', NS_CATEGORY ) |
361
|
|
|
); |
362
|
|
|
|
363
|
|
|
$provider['value'] = array( |
364
|
|
|
ChangeNotifications::VALUE_CHANGE, |
365
|
|
|
DIWikiPage::newFromText( 'Foo' ) |
366
|
|
|
); |
367
|
|
|
|
368
|
|
|
return $provider; |
369
|
|
|
} |
370
|
|
|
|
371
|
|
|
public function propertiesProvider() { |
372
|
|
|
|
373
|
|
|
$provider['value'] = array( |
|
|
|
|
374
|
|
|
ChangeNotifications::VALUE_CHANGE, |
375
|
|
|
array( |
376
|
|
|
DIWikiPage::newFromText( 'Foo', SMW_NS_PROPERTY ) |
377
|
|
|
) |
378
|
|
|
); |
379
|
|
|
|
380
|
|
|
$provider['value-empty-prop'] = array( |
381
|
|
|
ChangeNotifications::VALUE_CHANGE, |
382
|
|
|
array( |
383
|
|
|
DIWikiPage::newFromText( 'Foo', SMW_NS_PROPERTY ), |
384
|
|
|
DIWikiPage::newFromText( '', SMW_NS_PROPERTY ) |
385
|
|
|
) |
386
|
|
|
); |
387
|
|
|
|
388
|
|
|
return $provider; |
389
|
|
|
} |
390
|
|
|
|
391
|
|
|
} |
392
|
|
|
|
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:
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 thebar
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.