BasicBackendTest::testNext()   A
last analyzed

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
eloc 3
c 1
b 0
f 0
nc 2
nop 2
dl 0
loc 4
rs 10
1
<?php
2
/**
3
 * This file is part of the MediaWiki extension Lingo.
4
 *
5
 * @copyright 2011 - 2017, Stephan Gambke
6
 * @license GPL-2.0-or-later
7
 *
8
 * The Lingo extension is free software: you can redistribute it and/or modify
9
 * it under the terms of the GNU General Public License as published by the Free
10
 * Software Foundation; either version 2 of the License, or (at your option) any
11
 * later version.
12
 *
13
 * The Lingo extension is distributed in the hope that it will be useful, but
14
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
15
 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
16
 * details.
17
 *
18
 * You should have received a copy of the GNU General Public License along
19
 * with this program. If not, see <http://www.gnu.org/licenses/>.
20
 *
21
 * @author Stephan Gambke
22
 * @since 2.0
23
 * @file
24
 * @ingroup Lingo
25
 */
26
27
namespace Lingo\Tests\Unit;
28
29
use Lingo\BasicBackend;
30
use PHPUnit\Framework\MockObject\MockObject;
0 ignored issues
show
Bug introduced by
The type PHPUnit\Framework\MockObject\MockObject was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
31
32
/**
33
 * @group extensions-lingo
34
 * @group extensions-lingo-unit
35
 * @group mediawiki-databaseless
36
 *
37
 * @coversDefaultClass \Lingo\BasicBackend
38
 * @covers ::<private>
39
 * @covers ::<protected>
40
 *
41
 * @ingroup Lingo
42
 * @ingroup Test
43
 */
44
class BasicBackendTest extends BackendTest {
45
46
	/**
47
	 * @covers ::__construct
48
	 */
49
	public function testCanConstruct() {
50
		$this->assertInstanceOf(
51
			'\Lingo\BasicBackend',
52
			new \Lingo\BasicBackend()
53
		);
54
	}
55
56
	/**
57
	 * @covers ::purgeCache
58
	 */
59
	public function testPurgeCache() {
60
		$GLOBALS[ 'wgexLingoPage' ] = 'SomePage';
61
62
		$title = $this->getMockBuilder( 'Title' )
63
			->getMock();
64
65
		$wikiPage = $this->getMockBuilder( 'WikiPage' )
66
			->disableOriginalConstructor()
67
			->getMock();
68
69
		$lingoParser = $this->getMockBuilder( 'Lingo\LingoParser' )
70
			->getMock();
71
72
		$testObject = $this->getMockBuilder( 'Lingo\BasicBackend' )
73
			->setMethods( [ 'getLingoParser' ] )
74
			->getMock();
75
76
		// Assert that the wikipage is tested against the wgexLingoPage, i.e.
77
		// that $wikipage->getTitle()->getText() === $page is tested
78
79
		$wikiPage->expects( $this->once() )
80
			->method( 'getTitle' )
81
			->willReturn( $title );
82
83
		$title->expects( $this->once() )
84
			->method( 'getText' )
85
			->willReturn( 'SomePage' );
86
87
		// Assert that purgeGlossaryFromCache is called
88
		$lingoParser->expects( $this->once() )
89
			->method( 'purgeGlossaryFromCache' );
90
91
		$testObject->expects( $this->once() )
92
			->method( 'getLingoParser' )
93
			->willReturn( $lingoParser );
94
95
		$this->assertTrue( $testObject->purgeCache( $wikiPage ) );
96
	}
97
98
	/**
99
	 * @covers ::useCache
100
	 */
101
	public function testUseCache() {
102
		$backend = new BasicBackend();
103
		$this->assertTrue( $backend->useCache() );
104
	}
105
106
	/**
107
	 * @covers ::next
108
	 * @dataProvider provideForTestNext
109
	 */
110
	public function testNext( $lingoPageText, $expectedResults ) {
111
		$backend = $this->getTestObject( $lingoPageText );
112
		foreach ( $expectedResults as $expected ) {
113
			$this->assertEquals( $expected, $backend->next() );
114
		}
115
	}
116
117
	public function testNext_LingoPageIsInterwiki() {
118
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', 'someInterwiki' );
119
		$backend->getMessageLog()->expects( $this->once() )
120
			->method( 'addError' )
121
			->willReturn( null );
122
123
		$this->assertNull( $backend->next() );
124
	}
125
126
	public function testNext_LingoPageWasJustEdited() {
127
		$backend = $this->getTestObject( ';SOT:Some old text', 'submit' );
128
		$this->assertEquals( [ 'JST', 'Just saved text', null, null ], $backend->next() );
129
	}
130
131
	public function testNext_LingoPageDoesNotExist() {
132
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', null, false );
133
		$backend->getMessageLog()->expects( $this->once() )
134
			->method( 'addWarning' )
135
			->willReturn( null );
136
137
		$this->assertEquals( null, $backend->next() );
138
	}
139
140
	public function testNext_LingoPageNotAccessible() {
141
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, null );
142
		$this->assertEquals( null, $backend->next() );
143
	}
144
145
	public function testNext_LingoPageIsNotATextPage() {
146
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, 'This is not a TextContent object' );
147
		$backend->getMessageLog()->expects( $this->once() )
148
			->method( 'addError' )
149
			->willReturn( null );
150
151
		$this->assertEquals( null, $backend->next() );
152
	}
153
154
	public function testNext_ApprovedRevsEnabledButNotInstalled() {
155
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, false, ';SAT:Some approved text' );
156
		$backend->getMessageLog()->expects( $this->once() )
157
			->method( 'addWarning' )
158
			->willReturn( null );
159
160
		$GLOBALS[ 'wgexLingoEnableApprovedRevs' ] = true;
161
162
		$this->assertEquals( [ 'SOT', 'Some old text', null, null ], $backend->next() );
163
	}
164
165
	public function testNext_ApprovedRevsEnabledAndInstalled() {
166
		$backend = $this->getTestObject( ';SOT:Some old text', 'view', '', false, false, ';SAT:Some approved text' );
167
168
		$GLOBALS[ 'wgexLingoEnableApprovedRevs' ] = true;
169
		define( 'APPROVED_REVS_VERSION', '42' );
170
171
		$this->assertEquals( [ 'SAT', 'Some approved text', null, null ], $backend->next() );
172
	}
173
174
	/**
175
	 * @return array
176
	 */
177
	public function provideForTestNext() {
178
		return [
179
180
			// Empty page
181
			[
182
				'',
183
				[ null ]
184
			],
185
186
			// Simple entries
187
			[
188
<<<'TESTTEXT'
189
;CIP:Common image point
190
;CMP:Common midpoint
191
TESTTEXT
192
			,
193
				[
194
					[ 'CMP', 'Common midpoint', null, null ],
195
					[ 'CIP', 'Common image point', null, null ],
196
				],
197
			],
198
199
			// Simple entries with line break
200
			[
201
<<<'TESTTEXT'
202
;CIP
203
:Common image point
204
;CMP
205
:Common midpoint
206
TESTTEXT
207
			,
208
				[
209
					[ 'CMP', 'Common midpoint', null, null ],
210
					[ 'CIP', 'Common image point', null, null ],
211
				],
212
			],
213
214
			// Two terms having the same definition
215
			[
216
<<<'TESTTEXT'
217
;CIP
218
;CMP
219
:Common midpoint
220
TESTTEXT
221
			,
222
				[
223
					[ 'CMP', 'Common midpoint', null, null ],
224
					[ 'CIP', 'Common midpoint', null, null ],
225
				],
226
			],
227
228
			// One term having two definitions
229
			[
230
<<<'TESTTEXT'
231
;CIP
232
:Common image point
233
:Common midpoint
234
TESTTEXT
235
			,
236
				[
237
					[ 'CIP', 'Common image point', null, null ],
238
					[ 'CIP', 'Common midpoint', null, null ],
239
				],
240
			],
241
242
			// Two terms sharing two definitions
243
			[
244
<<<'TESTTEXT'
245
;CIP
246
;CMP
247
:Common image point
248
:Common midpoint
249
TESTTEXT
250
			,
251
				[
252
					[ 'CMP', 'Common image point', null, null ],
253
					[ 'CMP', 'Common midpoint', null, null ],
254
					[ 'CIP', 'Common image point', null, null ],
255
					[ 'CIP', 'Common midpoint', null, null ],
256
				],
257
			],
258
259
			// Mixed entries and noise
260
			[
261
<<<'TESTTEXT'
262
;CIP:Common image point
263
; CMP : Common midpoint
264
265
;DIMO
266
;DMO
267
:Dip move-out
268
269
== headline ==
270
Sed ut perspiciatis unde; omnis iste natus error: sit voluptatem accusantium...
271
272
;NMO:Normal move-out
273
TESTTEXT
274
			,
275
				[
276
					[ 'NMO', 'Normal move-out', null, null ],
277
					[ 'DMO', 'Dip move-out', null, null ],
278
					[ 'DIMO', 'Dip move-out', null, null ],
279
					[ 'CMP', 'Common midpoint', null, null ],
280
					[ 'CIP', 'Common image point', null, null ],
281
				],
282
			],
283
284
		];
285
	}
286
287
	/**
288
	 * @return MockObject
289
	 */
290
	protected function getTestObject( $lingoPageText = '', $action = 'view', $interwiki = '', $lingoPageRevision = false, $lingoPageContent = false, $lingoApprovedText = '' ) {
291
		$messageLog = $this->getMockBuilder( 'Lingo\MessageLog' )
292
			->getMock();
293
294
		$backend = $this->getMockBuilder( 'Lingo\BasicBackend' )
295
			->disableOriginalConstructor()
296
			->setMethods( [
297
				'getLatestRevisionFromTitle',
298
				'getApprovedRevisionFromTitle',
299
				'getTitleFromText',
300
			] )
301
			->getMock();
302
303
		$reflected = new \ReflectionClass( '\Lingo\BasicBackend' );
304
		$constructor = $reflected->getConstructor();
305
		$constructor->invokeArgs( $backend, [ &$messageLog ] );
306
307
		$GLOBALS[ 'wgLingoPageName' ] = 'SomePage';
308
309
		$lingoPageTitle = $this->getMockBuilder( 'Title' )
310
			->getMock();
311
		$lingoPageTitle->expects( $this->once() )
312
			->method( 'getInterwiki' )
313
			->willReturn( $interwiki );
314
		$lingoPageTitle->expects( $this->any() )
315
			->method( 'getArticleID' )
316
			->willReturn( 'Foom' );
317
318
		$backend->expects( $this->any() )
319
			->method( 'getTitleFromText' )
320
			->willReturn( $lingoPageTitle );
321
322
		$request = $this->getMockBuilder( 'FauxRequest' )
323
			->getMock();
324
		$request->expects( $this->any() )
325
			->method( 'getVal' )
326
			->willReturnMap( [
327
				[ 'action', 'view', $action ], // action = submit
328
				[ 'title', null, $lingoPageTitle ], // title = $lingoPageTitle
329
				[ 'wpTextbox1', null, ';JST:Just saved text' ]
330
			] );
331
332
		$GLOBALS[ 'wgRequest' ] = $request;
333
334
		unset( $GLOBALS[ 'wgexLingoEnableApprovedRevs' ] );
335
336
		$backend->expects( $this->any() )
337
			->method( 'getLatestRevisionFromTitle' )
338
			->willReturn( $this->getRevisionMock( $lingoPageText, $lingoPageRevision, $lingoPageContent ) );
339
340
		$backend->expects( $this->any() )
341
			->method( 'getApprovedRevisionFromTitle' )
342
			->willReturn( $this->getRevisionMock( $lingoApprovedText ) );
343
344
		return $backend;
345
	}
346
347
	/**
348
	 * @param $lingoPageText
349
	 * @param $lingoPageRevision
350
	 * @param $lingoPageContent
351
	 * @return MockObject
352
	 */
353
	protected function getRevisionMock( $lingoPageText, $lingoPageRevision = false, $lingoPageContent = false ) {
354
		if ( $lingoPageRevision === false ) {
355
356
			if ( $lingoPageContent === false ) {
357
				$lingoPageContent = $this->getMockBuilder( 'TextContent' )
358
					->disableOriginalConstructor()
359
					->getMock();
360
361
				// FIXME: getNativeData() is deprecated for MW 1.33+.
0 ignored issues
show
Coding Style introduced by
Comment refers to a FIXME task "getNativeData() is deprecated for MW 1.33+"
Loading history...
362
				$lingoPageContent->expects( $this->any() )
363
					->method( 'getNativeData' )
364
					->willReturn( $lingoPageText );
365
366
				$lingoPageContent->expects( $this->any() )
367
					->method( 'getText' )
368
					->willReturn( $lingoPageText );
369
			}
370
371
			$lingoPageRevision = $this->getMockBuilder( 'Revision' )
372
				->disableOriginalConstructor()
373
				->getMock();
374
375
			$lingoPageRevision->expects( $this->any() )
376
				->method( 'getContent' )
377
				->willReturn( $lingoPageContent );
378
379
			return $lingoPageRevision;
380
		}
381
		return $lingoPageRevision;
382
	}
383
384
}
385