diff --git a/core/modules/locale/tests/Drupal/locale/Tests/LocaleStringFallbackTest.php b/core/modules/locale/tests/Drupal/locale/Tests/LocaleStringFallbackTest.php index 65a139d..3ebc4d3 100644 --- a/core/modules/locale/tests/Drupal/locale/Tests/LocaleStringFallbackTest.php +++ b/core/modules/locale/tests/Drupal/locale/Tests/LocaleStringFallbackTest.php @@ -30,24 +30,29 @@ class LocaleStringFallbackTest extends UnitTestCase { /** * Instance of LocaleTranslation class. + * * @var \Drupal\locale\LocaleTranslation */ protected $translation; /** - * A mocked user object built from AccountInterface + * A mocked user object built from AccountInterface. + * * @var \PHPUnit_Framework_MockObject_MockObject */ protected $user; /** - * A mocked container object built from ContainerInterface + * A mocked container object built from ContainerInterface. + * * @var \PHPUnit_Framework_MockObject_MockObject */ protected $container; /** - * A mocked config facktory built with UnitTestCase::getConfigFactoryStub method + * A mocked config facktory built with UnitTestCase::getConfigFactoryStub + * method. + * * @var \PHPUnit_Framework_MockObject_MockBuilder */ protected $config_factory; @@ -82,24 +87,28 @@ protected function setUp() { ->method('getRoles') ->will($this->returnValue(array('anonymous'))); - $this->config_factory = $this->getConfigFactoryStub(array('locale.settings' => array('translate_english' => true, 'cache_strings' => false))); + $this->config_factory = $this->getConfigFactoryStub(array('locale.settings' => array('translate_english' => TRUE, 'cache_strings' => FALSE))); $this->language_manager = $this->getMock('Drupal\Core\Language\LanguageManagerInterface'); $this->container = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); - list($user, $config_factory, $language_manager) = array($this->user, $this->config_factory,$this->language_manager); - $this->container->expects($this->any()) - ->method('get') - ->will($this->returnCallback(function ($arg) use ($user, $config_factory, $language_manager) { - $map = array( - 'current_user' => $user, - 'config.factory' => $config_factory, - 'language_manager' => $language_manager - ); + list($user, $config_factory, $language_manager) = array( + $this->user, + $this->config_factory, + $this->language_manager, + ); - return $map[$arg]; - })); + $this->container->expects($this->any()) + ->method('get') + ->will($this->returnCallback(function ($arg) use ($user, $config_factory, $language_manager) { + $map = array( + 'current_user' => $user, + 'config.factory' => $config_factory, + 'language_manager' => $language_manager, + ); + return $map[$arg]; + })); \Drupal::setContainer($this->container); @@ -109,16 +118,16 @@ protected function setUp() { /** * Test basic string translation without fallback. */ - public function testStringTranslationWithoutFallback(){ + public function testStringTranslationWithoutFallback() { $args = array( 'language' => 'en', 'source' => 'test', - 'context' => 'irrelevant' + 'context' => 'irrelevant', ); - $result = (object)array( - 'translation' => 'test' + $result = (object) array( + 'translation' => 'test', ); $this->storage->expects($this->any()) @@ -126,17 +135,18 @@ public function testStringTranslationWithoutFallback(){ ->with($this->equalTo($args)) ->will($this->returnValue($result)); - $this->assertEquals((object)array('translation' => 'test'), $this->storage->findTranslation($args)); + $this->assertEquals((object) array('translation' => 'test'), $this->storage->findTranslation($args)); $t = $this->translation->getStringTranslation('en', 'test', 'irrelevant'); $this->assertEquals('test', $t); } /** * Test string translation with fallback. + * * Note that context is irrelevant here. * It is not used but it is required. */ - public function testStringTranslationWithFallback(){ + public function testStringTranslationWithFallback() { $languages = array('en', 'pl', 'cs'); @@ -144,8 +154,8 @@ public function testStringTranslationWithFallback(){ ->method('findTranslation') ->will($this->returnCallback('Drupal\locale\Tests\LocaleStringFallbackTest::stringStorageMockFindTranslation')); - // Dry run test with untranslated strings - foreach($languages as $lang) { + // Dry run test with untranslated strings. + foreach ($languages as $lang) { $this->assertEquals(FALSE, $this->translation->getStringTranslation($lang, 'not translated', 'irrelevant')); } @@ -153,14 +163,14 @@ public function testStringTranslationWithFallback(){ ->method('getFallbackCandidates') ->will($this->returnCallback('Drupal\locale\Tests\LocaleStringFallbackTest::getFallbackChain')); - // Testing CS with fallback to en + // Testing CS with fallback to en. $this->assertEquals('test v české', $this->translation->getStringTranslation('cs', 'test', 'irrelevant')); $this->assertEquals('falešný', $this->translation->getStringTranslation('cs', 'fake', 'irrelevant')); $this->assertEquals('chybějící pl', $this->translation->getStringTranslation('cs', 'missing pl', 'irrelevant')); $this->assertEquals('missing cs', $this->translation->getStringTranslation('cs', 'missing cs', 'irrelevant')); $this->assertEquals('missing both', $this->translation->getStringTranslation('cs', 'missing both', 'irrelevant')); - // Testing PL with fallback to cs, en + // Testing PL with fallback to cs, en. $this->assertEquals('test po polsku', $this->translation->getStringTranslation('pl', 'test', 'irrelevant')); $this->assertEquals('ściema', $this->translation->getStringTranslation('pl', 'fake', 'irrelevant')); $this->assertEquals('chybějící pl', $this->translation->getStringTranslation('pl', 'missing pl', 'irrelevant')); @@ -169,19 +179,20 @@ public function testStringTranslationWithFallback(){ } /** - * Helper method that returns different fallback chains - * depending on requested language. + * Helper method. + * + * Returns different fallback chains depending on requested language. * * @param string $arg - * Language code + * Language code * * @return array - * Array of language codes + * Array of language codes */ public static function getFallbackChain($arg) { $chains = array( 'pl' => array('cs', 'en'), - 'cs' => array('en') + 'cs' => array('en'), ); return $chains[$arg]; @@ -192,22 +203,38 @@ public static function getFallbackChain($arg) { * Helper function that mimics real StringStorage::findTranslation method. * * @param array $arg - * Array containing three keys: language, source and context. - * The third key is ignored for purpouse of this test. + * Array containing three keys: language, source and context. + * The third key is ignored for purpouse of this test. * * @return StdClass|boolean - * An object containing one property 'translation' if translations is found; otherwise TRUE. + * An object containing one property 'translation' if translations is found; + * otherwise TRUE. */ public static function stringStorageMockFindTranslation($arg) { // These are fake words! $translations = array( - 'en' => array('test' => 'test', 'fake' => 'fake', 'missing pl' => 'missing pl', 'missing cs' => 'missing cs', 'missing both' => 'missing both'), - 'pl' => array('test' => 'test po polsku', 'fake' => 'ściema', 'missing cs' => 'zaginiony czech'), - 'cs' => array('test' => 'test v české', 'fake' => 'falešný', 'missing pl' => 'chybějící pl'), + 'en' => array( + 'test' => 'test', + 'fake' => 'fake', + 'missing pl' => 'missing pl', + 'missing cs' => 'missing cs', + 'missing both' => 'missing both', + ), + 'pl' => array( + 'test' => 'test po polsku', + 'fake' => 'ściema', + 'missing cs' => 'zaginiony czech', + ), + 'cs' => array( + 'test' => 'test v české', + 'fake' => 'falešný', + 'missing pl' => 'chybějící pl', + ), ); - if (isset($translations[$arg['language']][$arg['source']])) + if (isset($translations[$arg['language']][$arg['source']])) { return (object) array('translation' => $translations[$arg['language']][$arg['source']]); + } return TRUE; }