1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Tests the fieldapi integration of viewsdata.
|
6
|
*/
|
7
|
|
8
|
/**
|
9
|
* @todo Test on a generic entity not on a node.
|
10
|
*
|
11
|
* What has to be tested:
|
12
|
* - Take sure that every wanted field is added to the according entity type.
|
13
|
* - Take sure the joins are done correct.
|
14
|
* - Use basic fields and take sure that the full wanted object is build.
|
15
|
* - Use relationships between different entity types, for example node and
|
16
|
* the node author(user).
|
17
|
*/
|
18
|
|
19
|
/**
|
20
|
* Provides some helper methods for testing fieldapi integration into views.
|
21
|
*/
|
22
|
class ViewsFieldApiTestHelper extends ViewsSqlTest {
|
23
|
|
24
|
/**
|
25
|
* Stores the field definitions used by the test.
|
26
|
*
|
27
|
* @var array
|
28
|
*/
|
29
|
public $fields;
|
30
|
|
31
|
/**
|
32
|
* Stores the instances of the fields. They have
|
33
|
* the same keys as the fields.
|
34
|
*
|
35
|
* @var array
|
36
|
*/
|
37
|
public $instances;
|
38
|
|
39
|
/**
|
40
|
*
|
41
|
*/
|
42
|
protected function createUser($extra_edit = array()) {
|
43
|
$permissions = array('access comments', 'access content', 'post comments', 'skip comment approval');
|
44
|
// Create a role with the given permission set.
|
45
|
if (!($rid = $this->drupalCreateRole($permissions))) {
|
46
|
return FALSE;
|
47
|
}
|
48
|
|
49
|
// Create a user assigned to that role.
|
50
|
$edit = array();
|
51
|
$edit['name'] = $this->randomName();
|
52
|
$edit['mail'] = $edit['name'] . '@example.com';
|
53
|
$edit['roles'] = array($rid => $rid);
|
54
|
$edit['pass'] = user_password();
|
55
|
$edit['status'] = 1;
|
56
|
$edit += $extra_edit;
|
57
|
|
58
|
$account = user_save(drupal_anonymous_user(), $edit);
|
59
|
|
60
|
$this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
|
61
|
if (empty($account->uid)) {
|
62
|
return FALSE;
|
63
|
}
|
64
|
|
65
|
// Add the raw password so that we can log in as this user.
|
66
|
$account->pass_raw = $edit['pass'];
|
67
|
return $account;
|
68
|
}
|
69
|
|
70
|
function setUpFields($amount = 3) {
|
71
|
// Create three fields.
|
72
|
$field_names = array();
|
73
|
for ($i = 0; $i < $amount; $i++) {
|
74
|
$field_names[$i] = 'field_name_' . $i;
|
75
|
$field = array('field_name' => $field_names[$i], 'type' => 'text');
|
76
|
|
77
|
$this->fields[$i] = $field = field_create_field($field);
|
78
|
}
|
79
|
return $field_names;
|
80
|
}
|
81
|
|
82
|
function setUpInstances($bundle = 'page') {
|
83
|
foreach ($this->fields as $key => $field) {
|
84
|
$instance = array(
|
85
|
'field_name' => $field['field_name'],
|
86
|
'entity_type' => 'node',
|
87
|
'bundle' => 'page',
|
88
|
);
|
89
|
$this->instances[$key] = field_create_instance($instance);
|
90
|
}
|
91
|
}
|
92
|
|
93
|
/**
|
94
|
* Clear all views caches and static caches which are required for the patch.
|
95
|
*/
|
96
|
function clearViewsCaches() {
|
97
|
// Reset views data cache.
|
98
|
drupal_static_reset('_views_fetch_data_cache');
|
99
|
drupal_static_reset('_views_fetch_data_recursion_protected');
|
100
|
drupal_static_reset('_views_fetch_data_fully_loaded');
|
101
|
}
|
102
|
|
103
|
}
|
104
|
|
105
|
/**
|
106
|
* Test the produced views_data.
|
107
|
*/
|
108
|
class viewsFieldApiDataTest extends ViewsFieldApiTestHelper {
|
109
|
/**
|
110
|
* Stores the fields for this test case.
|
111
|
*/
|
112
|
var $fields;
|
113
|
|
114
|
public static function getInfo() {
|
115
|
return array(
|
116
|
'name' => 'Fieldapi: Views Data',
|
117
|
'description' => 'Tests the fieldapi views data.',
|
118
|
'group' => 'Views Modules',
|
119
|
);
|
120
|
}
|
121
|
|
122
|
/**
|
123
|
* {@inheritdoc}
|
124
|
*/
|
125
|
public function setUp(array $modules = array()) {
|
126
|
parent::setUp($modules);
|
127
|
|
128
|
$langcode = LANGUAGE_NONE;
|
129
|
|
130
|
$field_names = $this->setUpFields();
|
131
|
|
132
|
// The first one will be attached to nodes only.
|
133
|
$instance = array(
|
134
|
'field_name' => $field_names[0],
|
135
|
'entity_type' => 'node',
|
136
|
'bundle' => 'page',
|
137
|
);
|
138
|
field_create_instance($instance);
|
139
|
|
140
|
// The second one will be attached to users only.
|
141
|
$instance = array(
|
142
|
'field_name' => $field_names[1],
|
143
|
'entity_type' => 'user',
|
144
|
'bundle' => 'user',
|
145
|
);
|
146
|
field_create_instance($instance);
|
147
|
|
148
|
// The third will be attached to both nodes and users.
|
149
|
$instance = array(
|
150
|
'field_name' => $field_names[2],
|
151
|
'entity_type' => 'node',
|
152
|
'bundle' => 'page',
|
153
|
);
|
154
|
field_create_instance($instance);
|
155
|
$instance = array(
|
156
|
'field_name' => $field_names[2],
|
157
|
'entity_type' => 'user',
|
158
|
'bundle' => 'user',
|
159
|
);
|
160
|
field_create_instance($instance);
|
161
|
|
162
|
// Now create some example nodes/users for the view result.
|
163
|
for ($i = 0; $i < 5; $i++) {
|
164
|
$edit = array(
|
165
|
// @todo Write a helper method to create such values.
|
166
|
'field_name_0' => array($langcode => array((array('value' => $this->randomName())))),
|
167
|
'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
|
168
|
);
|
169
|
$this->nodes[] = $this->drupalCreateNode($edit);
|
170
|
}
|
171
|
|
172
|
for ($i = 0; $i < 5; $i++) {
|
173
|
$edit = array(
|
174
|
'field_name_1' => array($langcode => array((array('value' => $this->randomName())))),
|
175
|
'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
|
176
|
);
|
177
|
$this->users[] = $this->createUser($edit);
|
178
|
}
|
179
|
|
180
|
// Reset views data cache.
|
181
|
$this->clearViewsCaches();
|
182
|
}
|
183
|
|
184
|
/**
|
185
|
* Unit testing the views data structure.
|
186
|
*
|
187
|
* We check data structure for both node and node revision tables.
|
188
|
*/
|
189
|
function testViewsData() {
|
190
|
$data = views_fetch_data();
|
191
|
|
192
|
// Check the table and the joins of the first field.
|
193
|
// Attached to node only.
|
194
|
$field = $this->fields[0];
|
195
|
$current_table = _field_sql_storage_tablename($field);
|
196
|
$revision_table = _field_sql_storage_revision_tablename($field);
|
197
|
|
198
|
$this->assertTrue(isset($data[$current_table]));
|
199
|
$this->assertTrue(isset($data[$revision_table]));
|
200
|
// The node field should join against node.
|
201
|
$this->assertTrue(isset($data[$current_table]['table']['join']['node']));
|
202
|
$this->assertTrue(isset($data[$revision_table]['table']['join']['node_revision']));
|
203
|
|
204
|
$expected_join = array(
|
205
|
'left_field' => 'nid',
|
206
|
'field' => 'entity_id',
|
207
|
'extra' => array(
|
208
|
array('field' => 'entity_type', 'value' => 'node'),
|
209
|
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
|
210
|
),
|
211
|
);
|
212
|
$this->assertEqual($expected_join, $data[$current_table]['table']['join']['node']);
|
213
|
$expected_join = array(
|
214
|
'left_field' => 'vid',
|
215
|
'field' => 'revision_id',
|
216
|
'extra' => array(
|
217
|
array('field' => 'entity_type', 'value' => 'node'),
|
218
|
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
|
219
|
),
|
220
|
);
|
221
|
$this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_revision']);
|
222
|
|
223
|
// Check the table and the joins of the second field.
|
224
|
// Attached to both node and user.
|
225
|
$field_2 = $this->fields[2];
|
226
|
$current_table_2 = _field_sql_storage_tablename($field_2);
|
227
|
$revision_table_2 = _field_sql_storage_revision_tablename($field_2);
|
228
|
|
229
|
$this->assertTrue(isset($data[$current_table_2]));
|
230
|
$this->assertTrue(isset($data[$revision_table_2]));
|
231
|
// The second field should join against both node and users.
|
232
|
$this->assertTrue(isset($data[$current_table_2]['table']['join']['node']));
|
233
|
$this->assertTrue(isset($data[$revision_table_2]['table']['join']['node_revision']));
|
234
|
$this->assertTrue(isset($data[$current_table_2]['table']['join']['users']));
|
235
|
|
236
|
$expected_join = array(
|
237
|
'left_field' => 'nid',
|
238
|
'field' => 'entity_id',
|
239
|
'extra' => array(
|
240
|
array('field' => 'entity_type', 'value' => 'node'),
|
241
|
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
|
242
|
),
|
243
|
);
|
244
|
$this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['node']);
|
245
|
$expected_join = array(
|
246
|
'left_field' => 'vid',
|
247
|
'field' => 'revision_id',
|
248
|
'extra' => array(
|
249
|
array('field' => 'entity_type', 'value' => 'node'),
|
250
|
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
|
251
|
),
|
252
|
);
|
253
|
$this->assertEqual($expected_join, $data[$revision_table_2]['table']['join']['node_revision']);
|
254
|
$expected_join = array(
|
255
|
'left_field' => 'uid',
|
256
|
'field' => 'entity_id',
|
257
|
'extra' => array(
|
258
|
array('field' => 'entity_type', 'value' => 'user'),
|
259
|
array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
|
260
|
),
|
261
|
);
|
262
|
$this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['users']);
|
263
|
|
264
|
// @todo Check the fields.
|
265
|
// @todo Check the arguments.
|
266
|
// @todo Check the sort criterias.
|
267
|
// @todo Check the relationships.
|
268
|
}
|
269
|
|
270
|
}
|
271
|
|
272
|
/**
|
273
|
* Tests the field_field handler.
|
274
|
*
|
275
|
* @todo Check a entity-type with bundles.
|
276
|
* @todo Check a entity-type without bundles.
|
277
|
* @todo Check locale:disabled, locale:enabled and locale:enabled with another language.
|
278
|
* @todo Check revisions.
|
279
|
*/
|
280
|
class viewsHandlerFieldFieldTest extends ViewsFieldApiTestHelper {
|
281
|
public $nodes;
|
282
|
|
283
|
public static function getInfo() {
|
284
|
return array(
|
285
|
'name' => 'Fieldapi: Field handler',
|
286
|
'description' => 'Tests the field itself of the fieldapi integration',
|
287
|
'group' => 'Views Modules',
|
288
|
);
|
289
|
}
|
290
|
|
291
|
/**
|
292
|
* {@inheritdoc}
|
293
|
*/
|
294
|
public function setUp(array $modules = array()) {
|
295
|
parent::setUp($modules);
|
296
|
|
297
|
// Setup basic fields.
|
298
|
$this->setUpFields(3);
|
299
|
|
300
|
// Setup a field with cardinality > 1.
|
301
|
$this->fields[3] = $field = field_create_field(array('field_name' => 'field_name_3', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
|
302
|
// Setup a field that will have no value.
|
303
|
$this->fields[4] = $field = field_create_field(array('field_name' => 'field_name_4', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
|
304
|
|
305
|
$this->setUpInstances();
|
306
|
|
307
|
$this->clearViewsCaches();
|
308
|
|
309
|
// Create some nodes.
|
310
|
$this->nodes = array();
|
311
|
for ($i = 0; $i < 3; $i++) {
|
312
|
$edit = array('type' => 'page');
|
313
|
|
314
|
for ($key = 0; $key < 3; $key++) {
|
315
|
$field = $this->fields[$key];
|
316
|
$edit[$field['field_name']][LANGUAGE_NONE][0]['value'] = $this->randomName(8);
|
317
|
}
|
318
|
for ($j = 0; $j < 5; $j++) {
|
319
|
$edit[$this->fields[3]['field_name']][LANGUAGE_NONE][$j]['value'] = $this->randomName(8);
|
320
|
}
|
321
|
// Set this field to be empty.
|
322
|
$edit[$this->fields[4]['field_name']] = array();
|
323
|
|
324
|
$this->nodes[$i] = $this->drupalCreateNode($edit);
|
325
|
}
|
326
|
}
|
327
|
|
328
|
public function testFieldRender() {
|
329
|
$this->_testSimpleFieldRender();
|
330
|
$this->_testFormatterSimpleFieldRender();
|
331
|
$this->_testMultipleFieldRender();
|
332
|
}
|
333
|
|
334
|
public function _testSimpleFieldRender() {
|
335
|
$view = $this->getFieldView();
|
336
|
$this->executeView($view);
|
337
|
|
338
|
// Tests that the rendered fields match the actual value of the fields.
|
339
|
for ($i = 0; $i < 3; $i++) {
|
340
|
for ($key = 0; $key < 2; $key++) {
|
341
|
$field = $this->fields[$key];
|
342
|
$rendered_field = $view->style_plugin->get_field($i, $field['field_name']);
|
343
|
$expected_field = $this->nodes[$i]->{$field['field_name']}[LANGUAGE_NONE][0]['value'];
|
344
|
$this->assertEqual($rendered_field, $expected_field);
|
345
|
}
|
346
|
}
|
347
|
}
|
348
|
|
349
|
/**
|
350
|
* Tests that fields with formatters runs as expected.
|
351
|
*/
|
352
|
public function _testFormatterSimpleFieldRender() {
|
353
|
$view = $this->getFieldView();
|
354
|
$view->display['default']->display_options['fields'][$this->fields[0]['field_name']]['type'] = 'text_trimmed';
|
355
|
$view->display['default']->display_options['fields'][$this->fields[0]['field_name']]['settings'] = array(
|
356
|
'trim_length' => 3,
|
357
|
);
|
358
|
$this->executeView($view);
|
359
|
|
360
|
// Take sure that the formatter works as expected.
|
361
|
// @todo actually there should be a specific formatter.
|
362
|
for ($i = 0; $i < 2; $i++) {
|
363
|
$rendered_field = $view->style_plugin->get_field($i, $this->fields[0]['field_name']);
|
364
|
$this->assertEqual(strlen($rendered_field), 3);
|
365
|
}
|
366
|
}
|
367
|
|
368
|
public function _testMultipleFieldRender() {
|
369
|
$view = $this->getFieldView();
|
370
|
|
371
|
// Test delta limit.
|
372
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
|
373
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
|
374
|
$this->executeView($view);
|
375
|
|
376
|
for ($i = 0; $i < 3; $i++) {
|
377
|
$rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
|
378
|
$items = array();
|
379
|
$pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
|
380
|
$pure_items = array_splice($pure_items, 0, 3);
|
381
|
foreach ($pure_items as $j => $item) {
|
382
|
$items[] = $pure_items[$j]['value'];
|
383
|
}
|
384
|
$this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
|
385
|
}
|
386
|
|
387
|
// Test that an empty field is rendered without error.
|
388
|
$rendered_field = $view->style_plugin->get_field(4, $this->fields[4]['field_name']);
|
389
|
|
390
|
$view->destroy();
|
391
|
|
392
|
// Test delta limit + offset.
|
393
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
|
394
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
|
395
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_offset'] = 1;
|
396
|
$this->executeView($view);
|
397
|
|
398
|
for ($i = 0; $i < 3; $i++) {
|
399
|
$rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
|
400
|
$items = array();
|
401
|
$pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
|
402
|
$pure_items = array_splice($pure_items, 1, 3);
|
403
|
foreach ($pure_items as $j => $item) {
|
404
|
$items[] = $pure_items[$j]['value'];
|
405
|
}
|
406
|
$this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
|
407
|
}
|
408
|
$view->destroy();
|
409
|
|
410
|
// Test delta limit + reverse.
|
411
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_offset'] = 0;
|
412
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
|
413
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
|
414
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_reversed'] = TRUE;
|
415
|
$this->executeView($view);
|
416
|
|
417
|
for ($i = 0; $i < 3; $i++) {
|
418
|
$rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
|
419
|
$items = array();
|
420
|
$pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
|
421
|
array_splice($pure_items, 0, -3);
|
422
|
$pure_items = array_reverse($pure_items);
|
423
|
foreach ($pure_items as $j => $item) {
|
424
|
$items[] = $pure_items[$j]['value'];
|
425
|
}
|
426
|
$this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
|
427
|
}
|
428
|
$view->destroy();
|
429
|
|
430
|
// Test delta first last.
|
431
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
|
432
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 0;
|
433
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_first_last'] = TRUE;
|
434
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_reversed'] = FALSE;
|
435
|
$this->executeView($view);
|
436
|
|
437
|
for ($i = 0; $i < 3; $i++) {
|
438
|
$rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
|
439
|
$items = array();
|
440
|
$pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
|
441
|
$items[] = $pure_items[0]['value'];
|
442
|
$items[] = $pure_items[4]['value'];
|
443
|
$this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
|
444
|
}
|
445
|
$view->destroy();
|
446
|
|
447
|
// Test delta limit + custom seperator.
|
448
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_first_last'] = FALSE;
|
449
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
|
450
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
|
451
|
$view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['separator'] = ':';
|
452
|
$this->executeView($view);
|
453
|
|
454
|
for ($i = 0; $i < 3; $i++) {
|
455
|
$rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
|
456
|
$items = array();
|
457
|
$pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
|
458
|
$pure_items = array_splice($pure_items, 0, 3);
|
459
|
foreach ($pure_items as $j => $item) {
|
460
|
$items[] = $pure_items[$j]['value'];
|
461
|
}
|
462
|
$this->assertEqual($rendered_field, implode(':', $items), 'Take sure that the amount of items are limited.');
|
463
|
}
|
464
|
}
|
465
|
|
466
|
protected function getFieldView() {
|
467
|
$view = new view();
|
468
|
$view->name = 'view_fieldapi';
|
469
|
$view->description = '';
|
470
|
$view->tag = 'default';
|
471
|
$view->base_table = 'node';
|
472
|
$view->human_name = 'view_fieldapi';
|
473
|
$view->core = 7;
|
474
|
$view->api_version = '3.0';
|
475
|
$view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
|
476
|
|
477
|
/* Display: Master */
|
478
|
$handler = $view->new_display('default', 'Master', 'default');
|
479
|
$handler->display->display_options['access']['type'] = 'perm';
|
480
|
$handler->display->display_options['cache']['type'] = 'none';
|
481
|
$handler->display->display_options['query']['type'] = 'views_query';
|
482
|
$handler->display->display_options['exposed_form']['type'] = 'basic';
|
483
|
$handler->display->display_options['pager']['type'] = 'full';
|
484
|
$handler->display->display_options['style_plugin'] = 'default';
|
485
|
$handler->display->display_options['row_plugin'] = 'fields';
|
486
|
|
487
|
$handler->display->display_options['fields']['nid']['id'] = 'nid';
|
488
|
$handler->display->display_options['fields']['nid']['table'] = 'node';
|
489
|
$handler->display->display_options['fields']['nid']['field'] = 'nid';
|
490
|
foreach ($this->fields as $key => $field) {
|
491
|
$handler->display->display_options['fields'][$field['field_name']]['id'] = $field['field_name'];
|
492
|
$handler->display->display_options['fields'][$field['field_name']]['table'] = 'field_data_' . $field['field_name'];
|
493
|
$handler->display->display_options['fields'][$field['field_name']]['field'] = $field['field_name'];
|
494
|
}
|
495
|
return $view;
|
496
|
}
|
497
|
|
498
|
}
|