views_clone.test 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. <?php
  2. /**
  3. * Tests cloning a view.
  4. */
  5. class ViewsCloneTest extends ViewsSqlTest {
  6. /**
  7. * Provide the test's meta information.
  8. */
  9. public static function getInfo() {
  10. return array(
  11. 'name' => 'Test cloning a view',
  12. 'description' => 'Tests clone_view method of views class',
  13. 'group' => 'Views',
  14. );
  15. }
  16. /**
  17. * Returns a new term with random properties in vocabulary $vocabulary.
  18. */
  19. protected function createTerm($vocabulary) {
  20. $term = new stdClass();
  21. $term->name = $this->randomName();
  22. $term->description = $this->randomName();
  23. // Use the first available text format.
  24. $term->format = db_query_range('SELECT format FROM {filter_format}', 0, 1)->fetchField();
  25. $term->vid = $vocabulary->vid;
  26. taxonomy_term_save($term);
  27. return $term;
  28. }
  29. /**
  30. * {@inheritdoc}
  31. */
  32. public function setUp() {
  33. parent::setUp();
  34. $vocabulary = taxonomy_vocabulary_machine_name_load('tags');
  35. $this->term = $this->createTerm($vocabulary);
  36. $node = array();
  37. $node['type'] = 'article';
  38. $node['field_tags'][LANGUAGE_NONE][]['tid'] = $this->term->tid;
  39. $this->node = $this->drupalCreateNode($node);
  40. }
  41. /**
  42. * Test cloning a view.
  43. */
  44. public function testClone() {
  45. // Prepare view to be cloned.
  46. $view = $this->getTestCloneView();
  47. $view->set_arguments(array(
  48. 0 => $this->node->nid,
  49. ));
  50. $view->set_exposed_input(array(
  51. 'field_tags_tid' => $this->term->tid,
  52. ));
  53. // Execute view to be cloned.
  54. $result = $view->execute();
  55. // To make sure that we are properly testing removal of all properties, we
  56. // first need to assert that they are actually present in the original view.
  57. $keys = array(
  58. 'current_display',
  59. 'display_handler',
  60. 'field',
  61. 'argument',
  62. 'filter',
  63. 'sort',
  64. 'relationship',
  65. 'header',
  66. 'footer',
  67. 'empty',
  68. 'query',
  69. 'inited',
  70. 'style_plugin',
  71. 'plugin_name',
  72. 'exposed_data',
  73. 'exposed_input',
  74. 'exposed_widgets',
  75. 'many_to_one_aliases',
  76. 'many_to_one_tables',
  77. 'feed_icon',
  78. );
  79. foreach ($keys as $key) {
  80. $this->assertTrue(isset($view->{$key}), $key . 'is set in original view.');
  81. }
  82. $this->assertTrue($view->built, 'Assert original view built.');
  83. $this->assertTrue($view->executed, 'Assert original view executed.');
  84. $this->assertNotEqual($view->build_info, array(), 'Assert original view has build_info.');
  85. $this->assertNotEqual($view->attachment_before, '', 'Assert original view has attachment_before.');
  86. $this->assertNotEqual($view->attachment_after, '', 'Assert original view has attachment_after.');
  87. $this->assertNotEqual($view->result, array(), 'Assert original view has result.');
  88. // Clone view.
  89. $clone = $view->clone_view();
  90. // Assert that all relevant properties have been removed or reset.
  91. $keys = array(
  92. 'current_display',
  93. 'display_handler',
  94. 'field',
  95. 'argument',
  96. 'filter',
  97. 'sort',
  98. 'relationship',
  99. 'header',
  100. 'footer',
  101. 'empty',
  102. 'query',
  103. 'inited',
  104. 'style_plugin',
  105. 'plugin_name',
  106. 'exposed_data',
  107. 'exposed_input',
  108. 'exposed_widgets',
  109. 'many_to_one_aliases',
  110. 'many_to_one_tables',
  111. 'feed_icon',
  112. );
  113. foreach ($keys as $key) {
  114. $this->assertFalse(isset($clone->{$key}), $key . ' has been removed in cloned view.');
  115. }
  116. foreach ($clone->display as $id => $display) {
  117. $this->assertFalse(isset($clone->display[$id]->handler), 'Make sure all display handlers have been destroyed.');
  118. }
  119. $this->assertFalse($clone->built, 'Assert cloned view not built.');
  120. $this->assertFalse($clone->executed, 'Assert cloned view not executed.');
  121. $this->assertEqual($clone->build_info, array(), 'Assert cloned view has empty build_info.');
  122. $this->assertEqual($clone->attachment_before, '', 'Assert cloned view has empty attachment_before.');
  123. $this->assertEqual($clone->attachment_after, '', 'Assert cloned view has empty attachment_after.');
  124. $this->assertEqual($clone->result, array(), 'Assert cloned view has empty result.');
  125. // Execute cloned view.
  126. $clone->execute();
  127. // Assert result sets are equal.
  128. $this->assertEqual($view->result, $clone->result, 'Result sets of cloned view and original view match.');
  129. }
  130. /**
  131. * Generate test_clone view.
  132. */
  133. protected function getTestCloneView() {
  134. $view = new view();
  135. $view->name = 'test_clone';
  136. $view->description = '';
  137. $view->tag = 'default';
  138. $view->base_table = 'node';
  139. $view->human_name = 'test_clone';
  140. $view->core = 7;
  141. $view->api_version = '3.0';
  142. $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
  143. /* Display: Master */
  144. $handler = $view->new_display('default', 'Master', 'default');
  145. $handler->display->display_options['title'] = 'test_clone';
  146. $handler->display->display_options['use_more_always'] = FALSE;
  147. $handler->display->display_options['access']['type'] = 'perm';
  148. $handler->display->display_options['cache']['type'] = 'none';
  149. $handler->display->display_options['query']['type'] = 'views_query';
  150. $handler->display->display_options['exposed_form']['type'] = 'basic';
  151. $handler->display->display_options['pager']['type'] = 'full';
  152. $handler->display->display_options['pager']['options']['items_per_page'] = '10';
  153. $handler->display->display_options['style_plugin'] = 'default';
  154. $handler->display->display_options['row_plugin'] = 'node';
  155. /* Header: Global: Text area */
  156. $handler->display->display_options['header']['area']['id'] = 'area';
  157. $handler->display->display_options['header']['area']['table'] = 'views';
  158. $handler->display->display_options['header']['area']['field'] = 'area';
  159. $handler->display->display_options['header']['area']['label'] = 'Header';
  160. $handler->display->display_options['header']['area']['content'] = 'Header';
  161. $handler->display->display_options['header']['area']['format'] = 'filtered_html';
  162. /* Footer: Global: Text area */
  163. $handler->display->display_options['footer']['area']['id'] = 'area';
  164. $handler->display->display_options['footer']['area']['table'] = 'views';
  165. $handler->display->display_options['footer']['area']['field'] = 'area';
  166. $handler->display->display_options['footer']['area']['label'] = 'Footer';
  167. $handler->display->display_options['footer']['area']['content'] = 'Footer';
  168. $handler->display->display_options['footer']['area']['format'] = 'filtered_html';
  169. /* No results behavior: Global: Text area */
  170. $handler->display->display_options['empty']['area']['id'] = 'area';
  171. $handler->display->display_options['empty']['area']['table'] = 'views';
  172. $handler->display->display_options['empty']['area']['field'] = 'area';
  173. $handler->display->display_options['empty']['area']['label'] = 'Empty';
  174. $handler->display->display_options['empty']['area']['empty'] = TRUE;
  175. $handler->display->display_options['empty']['area']['content'] = 'Empty';
  176. $handler->display->display_options['empty']['area']['format'] = 'filtered_html';
  177. /* Relationship: Comment: Last Comment */
  178. $handler->display->display_options['relationships']['cid']['id'] = 'cid';
  179. $handler->display->display_options['relationships']['cid']['table'] = 'node_comment_statistics';
  180. $handler->display->display_options['relationships']['cid']['field'] = 'cid';
  181. /* Field: Content: Title */
  182. $handler->display->display_options['fields']['title']['id'] = 'title';
  183. $handler->display->display_options['fields']['title']['table'] = 'node';
  184. $handler->display->display_options['fields']['title']['field'] = 'title';
  185. $handler->display->display_options['fields']['title']['label'] = '';
  186. $handler->display->display_options['fields']['title']['alter']['word_boundary'] = FALSE;
  187. $handler->display->display_options['fields']['title']['alter']['ellipsis'] = FALSE;
  188. /* Sort criterion: Content: Post date */
  189. $handler->display->display_options['sorts']['created']['id'] = 'created';
  190. $handler->display->display_options['sorts']['created']['table'] = 'node';
  191. $handler->display->display_options['sorts']['created']['field'] = 'created';
  192. $handler->display->display_options['sorts']['created']['order'] = 'DESC';
  193. /* Contextual filter: Content: Nid */
  194. $handler->display->display_options['arguments']['nid']['id'] = 'nid';
  195. $handler->display->display_options['arguments']['nid']['table'] = 'node';
  196. $handler->display->display_options['arguments']['nid']['field'] = 'nid';
  197. $handler->display->display_options['arguments']['nid']['default_argument_type'] = 'fixed';
  198. $handler->display->display_options['arguments']['nid']['summary']['number_of_records'] = '0';
  199. $handler->display->display_options['arguments']['nid']['summary']['format'] = 'default_summary';
  200. $handler->display->display_options['arguments']['nid']['summary_options']['items_per_page'] = '25';
  201. /* Filter criterion: 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. $handler->display->display_options['filters']['status']['value'] = 'All';
  206. $handler->display->display_options['filters']['status']['group'] = 1;
  207. $handler->display->display_options['filters']['status']['exposed'] = TRUE;
  208. $handler->display->display_options['filters']['status']['expose']['operator_id'] = '';
  209. $handler->display->display_options['filters']['status']['expose']['label'] = 'Published';
  210. $handler->display->display_options['filters']['status']['expose']['operator'] = 'status_op';
  211. $handler->display->display_options['filters']['status']['expose']['identifier'] = 'status';
  212. $handler->display->display_options['filters']['status']['expose']['remember_roles'] = array(
  213. 2 => '2',
  214. );
  215. /* Filter criterion: Content: Tags (field_tags) */
  216. $handler->display->display_options['filters']['field_tags_tid']['id'] = 'field_tags_tid';
  217. $handler->display->display_options['filters']['field_tags_tid']['table'] = 'field_data_field_tags';
  218. $handler->display->display_options['filters']['field_tags_tid']['field'] = 'field_tags_tid';
  219. $handler->display->display_options['filters']['field_tags_tid']['exposed'] = TRUE;
  220. $handler->display->display_options['filters']['field_tags_tid']['expose']['operator_id'] = 'field_tags_tid_op';
  221. $handler->display->display_options['filters']['field_tags_tid']['expose']['label'] = 'Tags (field_tags)';
  222. $handler->display->display_options['filters']['field_tags_tid']['expose']['operator'] = 'field_tags_tid_op';
  223. $handler->display->display_options['filters']['field_tags_tid']['expose']['identifier'] = 'field_tags_tid';
  224. $handler->display->display_options['filters']['field_tags_tid']['expose']['remember_roles'] = array(
  225. 2 => '2',
  226. );
  227. $handler->display->display_options['filters']['field_tags_tid']['reduce_duplicates'] = TRUE;
  228. $handler->display->display_options['filters']['field_tags_tid']['type'] = 'select';
  229. $handler->display->display_options['filters']['field_tags_tid']['vocabulary'] = 'tags';
  230. /* Display: Page */
  231. $handler = $view->new_display('page', 'Page', 'page');
  232. $handler->display->display_options['path'] = 'test-clone';
  233. /* Display: attachment_before */
  234. $handler = $view->new_display('attachment', 'attachment_before', 'attachment_1');
  235. $handler->display->display_options['pager']['type'] = 'some';
  236. $handler->display->display_options['displays'] = array(
  237. 'default' => 'default',
  238. 'page' => 'page',
  239. );
  240. $handler->display->display_options['inherit_exposed_filters'] = TRUE;
  241. /* Display: attachment_after */
  242. $handler = $view->new_display('attachment', 'attachment_after', 'attachment_2');
  243. $handler->display->display_options['pager']['type'] = 'some';
  244. $handler->display->display_options['displays'] = array(
  245. 'default' => 'default',
  246. 'page' => 'page',
  247. );
  248. $handler->display->display_options['attachment_position'] = 'after';
  249. $handler->display->display_options['inherit_exposed_filters'] = TRUE;
  250. /* Display: Feed */
  251. $handler = $view->new_display('feed', 'Feed', 'feed_1');
  252. $handler->display->display_options['pager']['type'] = 'some';
  253. $handler->display->display_options['style_plugin'] = 'rss';
  254. $handler->display->display_options['row_plugin'] = 'node_rss';
  255. $handler->display->display_options['path'] = 'test_clone/rss';
  256. $handler->display->display_options['displays'] = array(
  257. 'default' => 'default',
  258. 'page' => 'page',
  259. );
  260. return $view;
  261. }
  262. }