views_cache.test

Definition of ViewsCacheTest.

File

tests/views_cache.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsCacheTest.
  5. */
  6. /**
  7. * Basic test for pluggable caching.
  8. *
  9. * @see views_plugin_cache
  10. */
  11. class ViewsCacheTest extends ViewsSqlTest {
  12. public static function getInfo() {
  13. return array(
  14. 'name' => 'Cache',
  15. 'description' => 'Tests pluggable caching for views.',
  16. 'group' => 'Views Plugins'
  17. );
  18. }
  19. /**
  20. * Build and return a basic view of the views_test table.
  21. *
  22. * @return view
  23. */
  24. protected function getBasicView() {
  25. views_include('view');
  26. // Create the basic view.
  27. $view = new view();
  28. $view->name = 'test_view';
  29. $view->add_display('default');
  30. $view->base_table = 'views_test';
  31. // Set up the fields we need.
  32. $display = $view->new_display('default', 'Master', 'default');
  33. $display->override_option('fields', array(
  34. 'id' => array(
  35. 'id' => 'id',
  36. 'table' => 'views_test',
  37. 'field' => 'id',
  38. 'relationship' => 'none',
  39. ),
  40. 'name' => array(
  41. 'id' => 'name',
  42. 'table' => 'views_test',
  43. 'field' => 'name',
  44. 'relationship' => 'none',
  45. ),
  46. 'age' => array(
  47. 'id' => 'age',
  48. 'table' => 'views_test',
  49. 'field' => 'age',
  50. 'relationship' => 'none',
  51. ),
  52. ));
  53. // Set up the sort order.
  54. $display->override_option('sorts', array(
  55. 'id' => array(
  56. 'order' => 'ASC',
  57. 'id' => 'id',
  58. 'table' => 'views_test',
  59. 'field' => 'id',
  60. 'relationship' => 'none',
  61. ),
  62. ));
  63. return $view;
  64. }
  65. /**
  66. * Tests time based caching.
  67. *
  68. * @see views_plugin_cache_time
  69. */
  70. function testTimeCaching() {
  71. // Create a basic result which just 2 results.
  72. $view = $this->getBasicView();
  73. $view->set_display();
  74. $view->display_handler->override_option('cache', array(
  75. 'type' => 'time',
  76. 'results_lifespan' => '3600',
  77. 'output_lifespan' => '3600',
  78. ));
  79. $this->executeView($view);
  80. // Verify the result.
  81. $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
  82. // Add another man to the beatles.
  83. $record = array(
  84. 'name' => 'Rod Davis',
  85. 'age' => 29,
  86. 'job' => 'Banjo',
  87. );
  88. drupal_write_record('views_test', $record);
  89. // The Result should be the same as before, because of the caching.
  90. $view = $this->getBasicView();
  91. $view->set_display();
  92. $view->display_handler->override_option('cache', array(
  93. 'type' => 'time',
  94. 'results_lifespan' => '3600',
  95. 'output_lifespan' => '3600',
  96. ));
  97. $this->executeView($view);
  98. // Verify the result.
  99. $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
  100. }
  101. /**
  102. * Tests no caching.
  103. *
  104. * @see views_plugin_cache_time
  105. */
  106. function testNoneCaching() {
  107. // Create a basic result which just 2 results.
  108. $view = $this->getBasicView();
  109. $view->set_display();
  110. $view->display_handler->override_option('cache', array(
  111. 'type' => 'none',
  112. ));
  113. $this->executeView($view);
  114. // Verify the result.
  115. $this->assertEqual(5, count($view->result), t('The number of returned rows match.'));
  116. // Add another man to the beatles.
  117. $record = array(
  118. 'name' => 'Rod Davis',
  119. 'age' => 29,
  120. 'job' => 'Banjo',
  121. );
  122. drupal_write_record('views_test', $record);
  123. // The Result changes, because the view is not cached.
  124. $view = $this->getBasicView();
  125. $view->set_display();
  126. $view->display_handler->override_option('cache', array(
  127. 'type' => 'none',
  128. ));
  129. $this->executeView($view);
  130. // Verify the result.
  131. $this->assertEqual(6, count($view->result), t('The number of returned rows match.'));
  132. }
  133. /**
  134. * Tests css/js storage and restoring mechanism.
  135. */
  136. function testHeaderStorage() {
  137. // Create a view with output caching enabled.
  138. // Some hook_views_pre_render in views_test.module adds the test css/js file.
  139. // so they should be added to the css/js storage.
  140. $view = $this->getBasicView();
  141. $view->init_display();
  142. $view->name = 'test_cache_header_storage';
  143. $view->display_handler->override_option('cache', array(
  144. 'type' => 'time',
  145. 'output_lifespan' => '3600',
  146. ));
  147. $view->preview();
  148. $view->destroy();
  149. unset($view->pre_render_called);
  150. drupal_static_reset('drupal_add_css');
  151. drupal_static_reset('drupal_add_js');
  152. $view->init_display();
  153. $view->preview();
  154. $css = drupal_add_css();
  155. $css_path = drupal_get_path('module', 'views_test') . '/views_cache.test.css';
  156. $js_path = drupal_get_path('module', 'views_test') . '/views_cache.test.js';
  157. $js = drupal_add_js();
  158. $this->assertTrue(isset($css[$css_path]), 'Make sure the css is added for cached views.');
  159. $this->assertTrue(isset($js[$js_path]), 'Make sure the js is added for cached views.');
  160. $this->assertFalse(!empty($view->pre_render_called), 'Make sure hook_views_pre_render is not called for the cached view.');
  161. $view->destroy();
  162. // Now add some css/jss before running the view.
  163. // Make sure that this css is not added when running the cached view.
  164. $view->name = 'test_cache_header_storage_2';
  165. $system_css_path = drupal_get_path('module', 'system') . '/system.maintenance.css';
  166. drupal_add_css($system_css_path);
  167. $system_js_path = drupal_get_path('module', 'system') . '/system.cron.js';
  168. drupal_add_js($system_js_path);
  169. $view->init_display();
  170. $view->preview();
  171. $view->destroy();
  172. drupal_static_reset('drupal_add_css');
  173. drupal_static_reset('drupal_add_js');
  174. $view->init_display();
  175. $view->preview();
  176. $css = drupal_add_css();
  177. $js = drupal_add_js();
  178. $this->assertFalse(isset($css[$system_css_path]), 'Make sure that unrelated css is not added.');
  179. $this->assertFalse(isset($js[$system_js_path]), 'Make sure that unrelated js is not added.');
  180. }
  181. /**
  182. * Check that HTTP headers are cached for views.
  183. */
  184. function testHttpHeadersCaching() {
  185. // Create a few nodes to appear in RSS feed.
  186. for ($i = 0; $i < 5; $i++) {
  187. $this->drupalCreateNode();
  188. }
  189. // Make the first request and check returned content type.
  190. $this->drupalGet('test_feed_http_headers_caching');
  191. $first_content = $this->drupalGetContent();
  192. $first_content_type = $this->drupalGetHeader('content-type');
  193. $expected_type = 'application/rss+xml';
  194. $this->assertIdentical(0, strpos(trim($first_content_type), $expected_type), t('Expected content type returned.'));
  195. // Check that we have 5 items in RSS feed returned by the first request.
  196. $xml = simplexml_load_string($first_content);
  197. $items = $xml->xpath('/rss/channel/item');
  198. $this->assertEqual(5, count($items), t('The number of RSS feed items matched.'));
  199. // Create another node to be sure we get cached results on the second
  200. // request.
  201. $this->drupalCreateNode();
  202. // Make the second request, check content type and content matching.
  203. $this->drupalGet('test_feed_http_headers_caching');
  204. $second_content = $this->drupalGetContent();
  205. $this->assertEqual($first_content, $second_content, t('The second result fetched from cache.'));
  206. $second_content_type = $this->drupalGetHeader('content-type');
  207. $this->assertEqual($first_content_type, $second_content_type, t('Content types of responses are equal.'));
  208. }
  209. }