Skip to content

Commit 67655f8

Browse files
committed
Merge branch '2.5'
* 2.5: Remove Spaceless Blocks From Twig Templates Validate locales sets intos translator [Console] Remove estimated field from debug_nomax Fix UserPassword validator translation Remove Spaceless Blocks from Twig Form Templates [Validator] remove wrong deprecation message fixed typo [HttpFoundation] Fix to prevent magic bytes injection in JSONP responses (Prevents CVE-2014-4671) added regression test fix issue #8171 on configuration tree for twig extension -- pairing up with @cordoval [HttpFoundation] Fix wrong assertion in Response test [Upgrade] Fixed markdown syntax [2.3][Form] Cleanup & fix phpdocs Added verbosity methods
2 parents aae12f4 + a2a87c5 commit 67655f8

File tree

3 files changed

+215
-1
lines changed

3 files changed

+215
-1
lines changed

Tests/TranslatorTest.php

Lines changed: 175 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,33 @@
1616

1717
class TranslatorTest extends \PHPUnit_Framework_TestCase
1818
{
19+
20+
/**
21+
* @dataProvider getInvalidLocalesTests
22+
* @expectedException \InvalidArgumentException
23+
*/
24+
public function testConstructorInvalidLocale($locale)
25+
{
26+
$translator = new Translator($locale, new MessageSelector());
27+
}
28+
29+
/**
30+
* @dataProvider getValidLocalesTests
31+
*/
32+
public function testConstructorValidLocale($locale)
33+
{
34+
$translator = new Translator($locale, new MessageSelector());
35+
36+
$this->assertEquals($locale, $translator->getLocale());
37+
}
38+
39+
public function testConstructorWithoutLocale()
40+
{
41+
$translator = new Translator(null, new MessageSelector());
42+
43+
$this->assertNull($translator->getLocale());
44+
}
45+
1946
public function testSetGetLocale()
2047
{
2148
$translator = new Translator('en');
@@ -26,6 +53,27 @@ public function testSetGetLocale()
2653
$this->assertEquals('fr', $translator->getLocale());
2754
}
2855

56+
/**
57+
* @dataProvider getInvalidLocalesTests
58+
* @expectedException \InvalidArgumentException
59+
*/
60+
public function testSetInvalidLocale($locale)
61+
{
62+
$translator = new Translator('fr', new MessageSelector());
63+
$translator->setLocale($locale);
64+
}
65+
66+
/**
67+
* @dataProvider getValidLocalesTests
68+
*/
69+
public function testSetValidLocale($locale)
70+
{
71+
$translator = new Translator($locale, new MessageSelector());
72+
$translator->setLocale($locale);
73+
74+
$this->assertEquals($locale, $translator->getLocale());
75+
}
76+
2977
public function testSetFallbackLocales()
3078
{
3179
$translator = new Translator('en');
@@ -54,6 +102,27 @@ public function testSetFallbackLocalesMultiple()
54102
$this->assertEquals('bar (fr)', $translator->trans('bar'));
55103
}
56104

105+
106+
/**
107+
* @dataProvider getInvalidLocalesTests
108+
* @expectedException \InvalidArgumentException
109+
*/
110+
public function testSetFallbackInvalidLocales($locale)
111+
{
112+
$translator = new Translator('fr', new MessageSelector());
113+
$translator->setFallbackLocales(array('fr', $locale));
114+
}
115+
116+
/**
117+
* @dataProvider getValidLocalesTests
118+
*/
119+
public function testSetFallbackValidLocales($locale)
120+
{
121+
$translator = new Translator($locale, new MessageSelector());
122+
$translator->setFallbackLocales(array('fr', $locale));
123+
// no assertion. this method just asserts that no exception is thrown
124+
}
125+
57126
public function testTransWithFallbackLocale()
58127
{
59128
$translator = new Translator('fr_FR');
@@ -66,6 +135,26 @@ public function testTransWithFallbackLocale()
66135
$this->assertEquals('foobar', $translator->trans('bar'));
67136
}
68137

138+
/**
139+
* @dataProvider getInvalidLocalesTests
140+
* @expectedException \InvalidArgumentException
141+
*/
142+
public function testAddResourceInvalidLocales($locale)
143+
{
144+
$translator = new Translator('fr', new MessageSelector());
145+
$translator->addResource('array', array('foo' => 'foofoo'), $locale);
146+
}
147+
148+
/**
149+
* @dataProvider getValidLocalesTests
150+
*/
151+
public function testAddResourceValidLocales($locale)
152+
{
153+
$translator = new Translator('fr', new MessageSelector());
154+
$translator->addResource('array', array('foo' => 'foofoo'), $locale);
155+
// no assertion. this method just asserts that no exception is thrown
156+
}
157+
69158
public function testAddResourceAfterTrans()
70159
{
71160
$translator = new Translator('fr');
@@ -163,6 +252,32 @@ public function testTrans($expected, $id, $translation, $parameters, $locale, $d
163252
$this->assertEquals($expected, $translator->trans($id, $parameters, $domain, $locale));
164253
}
165254

255+
/**
256+
* @dataProvider getInvalidLocalesTests
257+
* @expectedException \InvalidArgumentException
258+
*/
259+
public function testTransInvalidLocale($locale)
260+
{
261+
$translator = new Translator('en', new MessageSelector());
262+
$translator->addLoader('array', new ArrayLoader());
263+
$translator->addResource('array', array('foo' => 'foofoo'), 'en');
264+
265+
$translator->trans('foo', array(), '', $locale);
266+
}
267+
268+
/**
269+
* @dataProvider getValidLocalesTests
270+
*/
271+
public function testTransValidLocale($locale)
272+
{
273+
$translator = new Translator('en', new MessageSelector());
274+
$translator->addLoader('array', new ArrayLoader());
275+
$translator->addResource('array', array('foo' => 'foofoo'), 'en');
276+
277+
$translator->trans('foo', array(), '', $locale);
278+
// no assertion. this method just asserts that no exception is thrown
279+
}
280+
166281
/**
167282
* @dataProvider getFlattenedTransTests
168283
*/
@@ -187,6 +302,33 @@ public function testTransChoice($expected, $id, $translation, $number, $paramete
187302
$this->assertEquals($expected, $translator->transChoice($id, $number, $parameters, $domain, $locale));
188303
}
189304

305+
/**
306+
* @dataProvider getInvalidLocalesTests
307+
* @expectedException \InvalidArgumentException
308+
*/
309+
public function testTransChoiceInvalidLocale($locale)
310+
{
311+
$translator = new Translator('en', new MessageSelector());
312+
$translator->addLoader('array', new ArrayLoader());
313+
$translator->addResource('array', array('foo' => 'foofoo'), 'en');
314+
315+
$translator->transChoice('foo', 1, array(), '', $locale);
316+
}
317+
318+
/**
319+
* @dataProvider getValidLocalesTests
320+
*/
321+
public function testTransChoiceValidLocale($locale)
322+
{
323+
$translator = new Translator('en', new MessageSelector());
324+
$translator->addLoader('array', new ArrayLoader());
325+
$translator->addResource('array', array('foo' => 'foofoo'), 'en');
326+
327+
$translator->transChoice('foo', 1, array(), '', $locale);
328+
// no assertion. this method just asserts that no exception is thrown
329+
}
330+
331+
190332
public function getTransFileTests()
191333
{
192334
return array(
@@ -257,6 +399,39 @@ public function getTransChoiceTests()
257399
);
258400
}
259401

402+
public function getInvalidLocalesTests()
403+
{
404+
return array(
405+
array('fr FR'),
406+
array('français'),
407+
array('fr+en'),
408+
array('utf#8'),
409+
array('fr&en'),
410+
array('fr~FR'),
411+
array(' fr'),
412+
array('fr '),
413+
array('fr*'),
414+
array('fr/FR'),
415+
array('fr\\FR'),
416+
);
417+
}
418+
419+
public function getValidLocalesTests()
420+
{
421+
return array(
422+
array(''),
423+
array(null),
424+
array('fr'),
425+
array('francais'),
426+
array('FR'),
427+
array('frFR'),
428+
array('fr-FR'),
429+
array('fr_FR'),
430+
array('fr.FR'),
431+
array('fr-FR.UTF8'),
432+
);
433+
}
434+
260435
public function testTransChoiceFallback()
261436
{
262437
$translator = new Translator('ru');

Translator.php

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,13 @@ class Translator implements TranslatorInterface
5959
* @param string $locale The locale
6060
* @param MessageSelector|null $selector The message selector for pluralization
6161
*
62+
* @throws \InvalidArgumentException If a locale contains invalid characters
63+
*
6264
* @api
6365
*/
6466
public function __construct($locale, MessageSelector $selector = null)
6567
{
66-
$this->locale = $locale;
68+
$this->setLocale($locale);
6769
$this->selector = $selector ?: new MessageSelector();
6870
}
6971

@@ -88,6 +90,8 @@ public function addLoader($format, LoaderInterface $loader)
8890
* @param string $locale The locale
8991
* @param string $domain The domain
9092
*
93+
* @throws \InvalidArgumentException If the locale contains invalid characters
94+
*
9195
* @api
9296
*/
9397
public function addResource($format, $resource, $locale, $domain = null)
@@ -96,6 +100,8 @@ public function addResource($format, $resource, $locale, $domain = null)
96100
$domain = 'messages';
97101
}
98102

103+
$this->assertValidLocale($locale);
104+
99105
$this->resources[$locale][] = array($format, $resource, $domain);
100106

101107
if (in_array($locale, $this->fallbackLocales)) {
@@ -112,6 +118,7 @@ public function addResource($format, $resource, $locale, $domain = null)
112118
*/
113119
public function setLocale($locale)
114120
{
121+
$this->assertValidLocale($locale);
115122
$this->locale = $locale;
116123
}
117124

@@ -130,6 +137,8 @@ public function getLocale()
130137
*
131138
* @param string|array $locales The fallback locale(s)
132139
*
140+
* @throws \InvalidArgumentException If a locale contains invalid characters
141+
*
133142
* @deprecated since 2.3, to be removed in 3.0. Use setFallbackLocales() instead.
134143
*
135144
* @api
@@ -144,13 +153,19 @@ public function setFallbackLocale($locales)
144153
*
145154
* @param array $locales The fallback locales
146155
*
156+
* @throws \InvalidArgumentException If a locale contains invalid characters
157+
*
147158
* @api
148159
*/
149160
public function setFallbackLocales(array $locales)
150161
{
151162
// needed as the fallback locales are linked to the already loaded catalogues
152163
$this->catalogues = array();
153164

165+
foreach ($locales as $locale) {
166+
$this->assertValidLocale($locale);
167+
}
168+
154169
$this->fallbackLocales = $locales;
155170
}
156171

@@ -175,6 +190,8 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
175190
{
176191
if (null === $locale) {
177192
$locale = $this->getLocale();
193+
} else {
194+
$this->assertValidLocale($locale);
178195
}
179196

180197
if (null === $domain) {
@@ -197,6 +214,8 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
197214
{
198215
if (null === $locale) {
199216
$locale = $this->getLocale();
217+
} else {
218+
$this->assertValidLocale($locale);
200219
}
201220

202221
if (null === $domain) {
@@ -289,4 +308,18 @@ protected function computeFallbackLocales($locale)
289308

290309
return array_unique($locales);
291310
}
311+
312+
/**
313+
* Asserts that the locale is valid, throws an Exception if not.
314+
*
315+
* @param string $locale Locale to tests
316+
*
317+
* @throws \InvalidArgumentException If the locale contains invalid characters
318+
*/
319+
private function assertValidLocale($locale)
320+
{
321+
if (0 !== preg_match('/[^a-z0-9_\\.\\-]+/i', $locale, $match)) {
322+
throw new \InvalidArgumentException(sprintf('Invalid locale: %s.', $locale));
323+
}
324+
}
292325
}

TranslatorInterface.php

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ interface TranslatorInterface
2828
* @param string|null $domain The domain for the message or null to use the default
2929
* @param string|null $locale The locale or null to use the default
3030
*
31+
* @throws \InvalidArgumentException If the locale contains invalid characters
32+
*
3133
* @return string The translated string
3234
*
3335
* @api
@@ -43,6 +45,8 @@ public function trans($id, array $parameters = array(), $domain = null, $locale
4345
* @param string|null $domain The domain for the message or null to use the default
4446
* @param string|null $locale The locale or null to use the default
4547
*
48+
* @throws \InvalidArgumentException If the locale contains invalid characters
49+
*
4650
* @return string The translated string
4751
*
4852
* @api
@@ -54,6 +58,8 @@ public function transChoice($id, $number, array $parameters = array(), $domain =
5458
*
5559
* @param string $locale The locale
5660
*
61+
* @throws \InvalidArgumentException If the locale contains invalid characters
62+
*
5763
* @api
5864
*/
5965
public function setLocale($locale);

0 commit comments

Comments
 (0)