Zend_ValidateTest.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  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. /**
  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-2010 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. // detect languages
  47. foreach (new DirectoryIterator($this->_langDir) as $entry) {
  48. if (!$entry->isDir()) {
  49. continue;
  50. }
  51. // skip "." or ".." or ".svn"
  52. $fname = $entry->getFilename();
  53. if ($fname[0] == '.') {
  54. continue;
  55. }
  56. // add all languages for testIsLocale
  57. $this->_languages[] = $fname;
  58. // include Zend_Validate translation tables
  59. $translationFile = $entry->getPathname() . DIRECTORY_SEPARATOR . 'Zend_Validate.php';
  60. if (file_exists($translationFile)) {
  61. $translation = include $translationFile;
  62. if (!is_array($translation)) {
  63. $this->fail("Invalid or empty translation table found for language '{$fname}'");
  64. }
  65. $this->_translations[$fname] = $translation;
  66. }
  67. }
  68. }
  69. public function testIsLocale()
  70. {
  71. foreach ($this->_languages as $lang) {
  72. if (!Zend_Locale::isLocale($lang, true, false)) {
  73. $this->fail("Language directory '{$lang}' not a valid locale");
  74. }
  75. }
  76. }
  77. public function testEnglishKeySameAsValue()
  78. {
  79. foreach ($this->_translations['en'] as $k => $v) {
  80. $this->assertEquals($k, $v);
  81. }
  82. }
  83. public function testTranslationAvailableAtEngligh()
  84. {
  85. foreach ($this->_translations as $lang => $translation) {
  86. if ($lang == 'en') {
  87. continue;
  88. }
  89. foreach ($translation as $k => $v) {
  90. $this->assertTrue(
  91. isset($this->_translations['en'][$k]),
  92. $lang . ': The key "' . $k . '" isn\'t available within english translation file'
  93. );
  94. }
  95. }
  96. }
  97. public function testTranslationDifferFromEnglish()
  98. {
  99. foreach ($this->_translations as $lang => $translation) {
  100. if ($lang == 'en') {
  101. continue;
  102. }
  103. foreach ($translation as $k => $v) {
  104. $this->assertTrue( ($k != $v),
  105. $lang . ': The translated message "' . $v . '" is the same the english version'
  106. );
  107. }
  108. }
  109. }
  110. public function testPlaceholder()
  111. {
  112. foreach ($this->_translations as $lang => $translation) {
  113. if ($lang == 'en') { // not needed to test - see testEnglishKeySameAsValue
  114. continue;
  115. }
  116. foreach ($translation as $k => $v) {
  117. if (preg_match_all('/(\%.+\%)/U', $k, $matches, PREG_SET_ORDER)) {
  118. foreach ($matches as $match) {
  119. $this->assertContains($match[1], $v,
  120. $lang . ': Missing placeholder "' . $match[1] . '" within "' . $v . '"');
  121. }
  122. }
  123. }
  124. }
  125. }
  126. public function testAllTranslated()
  127. {
  128. foreach ($this->_translations['en'] as $enK => $enV) {
  129. foreach ($this->_translations as $lang => $translation) {
  130. if ($lang == 'en') {
  131. continue;
  132. }
  133. $this->assertTrue(isset($translation[$enK]),
  134. $lang . ': Message "' . $enK . '" not translated');
  135. }
  136. }
  137. }
  138. }