Completed
Push — master ( 51933d...d0e318 )
by mw
38:37
created

testCanGetAnnotatedLanguageCodeOnValueWithDash()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 8
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 14
rs 9.4285
1
<?php
2
3
namespace SMW\Tests;
4
5
use Language;
6
use SMW\Localizer;
7
8
/**
9
 * @covers \SMW\Localizer
10
 * @group semantic-mediawiki
11
 *
12
 * @license GNU GPL v2+
13
 * @since 2.1
14
 *
15
 * @author mwjames
16
 */
17
class LocalizerTest extends \PHPUnit_Framework_TestCase {
18
19
	protected function tearDown() {
20
		Localizer::clear();
21
	}
22
23
	public function testCanConstruct() {
24
25
		$language = $this->getMockBuilder( '\Language' )
26
			->disableOriginalConstructor()
27
			->getMock();
28
29
		$this->assertInstanceOf(
30
			'\SMW\Localizer',
31
			new Localizer( $language )
32
		);
33
34
		$this->assertInstanceOf(
35
			'\SMW\Localizer',
36
			Localizer::getInstance()
37
		);
38
	}
39
40
	public function testGetContentLanguage() {
0 ignored issues
show
Coding Style introduced by
testGetContentLanguage uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
41
42
		$language = $this->getMockBuilder( '\Language' )
43
			->disableOriginalConstructor()
44
			->getMock();
45
46
		$instance = new Localizer( $language );
47
48
		$this->assertSame(
49
			$language,
50
			$instance->getContentLanguage()
51
		);
52
53
		$this->assertSame(
54
			$GLOBALS['wgContLang'],
55
			Localizer::getInstance()->getContentLanguage()
56
		);
57
	}
58
59
	public function testNamespaceTextById() {
60
61
		$instance = new Localizer( Language::factory( 'en') );
62
63
		$this->assertEquals(
64
			'Property',
65
			$instance->getNamespaceTextById( SMW_NS_PROPERTY )
66
		);
67
	}
68
69
	public function testNamespaceIndexByName() {
70
71
		$instance = new Localizer( Language::factory( 'en') );
72
73
		$this->assertEquals(
74
			SMW_NS_PROPERTY,
75
			$instance->getNamespaceIndexByName( 'property' )
76
		);
77
	}
78
79
	public function testSupportedLanguageForLowerCaseLetter() {
0 ignored issues
show
Coding Style introduced by
testSupportedLanguageForLowerCaseLetter uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
80
81
		if ( version_compare( $GLOBALS['wgVersion'], '1.20', '<' ) ) {
82
			$this->markTestSkipped( 'Skipping because `Language::isSupportedLanguage` is not supported on 1.19' );
83
		}
84
85
		$this->assertTrue(
86
			Localizer::isSupportedLanguage( 'en' )
87
		);
88
	}
89
90
	public function testSupportedLanguageForUpperCaseLetter() {
0 ignored issues
show
Coding Style introduced by
testSupportedLanguageForUpperCaseLetter uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
91
92
		if ( version_compare( $GLOBALS['wgVersion'], '1.20', '<' ) ) {
93
			$this->markTestSkipped( 'Skipping because `Language::isSupportedLanguage` is not supported on 1.19' );
94
		}
95
96
		$this->assertTrue(
97
			Localizer::isSupportedLanguage( 'ZH-HANS' )
98
		);
99
	}
100
101
	public function testAsBCP47FormattedLanguageCode() {
102
		$this->assertEquals(
103
			'zh-Hans',
104
			Localizer::asBCP47FormattedLanguageCode( 'zh-hans' )
105
		);
106
	}
107
108
	public function testCanGetAnnotatedLanguageCodeOnValidMarkedValue() {
109
110
		$value = 'Foo@en';
111
112
		$this->assertEquals(
113
			'en',
114
			Localizer::getAnnotatedLanguageCodeFrom( $value )
115
		);
116
117
		$this->assertEquals(
118
			'Foo',
119
			$value
120
		);
121
	}
122
123
	public function testCanGetAnnotatedLanguageCodeOnDoubledMarkedValue() {
124
125
		$value = 'Foo@@en';
126
127
		$this->assertEquals(
128
			'en',
129
			Localizer::getAnnotatedLanguageCodeFrom( $value )
130
		);
131
132
		$this->assertEquals(
133
			'Foo@',
134
			$value
135
		);
136
	}
137
138
	public function testCanGetAnnotatedLanguageCodeOnValueWithDash() {
139
140
		$value = 'Foo@zh-Hans';
141
142
		$this->assertEquals(
143
			'zh-Hans',
144
			Localizer::getAnnotatedLanguageCodeFrom( $value )
145
		);
146
147
		$this->assertEquals(
148
			'Foo',
149
			$value
150
		);
151
	}
152
153
	public function testCanNotGetAnnotatedLanguageCodeThatContainsInvalidCharacter() {
154
155
		$value = 'Foo@en#bar';
156
157
		$this->assertFalse(
158
			Localizer::getAnnotatedLanguageCodeFrom( $value )
159
		);
160
	}
161
162
	public function testCanNotGetLanguageCodeOnNonMarkedValue() {
163
164
		$value = 'Fooen';
165
166
		$this->assertFalse(
167
			Localizer::getLanguageCodeFrom( $value )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\Localizer::getLanguageCodeFrom() has been deprecated with message: 2.5, use Localizer::getAnnotatedLanguageCodeFrom instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
168
		);
169
170
		$this->assertEquals(
171
			'Fooen',
172
			$value
173
		);
174
	}
175
176
	public function testCanNotGetLanguageCodeOnMissingLanguageCode() {
177
178
		$value = 'Foo@';
179
180
		$this->assertFalse(
181
			Localizer::getLanguageCodeFrom( $value )
0 ignored issues
show
Deprecated Code introduced by
The method SMW\Localizer::getLanguageCodeFrom() has been deprecated with message: 2.5, use Localizer::getAnnotatedLanguageCodeFrom instead

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
182
		);
183
184
		$this->assertEquals(
185
			'Foo@',
186
			$value
187
		);
188
	}
189
190
	public function testGetLanguageCodeByRule_OnTitleExpectedToPageLanguage() {
191
192
		$contentLanguage = $this->getMockBuilder( '\Language' )
193
			->disableOriginalConstructor()
194
			->getMock();
195
196
		$instance = new Localizer( $contentLanguage );
197
198
		$pageLanguage = $this->getMockBuilder( '\Language' )
199
			->disableOriginalConstructor()
200
			->getMock();
201
202
		$title = $this->getMockBuilder( '\Title' )
203
			->disableOriginalConstructor()
204
			->getMock();
205
206
		$title->expects( $this->once() )
207
			->method( 'getPageLanguage' )
208
			->will( $this->returnValue( $pageLanguage ) );
209
210
		$this->assertEquals(
211
			$pageLanguage,
212
			$instance->getPreferredContentLanguage( $title )
213
		);
214
	}
215
216
	public function testGetLanguageCodeByRule_OnNotProvidedTitlePageLanguageExpectedToReturnUserLanguage() {
217
218
		$contentLanguage = $this->getMockBuilder( '\Language' )
219
			->disableOriginalConstructor()
220
			->getMock();
221
222
		$instance = new Localizer( $contentLanguage );
223
224
		$this->assertEquals(
225
			$instance->getContentLanguage(),
226
			$instance->getPreferredContentLanguage( null )
227
		);
228
	}
229
230
	public function testExtraneousLanguage() {
231
232
		$instance = Localizer::getInstance();
233
234
		$this->assertInstanceOf(
235
			'\SMW\ExtraneousLanguage',
236
			$instance->getExtraneousLanguage()
237
		);
238
239
		$this->assertInstanceOf(
240
			'\SMW\ExtraneousLanguage',
241
			$instance->getExtraneousLanguage( 'en' )
242
		);
243
244
		$language = $this->getMockBuilder( '\Language' )
245
			->disableOriginalConstructor()
246
			->getMock();
247
248
		$language->expects( $this->once() )
249
			->method( 'getCode' )
250
			->will( $this->returnValue( 'en' ) );
251
252
		$this->assertInstanceOf(
253
			'\SMW\ExtraneousLanguage',
254
			$instance->getExtraneousLanguage( $language )
255
		);
256
	}
257
258
	public function testConvertDoubleWidth() {
259
260
		$this->assertEquals(
261
			'2000',
262
			Localizer::convertDoubleWidth( '2000' )
263
		);
264
265
		$this->assertEquals(
266
			'aBc',
267
			Localizer::getInstance()->convertDoubleWidth( 'aBc' )
268
		);
269
	}
270
271
}
272