views_translatable.test 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsTranslatableTest.
  5. */
  6. /**
  7. * Tests Views pluggable translations.
  8. */
  9. class ViewsTranslatableTest extends ViewsSqlTest {
  10. var $strings;
  11. public static function getInfo() {
  12. return array(
  13. 'name' => 'Views Translatable Test',
  14. 'description' => 'Tests the pluggable translations',
  15. 'group' => 'Views',
  16. );
  17. }
  18. /**
  19. * The views plugin definition. Override it if you test provides a plugin.
  20. */
  21. public function viewsPlugins() {
  22. return array(
  23. 'localization' => array(
  24. 'test' => array(
  25. 'no ui' => TRUE,
  26. 'title' => t('Test'),
  27. 'help' => t('This is a test description.'),
  28. 'handler' => 'views_plugin_localization_test',
  29. 'parent' => 'parent',
  30. 'path' => drupal_get_path('module', 'views') . '/tests',
  31. ),
  32. ),
  33. );
  34. }
  35. public function setUp() {
  36. parent::setUp();
  37. variable_set('views_localization_plugin', 'test');
  38. // Reset the plugin data.
  39. views_fetch_plugin_data(NULL, NULL, TRUE);
  40. $this->strings = array('Master1', 'Apply1', 'Sort By1', 'Asc1', 'Desc1', 'more1', 'Reset1', 'Offset1', 'Master1', 'title1', 'Items per page1', 'fieldlabel1', 'filterlabel1');
  41. $this->enableViewsUi();
  42. }
  43. /**
  44. * Tests the unpack translation funtionality.
  45. */
  46. public function testUnpackTranslatable() {
  47. $view = $this->view_unpack_translatable();
  48. $view->init_localization();
  49. $this->assertEqual('views_plugin_localization_test', get_class($view->localization_plugin), 'Make sure that init_localization initializes the right translation plugin');
  50. $view->export_locale_strings();
  51. $expected_strings = $this->strings;
  52. $result_strings = $view->localization_plugin->get_export_strings();
  53. $this->assertEqual(sort($expected_strings), sort($result_strings), 'Make sure that the localization plugin got every translatable string.');
  54. }
  55. public function testUi() {
  56. // Make sure that the string is not translated in the UI.
  57. $view = $this->view_unpack_translatable();
  58. $view->save();
  59. views_invalidate_cache();
  60. $admin_user = $this->drupalCreateUser(array('administer views', 'administer site configuration'));
  61. $this->drupalLogin($admin_user);
  62. $this->drupalGet("admin/structure/views/view/$view->name/edit");
  63. $this->assertNoText('-translated', 'Make sure that no strings get translated in the UI.');
  64. }
  65. /**
  66. * Make sure that the translations get into the loaded view.
  67. */
  68. public function testTranslation() {
  69. $view = $this->view_unpack_translatable();
  70. $view->set_display('default');
  71. $this->executeView($view);
  72. $expected_strings = array();
  73. foreach ($this->strings as $string) {
  74. $expected_strings[] = $string .= '-translated';
  75. }
  76. $this->assertEqual(sort($expected_strings), sort($view->localization_plugin->translated_strings), 'Make sure that every string got loaded translated');
  77. }
  78. /**
  79. * Make sure that the different things have the right translation keys.
  80. */
  81. public function testTranslationKey() {
  82. $view = $this->view_unpack_translatable();
  83. $view->editing = TRUE;
  84. $view->init_display();
  85. // Don't run translation. We just want to get the right keys.
  86. foreach ($view->display as $display_id => $display) {
  87. $translatables = array();
  88. $display->handler->unpack_translatables($translatables);
  89. $this->string_keys = array(
  90. 'Master1' => array('title'),
  91. 'Apply1' => array('exposed_form', 'submit_button'),
  92. 'Sort By1' => array('exposed_form', 'exposed_sorts_label'),
  93. 'Asc1' => array('exposed_form', 'sort_asc_label'),
  94. 'Desc1' => array('exposed_form', 'sort_desc_label'),
  95. 'more1' => array('use_more_text'),
  96. 'Reset1' => array('exposed_form', 'reset_button_label'),
  97. 'Offset1' => array('pager', 'expose', 'offset_label'),
  98. 'title1' => array('title'),
  99. 'Tag first1' => array('pager', 'tags', 'first'),
  100. 'Tag prev1' => array('pager', 'tags', 'previous'),
  101. 'Tag next1' => array('pager', 'tags', 'next'),
  102. 'Tag last1' => array('pager', 'tags', 'last'),
  103. 'Items per page1' => array('pager', 'expose', 'items_per_page_label'),
  104. 'fieldlabel1' => array('field', 'node', 'nid', 'label'),
  105. 'filterlabel1' => array('filter', 'node', 'nid', 'expose', 'label'),
  106. '- All -' => array('pager', 'expose', 'items_per_page_options_all_label'),
  107. 'Header1' => array('header', 'views', 'area', 'content'),
  108. );
  109. $formats = array(
  110. 'Header1' => 'filtered_html',
  111. );
  112. foreach ($translatables as $translatable) {
  113. $this->assertEqual($translatable['keys'], $this->string_keys[$translatable['value']]);
  114. // Make sure the format is correct.
  115. if (isset($formats[$translatable['value']])) {
  116. $this->assertEqual($translatable['format'], $formats[$translatable['value']]);
  117. }
  118. else {
  119. $this->assertNull($translatable['format'], 'No format defined');
  120. }
  121. }
  122. }
  123. }
  124. public function view_unpack_translatable() {
  125. $view = new view();
  126. $view->name = 'view_unpack_translatable';
  127. $view->description = '';
  128. $view->tag = '';
  129. $view->base_table = 'node';
  130. $view->api_version = '3.0-alpha1';
  131. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  132. /* Display: Master */
  133. $handler = $view->new_display('default', 'Master1', 'default');
  134. $handler->display->display_options['title'] = 'title1';
  135. $handler->display->display_options['use_more_text'] = 'more1';
  136. $handler->display->display_options['access']['type'] = 'none';
  137. $handler->display->display_options['cache']['type'] = 'none';
  138. $handler->display->display_options['query']['type'] = 'views_query';
  139. $handler->display->display_options['exposed_form']['type'] = 'basic';
  140. $handler->display->display_options['exposed_form']['options']['submit_button'] = 'Apply1';
  141. $handler->display->display_options['exposed_form']['options']['reset_button'] = TRUE;
  142. $handler->display->display_options['exposed_form']['options']['reset_button_label'] = 'Reset1';
  143. $handler->display->display_options['exposed_form']['options']['exposed_sorts_label'] = 'Sort By1';
  144. $handler->display->display_options['exposed_form']['options']['sort_asc_label'] = 'Asc1';
  145. $handler->display->display_options['exposed_form']['options']['sort_desc_label'] = 'Desc1';
  146. $handler->display->display_options['pager']['type'] = 'full';
  147. $handler->display->display_options['pager']['options']['items_per_page'] = '10';
  148. $handler->display->display_options['pager']['options']['offset'] = '0';
  149. $handler->display->display_options['pager']['options']['id'] = '0';
  150. $handler->display->display_options['pager']['options']['quantity'] = '9';
  151. $handler->display->display_options['pager']['options']['tags']['first'] = 'Tag first1';
  152. $handler->display->display_options['pager']['options']['tags']['previous'] = 'Tag prev1';
  153. $handler->display->display_options['pager']['options']['tags']['next'] = 'Tag next1';
  154. $handler->display->display_options['pager']['options']['tags']['last'] = 'Tag last1';
  155. $handler->display->display_options['pager']['options']['expose']['items_per_page'] = TRUE;
  156. $handler->display->display_options['pager']['options']['expose']['items_per_page_label'] = 'Items per page1';
  157. $handler->display->display_options['pager']['options']['expose']['offset'] = TRUE;
  158. $handler->display->display_options['pager']['options']['expose']['offset_label'] = 'Offset1';
  159. $handler->display->display_options['style_plugin'] = 'default';
  160. $handler->display->display_options['row_plugin'] = 'fields';
  161. /* Global: Header */
  162. $handler->display->display_options['header']['area']['id'] = 'area';
  163. $handler->display->display_options['header']['area']['table'] = 'views';
  164. $handler->display->display_options['header']['area']['field'] = 'area';
  165. $handler->display->display_options['header']['area']['empty'] = FALSE;
  166. $handler->display->display_options['header']['area']['content'] = 'Header1';
  167. $handler->display->display_options['header']['area']['format'] = 'filtered_html';
  168. $handler->display->display_options['header']['area']['tokenize'] = 0;
  169. /* Field: Content: Nid */
  170. $handler->display->display_options['fields']['nid']['id'] = 'nid';
  171. $handler->display->display_options['fields']['nid']['table'] = 'node';
  172. $handler->display->display_options['fields']['nid']['field'] = 'nid';
  173. $handler->display->display_options['fields']['nid']['label'] = 'fieldlabel1';
  174. $handler->display->display_options['fields']['nid']['alter']['alter_text'] = 0;
  175. $handler->display->display_options['fields']['nid']['alter']['make_link'] = 0;
  176. $handler->display->display_options['fields']['nid']['alter']['trim'] = 0;
  177. $handler->display->display_options['fields']['nid']['alter']['word_boundary'] = 1;
  178. $handler->display->display_options['fields']['nid']['alter']['ellipsis'] = 1;
  179. $handler->display->display_options['fields']['nid']['alter']['strip_tags'] = 0;
  180. $handler->display->display_options['fields']['nid']['alter']['html'] = 0;
  181. $handler->display->display_options['fields']['nid']['hide_empty'] = 0;
  182. $handler->display->display_options['fields']['nid']['empty_zero'] = 0;
  183. $handler->display->display_options['fields']['nid']['link_to_node'] = 0;
  184. /* Filter: Content: Nid */
  185. $handler->display->display_options['filters']['nid']['id'] = 'nid';
  186. $handler->display->display_options['filters']['nid']['table'] = 'node';
  187. $handler->display->display_options['filters']['nid']['field'] = 'nid';
  188. $handler->display->display_options['filters']['nid']['exposed'] = TRUE;
  189. $handler->display->display_options['filters']['nid']['expose']['operator_id'] = 'nid_op';
  190. $handler->display->display_options['filters']['nid']['expose']['label'] = 'filterlabel1';
  191. $handler->display->display_options['filters']['nid']['expose']['identifier'] = 'nid';
  192. $handler->display->display_options['filters']['nid']['expose']['multiple'] = 1;
  193. $handler->display->display_options['filters']['nid']['expose']['reduce'] = 0;
  194. return $view;
  195. }
  196. }