![Markus Warg](/assets/img/avatar_default.png)
code was modified slightly, so the code differs from the original downloadable 1.9.5 version
234 lines
9.7 KiB
PHP
234 lines
9.7 KiB
PHP
<?php
|
|
/**
|
|
* Zend Framework
|
|
*
|
|
* LICENSE
|
|
*
|
|
* This source file is subject to the new BSD license that is bundled
|
|
* with this package in the file LICENSE.txt.
|
|
* It is also available through the world-wide-web at this URL:
|
|
* http://framework.zend.com/license/new-bsd
|
|
* If you did not receive a copy of the license and are unable to
|
|
* obtain it through the world-wide-web, please send an email
|
|
* to license@zend.com so we can send you a copy immediately.
|
|
*
|
|
* @category Zend
|
|
* @package Zend_Translate
|
|
* @subpackage UnitTests
|
|
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
|
* @version $Id: XliffTest.php 17363 2009-08-03 07:40:18Z bkarwin $
|
|
*/
|
|
|
|
/**
|
|
* Zend_Translate_Adapter_Xliff
|
|
*/
|
|
require_once 'Zend/Translate/Adapter/Xliff.php';
|
|
|
|
/**
|
|
* PHPUnit test case
|
|
*/
|
|
require_once 'PHPUnit/Framework/TestCase.php';
|
|
|
|
/**
|
|
* @category Zend
|
|
* @package Zend_Translate
|
|
* @subpackage UnitTests
|
|
* @copyright Copyright (c) 2005-2009 Zend Technologies USA Inc. (http://www.zend.com)
|
|
* @license http://framework.zend.com/license/new-bsd New BSD License
|
|
* @group Zend_Translate
|
|
*/
|
|
class Zend_Translate_Adapter_XliffTest extends PHPUnit_Framework_TestCase
|
|
{
|
|
/**
|
|
* Runs the test methods of this class.
|
|
*
|
|
* @return void
|
|
*/
|
|
public static function main()
|
|
{
|
|
$suite = new PHPUnit_Framework_TestSuite("Zend_Translate_Adapter_XliffTest");
|
|
$result = PHPUnit_TextUI_TestRunner::run($suite);
|
|
}
|
|
|
|
public function testCreate()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'en');
|
|
$this->assertTrue($adapter instanceof Zend_Translate_Adapter_Xliff);
|
|
|
|
try {
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/nofile.xliff', 'en');
|
|
$this->fail("exception expected");
|
|
} catch (Zend_Translate_Exception $e) {
|
|
$this->assertContains('is not readable', $e->getMessage());
|
|
}
|
|
|
|
try {
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/failed.xliff', 'en');
|
|
$this->fail("exception expected");
|
|
} catch (Zend_Translate_Exception $e) {
|
|
$this->assertContains('Mismatched tag at line', $e->getMessage());
|
|
}
|
|
}
|
|
|
|
public function testToString()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'en');
|
|
$this->assertEquals('Xliff', $adapter->toString());
|
|
}
|
|
|
|
public function testTranslate()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'fr');
|
|
$this->assertEquals('Message 1 (en)', $adapter->translate('Message 1'));
|
|
$this->assertEquals('Message 1 (en)', $adapter->_('Message 1'));
|
|
$this->assertEquals('Message 6', $adapter->translate('Message 6'));
|
|
$this->assertEquals('Küchen Möbel (en)', $adapter->translate('Cooking furniture'));
|
|
$this->assertEquals('Cooking furniture (en)', $adapter->translate('Küchen Möbel'));
|
|
}
|
|
|
|
public function testIsTranslated()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'en');
|
|
$this->assertTrue($adapter->isTranslated('Message 1'));
|
|
$this->assertFalse($adapter->isTranslated('Message 6'));
|
|
$this->assertTrue($adapter->isTranslated('Message 1', true));
|
|
$this->assertTrue($adapter->isTranslated('Message 1', true, 'en'));
|
|
$this->assertFalse($adapter->isTranslated('Message 1', false, 'es'));
|
|
}
|
|
|
|
public function testLoadTranslationData()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'fr');
|
|
$this->assertEquals('Message 1 (en)', $adapter->translate('Message 1'));
|
|
$this->assertEquals('Message 4 (en)', $adapter->translate('Message 4'));
|
|
$this->assertEquals('Message 2', $adapter->translate('Message 2', 'ru'));
|
|
$this->assertEquals('Message 1', $adapter->translate('Message 1', 'xx'));
|
|
$this->assertEquals('Message 1 (en)', $adapter->translate('Message 1', 'fr_FR'));
|
|
|
|
try {
|
|
$adapter->addTranslation(dirname(__FILE__) . '/_files/translation_en.xliff', 'xx');
|
|
$this->fail("exception expected");
|
|
} catch (Zend_Translate_Exception $e) {
|
|
$this->assertContains('does not exist', $e->getMessage());
|
|
}
|
|
|
|
$adapter->addTranslation(dirname(__FILE__) . '/_files/translation_en2.xliff', 'de', array('clear' => true));
|
|
$this->assertEquals('Nachricht 1', $adapter->translate('Message 1'));
|
|
$this->assertEquals('Message 4', $adapter->translate('Message 4'));
|
|
}
|
|
|
|
public function testOptions()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'en');
|
|
$adapter->setOptions(array('testoption' => 'testkey'));
|
|
$this->assertEquals(
|
|
array(
|
|
'testoption' => 'testkey',
|
|
'clear' => false,
|
|
'scan' => null,
|
|
'locale' => 'en',
|
|
'ignore' => '.',
|
|
'disableNotices' => false,
|
|
'log' => false,
|
|
'logMessage' => 'Untranslated message within \'%locale%\': %message%',
|
|
'logUntranslated' => false),
|
|
$adapter->getOptions());
|
|
$this->assertEquals('testkey', $adapter->getOptions('testoption'));
|
|
$this->assertTrue(is_null($adapter->getOptions('nooption')));
|
|
}
|
|
|
|
public function testClearing()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'fr');
|
|
$this->assertEquals('Message 1 (en)', $adapter->translate('Message 1'));
|
|
$this->assertEquals('Message 6', $adapter->translate('Message 6'));
|
|
$adapter->addTranslation(dirname(__FILE__) . '/_files/translation_en2.xliff', 'de', array('clear' => true));
|
|
$this->assertEquals('Nachricht 1', $adapter->translate('Message 1'));
|
|
$this->assertEquals('Message 5', $adapter->translate('Message 5'));
|
|
}
|
|
|
|
public function testLocale()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'en');
|
|
$this->assertEquals('en', $adapter->getLocale());
|
|
$locale = new Zend_Locale('en');
|
|
$adapter->setLocale($locale);
|
|
$this->assertEquals('en', $adapter->getLocale());
|
|
|
|
try {
|
|
$adapter->setLocale('nolocale');
|
|
$this->fail("exception expected");
|
|
} catch (Zend_Translate_Exception $e) {
|
|
$this->assertContains('does not exist', $e->getMessage());
|
|
}
|
|
|
|
set_error_handler(array($this, 'errorHandlerIgnore'));
|
|
$adapter->setLocale('it');
|
|
restore_error_handler();
|
|
$this->assertEquals('it', $adapter->getLocale());
|
|
}
|
|
|
|
public function testList()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en.xliff', 'en');
|
|
$this->assertEquals(array('en' => 'en', 'fr' => 'fr'), $adapter->getList());
|
|
$adapter->addTranslation(dirname(__FILE__) . '/_files/translation_en2.xliff', 'de');
|
|
$this->assertEquals(array('en' => 'en', 'de' => 'de', 'fr' => 'fr'), $adapter->getList());
|
|
$this->assertTrue($adapter->isAvailable('de'));
|
|
$locale = new Zend_Locale('en');
|
|
$this->assertTrue($adapter->isAvailable($locale));
|
|
$this->assertFalse($adapter->isAvailable('sr' ));
|
|
}
|
|
|
|
public function testOptionLocaleDirectory()
|
|
{
|
|
require_once 'Zend/Translate.php';
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/testxliff', 'de', array('scan' => Zend_Translate::LOCALE_DIRECTORY));
|
|
$this->assertEquals(array('de' => 'de', 'en' => 'en', 'fr' => 'fr'), $adapter->getList());
|
|
$this->assertEquals('Nachricht 1', $adapter->translate('Message 1'));
|
|
}
|
|
|
|
public function testOptionLocaleFilename()
|
|
{
|
|
require_once 'Zend/Translate.php';
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/testxliff', 'de', array('scan' => Zend_Translate::LOCALE_FILENAME));
|
|
$this->assertEquals(array('de' => 'de', 'en' => 'en', 'fr' => 'fr'), $adapter->getList());
|
|
$this->assertEquals('Nachricht 1', $adapter->translate('Message 1'));
|
|
}
|
|
|
|
public function testIsoEncoding()
|
|
{
|
|
$adapter = new Zend_Translate_Adapter_Xliff(dirname(__FILE__) . '/_files/translation_en3.xliff', 'en');
|
|
$this->assertEquals('Message 1 (en)', $adapter->translate('Message 1'));
|
|
$this->assertEquals('Message 1 (en)', $adapter->_('Message 1'));
|
|
|
|
if (PHP_OS == 'AIX') {
|
|
return;
|
|
// 'Charsets below are not supported on AIX';
|
|
}
|
|
|
|
$this->assertEquals(iconv('UTF-8', 'ISO-8859-1', 'Küchen Möbel (en)'), $adapter->translate('Cooking furniture'));
|
|
$this->assertEquals('Cooking furniture (en)', $adapter->translate(iconv('UTF-8', 'ISO-8859-1', 'Küchen Möbel')));
|
|
}
|
|
|
|
/**
|
|
* Ignores a raised PHP error when in effect, but throws a flag to indicate an error occurred
|
|
*
|
|
* @param integer $errno
|
|
* @param string $errstr
|
|
* @param string $errfile
|
|
* @param integer $errline
|
|
* @param array $errcontext
|
|
* @return void
|
|
*/
|
|
public function errorHandlerIgnore($errno, $errstr, $errfile, $errline, array $errcontext)
|
|
{
|
|
$this->_errorOccurred = true;
|
|
}
|
|
}
|
|
|
|
// Call Zend_Translate_Adapter_XliffTest::main() if this source file is executed directly.
|
|
if (PHPUnit_MAIN_METHOD == "Zend_Translate_Adapter_XliffTest::main") {
|
|
Zend_Translate_Adapter_XliffTest::main();
|
|
}
|