Completed
Push — master ( bb3081...b26d3d )
by mw
9s
created

testAddToIntermediaryCache()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 31
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
dl 0
loc 31
rs 8.8571
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
3
namespace SIL\Tests;
4
5
use SIL\PageContentLanguageOnTheFlyModifier;
6
7
/**
8
 * @covers \SIL\PageContentLanguageOnTheFlyModifier
9
 * @group semantic-interlanguage-links
10
 *
11
 * @license GNU GPL v2+
12
 * @since 1.0
13
 *
14
 * @author mwjames
15
 */
16
class PageContentLanguageOnTheFlyModifierTest extends \PHPUnit_Framework_TestCase {
17
18
	public function testCanConstruct() {
19
20
		$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
21
			->disableOriginalConstructor()
22
			->getMock();
23
24
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
25
			->disableOriginalConstructor()
26
			->getMock();
27
28
		$this->assertInstanceOf(
29
			'\SIL\PageContentLanguageOnTheFlyModifier',
30
			new PageContentLanguageOnTheFlyModifier( $interlanguageLinksLookup, $cache )
31
		);
32
	}
33
34
	public function testGetValidPageContentLanguage() {
35
36
		$pageLanguage = '';
37
38
		$title = $this->getMockBuilder( '\Title' )
39
			->disableOriginalConstructor()
40
			->getMock();
41
42
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
43
			->disableOriginalConstructor()
44
			->getMock();
45
46
		$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
47
			->disableOriginalConstructor()
48
			->getMock();
49
50
		$interlanguageLinksLookup->expects( $this->once() )
51
			->method( 'findPageLanguageForTarget' )
52
			->will( $this->returnValue(  'ja' ) );
53
54
		$instance = new PageContentLanguageOnTheFlyModifier(
55
			$interlanguageLinksLookup,
56
			$cache
57
		);
58
59
		$this->assertEquals(
60
			'ja',
61
			$instance->getPageContentLanguage( $title, $pageLanguage )
62
		);
63
	}
64
65
	public function testGetValidPageContentLanguageFromCache() {
66
67
		$pageLanguage = '';
68
69
		$title = $this->getMockBuilder( '\Title' )
70
			->disableOriginalConstructor()
71
			->getMock();
72
73
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
74
			->disableOriginalConstructor()
75
			->getMock();
76
77
		$cache->expects( $this->once() )
78
			->method( 'fetch' )
79
			->will( $this->returnValue(  'fr' ) );
80
81
		$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
82
			->disableOriginalConstructor()
83
			->getMock();
84
85
		$interlanguageLinksLookup->expects( $this->never() )
86
			->method( 'findPageLanguageForTarget' );
87
88
		$instance = new PageContentLanguageOnTheFlyModifier(
89
			$interlanguageLinksLookup,
90
			$cache
91
		);
92
93
		$this->assertEquals(
94
			'fr',
95
			$instance->getPageContentLanguage( $title, $pageLanguage )
96
		);
97
	}
98
99
	public function testGetPageContentLanguageToReturnLanguageCode() {
100
101
		$title = $this->getMockBuilder( '\Title' )
102
			->disableOriginalConstructor()
103
			->getMock();
104
105
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
106
			->disableOriginalConstructor()
107
			->getMock();
108
109
		$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
110
			->disableOriginalConstructor()
111
			->getMock();
112
113
		$pageLanguage = $this->getMockBuilder( '\Language' )
114
			->disableOriginalConstructor()
115
			->getMock();
116
117
		$pageLanguage->expects( $this->once() )
118
			->method( 'getCode' )
119
			->will( $this->returnValue( 'en' ) );
120
121
		$instance = new PageContentLanguageOnTheFlyModifier(
122
			$interlanguageLinksLookup,
123
			$cache
124
		);
125
126
		$this->assertEquals(
127
			'en',
128
			$instance->getPageContentLanguage( $title, $pageLanguage )
129
		);
130
	}
131
132
	/**
133
	 * @dataProvider invalidLanguageCodeProvider
134
	 */
135
	public function testPageContentLanguageOnInvalidLanguage( $invalidLanguageCode ) {
136
137
		$pageLanguage = '';
138
139
		$title = $this->getMockBuilder( '\Title' )
140
			->disableOriginalConstructor()
141
			->getMock();
142
143
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
144
			->disableOriginalConstructor()
145
			->getMock();
146
147
		$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
148
			->disableOriginalConstructor()
149
			->getMock();
150
151
		$interlanguageLinksLookup->expects( $this->once() )
152
			->method( 'findPageLanguageForTarget' )
153
			->will( $this->returnValue( $invalidLanguageCode ) );
154
155
		$instance = new PageContentLanguageOnTheFlyModifier(
156
			$interlanguageLinksLookup,
157
			$cache
158
		);
159
160
		$this->assertEmpty(
161
			$instance->getPageContentLanguage( $title, $pageLanguage )
162
		);
163
	}
164
165
	public function testAddToIntermediaryCache() {
166
167
		$title = $this->getMockBuilder( '\Title' )
168
			->disableOriginalConstructor()
169
			->getMock();
170
171
		$title->expects( $this->once() )
172
			->method( 'getPrefixedText' )
173
			->will( $this->returnValue( 'Foo' ) );
174
175
		$cache = $this->getMockBuilder( '\Onoi\Cache\Cache' )
176
			->disableOriginalConstructor()
177
			->getMock();
178
179
		$cache->expects( $this->once() )
180
			->method( 'save' )
181
			->with(
182
				$this->anything(),
183
				$this->equalTo( 'BAR' ) );
184
185
		$interlanguageLinksLookup = $this->getMockBuilder( '\SIL\InterlanguageLinksLookup' )
186
			->disableOriginalConstructor()
187
			->getMock();
188
189
		$instance = new PageContentLanguageOnTheFlyModifier(
190
			$interlanguageLinksLookup,
191
			$cache
192
		);
193
194
		$instance->addToIntermediaryCache( $title, 'BAR' );
195
	}
196
197
	public function invalidLanguageCodeProvider() {
198
199
		$provider = array(
200
			array( null ),
201
			array( '' ),
202
			array( false )
203
		);
204
205
		return $provider;
206
	}
207
208
}
209