Zend_ValidateTest.php 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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. public function testIsLocale()
  71. {
  72. foreach ($this->_languages as $lang) {
  73. if (!Zend_Locale::isLocale($lang, true, false)) {
  74. $this->fail("Language directory '{$lang}' not a valid locale");
  75. }
  76. }
  77. }
  78. public function testEnglishKeySameAsValue()
  79. {
  80. foreach ($this->_translations['en'] as $k => $v) {
  81. $this->assertEquals($k, $v);
  82. }
  83. }
  84. public function testTranslationAvailableInEnglish()
  85. {
  86. foreach ($this->_translations as $lang => $translation) {
  87. if ($lang == 'en') {
  88. continue;
  89. }
  90. foreach ($translation as $k => $v) {
  91. $this->assertTrue(
  92. isset($this->_translations['en'][$k]),
  93. $lang . ': The key "' . $k . '" isn\'t available within english translation file'
  94. );
  95. }
  96. }
  97. }
  98. public function testTranslationDiffersFromEnglish()
  99. {
  100. foreach ($this->_translations as $lang => $translation) {
  101. if ($lang == 'en') {
  102. continue;
  103. }
  104. foreach ($translation as $k => $v) {
  105. $this->assertTrue( ($k != $v),
  106. $lang . ': The translated message "' . $v . '" is the same the english version'
  107. );
  108. }
  109. }
  110. }
  111. public function testPlaceholder()
  112. {
  113. foreach ($this->_translations as $lang => $translation) {
  114. if ($lang == 'en') { // not needed to test - see testEnglishKeySameAsValue
  115. continue;
  116. }
  117. foreach ($translation as $k => $v) {
  118. if (preg_match_all('/(\%.+\%)/U', $k, $matches, PREG_SET_ORDER)) {
  119. foreach ($matches as $match) {
  120. $this->assertContains($match[1], $v,
  121. $lang . ': Missing placeholder "' . $match[1] . '" within "' . $v . '"');
  122. }
  123. }
  124. }
  125. }
  126. }
  127. public function testAllTranslated()
  128. {
  129. foreach ($this->_translations['en'] as $enK => $enV) {
  130. foreach ($this->_translations as $lang => $translation) {
  131. if ($lang == 'en') {
  132. continue;
  133. }
  134. $this->assertTrue(isset($translation[$enK]),
  135. $lang . ': Message "' . $enK . '" not translated');
  136. }
  137. }
  138. }
  139. }