/tests/resources/languages/Zend_ValidateTest.php

https://github.com/Shreef/zf2 · PHP · 207 lines · 154 code · 13 blank · 40 comment · 13 complexity · 62bde48ab994bd67168f3a0615d2e79b 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_Exception
  17. * @subpackage UnitTests
  18. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  19. * @license http://framework.zend.com/license/new-bsd New BSD License
  20. */
  21. /**
  22. * @category Zend
  23. * @package Zend_resources
  24. * @subpackage UnitTests
  25. * @group resources
  26. * @copyright Copyright (c) 2005-2010 Zend Technologies USA Inc. (http://www.zend.com)
  27. * @license http://framework.zend.com/license/new-bsd New BSD License
  28. */
  29. class resources_languages_Zend_ValidateTest extends PHPUnit_Framework_TestCase
  30. {
  31. protected $_langDir = null;
  32. protected $_languages = array();
  33. protected $_translations = array();
  34. public function setUp()
  35. {
  36. $this->_langDir = dirname(dirname(dirname(__DIR__)))
  37. . DIRECTORY_SEPARATOR . 'resources' . DIRECTORY_SEPARATOR . 'languages';
  38. if (!is_readable($this->_langDir)) {
  39. throw new Exception('Language resource directory "'.$this->_langDir.'" not readable.');
  40. }
  41. // detect languages
  42. foreach (new DirectoryIterator($this->_langDir) as $entry) {
  43. if (!$entry->isDir()) {
  44. continue;
  45. }
  46. // skip "." or ".." or ".svn"
  47. $fname = $entry->getFilename();
  48. if ($fname[0] == '.') {
  49. continue;
  50. }
  51. // add all languages for testIsLocale
  52. $this->_languages[] = $fname;
  53. // include Zend_Validate translation tables
  54. $translationFile = $entry->getPathname() . DIRECTORY_SEPARATOR . 'Zend_Validate.php';
  55. if (file_exists($translationFile)) {
  56. $translation = include $translationFile;
  57. if (!is_array($translation)) {
  58. $this->fail("Invalid or empty translation table found for language '{$fname}'");
  59. }
  60. $this->_translations[$fname] = $translation;
  61. }
  62. }
  63. }
  64. /**
  65. * Tests if the given language is really a language
  66. */
  67. public function testIsLocale()
  68. {
  69. foreach ($this->_languages as $lang) {
  70. if (!Zend_Locale::isLocale($lang, true, false)) {
  71. $this->fail("Language directory '{$lang}' not a valid locale");
  72. }
  73. }
  74. }
  75. /**
  76. * Tests if all english original keys have the same translations
  77. */
  78. public function testEnglishKeySameAsValue()
  79. {
  80. $errors = array();
  81. $cnt = 0;
  82. foreach ($this->_translations['en'] as $key => $value) {
  83. if ($key !== $value) {
  84. ++$cnt;
  85. $errors['en ' . $cnt] = "The key $key is not identical in the english original";
  86. }
  87. }
  88. if (!empty($errors)) {
  89. $this->fail(var_export($errors, true));
  90. }
  91. }
  92. /**
  93. * Tests if all translation keys are also available in the english original
  94. */
  95. public function testTranslationAvailableInEnglish()
  96. {
  97. $errors = array();
  98. $cnt = 0;
  99. foreach ($this->_translations as $lang => $translation) {
  100. if ($lang == 'en') {
  101. continue;
  102. }
  103. foreach ($translation as $key => $value) {
  104. if (!isset($this->_translations['en'][$key])) {
  105. ++$cnt;
  106. $errors[$lang . ' ' . $cnt] = "The key \"" . $key . "\" isn't available within english translation file";
  107. }
  108. }
  109. }
  110. if (!empty($errors)) {
  111. $this->fail(var_export($errors, true));
  112. }
  113. }
  114. /**
  115. * Tests if the key is translated
  116. */
  117. public function testTranslationDiffersFromEnglish()
  118. {
  119. $errors = array();
  120. $cnt = 0;
  121. foreach ($this->_translations as $lang => $translation) {
  122. if ($lang == 'en') {
  123. continue;
  124. }
  125. foreach ($translation as $key => $value) {
  126. if ($key == $value) {
  127. ++$cnt;
  128. $errors[$lang . ' ' . $cnt] = "The translated message \"" . $value . "\" is the same the english version";
  129. }
  130. }
  131. }
  132. if (!empty($errors)) {
  133. $this->fail(var_export($errors, true));
  134. }
  135. }
  136. /**
  137. * Tests if all placeholders from the original are also available within the translation
  138. */
  139. public function testPlaceholder()
  140. {
  141. $errors = array();
  142. $cnt = 0;
  143. foreach ($this->_translations as $lang => $translation) {
  144. if ($lang == 'en') { // not needed to test - see testEnglishKeySameAsValue
  145. continue;
  146. }
  147. foreach ($translation as $key => $value) {
  148. if (preg_match_all('/(\%.+\%)/U', $key, $matches, PREG_SET_ORDER)) {
  149. foreach ($matches as $match) {
  150. if (!strpos($value, $match[1])) {
  151. ++$cnt;
  152. $errors[$lang . ' ' . $cnt] = "Missing placeholder \"" . $match[1] . "\" within \"" . $value . "\"";
  153. }
  154. }
  155. }
  156. }
  157. }
  158. if (!empty($errors)) {
  159. $this->fail(var_export($errors, true));
  160. }
  161. }
  162. /**
  163. * Tests if all english originals are translated
  164. */
  165. public function testAllTranslated()
  166. {
  167. $errors = array();
  168. $cnt = 0;
  169. foreach ($this->_translations as $lang => $translation) {
  170. foreach ($this->_translations['en'] as $key => $value) {
  171. if ($lang == 'en') {
  172. continue;
  173. }
  174. if (!isset($translation[$key])) {
  175. ++$cnt;
  176. $errors[$lang . ' ' . $cnt] = "Message \"" . $key . "\" not translated";
  177. }
  178. }
  179. }
  180. if (!empty($errors)) {
  181. $this->fail(var_export($errors, true));
  182. }
  183. }
  184. }