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