Zend_ValidateTest.php 7.0 KB

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