Zend_ValidateTest.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. // detect languages
  48. foreach (new DirectoryIterator($this->_langDir) as $entry) {
  49. if (!$entry->isDir()) {
  50. continue;
  51. }
  52. // skip "." or ".." or ".svn"
  53. $fname = $entry->getFilename();
  54. if ($fname[0] == '.') {
  55. continue;
  56. }
  57. // add all languages for testIsLocale
  58. $this->_languages[] = $fname;
  59. // include Zend_Validate translation tables
  60. $translationFile = $entry->getPathname() . DIRECTORY_SEPARATOR . 'Zend_Validate.php';
  61. if (file_exists($translationFile)) {
  62. $translation = include $translationFile;
  63. if (!is_array($translation)) {
  64. $this->fail("Invalid or empty translation table found for language '{$fname}'");
  65. }
  66. $this->_translations[$fname] = $translation;
  67. }
  68. }
  69. }
  70. /**
  71. * Tests if the given language is really a language
  72. */
  73. public function testIsLocale()
  74. {
  75. foreach ($this->_languages as $lang) {
  76. if (!Zend_Locale::isLocale($lang, true, false)) {
  77. $this->fail("Language directory '{$lang}' not a valid locale");
  78. }
  79. }
  80. }
  81. /**
  82. * Tests if all english original keys have the same translations
  83. */
  84. public function testEnglishKeySameAsValue()
  85. {
  86. $errors = array();
  87. $cnt = 0;
  88. foreach ($this->_translations['en'] as $key => $value) {
  89. if ($key !== $value) {
  90. ++$cnt;
  91. $errors['en ' . $cnt] = "The key $key is not identical in the english original";
  92. }
  93. }
  94. if (!empty($errors)) {
  95. $this->fail(var_export($errors, true));
  96. }
  97. }
  98. /**
  99. * Tests if all translation keys are also available in the english original
  100. */
  101. public function testTranslationAvailableInEnglish()
  102. {
  103. $errors = array();
  104. $cnt = 0;
  105. foreach ($this->_translations as $lang => $translation) {
  106. if ($lang == 'en') {
  107. continue;
  108. }
  109. foreach ($translation as $key => $value) {
  110. if (!isset($this->_translations['en'][$key])) {
  111. ++$cnt;
  112. $errors[$lang . ' ' . $cnt] = "The key \"" . $key . "\" isn't available within english translation file";
  113. }
  114. }
  115. }
  116. if (!empty($errors)) {
  117. $this->fail(var_export($errors, true));
  118. }
  119. }
  120. /**
  121. * Tests if the key is translated
  122. */
  123. public function testTranslationDiffersFromEnglish()
  124. {
  125. $errors = array();
  126. $cnt = 0;
  127. foreach ($this->_translations as $lang => $translation) {
  128. if ($lang == 'en') {
  129. continue;
  130. }
  131. foreach ($translation as $key => $value) {
  132. if ($key == $value) {
  133. ++$cnt;
  134. $errors[$lang . ' ' . $cnt] = "The translated message \"" . $value . "\" is the same the english version";
  135. }
  136. }
  137. }
  138. if (!empty($errors)) {
  139. $this->fail(var_export($errors, true));
  140. }
  141. }
  142. /**
  143. * Tests if all placeholders from the original are also available within the translation
  144. */
  145. public function testPlaceholder()
  146. {
  147. $errors = array();
  148. $cnt = 0;
  149. foreach ($this->_translations as $lang => $translation) {
  150. if ($lang == 'en') { // not needed to test - see testEnglishKeySameAsValue
  151. continue;
  152. }
  153. foreach ($translation as $key => $value) {
  154. if (preg_match_all('/(\%.+\%)/U', $key, $matches, PREG_SET_ORDER)) {
  155. foreach ($matches as $match) {
  156. if (!strpos($value, $match[1])) {
  157. ++$cnt;
  158. $errors[$lang . ' ' . $cnt] = "Missing placeholder \"" . $match[1] . "\" within \"" . $value . "\"";
  159. }
  160. }
  161. }
  162. }
  163. }
  164. if (!empty($errors)) {
  165. $this->fail(var_export($errors, true));
  166. }
  167. }
  168. /**
  169. * Tests if all english originals are translated
  170. */
  171. public function testAllTranslated()
  172. {
  173. $errors = array();
  174. $cnt = 0;
  175. foreach ($this->_translations['en'] as $key => $value) {
  176. foreach ($this->_translations as $lang => $translation) {
  177. if ($lang == 'en') {
  178. continue;
  179. }
  180. if (!isset($translation[$key])) {
  181. ++$cnt;
  182. $errors[$lang . ' ' . $cnt] = "Message \"" . $key . "\" not translated";
  183. }
  184. }
  185. }
  186. if (!empty($errors)) {
  187. $this->fail(var_export($errors, true));
  188. }
  189. }
  190. }