Zend_ValidateTest.php 7.0 KB

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