Project

General

Profile

Paste
Download (13.9 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / tests / views_view.test @ 6de61b86

1
<?php
2

    
3
/**
4
 * @file
5
 * Views class tests.
6
 */
7

    
8
/**
9
 *
10
 */
11
class ViewsViewTest extends ViewsSqlTest {
12

    
13
  /**
14
   * Provide the test's meta information.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Test the view class',
19
      'description' => 'Tests some functionality of the view class',
20
      'group' => 'Views',
21
    );
22
  }
23

    
24
  /**
25
   * Ensure that every kind of heavy objects are removed by the destructor.
26
   */
27
  function testDestroy() {
28
    $view = $this->view_test_destroy();
29

    
30
    $view->preview();
31
    $view->destroy();
32

    
33
    $this->assertViewDestroy($view);
34

    
35
    // Manipulate the display variable to test a previous bug.
36
    $view = $this->view_test_destroy();
37
    $view->preview();
38

    
39
    $view->destroy();
40
    $this->assertViewDestroy($view);
41
  }
42

    
43
  /**
44
   * Assert that a given view is destroyed properly.
45
   *
46
   * @param object $view
47
   *   The view to destroy.
48
   */
49
  function assertViewDestroy($view) {
50
    $this->assertFalse(isset($view->display['default']->handler), 'Make sure all displays are destroyed.');
51
    $this->assertFalse(isset($view->display['attachment_1']->handler), 'Make sure all displays are destroyed.');
52

    
53
    $this->assertFalse(isset($view->filter), 'Make sure all handlers are destroyed');
54
    $this->assertFalse(isset($view->field), 'Make sure all handlers are destroyed');
55
    $this->assertFalse(isset($view->argument), 'Make sure all handlers are destroyed');
56
    $this->assertFalse(isset($view->relationship), 'Make sure all handlers are destroyed');
57
    $this->assertFalse(isset($view->sort), 'Make sure all handlers are destroyed');
58
    $this->assertFalse(isset($view->area), 'Make sure all handlers are destroyed');
59

    
60
    $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');
61
    foreach ($keys as $key) {
62
      $this->assertFalse(isset($view->{$key}), $key);
63
    }
64
    $this->assertEqual($view->built, FALSE);
65
    $this->assertEqual($view->executed, FALSE);
66
    $this->assertEqual($view->build_info, array());
67
    $this->assertEqual($view->attachment_before, '');
68
    $this->assertEqual($view->attachment_after, '');
69
  }
70

    
71
  /**
72
   * Test deleting a view.
73
   */
74
  function testDelete() {
75
    // Delete a database view.
76
    $view = $this->view_test_delete();
77
    $view->save();
78
    $view = views_get_view($view->name);
79
    $view->delete();
80

    
81
    $view = views_get_view($view->name);
82
    $this->assertNotNull($view, 'Make sure that the old view is still in the static cache.');
83

    
84
    $view = views_get_view($view->name, TRUE);
85
    $this->assertNull($view, "Make sure that the old view gets cleared by the reset parameter.");
86
  }
87

    
88
  /**
89
   * Test validation.
90
   */
91
  function testValidate() {
92
    // Test a view with multiple displays.
93
    // Validating a view shouldn't change the active display.
94
    // @todo Create an extra validation view.
95
    $view = $this->view_test_destroy();
96
    $view->set_display('page_1');
97

    
98
    $view->validate();
99

    
100
    $this->assertEqual('page_1', $view->current_display, "The display should be constant while validating");
101

    
102
    // @todo Write real tests for the validation.
103
    // In general the following things could be tested:
104
    // - Deleted displays shouldn't be validated.
105
    // - Multiple displays are validating and the errors are merged together.
106
  }
107

    
108
  /**
109
   * Generate an example view.
110
   *
111
   * Includes:
112
   *   filters, fields, arguments, relationships, sorts, areas and attachments.
113
   */
114
  function view_test_destroy() {
115
    $view = new view();
116
    $view->name = 'test_destroy';
117
    $view->description = '';
118
    $view->tag = '';
119
    $view->base_table = 'node';
120
    $view->human_name = '';
121
    $view->api_version = '3.0-alpha1';
122
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
123

    
124
    /* Display: Master */
125
    $handler = $view->new_display('default', 'Master', 'default');
126
    $handler->display->display_options['access']['type'] = 'none';
127
    $handler->display->display_options['cache']['type'] = 'none';
128
    $handler->display->display_options['query']['type'] = 'views_query';
129
    $handler->display->display_options['exposed_form']['type'] = 'basic';
130
    $handler->display->display_options['pager']['type'] = 'full';
131
    $handler->display->display_options['style_plugin'] = 'default';
132
    $handler->display->display_options['row_plugin'] = 'fields';
133
    /* Header: Global: Text area */
134
    $handler->display->display_options['header']['area']['id'] = 'area';
135
    $handler->display->display_options['header']['area']['table'] = 'views';
136
    $handler->display->display_options['header']['area']['field'] = 'area';
137
    $handler->display->display_options['header']['area']['empty'] = FALSE;
138
    /* Header: Global: Text area */
139
    $handler->display->display_options['header']['area_1']['id'] = 'area_1';
140
    $handler->display->display_options['header']['area_1']['table'] = 'views';
141
    $handler->display->display_options['header']['area_1']['field'] = 'area';
142
    $handler->display->display_options['header']['area_1']['empty'] = FALSE;
143
    /* Footer: Global: Text area */
144
    $handler->display->display_options['footer']['area']['id'] = 'area';
145
    $handler->display->display_options['footer']['area']['table'] = 'views';
146
    $handler->display->display_options['footer']['area']['field'] = 'area';
147
    $handler->display->display_options['footer']['area']['empty'] = FALSE;
148
    /* Footer: Global: Text area */
149
    $handler->display->display_options['footer']['area_1']['id'] = 'area_1';
150
    $handler->display->display_options['footer']['area_1']['table'] = 'views';
151
    $handler->display->display_options['footer']['area_1']['field'] = 'area';
152
    $handler->display->display_options['footer']['area_1']['empty'] = FALSE;
153
    /* Empty text: Global: Text area */
154
    $handler->display->display_options['empty']['area']['id'] = 'area';
155
    $handler->display->display_options['empty']['area']['table'] = 'views';
156
    $handler->display->display_options['empty']['area']['field'] = 'area';
157
    $handler->display->display_options['empty']['area']['empty'] = FALSE;
158
    /* Empty text: Global: Text area */
159
    $handler->display->display_options['empty']['area_1']['id'] = 'area_1';
160
    $handler->display->display_options['empty']['area_1']['table'] = 'views';
161
    $handler->display->display_options['empty']['area_1']['field'] = 'area';
162
    $handler->display->display_options['empty']['area_1']['empty'] = FALSE;
163
    /* Relationship: Comment: Node */
164
    $handler->display->display_options['relationships']['nid']['id'] = 'nid';
165
    $handler->display->display_options['relationships']['nid']['table'] = 'comments';
166
    $handler->display->display_options['relationships']['nid']['field'] = 'nid';
167
    /* Relationship: Comment: Parent comment */
168
    $handler->display->display_options['relationships']['pid']['id'] = 'pid';
169
    $handler->display->display_options['relationships']['pid']['table'] = 'comments';
170
    $handler->display->display_options['relationships']['pid']['field'] = 'pid';
171
    /* Relationship: Comment: User */
172
    $handler->display->display_options['relationships']['uid']['id'] = 'uid';
173
    $handler->display->display_options['relationships']['uid']['table'] = 'comments';
174
    $handler->display->display_options['relationships']['uid']['field'] = 'uid';
175
    /* Field: Content: Nid */
176
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
177
    $handler->display->display_options['fields']['nid']['table'] = 'node';
178
    $handler->display->display_options['fields']['nid']['field'] = 'nid';
179
    /* Field: Content: Path */
180
    $handler->display->display_options['fields']['path']['id'] = 'path';
181
    $handler->display->display_options['fields']['path']['table'] = 'node';
182
    $handler->display->display_options['fields']['path']['field'] = 'path';
183
    /* Field: Content: Post date */
184
    $handler->display->display_options['fields']['created']['id'] = 'created';
185
    $handler->display->display_options['fields']['created']['table'] = 'node';
186
    $handler->display->display_options['fields']['created']['field'] = 'created';
187
    /* Sort criterion: Content: In moderation */
188
    $handler->display->display_options['sorts']['moderate']['id'] = 'moderate';
189
    $handler->display->display_options['sorts']['moderate']['table'] = 'node';
190
    $handler->display->display_options['sorts']['moderate']['field'] = 'moderate';
191
    /* Sort criterion: Content: Last comment author */
192
    $handler->display->display_options['sorts']['last_comment_name']['id'] = 'last_comment_name';
193
    $handler->display->display_options['sorts']['last_comment_name']['table'] = 'node_comment_statistics';
194
    $handler->display->display_options['sorts']['last_comment_name']['field'] = 'last_comment_name';
195
    /* Sort criterion: Content: Last comment time */
196
    $handler->display->display_options['sorts']['last_comment_timestamp']['id'] = 'last_comment_timestamp';
197
    $handler->display->display_options['sorts']['last_comment_timestamp']['table'] = 'node_comment_statistics';
198
    $handler->display->display_options['sorts']['last_comment_timestamp']['field'] = 'last_comment_timestamp';
199
    /* Argument: Content: Created date */
200
    $handler->display->display_options['arguments']['created_fulldate']['id'] = 'created_fulldate';
201
    $handler->display->display_options['arguments']['created_fulldate']['table'] = 'node';
202
    $handler->display->display_options['arguments']['created_fulldate']['field'] = 'created_fulldate';
203
    $handler->display->display_options['arguments']['created_fulldate']['style_plugin'] = 'default_summary';
204
    $handler->display->display_options['arguments']['created_fulldate']['default_argument_type'] = 'fixed';
205
    /* Argument: Content: Created day */
206
    $handler->display->display_options['arguments']['created_day']['id'] = 'created_day';
207
    $handler->display->display_options['arguments']['created_day']['table'] = 'node';
208
    $handler->display->display_options['arguments']['created_day']['field'] = 'created_day';
209
    $handler->display->display_options['arguments']['created_day']['style_plugin'] = 'default_summary';
210
    $handler->display->display_options['arguments']['created_day']['default_argument_type'] = 'fixed';
211
    /* Argument: Content: Created month */
212
    $handler->display->display_options['arguments']['created_month']['id'] = 'created_month';
213
    $handler->display->display_options['arguments']['created_month']['table'] = 'node';
214
    $handler->display->display_options['arguments']['created_month']['field'] = 'created_month';
215
    $handler->display->display_options['arguments']['created_month']['style_plugin'] = 'default_summary';
216
    $handler->display->display_options['arguments']['created_month']['default_argument_type'] = 'fixed';
217
    /* Filter: Content: Nid */
218
    $handler->display->display_options['filters']['nid']['id'] = 'nid';
219
    $handler->display->display_options['filters']['nid']['table'] = 'node';
220
    $handler->display->display_options['filters']['nid']['field'] = 'nid';
221
    /* Filter: Content: Published */
222
    $handler->display->display_options['filters']['status']['id'] = 'status';
223
    $handler->display->display_options['filters']['status']['table'] = 'node';
224
    $handler->display->display_options['filters']['status']['field'] = 'status';
225
    /* Filter: Content: Title */
226
    $handler->display->display_options['filters']['title']['id'] = 'title';
227
    $handler->display->display_options['filters']['title']['table'] = 'node';
228
    $handler->display->display_options['filters']['title']['field'] = 'title';
229

    
230
    /* Display: Page */
231
    $handler = $view->new_display('page', 'Page', 'page_1');
232
    $handler->display->display_options['path'] = 'test_destroy';
233

    
234
    /* Display: Attachment */
235
    $handler = $view->new_display('attachment', 'Attachment', 'attachment_1');
236
    $handler->display->display_options['pager']['type'] = 'some';
237
    $handler->display->display_options['displays'] = array(
238
      'default' => 'default',
239
      'page_1' => 'page_1',
240
    );
241

    
242
    /* Display: Attachment */
243
    $handler = $view->new_display('attachment', 'Attachment', 'attachment_2');
244
    $handler->display->display_options['pager']['type'] = 'some';
245
    $handler->display->display_options['displays'] = array(
246
      'default' => 'default',
247
      'page_1' => 'page_1',
248
    );
249

    
250
    // @codingStandardsIgnoreLine
251
    $translatables['test_destroy'] = array(
252
      t('Master'),
253
      t('more'),
254
      t('Apply'),
255
      t('Reset'),
256
      t('Sort By'),
257
      t('Asc'),
258
      t('Desc'),
259
      t('Items per page'),
260
      t('- All -'),
261
      t('Offset'),
262
      t('Text area'),
263
      t('Content'),
264
      t('Parent comment'),
265
      t('User'),
266
      t('Nid'),
267
      t('Path'),
268
      t('Post date'),
269
      t('All'),
270
      t('Page'),
271
      t('Attachment'),
272
    );
273

    
274
    return $view;
275
  }
276

    
277
  /**
278
   * Creates a test view.
279
   */
280
  function view_test_delete() {
281
    $view = new view();
282
    $view->name = 'test_view_delete';
283
    $view->description = '';
284
    $view->tag = '';
285
    $view->base_table = 'node';
286
    $view->human_name = 'test_view_delete';
287
    $view->core = 7;
288
    $view->api_version = '3.0-alpha1';
289
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
290

    
291
    /* Display: Defaults */
292
    $handler = $view->new_display('default', 'Defaults', 'default');
293
    $handler->display->display_options['access']['type'] = 'none';
294
    $handler->display->display_options['cache']['type'] = 'none';
295
    $handler->display->display_options['query']['type'] = 'views_query';
296
    $handler->display->display_options['exposed_form']['type'] = 'basic';
297
    $handler->display->display_options['pager']['type'] = 'full';
298
    $handler->display->display_options['style_plugin'] = 'default';
299
    $handler->display->display_options['row_plugin'] = 'fields';
300

    
301
    // @codingStandardsIgnoreLine
302
    $translatables['test_view_delete'] = array(
303
      t('Defaults'),
304
      t('more'),
305
      t('Apply'),
306
      t('Reset'),
307
      t('Sort By'),
308
      t('Asc'),
309
      t('Desc'),
310
      t('Items per page'),
311
      t('- All -'),
312
      t('Offset'),
313
    );
314

    
315
    return $view;
316
  }
317

    
318
}