views_module.test 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241
  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;
  85. // false is enabled.
  86. variable_set('views_defaults', $views_status);
  87. $existing = array();
  88. $type = array();
  89. $theme = array();
  90. $path = array();
  91. $registry = views_theme($existing, $type, $theme, $path);
  92. $this->assertTrue(isset($registry['views_view__frontpage']));
  93. }
  94. /**
  95. * Tests the views_get_handler method.
  96. */
  97. function testviews_get_handler() {
  98. $types = array('field', 'area', 'filter');
  99. foreach ($types as $type) {
  100. $handler = views_get_handler($this->randomName(), $this->randomName(), $type);
  101. $this->assertEqual('views_handler_' . $type . '_broken', get_class($handler), t('Make sure that a broken handler of type: @type are created', array('@type' => $type)));
  102. }
  103. $views_data = $this->viewsData();
  104. $test_tables = array('views_test' => array('id', 'name'));
  105. foreach ($test_tables as $table => $fields) {
  106. foreach ($fields as $field) {
  107. $data = $views_data[$table][$field];
  108. foreach ($data as $id => $field_data) {
  109. if (!in_array($id, array('title', 'help'))) {
  110. $handler = views_get_handler($table, $field, $id);
  111. $this->assertInstanceHandler($handler, $table, $field, $id);
  112. }
  113. }
  114. }
  115. }
  116. // Test the automatic conversion feature.
  117. // Test the automatic table renaming.
  118. $handler = views_get_handler('views_test_previous', 'id', 'field');
  119. $this->assertInstanceHandler($handler, 'views_test', 'id', 'field');
  120. $handler = views_get_handler('views_test_previous', 'id', 'filter');
  121. $this->assertInstanceHandler($handler, 'views_test', 'id', 'filter');
  122. // Test the automatic field renaming.
  123. $handler = views_get_handler('views_test', 'age_previous', 'field');
  124. $this->assertInstanceHandler($handler, 'views_test', 'age', 'field');
  125. $handler = views_get_handler('views_test', 'age_previous', 'sort');
  126. $this->assertInstanceHandler($handler, 'views_test', 'age', 'sort');
  127. // Test the automatic table and field renaming.
  128. $handler = views_get_handler('views_test_previous', 'name_previous', 'field');
  129. $this->assertInstanceHandler($handler, 'views_test', 'name', 'field');
  130. $handler = views_get_handler('views_test_previous', 'name_previous', 'argument');
  131. $this->assertInstanceHandler($handler, 'views_test', 'name', 'argument');
  132. // Test the override handler feature.
  133. $handler = views_get_handler('views_test', 'job', 'filter', 'views_handler_filter');
  134. $this->assertEqual('views_handler_filter', get_class($handler));
  135. }
  136. /**
  137. * Tests views_fetch_data().
  138. */
  139. function testFetchData() {
  140. // Make sure we start with a empty cache.
  141. $this->resetStaticViewsDataCache();
  142. cache_clear_all('*', 'cache_views', TRUE);
  143. variable_set('views_test_views_data_count', 0);
  144. // Request info about an existing table.
  145. $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
  146. // This should have triggered a views data rebuild, and written a cache
  147. // entry for all tables and the requested table but no other tables.
  148. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  149. $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables written.');
  150. $this->assertTrue(cache_get('views_data:views_test:en', 'cache_views'), 'Cache for requested table written.');
  151. $this->assertFalse(cache_get('views_data:views_test_previous:en', 'cache_views'), 'No Cache written for not requested table.');
  152. $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');
  153. $this->resetStaticViewsDataCache();
  154. // Request the same table again.
  155. $this->assertTrue(views_fetch_data('views_test'), 'Data about existing table returned');
  156. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  157. $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');
  158. $this->resetStaticViewsDataCache();
  159. // Request a missing table, this should load the full cache from cache but
  160. // not rebuilt.
  161. $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
  162. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  163. $this->assertTrue(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is fully loaded');
  164. $this->resetStaticViewsDataCache();
  165. // Request the same empty table again, this should load only that (empty)
  166. // cache for that table.
  167. $this->assertFalse(views_fetch_data('views_test_missing'), 'No data about missing table returned');
  168. $this->assertEqual(variable_get('views_test_views_data_count', 0), 1, 'Views data rebuilt once');
  169. $this->assertFalse(drupal_static('_views_fetch_data_fully_loaded'), 'Views data is not fully loaded');
  170. // Test if the cache consistency is ensured. There was an issue where
  171. // calling _views_fetch_data() first with a table would prevent the function
  172. // from properly rebuilt a missing the general cache entry.
  173. // See https://www.drupal.org/node/2475669 for details.
  174. // Make sure we start with a empty cache.
  175. $this->resetStaticViewsDataCache();
  176. cache_clear_all('*', 'cache_views', TRUE);
  177. // Prime the static cache of _views_fetch_data() by calling it with a table
  178. // first.
  179. views_fetch_data('views_test');
  180. // Now remove the general cache.
  181. cache_clear_all('views_data:en', 'cache_views');
  182. // Reset the static cache to see if fetches from the persistent cache
  183. // properly rebuild the static cache.
  184. $this->resetStaticViewsDataCache();
  185. // Prime the static cache of _views_fetch_data() by calling it with a table
  186. // first.
  187. views_fetch_data('views_test');
  188. // Fetch the general cache, which was deleted, an see if it is rebuild
  189. // properly.
  190. views_fetch_data();
  191. $this->assertTrue(cache_get('views_data:en', 'cache_views'), 'Cache for all tables was properly rebuild.');
  192. }
  193. /**
  194. * Ensure that a certain handler is a instance of a certain table/field.
  195. */
  196. function assertInstanceHandler($handler, $table, $field, $id) {
  197. $table_data = views_fetch_data($table);
  198. $field_data = $table_data[$field][$id];
  199. $this->assertEqual($field_data['handler'], get_class($handler));
  200. }
  201. /**
  202. * Resets the views data cache.
  203. */
  204. protected function resetStaticViewsDataCache() {
  205. drupal_static_reset('_views_fetch_data_cache');
  206. drupal_static_reset('_views_fetch_data_recursion_protected');
  207. drupal_static_reset('_views_fetch_data_fully_loaded');
  208. }
  209. }