views_module.test

Definition of ViewsModuleTest.

File

tests/views_module.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsModuleTest.
  5. */
  6. /**
  7. * Tests basic functions from the Views module.
  8. */
  9. class ViewsModuleTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Tests views.module',
  13. 'description' => 'Tests some basic functions of views.module',
  14. 'group' => 'Views',
  15. );
  16. }
  17. public function setUp() {
  18. parent::setUp();
  19. drupal_theme_rebuild();
  20. }
  21. public function viewsData() {
  22. $data = parent::viewsData();
  23. $data['views_test_previous'] = array();
  24. $data['views_test_previous']['id']['field']['moved to'] = array('views_test', 'id');
  25. $data['views_test_previous']['id']['filter']['moved to'] = array('views_test', 'id');
  26. $data['views_test']['age_previous']['field']['moved to'] = array('views_test', 'age');
  27. $data['views_test']['age_previous']['sort']['moved to'] = array('views_test', 'age');
  28. $data['views_test_previous']['name_previous']['field']['moved to'] = array('views_test', 'name');
  29. $data['views_test_previous']['name_previous']['argument']['moved to'] = array('views_test', 'name');
  30. return $data;
  31. }
  32. public function test_views_trim_text() {
  33. // Test unicode, @see http://drupal.org/node/513396#comment-2839416
  34. $text = array(
  35. 'Tuy nhiên, những hi vọng',
  36. 'Giả sử chúng tôi có 3 Apple',
  37. 'siêu nhỏ này là bộ xử lý',
  38. 'Di động của nhà sản xuất Phần Lan',
  39. 'khoảng cách từ đại lí đến',
  40. 'của hãng bao gồm ba dòng',
  41. 'сд асд асд ас',
  42. 'асд асд асд ас'
  43. );
  44. // Just test maxlength without word boundry.
  45. $alter = array(
  46. 'max_length' => 10,
  47. );
  48. $expect = array(
  49. 'Tuy nhiên,',
  50. 'Giả sử chú',
  51. 'siêu nhỏ n',
  52. 'Di động củ',
  53. 'khoảng các',
  54. 'của hãng b',
  55. 'сд асд асд',
  56. 'асд асд ас',
  57. );
  58. foreach ($text as $key => $line) {
  59. $result_text = views_trim_text($alter, $line);
  60. $this->assertEqual($result_text, $expect[$key]);
  61. }
  62. // Test also word_boundary
  63. $alter['word_boundary'] = TRUE;
  64. $expect = array(
  65. 'Tuy nhiên',
  66. 'Giả sử',
  67. 'siêu nhỏ',
  68. 'Di động',
  69. 'khoảng',
  70. 'của hãng',
  71. 'сд асд',
  72. 'асд асд',
  73. );
  74. foreach ($text as $key => $line) {
  75. $result_text = views_trim_text($alter, $line);
  76. $this->assertEqual($result_text, $expect[$key]);
  77. }
  78. }
  79. /**
  80. * Tests the dynamic includes of templates via module feature.
  81. */
  82. function testModuleTemplates() {
  83. $views_status = variable_get('views_defaults', array());
  84. $views_status['frontpage'] = FALSE; // false is enabled
  85. variable_set('views_defaults', $views_status);
  86. $existing = array();
  87. $type = array();
  88. $theme = array();
  89. $path = array();
  90. $registry = views_theme($existing, $type, $theme, $path);
  91. $this->assertTrue(isset($registry['views_view__frontpage']));
  92. }
  93. /**
  94. * Tests the views_get_handler method.
  95. */
  96. function testviews_get_handler() {
  97. $types = array('field', 'area', 'filter');
  98. foreach ($types as $type) {
  99. $handler = views_get_handler($this->randomName(), $this->randomName(), $type);
  100. $this->assertEqual('views_handler_' . $type . '_broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type)));
  101. }
  102. $views_data = $this->viewsData();
  103. $test_tables = array('views_test' => array('id', 'name'));
  104. foreach ($test_tables as $table => $fields) {
  105. foreach ($fields as $field) {
  106. $data = $views_data[$table][$field];
  107. foreach ($data as $id => $field_data) {
  108. if (!in_array($id, array('title', 'help'))) {
  109. $handler = views_get_handler($table, $field, $id);
  110. $this->assertInstanceHandler($handler, $table, $field, $id);
  111. }
  112. }
  113. }
  114. }
  115. // Test the automatic conversion feature.
  116. // Test the automatic table renaming.
  117. $handler = views_get_handler('views_test_previous', 'id', 'field');
  118. $this->assertInstanceHandler($handler, 'views_test', 'id', 'field');
  119. $handler = views_get_handler('views_test_previous', 'id', 'filter');
  120. $this->assertInstanceHandler($handler, 'views_test', 'id', 'filter');
  121. // Test the automatic field renaming.
  122. $handler = views_get_handler('views_test', 'age_previous', 'field');
  123. $this->assertInstanceHandler($handler, 'views_test', 'age', 'field');
  124. $handler = views_get_handler('views_test', 'age_previous', 'sort');
  125. $this->assertInstanceHandler($handler, 'views_test', 'age', 'sort');
  126. // Test the automatic table and field renaming.
  127. $handler = views_get_handler('views_test_previous', 'name_previous', 'field');
  128. $this->assertInstanceHandler($handler, 'views_test', 'name', 'field');
  129. $handler = views_get_handler('views_test_previous', 'name_previous', 'argument');
  130. $this->assertInstanceHandler($handler, 'views_test', 'name', 'argument');
  131. // Test the override handler feature.
  132. $handler = views_get_handler('views_test', 'job', 'filter', 'views_handler_filter');
  133. $this->assertEqual('views_handler_filter', get_class($handler));
  134. }
  135. /**
  136. * Tests views_fetch_data().
  137. */
  138. function testFetchData() {
  139. // Make sure we start with a empty cache.
  140. $this->resetStaticViewsDataCache();
  141. cache_clear_all('*', 'cache_views', TRUE);
  142. variable_set('views_test_views_data_count', 0);
  143. // Request info about an existing table.
  144. $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
  145. // This should have triggered a views data rebuild, and written a cache
  146. // entry for all tables and the requested table but no other tables.
  147. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  148. $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables written.');
  149. $this->assertTrue(cache_get('views_data:views_test:en', 'cache_views'), 'Cache for requested table written.');
  150. $this->assertFalse(cache_get('views_data:views_test_previous:en', 'cache_views'), 'No Cache written for not requested table.');
  151. $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');
  152. $this->resetStaticViewsDataCache();
  153. // Request the same table again.
  154. $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
  155. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  156. $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');
  157. $this->resetStaticViewsDataCache();
  158. // Request a missing table, this should load the full cache from cache but
  159. // not rebuilt.
  160. $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
  161. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  162. $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');
  163. $this->resetStaticViewsDataCache();
  164. // Request the same empty table again, this should load only that (empty)
  165. // cache for that table.
  166. $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
  167. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  168. $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');
  169. }
  170. /**
  171. * Ensure that a certain handler is a instance of a certain table/field.
  172. */
  173. function assertInstanceHandler($handler, $table, $field, $id) {
  174. $table_data = views_fetch_data($table);
  175. $field_data = $table_data[$field][$id];
  176. $this->assertEqual($field_data['handler'], get_class($handler));
  177. }
  178. /**
  179. * Resets the views data cache.
  180. */
  181. protected function resetStaticViewsDataCache() {
  182. drupal_static_reset('_views_fetch_data_cache');
  183. drupal_static_reset('_views_fetch_data_recursion_protected');
  184. drupal_static_reset('_views_fetch_data_fully_loaded');
  185. }
  186. }