PageRenderTime 6ms CodeModel.GetById 80ms app.highlight 50ms RepoModel.GetById 30ms app.codeStats 1ms

/tests/Zend/TranslateTest.php

https://bitbucket.org/ksekar/campus
PHP | 927 lines | 660 code | 129 blank | 138 comment | 4 complexity | 78a043aca1b34a2de41ccd8850604570 MD5 | raw file
  1<?php
  2/**
  3 * Zend Framework
  4 *
  5 * LICENSE
  6 *
  7 * This source file is subject to the new BSD license that is bundled
  8 * with this package in the file LICENSE.txt.
  9 * It is also available through the world-wide-web at this URL:
 10 * http://framework.zend.com/license/new-bsd
 11 * If you did not receive a copy of the license and are unable to
 12 * obtain it through the world-wide-web, please send an email
 13 * to license@zend.com so we can send you a copy immediately.
 14 *
 15 * @category   Zend
 16 * @package    Zend_Translate
 17 * @subpackage UnitTests
 18 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 19 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 20 * @version    $Id $
 21 */
 22
 23/**
 24 * Zend_Translate
 25 */
 26require_once 'Zend/Translate.php';
 27
 28/**
 29 * Zend_Translate_Plural
 30 */
 31require_once 'Zend/Translate/Plural.php';
 32
 33/**
 34 * @category   Zend
 35 * @package    Zend_Translate
 36 * @subpackage UnitTests
 37 * @copyright  Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 38 * @license    http://framework.zend.com/license/new-bsd     New BSD License
 39 * @group      Zend_Translate
 40 */
 41class Zend_TranslateTest extends PHPUnit_Framework_TestCase
 42{
 43    /**
 44     * Runs the test methods of this class.
 45     *
 46     * @return void
 47     */
 48    public static function main()
 49    {
 50        $suite  = new PHPUnit_Framework_TestSuite("Zend_TranslateTest");
 51        $result = PHPUnit_TextUI_TestRunner::run($suite);
 52    }
 53
 54    public function setUp()
 55    {
 56        if (Zend_Translate::hasCache()) {
 57            Zend_Translate::removeCache();
 58        }
 59
 60        require_once 'Zend/Translate/Adapter/Array.php';
 61        if (Zend_Translate_Adapter_Array::hasCache()) {
 62            Zend_Translate_Adapter_Array::removeCache();
 63        }
 64    }
 65
 66    public function testCreate()
 67    {
 68        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('1' => '1'));
 69        $this->assertTrue($lang instanceof Zend_Translate);
 70    }
 71
 72    public function testLocaleInitialization()
 73    {
 74        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'message1'), 'en');
 75        $this->assertEquals('en', $lang->getLocale());
 76    }
 77
 78    public function testDefaultLocale()
 79    {
 80        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'message1'));
 81        $defaultLocale = new Zend_Locale();
 82        $this->assertEquals($defaultLocale->toString(), $lang->getLocale());
 83    }
 84
 85    public function testGetAdapter()
 86    {
 87        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY , array('1' => '1'), 'en');
 88        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Array);
 89
 90        $lang = new Zend_Translate(Zend_Translate::AN_GETTEXT , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.mo', 'en');
 91        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Gettext);
 92
 93        $lang = new Zend_Translate(Zend_Translate::AN_TMX , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.tmx', 'en');
 94        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Tmx);
 95
 96        $lang = new Zend_Translate(Zend_Translate::AN_CSV , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.csv', 'en');
 97        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Csv);
 98
 99        $lang = new Zend_Translate(Zend_Translate::AN_XLIFF , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.xliff', 'en');
100        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Xliff);
101
102        $lang = new Zend_Translate('Qt' , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en2.ts', 'en');
103        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Qt);
104
105        $lang = new Zend_Translate('XmlTm' , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.xmltm', 'en');
106        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_XmlTm);
107
108        $lang = new Zend_Translate('Tbx' , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.tbx', 'en');
109        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Tbx);
110    }
111
112    public function testSetAdapter()
113    {
114        $lang = new Zend_Translate(Zend_Translate::AN_GETTEXT , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.mo', 'en');
115        $lang->setAdapter(Zend_Translate::AN_ARRAY, array('de' => 'de'));
116        $this->assertTrue($lang->getAdapter() instanceof Zend_Translate_Adapter_Array);
117
118        try {
119            $lang->xxxFunction();
120            $this->fail("exception expected");
121        } catch (Zend_Translate_Exception $e) {
122            // success
123        }
124    }
125
126    public function testAddTranslation()
127    {
128        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
129
130        $this->assertEquals('msg2', $lang->_('msg2'));
131
132        $lang->addTranslation(array('msg2' => 'Message 2'), 'en');
133        $this->assertEquals('Message 2', $lang->_('msg2'));
134        $this->assertEquals('msg3',      $lang->_('msg3'));
135
136        $lang->addTranslation(array('msg3' => 'Message 3'), 'en', array('clear' => true));
137        $this->assertEquals('msg2',      $lang->_('msg2'));
138        $this->assertEquals('Message 3', $lang->_('msg3'));
139    }
140
141    public function testGetLocale()
142    {
143        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
144        $this->assertEquals('en', $lang->getLocale());
145    }
146
147    public function testSetLocale()
148    {
149        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
150        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
151        $this->assertEquals('ru', $lang->getLocale());
152
153        $lang->setLocale('en');
154        $this->assertEquals('en', $lang->getLocale());
155
156        $lang->setLocale('ru');
157        $this->assertEquals('ru', $lang->getLocale());
158
159        $lang->setLocale('ru_RU');
160        $this->assertEquals('ru', $lang->getLocale());
161    }
162
163    public function testSetLanguage()
164    {
165        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
166        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
167        $this->assertEquals('ru', $lang->getLocale());
168
169        $lang->setLocale('en');
170        $this->assertEquals('en', $lang->getLocale());
171    }
172
173    public function testGetLanguageList()
174    {
175        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
176        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
177        $this->assertEquals(2, count($lang->getList()));
178        $this->assertTrue(in_array('en', $lang->getList()));
179        $this->assertTrue(in_array('ru', $lang->getList()));
180    }
181
182    public function testIsAvailable()
183    {
184        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
185        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
186        $this->assertTrue( $lang->isAvailable('en'));
187        $this->assertTrue( $lang->isAvailable('ru'));
188        $this->assertFalse($lang->isAvailable('fr'));
189    }
190
191    public function testTranslate()
192    {
193        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1 (en)'), 'en');
194        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
195        $this->assertEquals('Message 1 (en)', $lang->_('msg1', 'en'        ));
196        $this->assertEquals('Message 1 (ru)', $lang->_('msg1'              ));
197        $this->assertEquals('msg2',           $lang->_('msg2', 'en'        ));
198        $this->assertEquals('msg2',           $lang->_('msg2'              ));
199        $this->assertEquals('Message 1 (en)', $lang->translate('msg1', 'en'));
200        $this->assertEquals('Message 1 (ru)', $lang->translate('msg1'      ));
201        $this->assertEquals('msg2',           $lang->translate('msg2', 'en'));
202        $this->assertEquals('msg2',           $lang->translate('msg2'      ));
203    }
204
205    public function testIsTranslated()
206    {
207        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1 (en)'), 'en_US');
208        $this->assertTrue( $lang->isTranslated('msg1'             ));
209        $this->assertFalse($lang->isTranslated('msg2'             ));
210        $this->assertFalse($lang->isTranslated('msg1', false, 'en'));
211        $this->assertFalse($lang->isTranslated('msg1', true,  'en'));
212        $this->assertFalse($lang->isTranslated('msg1', false, 'ru'));
213    }
214
215    public function testWithOption()
216    {
217        $lang = new Zend_Translate(Zend_Translate::AN_CSV , dirname(__FILE__) . '/Translate/Adapter/_files/translation_otherdelimiter.csv', 'en', array('delimiter' => ','));
218        $this->assertEquals('Message 1 (en)', $lang->translate('Message 1'));
219        $this->assertEquals('Message 4 (en)', $lang->translate('Message 4,'));
220        $this->assertEquals('Message 5, (en)', $lang->translate('Message 5'));
221    }
222
223    public function testDirectorySearch()
224    {
225        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files/testcsv', 'de_AT', array('scan' => Zend_Translate::LOCALE_DIRECTORY));
226        $this->assertEquals(array('de_AT' => 'de_AT', 'en_GB' => 'en_GB'), $lang->getList());
227        $this->assertEquals('Nachricht 8', $lang->translate('Message 8'));
228    }
229
230    public function testFileSearch()
231    {
232        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files/testcsv', 'de_DE', array('scan' => Zend_Translate::LOCALE_FILENAME));
233        $this->assertEquals(array('de_DE' => 'de_DE', 'en_US' => 'en_US'), $lang->getList());
234        $this->assertEquals('Nachricht 8', $lang->translate('Message 8'));
235    }
236
237    public function testTestingCacheHandling()
238    {
239        require_once 'Zend/Cache.php';
240        $cache = Zend_Cache::factory('Core', 'File',
241            array('lifetime' => 120, 'automatic_serialization' => true),
242            array('cache_dir' => dirname(__FILE__) . '/_files/'));
243        Zend_Translate::setCache($cache);
244
245        $cache = Zend_Translate::getCache();
246        $this->assertTrue($cache instanceof Zend_Cache_Core);
247        $this->assertTrue(Zend_Translate::hasCache());
248
249        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1 (en)'), 'en');
250        $adapter = $lang->getAdapter();
251        $this->assertTrue($adapter instanceof Zend_Translate_Adapter_Array);
252        $adaptercache = $adapter->getCache();
253        $this->assertTrue($adaptercache instanceof Zend_Cache_Core);
254
255        Zend_Translate::clearCache();
256        $this->assertTrue(Zend_Translate::hasCache());
257        Zend_Translate::removeCache();
258        $this->assertFalse(Zend_Translate::hasCache());
259    }
260
261    public function testExceptionWhenNoAdapterClassWasSet()
262    {
263        try {
264            $lang = new Zend_Translate('Zend_Locale', dirname(__FILE__) . '/Translate/_files/test2', null, array('scan' => Zend_Translate::LOCALE_FILENAME));
265            $this->fail('Exception due to false adapter class expected');
266        } catch (Zend_Translate_Exception $e) {
267            $this->assertContains('does not extend Zend_Translate_Adapter', $e->getMessage());
268        }
269    }
270
271    public function testZF3679()
272    {
273        require_once 'Zend/Locale.php';
274        $locale = new Zend_Locale('de_AT');
275        require_once 'Zend/Registry.php';
276        Zend_Registry::set('Zend_Locale', $locale);
277
278        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'message1'), 'de_AT');
279        $this->assertEquals('de_AT', $lang->getLocale());
280        Zend_Registry::_unsetInstance();
281    }
282
283    /**
284     * ZF-4994
285     */
286    public function testCamelCasedOptions()
287    {
288        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files/translation_otherdelimiter.csv', 'en', array('delimiter' => ','));
289        $lang->setOptions(array('myOption' => true));
290        $this->assertTrue($lang->getOptions('myOption'));
291    }
292
293    /**
294     * ZF-4905
295     */
296    public function testPathNameWithColonResolution()
297    {
298        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/../Adapter/_files', 'en', array('delimiter' => ','));
299        $this->assertEquals('en', $lang->getLocale());
300    }
301
302    public function testUntranslatedMessageWithTriggeredError()
303    {
304        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
305        $this->assertEquals('ignored', $lang->translate('ignored'));
306
307        $this->_errorOccured = false;
308        $lang->setOptions(array('logUntranslated' => true));
309        set_error_handler(array($this, 'errorHandlerIgnore'));
310        $this->assertEquals('ignored', $lang->translate('ignored'));
311        $this->assertTrue($this->_errorOccured);
312        restore_error_handler();
313    }
314
315    public function testLogUntranslatedMessage()
316    {
317        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
318        $this->assertEquals('ignored', $lang->translate('ignored'));
319
320        $stream = fopen('php://memory', 'w+');
321        require_once 'Zend/Log/Writer/Stream.php';
322        $writer = new Zend_Log_Writer_Stream($stream);
323        require_once 'Zend/Log.php';
324        $log    = new Zend_Log($writer);
325
326        $lang->setOptions(array('logUntranslated' => true, 'log' => $log));
327        $this->assertEquals('ignored', $lang->translate('ignored'));
328
329        rewind($stream);
330        $this->assertContains('ignored', stream_get_contents($stream));
331    }
332
333    public function testSettingUnknownLocaleWithTriggeredError()
334    {
335        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
336        $this->_errorOccured = false;
337        set_error_handler(array($this, 'errorHandlerIgnore'));
338        $lang->setLocale('ru');
339        $this->assertEquals('ru', $lang->getLocale('ru'));
340        $this->assertTrue($this->_errorOccured);
341        restore_error_handler();
342    }
343
344    public function testSettingUnknownLocaleWritingToLog()
345    {
346        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
347
348        $stream = fopen('php://memory', 'w+');
349        require_once 'Zend/Log/Writer/Stream.php';
350        $writer = new Zend_Log_Writer_Stream($stream);
351        require_once 'Zend/Log.php';
352        $log    = new Zend_Log($writer);
353
354        $lang->setOptions(array('log' => $log));
355        $lang->setLocale('ru');
356
357        rewind($stream);
358        $this->assertContains('has to be added', stream_get_contents($stream));
359    }
360
361    public function testSettingNoLogAsLog()
362    {
363        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
364
365        try {
366            $lang->setOptions(array('log' => 'nolog'));
367            $this->fail();
368        } catch (Zend_Translate_Exception $e) {
369            $this->assertContains('Instance of Zend_Log expected', $e->getMessage());
370        }
371    }
372
373    public function testSettingUnknownLocaleWritingToSelfDefinedLog()
374    {
375        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
376        $this->assertEquals('ignored', $lang->translate('ignored'));
377
378        $stream = fopen('php://memory', 'w+');
379        require_once 'Zend/Log/Writer/Stream.php';
380        $writer = new Zend_Log_Writer_Stream($stream);
381        require_once 'Zend/Log.php';
382        $log    = new Zend_Log($writer);
383
384        $lang->setOptions(array('logUntranslated' => true, 'log' => $log, 'logMessage' => 'Self defined log message'));
385        $this->assertEquals('ignored', $lang->translate('ignored'));
386
387        rewind($stream);
388        $this->assertContains('Self defined log message', stream_get_contents($stream));
389    }
390
391    /**
392     * Tests if cached options are read from the cache for a new instance
393     */
394    public function testGetOptionsFromCache()
395    {
396        require_once 'Zend/Cache.php';
397        $cache = Zend_Cache::factory('Core', 'File',
398            array('lifetime' => 120, 'automatic_serialization' => true),
399            array('cache_dir' => dirname(__FILE__) . '/_files/'));
400        Zend_Translate::setCache($cache);
401
402        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
403        $lang->setOptions(array('logMessage' => 'test'));
404        $this->assertEquals('test', $lang->getOptions('logMessage'));
405        unset($lang);
406
407        $lang2 = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
408        $this->assertEquals('test', $lang2->getOptions('logMessage'));
409    }
410
411    /**
412     * Tests if setting locale as options sets locale
413     */
414    public function testSetLocaleAsOption()
415    {
416        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
417        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
418        $lang->setOptions(array('locale' => 'ru'));
419        $this->assertEquals('ru', $lang->getLocale());
420        $lang->setOptions(array('locale' => 'en'));
421        $this->assertEquals('en', $lang->getLocale());
422    }
423
424    /**
425     * Tests getting null returns all options
426     */
427    public function testGettingAllOptions()
428    {
429        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
430        $this->assertTrue(is_array($lang->getOptions()));
431    }
432
433    /**
434     * Tests if setting locale as options sets locale
435     */
436    public function testGettingUnknownOption()
437    {
438        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
439        $this->assertEquals(null, $lang->getOptions('unknown'));
440    }
441
442    /**
443     * Tests getting of all message ids works
444     */
445    public function testGettingAllMessageIds()
446    {
447        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1', 'msg2' => 'Message 2'), 'en');
448        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
449        $this->assertEquals(array('msg1'), $lang->getMessageIds());
450        $this->assertEquals(array('msg1', 'msg2'), $lang->getMessageIds('en'));
451    }
452
453    /**
454     * Tests getting of single message ids
455     */
456    public function testGettingSingleMessageIds()
457    {
458        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1', 'msg2' => 'Message 2'), 'en');
459        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
460        $this->assertEquals('msg1', $lang->getMessageId('Message 1 (ru)'));
461        $this->assertEquals('msg2', $lang->getMessageId('Message 2', 'en'));
462        $this->assertFalse($lang->getMessageId('Message 5'));
463    }
464
465    /**
466     * Tests getting of all messages
467     */
468    public function testGettingAllMessages()
469    {
470        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1', 'msg2' => 'Message 2'), 'en');
471        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'ru');
472        $this->assertEquals(array('msg1' => 'Message 1 (ru)'), $lang->getMessages());
473        $this->assertEquals(
474            array('msg1' => 'Message 1', 'msg2' => 'Message 2'),
475            $lang->getMessages('en'));
476        $this->assertEquals(
477            array(
478                'en' => array('msg1' => 'Message 1', 'msg2' => 'Message 2'),
479                'ru' => array('msg1' => 'Message 1 (ru)')),
480            $lang->getMessages('all'));
481    }
482
483    /**
484     * Tests getting default plurals
485     */
486    public function testGettingPlurals()
487    {
488        $lang = new Zend_Translate(
489            Zend_Translate::AN_ARRAY,
490            array('singular' =>
491                array('plural_0 (en)',
492                    'plural_1 (en)',
493                    'plural_2 (en)',
494                    'plural_3 (en)'),
495                'plural' => ''), 'en'
496        );
497
498        $this->assertEquals('plural_0 (en)', $lang->translate(array('singular', 'plural', 1)));
499        $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 2)));
500
501        $this->assertEquals('plural_0 (en)', $lang->plural('singular', 'plural', 1));
502        $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 2));
503    }
504
505    /**
506     * Tests getting plurals from lowered locale
507     */
508    public function testGettingPluralsFromLoweredLocale()
509    {
510        $lang = new Zend_Translate(
511            Zend_Translate::AN_ARRAY,
512            array('singular' =>
513                array('plural_0 (en)',
514                    'plural_1 (en)',
515                    'plural_2 (en)',
516                    'plural_3 (en)'),
517                'plural' => ''), 'en'
518        );
519        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'en_US');
520        $lang->setLocale('en_US');
521
522        $this->assertEquals('plural_0 (en)', $lang->translate(array('singular', 'plural', 1)));
523        $this->assertEquals('plural_0 (en)', $lang->plural('singular', 'plural', 1));
524    }
525
526    /**
527     * Tests getting plurals from lowered locale
528     */
529    public function testGettingPluralsFromUnknownLocale()
530    {
531        $lang = new Zend_Translate(
532            Zend_Translate::AN_ARRAY,
533            array('singular' =>
534                array('plural_0 (en)',
535                    'plural_1 (en)',
536                    'plural_2 (en)',
537                    'plural_3 (en)'),
538                'plural' => ''), 'en'
539        );
540
541        $this->assertEquals('singular', $lang->translate(array('singular', 'plural', 1), 'ru'));
542        $this->assertEquals('singular', $lang->plural('singular', 'plural', 1, 'ru'));
543        $this->assertEquals('plural', $lang->translate(array('singular', 'plural', 'plural2', 2, 'en'), 'ru'));
544        $this->assertEquals('plural', $lang->plural('singular', 'plural', 2, 'ru'));
545    }
546
547    public function testPluralsWithGettext()
548    {
549        $lang = new Zend_Translate(Zend_Translate::AN_GETTEXT , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.mo', 'en');
550
551        $this->assertEquals('Message 5 (en) Plural 0', $lang->translate(array('Message 5', 'Message 5 Plural', 1)));
552        $this->assertEquals('Message 5 (en) Plural 0', $lang->plural('Message 5', 'Message 5 Plural', 1));
553        $this->assertEquals('Message 5 (en) Plural 1', $lang->translate(array('Message 5', 'Message 5 Plural', 2)));
554        $this->assertEquals('Message 5 (en) Plural 1', $lang->plural('Message 5', 'Message 5 Plural', 2));
555    }
556
557    public function testPluralsWithCsv()
558    {
559        $lang = new Zend_Translate(Zend_Translate::AN_CSV , dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.csv', 'en');
560
561        $this->assertEquals('Message 6 (en) Plural 0', $lang->translate(array('Message 6', 'Message 6 Plural1', 1)));
562        $this->assertEquals('Message 6 (en) Plural 0', $lang->plural('Message 6', 'Message 6 Plural1', 1));
563        $this->assertEquals('Message 6 (en) Plural 1', $lang->translate(array('Message 6', 'Message 6 Plural1', 2)));
564        $this->assertEquals('Message 6 (en) Plural 1', $lang->plural('Message 6', 'Message 6 Plural1', 2));
565    }
566
567    /**
568     * ZF-6671
569     */
570    public function testAddTranslationAfterwards()
571    {
572        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array('msg1' => 'Message 1'), 'en');
573        $this->assertEquals('Message 1', $lang->_('msg1'));
574
575        $lang->addTranslation(array('msg1' => 'Message 1 (en)'), 'en');
576        $this->assertEquals('Message 1 (en)', $lang->_('msg1'));
577    }
578
579    /**
580     * ZF-7560
581     */
582    public function testUseNumericTranslations()
583    {
584        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, array(0 => 'Message 1', 2 => 'Message 2'), 'en');
585        $this->assertEquals('Message 1', $lang->_(0));
586        $this->assertEquals('Message 2', $lang->_(2));
587
588        $lang->addTranslation(array(4 => 'Message 4'), 'en');
589        $this->assertEquals('Message 4', $lang->_(4));
590    }
591
592    /**
593     * ZF-7508
594     */
595    public function testDontLogUntranslatedMessageWithIsTranslated()
596    {
597        $lang = new Zend_Translate(Zend_Translate::AN_CSV, dirname(__FILE__) . '/Translate/Adapter/_files', 'en', array('delimiter' => ','));
598        $this->assertFalse($lang->isTranslated('ignored'));
599
600        $stream = fopen('php://memory', 'w+');
601        require_once 'Zend/Log/Writer/Stream.php';
602        $writer = new Zend_Log_Writer_Stream($stream);
603        require_once 'Zend/Log.php';
604        $log    = new Zend_Log($writer);
605
606        $lang->setOptions(array('logUntranslated' => true, 'log' => $log));
607        $this->assertFalse($lang->isTranslated('ignored'));
608
609        rewind($stream);
610        $this->assertNotContains('ignored', stream_get_contents($stream));
611    }
612
613    /**
614     * ZF-7130
615     */
616    public function testMultiFolderScan()
617    {
618        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, dirname(__FILE__) . '/Translate/Adapter/_files/testarray', 'en_GB', array('scan' => Zend_Translate::LOCALE_DIRECTORY));
619        $this->assertEquals('Message 1 (ja)', $lang->_('Message 1', 'ja'        ));
620        $this->assertEquals('Message 1 (en)', $lang->_('Message 1'              ));
621    }
622
623    /**
624     * ZF-7214
625     */
626    public function testMultiClear()
627    {
628        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, dirname(__FILE__) . '/Translate/Adapter/_files/testarray', 'en_GB', array('scan' => Zend_Translate::LOCALE_DIRECTORY));
629        $this->assertEquals('Message 1 (ja)', $lang->_('Message 1', 'ja'));
630        $lang->addTranslation(dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.php', 'ja', array('clear'));
631        $this->assertEquals('Message 1 (en)', $lang->_('Message 1', 'ja'));
632    }
633
634    /**
635     * ZF-7941
636     */
637    public function testEmptyTranslation()
638    {
639        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, null, null, array('disableNotices' => true));
640        $this->assertEquals(0, count($lang->getList()));
641    }
642
643    /**
644     * Translating Object
645     */
646    public function testObjectTranslation()
647    {
648        $lang = new Zend_Translate(Zend_Translate::AN_ARRAY, dirname(__FILE__) . '/Translate/Adapter/_files/testarray', 'en_GB', array('scan' => Zend_Translate::LOCALE_DIRECTORY));
649        $this->assertEquals('Message 1 (ja)', $lang->_('Message 1', 'ja'));
650
651        $this->assertEquals($lang, $lang->translate($lang));
652    }
653
654    /**
655     * Tests getting plurals from lowered locale
656     */
657    public function testGettingPluralsUsingOwnRule()
658    {
659        $lang = new Zend_Translate(
660            Zend_Translate::AN_ARRAY,
661            array('singular' =>
662                array('plural_0 (en)',
663                    'plural_1 (en)',
664                    'plural_2 (en)',
665                    'plural_3 (en)'),
666                'plural' => ''), 'en'
667        );
668        $lang->addTranslation(array('msg1' => 'Message 1 (ru)'), 'en_US');
669        $lang->setLocale('en_US');
670
671        Zend_Translate_Plural::setPlural(array($this, 'customPlural'), 'en_US');
672        $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 1)));
673        $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 1));
674        $this->assertEquals('plural_1 (en)', $lang->translate(array('singular', 'plural', 0)));
675        $this->assertEquals('plural_1 (en)', $lang->plural('singular', 'plural', 0));
676    }
677
678    /**
679     * @group ZF-9489
680     */
681    public function testAddingAdapterToSourceUsingOwnRule()
682    {
683        $translate = new Zend_Translate(
684            Zend_Translate::AN_ARRAY,
685            array('singular' =>
686                array('plural_0 (en)',
687                    'plural_1 (en)',
688                    'plural_2 (en)',
689                    'plural_3 (en)'),
690                'plural' => ''), 'en'
691        );
692
693        $this->assertFalse($translate->isTranslated('Message 1'));
694        $adapter = new Zend_Translate_Adapter_Gettext(dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.mo', 'en');
695        $translate->addTranslation($adapter);
696
697        $this->assertTrue($adapter->isTranslated('Message 1'));
698
699        $adapter2 = new Zend_Translate_Adapter_Gettext(dirname(__FILE__) . '/Translate/Adapter/_files/testmo/de_AT/LC_TEST/translation-de_DE.mo', 'de_AT');
700        $adapter2->addTranslation(dirname(__FILE__) . '/Translate/Adapter/_files/translation_en2.mo', 'fr');
701        $translate->addTranslation($adapter2, 'fr');
702
703        $languages = $translate->getList();
704        $this->assertFalse(array_key_exists('de_AT', $languages));
705        $this->assertTrue(array_key_exists('fr', $languages));
706    }
707
708    /**
709     * @group ZF-9500
710     */
711    public function testIgnoreMultipleDirectories()
712    {
713        $translate = new Zend_Translate(
714            Zend_Translate::AN_ARRAY,
715            dirname(__FILE__) . '/Translate/Adapter/_files/testarray/',
716            'auto',
717            array(
718                'scan' => Zend_Translate::LOCALE_FILENAME,
719                'ignore' => array('.', 'ignoreme', 'LC_TEST')
720            )
721        );
722
723        $langs = $translate->getList();
724        $this->assertFalse(array_key_exists('de_DE', $langs));
725        $this->assertTrue(array_key_exists('ja', $langs));
726        $this->assertTrue(array_key_exists('en_US', $langs));
727
728        $translate2 = new Zend_Translate(
729            Zend_Translate::AN_ARRAY,
730            dirname(__FILE__) . '/Translate/Adapter/_files/testarray/',
731            'auto',
732            array(
733                'scan' => Zend_Translate::LOCALE_FILENAME,
734                'ignore' => array('.', 'regex_1' => '/de_DE/', 'regex' => '/ja/')
735            )
736        );
737
738        $langs = $translate2->getList();
739        $this->assertFalse(array_key_exists('de_DE', $langs));
740        $this->assertFalse(array_key_exists('ja', $langs));
741        $this->assertTrue(array_key_exists('en_US', $langs));
742    }
743
744    /**
745     * @group ZF-2736
746     */
747    public function testReroutingForTranslations()
748    {
749        $translate = new Zend_Translate(
750            array(
751                'adapter' => Zend_Translate::AN_ARRAY,
752                'content' => dirname(__FILE__) . '/Translate/Adapter/_files/testarray/',
753                'locale'  => 'auto',
754                'scan'    => Zend_Translate::LOCALE_FILENAME,
755                'ignore'  => array('.', 'ignoreme', 'LC_OTHER'),
756                'route'   => array('ja' => 'en_US'),
757            )
758        );
759
760        $translate2 = new Zend_Translate(
761            array(
762                'adapter' => Zend_Translate::AN_CSV,
763                'content' => dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.csv',
764                'locale'  => 'en_US',
765            )
766        );
767
768        $translate->addTranslation($translate2);
769        $langs = $translate->getList();
770        $this->assertFalse(array_key_exists('de_AT', $langs));
771        $this->assertTrue(array_key_exists('ja', $langs));
772        $this->assertTrue(array_key_exists('en_US', $langs));
773        $this->assertEquals('Message 5 (en)', $translate->translate('Message 5', 'ja'));
774    }
775
776    /**
777     * @group ZF-2736
778     */
779    public function testCircleReroutingForTranslations()
780    {
781        $translate = new Zend_Translate(
782            array(
783                'adapter' => Zend_Translate::AN_ARRAY,
784                'content' => dirname(__FILE__) . '/Translate/Adapter/_files/testarray/',
785                'locale'  => 'auto',
786                'scan'    => Zend_Translate::LOCALE_FILENAME,
787                'ignore'  => array('.', 'ignoreme', 'LC_TEST'),
788                'route'   => array('ja' => 'en_US', 'en_US' => 'ja'),
789            )
790        );
791
792        $translate2 = new Zend_Translate(
793            array(
794                'adapter' => Zend_Translate::AN_CSV,
795                'content' => dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.csv',
796                'locale'  => 'en_US',
797            )
798        );
799
800        $translate->addTranslation($translate2);
801
802        $langs = $translate->getList();
803        $this->assertFalse(array_key_exists('de_DE', $langs));
804        $this->assertTrue(array_key_exists('ja', $langs));
805        $this->assertTrue(array_key_exists('en_US', $langs));
806        $this->assertEquals('Message 5 (en)', $translate->translate('Message 5', 'ja'));
807        $this->assertEquals('Message 10', $translate->translate('Message 10', 'ja'));
808    }
809
810    /**
811     * @group ZF-2736
812     */
813    public function testDoubleReroutingForTranslations()
814    {
815        $translate = new Zend_Translate(
816            array(
817                'adapter' => Zend_Translate::AN_ARRAY,
818                'content' => dirname(__FILE__) . '/Translate/Adapter/_files/testarray/',
819                'locale'  => 'auto',
820                'scan'    => Zend_Translate::LOCALE_FILENAME,
821                'ignore'  => array('.', 'ignoreme', 'LC_TEST'),
822                'route'   => array('ja' => 'en_US', 'en_US' => 'ja'),
823            )
824        );
825
826        $translate2 = new Zend_Translate(
827            array(
828                'adapter' => Zend_Translate::AN_CSV,
829                'content' => dirname(__FILE__) . '/Translate/Adapter/_files/translation_en.csv',
830                'locale'  => 'en_US',
831            )
832        );
833
834        $translate->addTranslation($translate2);
835
836        $langs = $translate->getList();
837        $this->assertFalse(array_key_exists('de_DE', $langs));
838        $this->assertTrue(array_key_exists('ja', $langs));
839        $this->assertTrue(array_key_exists('en_US', $langs));
840        $this->assertEquals('Message 5 (en)', $translate->translate('Message 5', 'ja'));
841        $this->assertEquals('Message 5 (en)', $translate->translate('Message 5', 'ja'));
842    }
843
844    /**
845     * ZF-9877
846     */
847    public function testSetCacheThroughOptions()
848    {
849        require_once 'Zend/Cache.php';
850        $cache = Zend_Cache::factory('Core', 'File',
851            array('lifetime' => 120, 'automatic_serialization' => true),
852            array('cache_dir' => dirname(__FILE__) . '/_files/'));
853
854        $translate = new Zend_Translate(array(
855            'adapter' => Zend_Translate::AN_ARRAY,
856            'content' => array('msg1' => 'Message 1 (en)'),
857            'locale'  => 'en',
858            'cache'   => $cache,
859        ));
860
861        $return = Zend_Translate::getCache();
862        $this->assertTrue($return instanceof Zend_Cache_Core);
863        $this->assertTrue(Zend_Translate::hasCache());
864    }
865
866    /**
867     * @ZF-10051
868     */
869    public function testSettingLogPriorityForLog()
870    {
871        $stream = fopen('php://memory', 'w+');
872        require_once 'Zend/Log/Writer/Stream.php';
873        $writer = new Zend_Log_Writer_Stream($stream);
874        require_once 'Zend/Log.php';
875        $log    = new Zend_Log($writer);
876
877        $lang = new Zend_Translate(array(
878            'adapter'     => Zend_Translate::AN_CSV,
879            'content'     => dirname(__FILE__) . '/Translate/Adapter/_files',
880            'locale'      => 'en',
881            'delimiter'   => ',',
882            'logPriority' => 3,
883            'log'         => $log)
884        );
885
886        $lang->setLocale('ru');
887
888        rewind($stream);
889        $this->assertContains('ERR (3)', stream_get_contents($stream));
890
891        $lang->setOptions(array('logPriority' => 1));
892        $lang->setLocale('sv');
893
894        rewind($stream);
895        $this->assertContains('ALERT (1)', stream_get_contents($stream));
896    }
897
898    /**
899     * Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
900     *
901     * @param  integer $errno
902     * @param  string  $errstr
903     * @param  string  $errfile
904     * @param  integer $errline
905     * @param  array   $errcontext
906     * @return void
907     */
908    public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $errcontext)
909    {
910        $this->_errorOccured = true;
911    }
912
913    /**
914     * Custom callback for testGettingPluralsUsingOwnRule
915     *
916     * @param  integer $number
917     * @return integer
918     */
919    public function customPlural($number) {
920        return 1;
921    }
922}
923
924// Call Zend_TranslateTest::main() if this source file is executed directly.
925if (PHPUnit_MAIN_METHOD == "Zend_TranslateTest::main") {
926    Zend_TranslateTest::main();
927}