| 1 |
|
<?php |
| 2 |
|
// $Id: l10n_community.test,v 1.1.2.3 2009/09/08 11:23:30 goba Exp $ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* @file |
| 6 |
|
* Tests to ensure that the template extractor works as intended. |
| 7 |
|
*/ |
| 8 |
|
|
| 9 |
|
class PotxTestCase extends DrupalWebTestCase { |
| 10 |
|
|
| 11 |
|
public static function getInfo() { |
| 12 |
|
return array( |
| 13 |
|
'name' => t('Translation template extractor'), |
| 14 |
|
'description' => t('Ensure that the translation template extractor functions properly.'), |
| 15 |
|
'group' => t('Translation template extractor'), |
| 16 |
|
); |
| 17 |
|
} |
| 18 |
|
|
| 19 |
|
public function setUp() { |
| 20 |
|
// Set up required modules for l10n_community. |
| 21 |
|
parent::setUp('locale', 'potx'); |
| 22 |
|
// Add potx.inc which we test for its functionality. |
| 23 |
|
include_once(drupal_get_path('module', 'potx') .'/potx.inc'); |
| 24 |
|
} |
| 25 |
|
|
| 26 |
|
/** |
| 27 |
|
* Test parsing of Drupal 5 module. |
| 28 |
|
*/ |
| 29 |
|
public function testDrupal5() { |
| 30 |
|
// Parse and build the Drupal 5 module file. |
| 31 |
|
$filename = drupal_get_path('module', 'potx') .'/tests/potx_test_5.module'; |
| 32 |
|
$this->parseFile($filename, POTX_API_5); |
| 33 |
|
|
| 34 |
|
// Assert strings found in module source code. |
| 35 |
|
$this->assertMsgID('Test menu item in 5'); |
| 36 |
|
$this->assertMsgID('This is a test menu item in 5'); |
| 37 |
|
$this->assertMsgID('This is a test string.'); |
| 38 |
|
$this->assertMsgID('test watchdog type'); |
| 39 |
|
// No support for instant t() in watchdog. |
| 40 |
|
$this->assertNoMsgID('My watchdog message'); |
| 41 |
|
$this->assertMsgID('test potx permission'); |
| 42 |
|
$this->assertMsgID('one more test potx permission'); |
| 43 |
|
$this->assertMsgID('1 test string'); |
| 44 |
|
$this->assertPluralID('@count test strings'); |
| 45 |
|
|
| 46 |
|
// Installer string should not appear in runtime output. |
| 47 |
|
$this->assertNoMsgID('Installer only test string'); |
| 48 |
|
$this->assertMsgID('Dynamic callback test string'); |
| 49 |
|
|
| 50 |
|
// No context support yet. |
| 51 |
|
$this->assertNoMsgIDContext('Test string in context', 'Test context'); |
| 52 |
|
$this->assertMsgID('Test string in context'); |
| 53 |
|
|
| 54 |
|
// Look at installer strings. |
| 55 |
|
$this->parseFile($filename, POTX_API_5, POTX_STRING_INSTALLER); |
| 56 |
|
$this->assertMsgID('Installer only test string'); |
| 57 |
|
$this->assertMsgID('Dynamic callback test string'); |
| 58 |
|
$this->assertNoMsgID('1 test string'); |
| 59 |
|
$this->assertNoMsgID('This is a test string.'); |
| 60 |
|
} |
| 61 |
|
|
| 62 |
|
/** |
| 63 |
|
* Test parsing of Drupal 6 module. |
| 64 |
|
*/ |
| 65 |
|
public function testDrupal6() { |
| 66 |
|
// Parse and build the Drupal 6 module file. |
| 67 |
|
$filename = drupal_get_path('module', 'potx') .'/tests/potx_test_6.module'; |
| 68 |
|
$this->parseFile($filename, POTX_API_6); |
| 69 |
|
|
| 70 |
|
// Assert strings found in module source code. |
| 71 |
|
$this->assertMsgID('Test menu item'); |
| 72 |
|
$this->assertMsgID('This is a test menu item'); |
| 73 |
|
$this->assertMsgID('This is a test string.'); |
| 74 |
|
$this->assertMsgID('test watchdog type'); |
| 75 |
|
$this->assertMsgID('My watchdog message'); |
| 76 |
|
$this->assertMsgID('test potx permission'); |
| 77 |
|
$this->assertMsgID('one more test potx permission'); |
| 78 |
|
$this->assertMsgID('1 test string'); |
| 79 |
|
$this->assertPluralID('@count test strings'); |
| 80 |
|
|
| 81 |
|
// Installer string should not appear in runtime output. |
| 82 |
|
$this->assertNoMsgID('Installer only test string'); |
| 83 |
|
$this->assertMsgID('Dynamic callback test string'); |
| 84 |
|
|
| 85 |
|
// No context support yet. |
| 86 |
|
$this->assertNoMsgIDContext('Test string in context', 'Test context'); |
| 87 |
|
$this->assertMsgID('Test string in context'); |
| 88 |
|
|
| 89 |
|
// Look at installer strings. |
| 90 |
|
$this->parseFile($filename, POTX_API_6, POTX_STRING_INSTALLER); |
| 91 |
|
$this->assertMsgID('Installer only test string'); |
| 92 |
|
$this->assertMsgID('Dynamic callback test string'); |
| 93 |
|
$this->assertNoMsgID('1 test string'); |
| 94 |
|
$this->assertNoMsgID('This is a test string.'); |
| 95 |
|
} |
| 96 |
|
|
| 97 |
|
/** |
| 98 |
|
* Test parsing of Drupal 7 module. |
| 99 |
|
*/ |
| 100 |
|
public function testDrupal7() { |
| 101 |
|
// Parse and build the Drupal 7 module file. |
| 102 |
|
$filename = drupal_get_path('module', 'potx') .'/tests/potx_test_7.module'; |
| 103 |
|
$this->parseFile($filename, POTX_API_7); |
| 104 |
|
|
| 105 |
|
// Assert strings found in module source code. |
| 106 |
|
$this->assertMsgID('Test menu item'); |
| 107 |
|
$this->assertMsgID('This is a test menu item'); |
| 108 |
|
$this->assertMsgID('This is a test string.'); |
| 109 |
|
$this->assertMsgID('test watchdog type'); |
| 110 |
|
$this->assertMsgID('My watchdog message'); |
| 111 |
|
|
| 112 |
|
// No support for hook_perm() anymore. t() in hook_permissions(). |
| 113 |
|
$this->assertNoMsgID('test potx permission'); |
| 114 |
|
$this->assertNoMsgID('one more test potx permission'); |
| 115 |
|
$this->assertMsgID('Test potx permission'); |
| 116 |
|
$this->assertMsgID('Test potx permission description'); |
| 117 |
|
$this->assertMsgID('One more test potx permission'); |
| 118 |
|
$this->assertMsgID('One more test potx permission description'); |
| 119 |
|
|
| 120 |
|
$this->assertMsgID('1 test string'); |
| 121 |
|
$this->assertPluralID('@count test strings'); |
| 122 |
|
|
| 123 |
|
$this->assertNoMsgID('Installer only test string'); |
| 124 |
|
$this->assertMsgID('Dynamic callback test string'); |
| 125 |
|
|
| 126 |
|
// Context support added. |
| 127 |
|
$this->assertMsgIDContext('Test string in context', 'Test context'); |
| 128 |
|
|
| 129 |
|
// Look at installer strings. |
| 130 |
|
$this->parseFile($filename, POTX_API_7, POTX_STRING_INSTALLER); |
| 131 |
|
$this->assertMsgID('Installer only test string'); |
| 132 |
|
$this->assertMsgID('Dynamic callback test string'); |
| 133 |
|
$this->assertNoMsgID('1 test string'); |
| 134 |
|
$this->assertNoMsgID('This is a test string.'); |
| 135 |
|
} |
| 136 |
|
|
| 137 |
|
/** |
| 138 |
|
* Test parsing of Drupal 6 info file. Drupal 5 and 7 have no other rules. |
| 139 |
|
*/ |
| 140 |
|
public function testDrupalInfo() { |
| 141 |
|
// Parse and build the Drupal 6 module file. |
| 142 |
|
$filename = drupal_get_path('module', 'potx') .'/tests/potx_test_6.info'; |
| 143 |
|
$this->parseFile($filename, POTX_API_6); |
| 144 |
|
|
| 145 |
|
// Look for name, description and package name extracted. |
| 146 |
|
$this->assertMsgID('Translation template extractor tester'); |
| 147 |
|
$this->assertMsgID('Test description'); |
| 148 |
|
$this->assertMsgID('Test package'); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
/** |
| 152 |
|
* Test parsing of a Drupal JS file. |
| 153 |
|
*/ |
| 154 |
|
public function testDrupalJS() { |
| 155 |
|
// Parse and build the Drupal JS file (from above Drupal 5). |
| 156 |
|
$filename = drupal_get_path('module', 'potx') .'/tests/potx_test.js'; |
| 157 |
|
$this->parseFile($filename, POTX_API_6); |
| 158 |
|
|
| 159 |
|
// Assert strings found in JS source code. |
| 160 |
|
$this->assertMsgID('Test string in JS'); |
| 161 |
|
$this->assertMsgID('1 test string in JS'); |
| 162 |
|
$this->assertPluralID('@count test strings in JS'); |
| 163 |
|
$this->assertMsgID('Another test string in JS'); |
| 164 |
|
$this->assertMsgID('Embedded test string in JS'); |
| 165 |
|
} |
| 166 |
|
|
| 167 |
|
/** |
| 168 |
|
* Parse the given file with the given API version. |
| 169 |
|
*/ |
| 170 |
|
private function parseFile($filename, $api_version, $string_mode = POTX_STRING_RUNTIME) { |
| 171 |
|
global $_potx_store, $_potx_strings, $_potx_install; |
| 172 |
|
$_potx_store = $_potx_strings = $_potx_install = array(); |
| 173 |
|
|
| 174 |
|
_potx_process_file($filename, 0, '_potx_save_string', '_potx_save_version', $api_version); |
| 175 |
|
_potx_build_files($string_mode, POTX_BUILD_SINGLE, 'general', '_potx_save_string', '_potx_save_version', '_potx_get_header', NULL, NULL, $api_version); |
| 176 |
|
|
| 177 |
|
// Grab .po representation of parsed content. |
| 178 |
|
ob_start(); |
| 179 |
|
_potx_write_files('potx-test.po'); |
| 180 |
|
$this->potx_output = ob_get_clean(); |
| 181 |
|
$this->pass(var_export($this->potx_output, TRUE)); |
| 182 |
|
} |
| 183 |
|
|
| 184 |
|
/** |
| 185 |
|
* Helper function to assert an msgid construct in the .po file. |
| 186 |
|
*/ |
| 187 |
|
private function assertMsgID($string, $message = '', $group = 'Other') { |
| 188 |
|
if (!$message) { |
| 189 |
|
$message = t('MsgID "@raw" found', array('@raw' => check_plain($string))); |
| 190 |
|
} |
| 191 |
|
$this->assert(strpos($this->potx_output, 'msgid "'. _potx_format_quoted_string('"'. $string . '"') .'"') !== FALSE, $message, $group); |
| 192 |
|
} |
| 193 |
|
|
| 194 |
|
/** |
| 195 |
|
* Helper function to assert a missing msgid construct in the .po file. |
| 196 |
|
*/ |
| 197 |
|
private function assertNoMsgID($string, $message = '', $group = 'Other') { |
| 198 |
|
if (!$message) { |
| 199 |
|
$message = t('MsgID "@raw" not found', array('@raw' => check_plain($string))); |
| 200 |
|
} |
| 201 |
|
$this->assert(strpos($this->potx_output, 'msgid "'. _potx_format_quoted_string('"'. $string . '"') .'"') === FALSE, $message, $group); |
| 202 |
|
} |
| 203 |
|
|
| 204 |
|
/** |
| 205 |
|
* Helper function to assert an msgid with context in the .po file. |
| 206 |
|
*/ |
| 207 |
|
private function assertMsgIDContext($string, $context, $message = '', $group = 'Other') { |
| 208 |
|
if (!$message) { |
| 209 |
|
$message = t('MsgID "@raw" in context "@context" found', array('@raw' => check_plain($string), '@context' => check_plain($context))); |
| 210 |
|
} |
| 211 |
|
$this->assert(strpos($this->potx_output, 'msgid "'. _potx_format_quoted_string('"'. $string . '"') .'"'. "\nmsgctxt \"". _potx_format_quoted_string('"'. $context . '"') . '"') !== FALSE, $message, $group); |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
/** |
| 215 |
|
* Helper function to assert an msgid with context in the .po file. |
| 216 |
|
*/ |
| 217 |
|
private function assertNoMsgIDContext($string, $context, $message = '', $group = 'Other') { |
| 218 |
|
if (!$message) { |
| 219 |
|
$message = t('No MsgID "@raw" in context "@context" found', array('@raw' => check_plain($string), '@context' => check_plain($context))); |
| 220 |
|
} |
| 221 |
|
$this->assert(strpos($this->potx_output, 'msgid "'. _potx_format_quoted_string('"'. $string . '"') .'"'. "\nmsgctxt \"". _potx_format_quoted_string('"'. $context . '"') . '"') === FALSE, $message, $group); |
| 222 |
|
} |
| 223 |
|
|
| 224 |
|
/** |
| 225 |
|
* Helper function to assert an msgid_plural construct in the .po file. |
| 226 |
|
*/ |
| 227 |
|
private function assertPluralID($string, $message = '', $group = 'Other') { |
| 228 |
|
if (!$message) { |
| 229 |
|
$message = t('Plural ID "@raw" found', array('@raw' => check_plain($string))); |
| 230 |
|
} |
| 231 |
|
$this->assert(strpos($this->potx_output, 'msgid_plural "'. _potx_format_quoted_string('"'. $string . '"') .'"') !== FALSE, $message, $group); |
| 232 |
|
} |
| 233 |
|
|
| 234 |
|
} |