Projet

Général

Profil

Paste
Télécharger (17,8 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / views / tests / field / views_fieldapi.test @ 5d12d676

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
  function setUp() {
123
    parent::setUp();
124

    
125
    $langcode = LANGUAGE_NONE;
126

    
127
    $field_names = $this->setUpFields();
128

    
129
    // The first one will be attached to nodes only.
130
    $instance = array(
131
      'field_name' => $field_names[0],
132
      'entity_type' => 'node',
133
      'bundle' => 'page',
134
    );
135
    field_create_instance($instance);
136

    
137
    // The second one will be attached to users only.
138
    $instance = array(
139
      'field_name' => $field_names[1],
140
      'entity_type' => 'user',
141
      'bundle' => 'user',
142
    );
143
    field_create_instance($instance);
144

    
145
    // The third will be attached to both nodes and users.
146
    $instance = array(
147
      'field_name' => $field_names[2],
148
      'entity_type' => 'node',
149
      'bundle' => 'page',
150
    );
151
    field_create_instance($instance);
152
    $instance = array(
153
      'field_name' => $field_names[2],
154
      'entity_type' => 'user',
155
      'bundle' => 'user',
156
    );
157
    field_create_instance($instance);
158

    
159
    // Now create some example nodes/users for the view result.
160
    for ($i = 0; $i < 5; $i++) {
161
      $edit = array(
162
        // @todo Write a helper method to create such values.
163
        'field_name_0' => array($langcode => array((array('value' => $this->randomName())))),
164
        'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
165
      );
166
      $this->nodes[] = $this->drupalCreateNode($edit);
167
    }
168

    
169
    for ($i = 0; $i < 5; $i++) {
170
      $edit = array(
171
        'field_name_1' => array($langcode => array((array('value' => $this->randomName())))),
172
        'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
173
      );
174
      $this->users[] = $this->createUser($edit);
175
    }
176

    
177
    // Reset views data cache.
178
    $this->clearViewsCaches();
179
  }
180

    
181
  /**
182
   * Unit testing the views data structure.
183
   *
184
   * We check data structure for both node and node revision tables.
185
   */
186
  function testViewsData() {
187
    $data = views_fetch_data();
188

    
189
    // Check the table and the joins of the first field.
190
    // Attached to node only.
191
    $field = $this->fields[0];
192
    $current_table = _field_sql_storage_tablename($field);
193
    $revision_table = _field_sql_storage_revision_tablename($field);
194

    
195
    $this->assertTrue(isset($data[$current_table]));
196
    $this->assertTrue(isset($data[$revision_table]));
197
    // The node field should join against node.
198
    $this->assertTrue(isset($data[$current_table]['table']['join']['node']));
199
    $this->assertTrue(isset($data[$revision_table]['table']['join']['node_revision']));
200

    
201
    $expected_join = array(
202
      'left_field' => 'nid',
203
      'field' => 'entity_id',
204
      'extra' => array(
205
        array('field' => 'entity_type', 'value' => 'node'),
206
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
207
      ),
208
    );
209
    $this->assertEqual($expected_join, $data[$current_table]['table']['join']['node']);
210
    $expected_join = array(
211
      'left_field' => 'vid',
212
      'field' => 'revision_id',
213
      'extra' => array(
214
        array('field' => 'entity_type', 'value' => 'node'),
215
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
216
      ),
217
    );
218
    $this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_revision']);
219

    
220
    // Check the table and the joins of the second field.
221
    // Attached to both node and user.
222
    $field_2 = $this->fields[2];
223
    $current_table_2 = _field_sql_storage_tablename($field_2);
224
    $revision_table_2 = _field_sql_storage_revision_tablename($field_2);
225

    
226
    $this->assertTrue(isset($data[$current_table_2]));
227
    $this->assertTrue(isset($data[$revision_table_2]));
228
    // The second field should join against both node and users.
229
    $this->assertTrue(isset($data[$current_table_2]['table']['join']['node']));
230
    $this->assertTrue(isset($data[$revision_table_2]['table']['join']['node_revision']));
231
    $this->assertTrue(isset($data[$current_table_2]['table']['join']['users']));
232

    
233
    $expected_join = array(
234
      'left_field' => 'nid',
235
      'field' => 'entity_id',
236
      'extra' => array(
237
        array('field' => 'entity_type', 'value' => 'node'),
238
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
239
      ),
240
    );
241
    $this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['node']);
242
    $expected_join = array(
243
      'left_field' => 'vid',
244
      'field' => 'revision_id',
245
      'extra' => array(
246
        array('field' => 'entity_type', 'value' => 'node'),
247
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
248
      ),
249
    );
250
    $this->assertEqual($expected_join, $data[$revision_table_2]['table']['join']['node_revision']);
251
    $expected_join = array(
252
      'left_field' => 'uid',
253
      'field' => 'entity_id',
254
      'extra' => array(
255
        array('field' => 'entity_type', 'value' => 'user'),
256
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
257
      ),
258
    );
259
    $this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['users']);
260

    
261
    // @todo Check the fields.
262
    // @todo Check the arguments.
263
    // @todo Check the sort criterias.
264
    // @todo Check the relationships.
265
  }
266

    
267
}
268

    
269
/**
270
 * Tests the field_field handler.
271
 *
272
 * @todo Check a entity-type with bundles.
273
 * @todo Check a entity-type without bundles.
274
 * @todo Check locale:disabled, locale:enabled and locale:enabled with another language.
275
 * @todo Check revisions.
276
 */
277
class viewsHandlerFieldFieldTest extends ViewsFieldApiTestHelper {
278
  public $nodes;
279

    
280
  public static function getInfo() {
281
    return array(
282
      'name' => 'Fieldapi: Field handler',
283
      'description' => 'Tests the field itself of the fieldapi integration',
284
      'group' => 'Views Modules',
285
    );
286
  }
287

    
288
  protected function setUp() {
289
    parent::setUp();
290

    
291
    // Setup basic fields.
292
    $this->setUpFields(3);
293

    
294
    // Setup a field with cardinality > 1.
295
    $this->fields[3] = $field = field_create_field(array('field_name' => 'field_name_3', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
296
    // Setup a field that will have no value.
297
    $this->fields[4] = $field = field_create_field(array('field_name' => 'field_name_4', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
298

    
299
    $this->setUpInstances();
300

    
301
    $this->clearViewsCaches();
302

    
303
    // Create some nodes.
304
    $this->nodes = array();
305
    for ($i = 0; $i < 3; $i++) {
306
      $edit = array('type' => 'page');
307

    
308
      for ($key = 0; $key < 3; $key++) {
309
        $field = $this->fields[$key];
310
        $edit[$field['field_name']][LANGUAGE_NONE][0]['value'] = $this->randomName(8);
311
      }
312
      for ($j = 0; $j < 5; $j++) {
313
        $edit[$this->fields[3]['field_name']][LANGUAGE_NONE][$j]['value'] = $this->randomName(8);
314
      }
315
      // Set this field to be empty.
316
      $edit[$this->fields[4]['field_name']] = array();
317

    
318
      $this->nodes[$i] = $this->drupalCreateNode($edit);
319
    }
320
  }
321

    
322
  public function testFieldRender() {
323
    $this->_testSimpleFieldRender();
324
    $this->_testFormatterSimpleFieldRender();
325
    $this->_testMultipleFieldRender();
326
  }
327

    
328
  public function _testSimpleFieldRender() {
329
    $view = $this->getFieldView();
330
    $this->executeView($view);
331

    
332
    // Tests that the rendered fields match the actual value of the fields.
333
    for ($i = 0; $i < 3; $i++) {
334
      for ($key = 0; $key < 2; $key++) {
335
        $field = $this->fields[$key];
336
        $rendered_field = $view->style_plugin->get_field($i, $field['field_name']);
337
        $expected_field = $this->nodes[$i]->{$field['field_name']}[LANGUAGE_NONE][0]['value'];
338
        $this->assertEqual($rendered_field, $expected_field);
339
      }
340
    }
341
  }
342

    
343
  /**
344
   * Tests that fields with formatters runs as expected.
345
   */
346
  public function _testFormatterSimpleFieldRender() {
347
    $view = $this->getFieldView();
348
    $view->display['default']->display_options['fields'][$this->fields[0]['field_name']]['type'] = 'text_trimmed';
349
    $view->display['default']->display_options['fields'][$this->fields[0]['field_name']]['settings'] = array(
350
      'trim_length' => 3,
351
    );
352
    $this->executeView($view);
353

    
354
    // Take sure that the formatter works as expected.
355
    // @todo actually there should be a specific formatter.
356
    for ($i = 0; $i < 2; $i++) {
357
      $rendered_field = $view->style_plugin->get_field($i, $this->fields[0]['field_name']);
358
      $this->assertEqual(strlen($rendered_field), 3);
359
    }
360
  }
361

    
362
  public function _testMultipleFieldRender() {
363
    $view = $this->getFieldView();
364

    
365
    // Test delta limit.
366
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
367
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
368
    $this->executeView($view);
369

    
370
    for ($i = 0; $i < 3; $i++) {
371
      $rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
372
      $items = array();
373
      $pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
374
      $pure_items = array_splice($pure_items, 0, 3);
375
      foreach ($pure_items as $j => $item) {
376
        $items[] = $pure_items[$j]['value'];
377
      }
378
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
379
    }
380

    
381
    // Test that an empty field is rendered without error.
382
    $rendered_field = $view->style_plugin->get_field(4, $this->fields[4]['field_name']);
383

    
384
    $view->destroy();
385

    
386
    // Test delta limit + offset.
387
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
388
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
389
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_offset'] = 1;
390
    $this->executeView($view);
391

    
392
    for ($i = 0; $i < 3; $i++) {
393
      $rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
394
      $items = array();
395
      $pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
396
      $pure_items = array_splice($pure_items, 1, 3);
397
      foreach ($pure_items as $j => $item) {
398
        $items[] = $pure_items[$j]['value'];
399
      }
400
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
401
    }
402
    $view->destroy();
403

    
404
    // Test delta limit + reverse.
405
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_offset'] = 0;
406
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
407
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
408
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_reversed'] = TRUE;
409
    $this->executeView($view);
410

    
411
    for ($i = 0; $i < 3; $i++) {
412
      $rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
413
      $items = array();
414
      $pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
415
      array_splice($pure_items, 0, -3);
416
      $pure_items = array_reverse($pure_items);
417
      foreach ($pure_items as $j => $item) {
418
        $items[] = $pure_items[$j]['value'];
419
      }
420
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
421
    }
422
    $view->destroy();
423

    
424
    // Test delta first last.
425
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
426
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 0;
427
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_first_last'] = TRUE;
428
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_reversed'] = FALSE;
429
    $this->executeView($view);
430

    
431
    for ($i = 0; $i < 3; $i++) {
432
      $rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
433
      $items = array();
434
      $pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
435
      $items[] = $pure_items[0]['value'];
436
      $items[] = $pure_items[4]['value'];
437
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
438
    }
439
    $view->destroy();
440

    
441
    // Test delta limit + custom seperator.
442
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_first_last'] = FALSE;
443
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['delta_limit'] = 3;
444
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['group_rows'] = TRUE;
445
    $view->display['default']->display_options['fields'][$this->fields[3]['field_name']]['separator'] = ':';
446
    $this->executeView($view);
447

    
448
    for ($i = 0; $i < 3; $i++) {
449
      $rendered_field = $view->style_plugin->get_field($i, $this->fields[3]['field_name']);
450
      $items = array();
451
      $pure_items = $this->nodes[$i]->{$this->fields[3]['field_name']}[LANGUAGE_NONE];
452
      $pure_items = array_splice($pure_items, 0, 3);
453
      foreach ($pure_items as $j => $item) {
454
        $items[] = $pure_items[$j]['value'];
455
      }
456
      $this->assertEqual($rendered_field, implode(':', $items), 'Take sure that the amount of items are limited.');
457
    }
458
  }
459

    
460
  protected function getFieldView() {
461
    $view = new view();
462
    $view->name = 'view_fieldapi';
463
    $view->description = '';
464
    $view->tag = 'default';
465
    $view->base_table = 'node';
466
    $view->human_name = 'view_fieldapi';
467
    $view->core = 7;
468
    $view->api_version = '3.0';
469
    $view->disabled = FALSE; /* Edit this to true to make a default view disabled initially */
470

    
471
    /* Display: Master */
472
    $handler = $view->new_display('default', 'Master', 'default');
473
    $handler->display->display_options['access']['type'] = 'perm';
474
    $handler->display->display_options['cache']['type'] = 'none';
475
    $handler->display->display_options['query']['type'] = 'views_query';
476
    $handler->display->display_options['exposed_form']['type'] = 'basic';
477
    $handler->display->display_options['pager']['type'] = 'full';
478
    $handler->display->display_options['style_plugin'] = 'default';
479
    $handler->display->display_options['row_plugin'] = 'fields';
480

    
481
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
482
    $handler->display->display_options['fields']['nid']['table'] = 'node';
483
    $handler->display->display_options['fields']['nid']['field'] = 'nid';
484
    foreach ($this->fields as $key => $field) {
485
      $handler->display->display_options['fields'][$field['field_name']]['id'] = $field['field_name'];
486
      $handler->display->display_options['fields'][$field['field_name']]['table'] = 'field_data_' . $field['field_name'];
487
      $handler->display->display_options['fields'][$field['field_name']]['field'] = $field['field_name'];
488
    }
489
    return $view;
490
  }
491

    
492
}