views_view.test 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. /**
  3. * @file
  4. * Views class tests.
  5. */
  6. /**
  7. *
  8. */
  9. class ViewsViewTest extends ViewsSqlTest {
  10. /**
  11. * Provide the test's meta information.
  12. */
  13. public static function getInfo() {
  14. return array(
  15. 'name' => 'Test the view class',
  16. 'description' => 'Tests some functionality of the view class',
  17. 'group' => 'Views',
  18. );
  19. }
  20. /**
  21. * Ensure that every kind of heavy objects are removed by the destructor.
  22. */
  23. function testDestroy() {
  24. $view = $this->view_test_destroy();
  25. $view->preview();
  26. $view->destroy();
  27. $this->assertViewDestroy($view);
  28. // Manipulate the display variable to test a previous bug.
  29. $view = $this->view_test_destroy();
  30. $view->preview();
  31. $view->destroy();
  32. $this->assertViewDestroy($view);
  33. }
  34. /**
  35. * Assert that a given view is destroyed properly.
  36. *
  37. * @param object $view
  38. * The view to destroy.
  39. */
  40. function assertViewDestroy($view) {
  41. $this->assertFalse(isset($view->display['default']->handler), 'Make sure all displays are destroyed.');
  42. $this->assertFalse(isset($view->display['attachment_1']->handler), 'Make sure all displays are destroyed.');
  43. $this->assertFalse(isset($view->filter), 'Make sure all handlers are destroyed');
  44. $this->assertFalse(isset($view->field), 'Make sure all handlers are destroyed');
  45. $this->assertFalse(isset($view->argument), 'Make sure all handlers are destroyed');
  46. $this->assertFalse(isset($view->relationship), 'Make sure all handlers are destroyed');
  47. $this->assertFalse(isset($view->sort), 'Make sure all handlers are destroyed');
  48. $this->assertFalse(isset($view->area), 'Make sure all handlers are destroyed');
  49. $keys = array('current_display', 'display_handler', 'field', 'argument', 'filter', 'sort', 'relationship', 'header', 'footer', 'empty', 'query', 'result', 'inited', 'style_plugin', 'plugin_name', 'exposed_data', 'exposed_input', 'many_to_one_tables');
  50. foreach ($keys as $key) {
  51. $this->assertFalse(isset($view->{$key}), $key);
  52. }
  53. $this->assertEqual($view->built, FALSE);
  54. $this->assertEqual($view->executed, FALSE);
  55. $this->assertEqual($view->build_info, array());
  56. $this->assertEqual($view->attachment_before, '');
  57. $this->assertEqual($view->attachment_after, '');
  58. }
  59. /**
  60. * Test deleting a view.
  61. */
  62. function testDelete() {
  63. // Delete a database view.
  64. $view = $this->view_test_delete();
  65. $view->save();
  66. $view = views_get_view($view->name);
  67. $view->delete();
  68. $view = views_get_view($view->name);
  69. $this->assertNotNull($view, 'Make sure that the old view is still in the static cache.');
  70. $view = views_get_view($view->name, TRUE);
  71. $this->assertNull($view, "Make sure that the old view gets cleared by the reset parameter.");
  72. }
  73. /**
  74. * Test validation.
  75. */
  76. function testValidate() {
  77. // Test a view with multiple displays.
  78. // Validating a view shouldn't change the active display.
  79. // @todo Create an extra validation view.
  80. $view = $this->view_test_destroy();
  81. $view->set_display('page_1');
  82. $view->validate();
  83. $this->assertEqual('page_1', $view->current_display, "The display should be constant while validating");
  84. // @todo Write real tests for the validation.
  85. // In general the following things could be tested:
  86. // - Deleted displays shouldn't be validated.
  87. // - Multiple displays are validating and the errors are merged together.
  88. }
  89. /**
  90. * Generate an example view.
  91. *
  92. * Includes:
  93. * filters, fields, arguments, relationships, sorts, areas and attachments.
  94. */
  95. function view_test_destroy() {
  96. $view = new view();
  97. $view->name = 'test_destroy';
  98. $view->description = '';
  99. $view->tag = '';
  100. $view->base_table = 'node';
  101. $view->human_name = '';
  102. $view->api_version = '3.0-alpha1';
  103. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  104. /* Display: Master */
  105. $handler = $view->new_display('default', 'Master', 'default');
  106. $handler->display->display_options['access']['type'] = 'none';
  107. $handler->display->display_options['cache']['type'] = 'none';
  108. $handler->display->display_options['query']['type'] = 'views_query';
  109. $handler->display->display_options['exposed_form']['type'] = 'basic';
  110. $handler->display->display_options['pager']['type'] = 'full';
  111. $handler->display->display_options['style_plugin'] = 'default';
  112. $handler->display->display_options['row_plugin'] = 'fields';
  113. /* Header: Global: Text area */
  114. $handler->display->display_options['header']['area']['id'] = 'area';
  115. $handler->display->display_options['header']['area']['table'] = 'views';
  116. $handler->display->display_options['header']['area']['field'] = 'area';
  117. $handler->display->display_options['header']['area']['empty'] = FALSE;
  118. /* Header: Global: Text area */
  119. $handler->display->display_options['header']['area_1']['id'] = 'area_1';
  120. $handler->display->display_options['header']['area_1']['table'] = 'views';
  121. $handler->display->display_options['header']['area_1']['field'] = 'area';
  122. $handler->display->display_options['header']['area_1']['empty'] = FALSE;
  123. /* Footer: Global: Text area */
  124. $handler->display->display_options['footer']['area']['id'] = 'area';
  125. $handler->display->display_options['footer']['area']['table'] = 'views';
  126. $handler->display->display_options['footer']['area']['field'] = 'area';
  127. $handler->display->display_options['footer']['area']['empty'] = FALSE;
  128. /* Footer: Global: Text area */
  129. $handler->display->display_options['footer']['area_1']['id'] = 'area_1';
  130. $handler->display->display_options['footer']['area_1']['table'] = 'views';
  131. $handler->display->display_options['footer']['area_1']['field'] = 'area';
  132. $handler->display->display_options['footer']['area_1']['empty'] = FALSE;
  133. /* Empty text: Global: Text area */
  134. $handler->display->display_options['empty']['area']['id'] = 'area';
  135. $handler->display->display_options['empty']['area']['table'] = 'views';
  136. $handler->display->display_options['empty']['area']['field'] = 'area';
  137. $handler->display->display_options['empty']['area']['empty'] = FALSE;
  138. /* Empty text: Global: Text area */
  139. $handler->display->display_options['empty']['area_1']['id'] = 'area_1';
  140. $handler->display->display_options['empty']['area_1']['table'] = 'views';
  141. $handler->display->display_options['empty']['area_1']['field'] = 'area';
  142. $handler->display->display_options['empty']['area_1']['empty'] = FALSE;
  143. /* Relationship: Comment: Node */
  144. $handler->display->display_options['relationships']['nid']['id'] = 'nid';
  145. $handler->display->display_options['relationships']['nid']['table'] = 'comments';
  146. $handler->display->display_options['relationships']['nid']['field'] = 'nid';
  147. /* Relationship: Comment: Parent comment */
  148. $handler->display->display_options['relationships']['pid']['id'] = 'pid';
  149. $handler->display->display_options['relationships']['pid']['table'] = 'comments';
  150. $handler->display->display_options['relationships']['pid']['field'] = 'pid';
  151. /* Relationship: Comment: User */
  152. $handler->display->display_options['relationships']['uid']['id'] = 'uid';
  153. $handler->display->display_options['relationships']['uid']['table'] = 'comments';
  154. $handler->display->display_options['relationships']['uid']['field'] = 'uid';
  155. /* Field: Content: Nid */
  156. $handler->display->display_options['fields']['nid']['id'] = 'nid';
  157. $handler->display->display_options['fields']['nid']['table'] = 'node';
  158. $handler->display->display_options['fields']['nid']['field'] = 'nid';
  159. /* Field: Content: Path */
  160. $handler->display->display_options['fields']['path']['id'] = 'path';
  161. $handler->display->display_options['fields']['path']['table'] = 'node';
  162. $handler->display->display_options['fields']['path']['field'] = 'path';
  163. /* Field: Content: Post date */
  164. $handler->display->display_options['fields']['created']['id'] = 'created';
  165. $handler->display->display_options['fields']['created']['table'] = 'node';
  166. $handler->display->display_options['fields']['created']['field'] = 'created';
  167. /* Sort criterion: Content: In moderation */
  168. $handler->display->display_options['sorts']['moderate']['id'] = 'moderate';
  169. $handler->display->display_options['sorts']['moderate']['table'] = 'node';
  170. $handler->display->display_options['sorts']['moderate']['field'] = 'moderate';
  171. /* Sort criterion: Content: Last comment author */
  172. $handler->display->display_options['sorts']['last_comment_name']['id'] = 'last_comment_name';
  173. $handler->display->display_options['sorts']['last_comment_name']['table'] = 'node_comment_statistics';
  174. $handler->display->display_options['sorts']['last_comment_name']['field'] = 'last_comment_name';
  175. /* Sort criterion: Content: Last comment time */
  176. $handler->display->display_options['sorts']['last_comment_timestamp']['id'] = 'last_comment_timestamp';
  177. $handler->display->display_options['sorts']['last_comment_timestamp']['table'] = 'node_comment_statistics';
  178. $handler->display->display_options['sorts']['last_comment_timestamp']['field'] = 'last_comment_timestamp';
  179. /* Argument: Content: Created date */
  180. $handler->display->display_options['arguments']['created_fulldate']['id'] = 'created_fulldate';
  181. $handler->display->display_options['arguments']['created_fulldate']['table'] = 'node';
  182. $handler->display->display_options['arguments']['created_fulldate']['field'] = 'created_fulldate';
  183. $handler->display->display_options['arguments']['created_fulldate']['style_plugin'] = 'default_summary';
  184. $handler->display->display_options['arguments']['created_fulldate']['default_argument_type'] = 'fixed';
  185. /* Argument: Content: Created day */
  186. $handler->display->display_options['arguments']['created_day']['id'] = 'created_day';
  187. $handler->display->display_options['arguments']['created_day']['table'] = 'node';
  188. $handler->display->display_options['arguments']['created_day']['field'] = 'created_day';
  189. $handler->display->display_options['arguments']['created_day']['style_plugin'] = 'default_summary';
  190. $handler->display->display_options['arguments']['created_day']['default_argument_type'] = 'fixed';
  191. /* Argument: Content: Created month */
  192. $handler->display->display_options['arguments']['created_month']['id'] = 'created_month';
  193. $handler->display->display_options['arguments']['created_month']['table'] = 'node';
  194. $handler->display->display_options['arguments']['created_month']['field'] = 'created_month';
  195. $handler->display->display_options['arguments']['created_month']['style_plugin'] = 'default_summary';
  196. $handler->display->display_options['arguments']['created_month']['default_argument_type'] = 'fixed';
  197. /* Filter: Content: Nid */
  198. $handler->display->display_options['filters']['nid']['id'] = 'nid';
  199. $handler->display->display_options['filters']['nid']['table'] = 'node';
  200. $handler->display->display_options['filters']['nid']['field'] = 'nid';
  201. /* Filter: Content: Published */
  202. $handler->display->display_options['filters']['status']['id'] = 'status';
  203. $handler->display->display_options['filters']['status']['table'] = 'node';
  204. $handler->display->display_options['filters']['status']['field'] = 'status';
  205. /* Filter: Content: Title */
  206. $handler->display->display_options['filters']['title']['id'] = 'title';
  207. $handler->display->display_options['filters']['title']['table'] = 'node';
  208. $handler->display->display_options['filters']['title']['field'] = 'title';
  209. /* Display: Page */
  210. $handler = $view->new_display('page', 'Page', 'page_1');
  211. $handler->display->display_options['path'] = 'test_destroy';
  212. /* Display: Attachment */
  213. $handler = $view->new_display('attachment', 'Attachment', 'attachment_1');
  214. $handler->display->display_options['pager']['type'] = 'some';
  215. $handler->display->display_options['displays'] = array(
  216. 'default' => 'default',
  217. 'page_1' => 'page_1',
  218. );
  219. /* Display: Attachment */
  220. $handler = $view->new_display('attachment', 'Attachment', 'attachment_2');
  221. $handler->display->display_options['pager']['type'] = 'some';
  222. $handler->display->display_options['displays'] = array(
  223. 'default' => 'default',
  224. 'page_1' => 'page_1',
  225. );
  226. // @codingStandardsIgnoreLine
  227. $translatables['test_destroy'] = array(
  228. t('Master'),
  229. t('more'),
  230. t('Apply'),
  231. t('Reset'),
  232. t('Sort By'),
  233. t('Asc'),
  234. t('Desc'),
  235. t('Items per page'),
  236. t('- All -'),
  237. t('Offset'),
  238. t('Text area'),
  239. t('Content'),
  240. t('Parent comment'),
  241. t('User'),
  242. t('Nid'),
  243. t('Path'),
  244. t('Post date'),
  245. t('All'),
  246. t('Page'),
  247. t('Attachment'),
  248. );
  249. return $view;
  250. }
  251. /**
  252. * Creates a test view.
  253. */
  254. function view_test_delete() {
  255. $view = new view();
  256. $view->name = 'test_view_delete';
  257. $view->description = '';
  258. $view->tag = '';
  259. $view->base_table = 'node';
  260. $view->human_name = 'test_view_delete';
  261. $view->core = 7;
  262. $view->api_version = '3.0-alpha1';
  263. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  264. /* Display: Defaults */
  265. $handler = $view->new_display('default', 'Defaults', 'default');
  266. $handler->display->display_options['access']['type'] = 'none';
  267. $handler->display->display_options['cache']['type'] = 'none';
  268. $handler->display->display_options['query']['type'] = 'views_query';
  269. $handler->display->display_options['exposed_form']['type'] = 'basic';
  270. $handler->display->display_options['pager']['type'] = 'full';
  271. $handler->display->display_options['style_plugin'] = 'default';
  272. $handler->display->display_options['row_plugin'] = 'fields';
  273. // @codingStandardsIgnoreLine
  274. $translatables['test_view_delete'] = array(
  275. t('Defaults'),
  276. t('more'),
  277. t('Apply'),
  278. t('Reset'),
  279. t('Sort By'),
  280. t('Asc'),
  281. t('Desc'),
  282. t('Items per page'),
  283. t('- All -'),
  284. t('Offset'),
  285. );
  286. return $view;
  287. }
  288. }