1
|
<?php
|
2
|
|
3
|
abstract class ViewsDataExportBaseTest extends ViewsTestCase {
|
4
|
|
5
|
protected function setUp() {
|
6
|
$modules = func_get_args();
|
7
|
if (isset($modules[0]) && is_array($modules[0])) {
|
8
|
$modules = $modules[0];
|
9
|
}
|
10
|
|
11
|
$modules[] = 'views_data_export';
|
12
|
$modules[] = 'views_ui';
|
13
|
$modules[] = 'views';
|
14
|
parent::setUp($modules);
|
15
|
|
16
|
// Define the schema and views data variable before enabling the test module.
|
17
|
variable_set('views_test_schema', $this->schemaDefinition());
|
18
|
variable_set('views_test_views_data', $this->viewsData());
|
19
|
variable_set('views_test_views_plugins', $this->viewsPlugins());
|
20
|
|
21
|
module_enable(array('views_test'));
|
22
|
$this->resetAll();
|
23
|
|
24
|
// Load the test dataset.
|
25
|
$data_set = $this->dataSet();
|
26
|
$query = db_insert('views_test')
|
27
|
->fields(array_keys($data_set[0]));
|
28
|
foreach ($data_set as $record) {
|
29
|
$query->values($record);
|
30
|
}
|
31
|
$query->execute();
|
32
|
$this->checkPermissions(array(), TRUE);
|
33
|
}
|
34
|
|
35
|
/**
|
36
|
* This function allows to enable views ui from a higher class which can't change the setup function anymore.
|
37
|
*
|
38
|
* @TODO
|
39
|
* Convert existing setUp functions.
|
40
|
*/
|
41
|
function enableViewsUi() {
|
42
|
module_enable(array('views_ui'));
|
43
|
// @TODO Figure out why it's required to clear the cache here.
|
44
|
views_module_include('views_default', TRUE);
|
45
|
views_get_all_views(TRUE);
|
46
|
menu_rebuild();
|
47
|
}
|
48
|
|
49
|
/**
|
50
|
* The schema definition.
|
51
|
*/
|
52
|
protected function schemaDefinition() {
|
53
|
$schema['views_test'] = array(
|
54
|
'description' => 'Basic test table for Views tests.',
|
55
|
'fields' => array(
|
56
|
'id' => array(
|
57
|
'type' => 'serial',
|
58
|
'unsigned' => TRUE,
|
59
|
'not null' => TRUE,
|
60
|
),
|
61
|
'name' => array(
|
62
|
'description' => "A person's name",
|
63
|
'type' => 'varchar',
|
64
|
'length' => 255,
|
65
|
'not null' => TRUE,
|
66
|
'default' => '',
|
67
|
),
|
68
|
'age' => array(
|
69
|
'description' => "The person's age",
|
70
|
'type' => 'int',
|
71
|
'unsigned' => TRUE,
|
72
|
'not null' => TRUE,
|
73
|
'default' => 0),
|
74
|
'job' => array(
|
75
|
'description' => "The person's job",
|
76
|
'type' => 'varchar',
|
77
|
'length' => 255,
|
78
|
'not null' => TRUE,
|
79
|
'default' => 'Undefined',
|
80
|
),
|
81
|
'created' => array(
|
82
|
'description' => "The creation date of this record",
|
83
|
'type' => 'int',
|
84
|
'unsigned' => TRUE,
|
85
|
'not null' => TRUE,
|
86
|
'default' => 0,
|
87
|
),
|
88
|
),
|
89
|
'primary key' => array('id'),
|
90
|
'unique keys' => array(
|
91
|
'name' => array('name')
|
92
|
),
|
93
|
'indexes' => array(
|
94
|
'ages' => array('age'),
|
95
|
),
|
96
|
);
|
97
|
return $schema;
|
98
|
}
|
99
|
|
100
|
/**
|
101
|
* The views data definition.
|
102
|
*/
|
103
|
protected function viewsData() {
|
104
|
// Declaration of the base table.
|
105
|
$data['views_test']['table'] = array(
|
106
|
'group' => t('Views test'),
|
107
|
'base' => array(
|
108
|
'field' => 'id',
|
109
|
'title' => t('Views test'),
|
110
|
'help' => t('Users who have created accounts on your site.'),
|
111
|
),
|
112
|
);
|
113
|
|
114
|
// Declaration of fields.
|
115
|
$data['views_test']['id'] = array(
|
116
|
'title' => t('ID'),
|
117
|
'help' => t('The test data ID'),
|
118
|
'field' => array(
|
119
|
'handler' => 'views_handler_field_numeric',
|
120
|
'click sortable' => TRUE,
|
121
|
),
|
122
|
'argument' => array(
|
123
|
'handler' => 'views_handler_argument_numeric',
|
124
|
),
|
125
|
'filter' => array(
|
126
|
'handler' => 'views_handler_filter_numeric',
|
127
|
),
|
128
|
'sort' => array(
|
129
|
'handler' => 'views_handler_sort',
|
130
|
),
|
131
|
);
|
132
|
$data['views_test']['name'] = array(
|
133
|
'title' => t('Name'),
|
134
|
'help' => t('The name of the person'),
|
135
|
'field' => array(
|
136
|
'handler' => 'views_handler_field',
|
137
|
'click sortable' => TRUE,
|
138
|
),
|
139
|
'argument' => array(
|
140
|
'handler' => 'views_handler_argument_string',
|
141
|
),
|
142
|
'filter' => array(
|
143
|
'handler' => 'views_handler_filter_string',
|
144
|
),
|
145
|
'sort' => array(
|
146
|
'handler' => 'views_handler_sort',
|
147
|
),
|
148
|
);
|
149
|
$data['views_test']['age'] = array(
|
150
|
'title' => t('Age'),
|
151
|
'help' => t('The age of the person'),
|
152
|
'field' => array(
|
153
|
'handler' => 'views_handler_field_numeric',
|
154
|
'click sortable' => TRUE,
|
155
|
),
|
156
|
'argument' => array(
|
157
|
'handler' => 'views_handler_argument_numeric',
|
158
|
),
|
159
|
'filter' => array(
|
160
|
'handler' => 'views_handler_filter_numeric',
|
161
|
),
|
162
|
'sort' => array(
|
163
|
'handler' => 'views_handler_sort',
|
164
|
),
|
165
|
);
|
166
|
$data['views_test']['job'] = array(
|
167
|
'title' => t('Job'),
|
168
|
'help' => t('The job of the person'),
|
169
|
'field' => array(
|
170
|
'handler' => 'views_handler_field',
|
171
|
'click sortable' => TRUE,
|
172
|
),
|
173
|
'argument' => array(
|
174
|
'handler' => 'views_handler_argument_string',
|
175
|
),
|
176
|
'filter' => array(
|
177
|
'handler' => 'views_handler_filter_string',
|
178
|
),
|
179
|
'sort' => array(
|
180
|
'handler' => 'views_handler_sort',
|
181
|
),
|
182
|
);
|
183
|
$data['views_test']['created'] = array(
|
184
|
'title' => t('Created'),
|
185
|
'help' => t('The creation date of this record'),
|
186
|
'field' => array(
|
187
|
'handler' => 'views_handler_field_date',
|
188
|
'click sortable' => TRUE,
|
189
|
),
|
190
|
'argument' => array(
|
191
|
'handler' => 'views_handler_argument_date',
|
192
|
),
|
193
|
'filter' => array(
|
194
|
'handler' => 'views_handler_filter_date',
|
195
|
),
|
196
|
'sort' => array(
|
197
|
'handler' => 'views_handler_sort_date',
|
198
|
),
|
199
|
);
|
200
|
return $data;
|
201
|
}
|
202
|
|
203
|
protected function viewsPlugins() {
|
204
|
return array();
|
205
|
}
|
206
|
|
207
|
/**
|
208
|
* A very simple test dataset.
|
209
|
*/
|
210
|
protected function dataSet() {
|
211
|
return array(
|
212
|
array(
|
213
|
'name' => 'John',
|
214
|
'age' => 25,
|
215
|
'job' => "Singer\r\nSongwriter\r\nPianist",
|
216
|
'created' => gmmktime(0, 0, 0, 1, 1, 2000),
|
217
|
),
|
218
|
array(
|
219
|
'name' => 'George',
|
220
|
'age' => 27,
|
221
|
'job' => "Singer\nGuitar player\nSitar player",
|
222
|
'created' => gmmktime(0, 0, 0, 1, 2, 2000),
|
223
|
),
|
224
|
array(
|
225
|
'name' => 'Ringo',
|
226
|
'age' => 28,
|
227
|
'job' => 'Drummer',
|
228
|
'created' => gmmktime(6, 30, 30, 1, 1, 2000),
|
229
|
),
|
230
|
array(
|
231
|
'name' => 'Paul',
|
232
|
'age' => 26,
|
233
|
'job' => "Songwriter\rBass guitarist",
|
234
|
'created' => gmmktime(6, 0, 0, 1, 1, 2000),
|
235
|
),
|
236
|
array(
|
237
|
'name' => 'Meredith',
|
238
|
'age' => 30,
|
239
|
'job' => 'Speaker',
|
240
|
'created' => gmmktime(6, 30, 10, 1, 1, 2000),
|
241
|
),
|
242
|
);
|
243
|
}
|
244
|
|
245
|
/**
|
246
|
* Build and return a basic view of the views_test table.
|
247
|
*
|
248
|
* @return view
|
249
|
*/
|
250
|
protected function getBasicView() {
|
251
|
views_include('view');
|
252
|
|
253
|
// Create the basic view.
|
254
|
$view = new view();
|
255
|
$view->vid = 'test_view';
|
256
|
$view->add_display('default');
|
257
|
$view->base_table = 'views_test';
|
258
|
|
259
|
// Set up the fields we need.
|
260
|
$display = $view->new_display('default', 'Master', 'default');
|
261
|
$display->override_option('fields', array(
|
262
|
'id' => array(
|
263
|
'id' => 'id',
|
264
|
'table' => 'views_test',
|
265
|
'field' => 'id',
|
266
|
'relationship' => 'none',
|
267
|
),
|
268
|
'name' => array(
|
269
|
'id' => 'name',
|
270
|
'table' => 'views_test',
|
271
|
'field' => 'name',
|
272
|
'relationship' => 'none',
|
273
|
),
|
274
|
'age' => array(
|
275
|
'id' => 'age',
|
276
|
'table' => 'views_test',
|
277
|
'field' => 'age',
|
278
|
'relationship' => 'none',
|
279
|
),
|
280
|
));
|
281
|
|
282
|
// Set up the sort order.
|
283
|
$display->override_option('sorts', array(
|
284
|
'id' => array(
|
285
|
'order' => 'ASC',
|
286
|
'id' => 'id',
|
287
|
'table' => 'views_test',
|
288
|
'field' => 'id',
|
289
|
'relationship' => 'none',
|
290
|
),
|
291
|
));
|
292
|
|
293
|
// Set up the pager.
|
294
|
$display->override_option('pager', array(
|
295
|
'type' => 'none',
|
296
|
'options' => array('offset' => 0),
|
297
|
));
|
298
|
|
299
|
return $view;
|
300
|
}
|
301
|
|
302
|
public function logViewResult($result, $prefix = 'View result:<br>') {
|
303
|
$this->verbose($prefix . '<br><pre>' . check_plain($result) .'</pre>');
|
304
|
}
|
305
|
|
306
|
public function assertBatchedExportEqual($path, $expected, $message) {
|
307
|
$this->drupalGet($path);
|
308
|
$output = $this->drupalGetContent();
|
309
|
$this->logViewResult($output);
|
310
|
$this->logViewResult($expected, 'Expected result:<br>');
|
311
|
$this->assertEqual($this->normaliseString($output), $this->normaliseString($expected), $message);
|
312
|
}
|
313
|
|
314
|
public function assertExportEqual($a, $b, $message) {
|
315
|
$this->logViewResult($a);
|
316
|
$this->logViewResult($b, 'Expected result:<br>');
|
317
|
$this->assertEqual($this->normaliseString($a), $this->normaliseString($b), $message);
|
318
|
}
|
319
|
|
320
|
protected function normaliseString($s) {
|
321
|
// Normalize line endings
|
322
|
// Convert all line-endings to UNIX format
|
323
|
$s = str_replace("\r\n", "\n", $s);
|
324
|
$s = str_replace("\r", "\n", $s);
|
325
|
$s = trim($s);
|
326
|
return $s;
|
327
|
}
|
328
|
|
329
|
}
|
330
|
|
331
|
abstract class ViewsDataExportSimpleExportTest extends ViewsDataExportBaseTest {
|
332
|
|
333
|
protected $vde_export_type;
|
334
|
|
335
|
/**
|
336
|
* Tests the non-batched export functionality for this style.
|
337
|
*/
|
338
|
public function testNonBatchedExport() {
|
339
|
$path = 'vde_test/' . $this->randomName();
|
340
|
list($view, $expected) = $this->getExportView($path);
|
341
|
// Save this view so we can hit the path.
|
342
|
$view->save();
|
343
|
// Ensure that the menu router system is rebuilt on the next page load.
|
344
|
variable_set('menu_rebuild_needed', TRUE);
|
345
|
|
346
|
$this->drupalGet($path);
|
347
|
$result = $this->drupalGetContent();
|
348
|
|
349
|
$this->assertExportEqual($result, $expected, 'Non batched ' . $this->vde_export_type . ' export matched expected output.');
|
350
|
}
|
351
|
|
352
|
/**
|
353
|
* Tests the batched export functionality for this style.
|
354
|
*/
|
355
|
public function testBatchedExport() {
|
356
|
$path = 'vde_test/' . $this->randomName();
|
357
|
list($view, $expected) = $this->getExportView($path);
|
358
|
$display = &$view->display['vde_test']->handler;
|
359
|
// Set this view to be batched.
|
360
|
$display->override_option('use_batch', 'batch');
|
361
|
// Save this view so we can hit the path.
|
362
|
$view->save();
|
363
|
// Ensure that the menu router system is rebuilt on the next page load.
|
364
|
variable_set('menu_rebuild_needed', TRUE);
|
365
|
|
366
|
$this->assertBatchedExportEqual($path, $expected, 'Batched ' . $this->vde_export_type . ' export matched expected output.');
|
367
|
}
|
368
|
|
369
|
/**
|
370
|
* Get a very basic view and expected output for this style.
|
371
|
*
|
372
|
* @return
|
373
|
* An array containing two elements:
|
374
|
* - A View object, for the export.
|
375
|
* - The expected out from that view, if is was executed without further
|
376
|
* changes.
|
377
|
*/
|
378
|
abstract protected function getExportView($path = 'vde_test');
|
379
|
|
380
|
/**
|
381
|
* Build and return a basic view of the views_test table.
|
382
|
*
|
383
|
* @return view
|
384
|
*/
|
385
|
protected function getBasicExportView() {
|
386
|
views_include('view');
|
387
|
|
388
|
// Create the basic view.
|
389
|
$view = new view();
|
390
|
$view->vid = 'new';
|
391
|
$view->base_table = 'views_test';
|
392
|
|
393
|
// Set up the fields we need.
|
394
|
$display = $view->new_display('default', 'Master', 'default');
|
395
|
|
396
|
$display->override_option('fields', array(
|
397
|
'id' => array(
|
398
|
'id' => 'id',
|
399
|
'table' => 'views_test',
|
400
|
'field' => 'id',
|
401
|
'relationship' => 'none',
|
402
|
),
|
403
|
'name' => array(
|
404
|
'id' => 'name',
|
405
|
'table' => 'views_test',
|
406
|
'field' => 'name',
|
407
|
'relationship' => 'none',
|
408
|
),
|
409
|
'age' => array(
|
410
|
'id' => 'age',
|
411
|
'table' => 'views_test',
|
412
|
'field' => 'age',
|
413
|
'relationship' => 'none',
|
414
|
),
|
415
|
));
|
416
|
|
417
|
// Set up the sort order.
|
418
|
$display->override_option('sorts', array(
|
419
|
'id' => array(
|
420
|
'order' => 'ASC',
|
421
|
'id' => 'id',
|
422
|
'table' => 'views_test',
|
423
|
'field' => 'id',
|
424
|
'relationship' => 'none',
|
425
|
),
|
426
|
));
|
427
|
|
428
|
// Set up the pager.
|
429
|
$display->override_option('pager', array(
|
430
|
'type' => 'none',
|
431
|
'options' => array('offset' => 0),
|
432
|
));
|
433
|
|
434
|
return $view;
|
435
|
}
|
436
|
|
437
|
/**
|
438
|
* Get a view that's our basic view, but with hide if empty/0 support.
|
439
|
*
|
440
|
* We add this to the 'age' field.
|
441
|
*/
|
442
|
protected function getHideIfEmptyExportView() {
|
443
|
$view = $this->getBasicExportView();
|
444
|
|
445
|
$display = $view->display['default']->handler;
|
446
|
|
447
|
$display->override_option('fields', array(
|
448
|
'id' => array(
|
449
|
'id' => 'id',
|
450
|
'table' => 'views_test',
|
451
|
'field' => 'id',
|
452
|
'relationship' => 'none',
|
453
|
),
|
454
|
'name' => array(
|
455
|
'id' => 'name',
|
456
|
'table' => 'views_test',
|
457
|
'field' => 'name',
|
458
|
'relationship' => 'none',
|
459
|
// Hide their name if its empty.
|
460
|
'hide_empty' => TRUE,
|
461
|
// But we don't hide it if it's: 0.
|
462
|
'empty_zero' => FALSE,
|
463
|
),
|
464
|
'age' => array(
|
465
|
'id' => 'age',
|
466
|
'table' => 'views_test',
|
467
|
'field' => 'age',
|
468
|
'relationship' => 'none',
|
469
|
// Hide their age if it's empty.
|
470
|
'hide_empty' => TRUE,
|
471
|
'empty_zero' => TRUE,
|
472
|
),
|
473
|
));
|
474
|
|
475
|
return $view;
|
476
|
}
|
477
|
|
478
|
/**
|
479
|
* Execute a given view very simply.
|
480
|
*
|
481
|
* This will take a view, and add a display plugin of the correct export type,
|
482
|
* and then run it and compare it with the expected output.
|
483
|
*/
|
484
|
protected function executeAndCompareGivenView(view $view, $expected, $message = '', $style_options = array()) {
|
485
|
$path = 'vde_test/' . $this->randomName();
|
486
|
|
487
|
$display = $view->new_display('views_data_export', 'Data export', 'vde_test');
|
488
|
$display->override_option('style_plugin', $this->getStylePluginName());
|
489
|
$display->override_option('style_options', $style_options);
|
490
|
$display->override_option('path', $path);
|
491
|
|
492
|
// Save this view so we can hit the path.
|
493
|
$view->save();
|
494
|
|
495
|
// Ensure that the menu router system is rebuilt on the next page load.
|
496
|
variable_set('menu_rebuild_needed', TRUE);
|
497
|
|
498
|
$this->drupalGet($path);
|
499
|
$result = $this->drupalGetContent();
|
500
|
|
501
|
$this->assertExportEqual($result, $expected, $message);
|
502
|
}
|
503
|
|
504
|
/**
|
505
|
* Return the name of the style plugin represented by this test.
|
506
|
*/
|
507
|
abstract protected function getStylePluginName();
|
508
|
}
|