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 @ 76df55b7

1
<?php
2

    
3
/**
4
 * @file
5
 * Tests the fieldapi integration of viewsdata.
6
 */
7

    
8
/**
9
 * @TODO
10
 *   - Test on a generic entity not on a node.
11
 *
12
 * What has to be tested:
13
 *   - Take sure that every wanted field is added to the according entity type.
14
 *   - Take sure the joins are done correct.
15
 *   - Use basic fields and take sure that the full wanted object is build.
16
 *   - Use relationships between different entity types, for example node and 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
   * Stores the field definitions used by the test.
25
   * @var array
26
   */
27
  public $fields;
28
  /**
29
   * Stores the instances of the fields. They have
30
   * the same keys as the fields.
31
   * @var array
32
   */
33
  public $instances;
34

    
35
  protected function CreateUser($extra_edit = array()) {
36
    $permissions = array('access comments', 'access content', 'post comments', 'skip comment approval');
37
    // Create a role with the given permission set.
38
    if (!($rid = $this->drupalCreateRole($permissions))) {
39
      return FALSE;
40
    }
41

    
42
    // Create a user assigned to that role.
43
    $edit = array();
44
    $edit['name']   = $this->randomName();
45
    $edit['mail']   = $edit['name'] . '@example.com';
46
    $edit['roles']  = array($rid => $rid);
47
    $edit['pass']   = user_password();
48
    $edit['status'] = 1;
49
    $edit += $extra_edit;
50

    
51
    $account = user_save(drupal_anonymous_user(), $edit);
52

    
53
    $this->assertTrue(!empty($account->uid), t('User created with name %name and pass %pass', array('%name' => $edit['name'], '%pass' => $edit['pass'])), t('User login'));
54
    if (empty($account->uid)) {
55
      return FALSE;
56
    }
57

    
58
    // Add the raw password so that we can log in as this user.
59
    $account->pass_raw = $edit['pass'];
60
    return $account;
61
  }
62

    
63
  function setUpFields($amount = 3) {
64
    // Create three fields.
65
    $field_names = array();
66
    for ($i = 0; $i < $amount; $i++) {
67
      $field_names[$i] = 'field_name_' . $i;
68
      $field = array('field_name' => $field_names[$i], 'type' => 'text');
69

    
70
      $this->fields[$i] = $field = field_create_field($field);
71
    }
72
    return $field_names;
73
  }
74

    
75
  function setUpInstances($bundle = 'page') {
76
    foreach ($this->fields as $key => $field) {
77
      $instance = array(
78
        'field_name' => $field['field_name'],
79
        'entity_type' => 'node',
80
        'bundle' => 'page',
81
      );
82
      $this->instances[$key] = field_create_instance($instance);
83
    }
84
  }
85

    
86
  /**
87
   * Clear all views caches and static caches which are required for the patch.
88
   */
89
  function clearViewsCaches() {
90
    // Reset views data cache.
91
    drupal_static_reset('_views_fetch_data_cache');
92
    drupal_static_reset('_views_fetch_data_recursion_protected');
93
    drupal_static_reset('_views_fetch_data_fully_loaded');
94
  }
95
}
96

    
97
/**
98
 * Test the produced views_data.
99
 */
100
class viewsFieldApiDataTest extends ViewsFieldApiTestHelper {
101
  /**
102
   * Stores the fields for this test case.
103
   */
104
  var $fields;
105

    
106
  public static function getInfo() {
107
    return array(
108
      'name' => 'Fieldapi: Views Data',
109
      'description' => 'Tests the fieldapi views data.',
110
      'group' => 'Views Modules',
111
    );
112
  }
113

    
114
  function setUp() {
115
    parent::setUp();
116

    
117
    $langcode = LANGUAGE_NONE;
118

    
119

    
120
    $field_names = $this->setUpFields();
121

    
122
    // The first one will be attached to nodes only.
123
    $instance = array(
124
      'field_name' => $field_names[0],
125
      'entity_type' => 'node',
126
      'bundle' => 'page',
127
    );
128
    field_create_instance($instance);
129

    
130
    // The second one will be attached to users only.
131
    $instance = array(
132
      'field_name' => $field_names[1],
133
      'entity_type' => 'user',
134
      'bundle' => 'user',
135
    );
136
    field_create_instance($instance);
137

    
138
    // The third will be attached to both nodes and users.
139
    $instance = array(
140
      'field_name' => $field_names[2],
141
      'entity_type' => 'node',
142
      'bundle' => 'page',
143
    );
144
    field_create_instance($instance);
145
    $instance = array(
146
      'field_name' => $field_names[2],
147
      'entity_type' => 'user',
148
      'bundle' => 'user',
149
    );
150
    field_create_instance($instance);
151

    
152
    // Now create some example nodes/users for the view result.
153
    for ($i = 0; $i < 5; $i++) {
154
      $edit = array(
155
        // @TODO Write a helper method to create such values.
156
        'field_name_0' => array($langcode => array((array('value' => $this->randomName())))),
157
        'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
158
      );
159
      $this->nodes[] = $this->drupalCreateNode($edit);
160
    }
161

    
162
    for ($i = 0; $i < 5; $i++) {
163
      $edit = array(
164
        'field_name_1' => array($langcode => array((array('value' => $this->randomName())))),
165
        'field_name_2' => array($langcode => array((array('value' => $this->randomName())))),
166
      );
167
      $this->users[] = $this->CreateUser($edit);
168
    }
169

    
170
    // Reset views data cache.
171
    $this->clearViewsCaches();
172
  }
173

    
174
  /**
175
   * Unit testing the views data structure.
176
   *
177
   * We check data structure for both node and node revision tables.
178
   */
179
  function testViewsData() {
180
    $data = views_fetch_data();
181
    
182
    // Check the table and the joins of the first field.
183
    // Attached to node only.
184
    $field = $this->fields[0];
185
    $current_table = _field_sql_storage_tablename($field);
186
    $revision_table = _field_sql_storage_revision_tablename($field);
187

    
188
    $this->assertTrue(isset($data[$current_table]));
189
    $this->assertTrue(isset($data[$revision_table]));
190
    // The node field should join against node.
191
    $this->assertTrue(isset($data[$current_table]['table']['join']['node']));
192
    $this->assertTrue(isset($data[$revision_table]['table']['join']['node_revision']));
193

    
194
    $expected_join = array(
195
      'left_field' => 'nid',
196
      'field' => 'entity_id',
197
      'extra' => array(
198
        array('field' => 'entity_type', 'value' => 'node'),
199
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
200
      ),
201
    );
202
    $this->assertEqual($expected_join, $data[$current_table]['table']['join']['node']);
203
    $expected_join = array(
204
      'left_field' => 'vid',
205
      'field' => 'revision_id',
206
      'extra' => array(
207
        array('field' => 'entity_type', 'value' => 'node'),
208
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
209
      ),
210
    );
211
    $this->assertEqual($expected_join, $data[$revision_table]['table']['join']['node_revision']);
212

    
213

    
214
    // Check the table and the joins of the second field.
215
    // Attached to both node and user.
216
    $field_2 = $this->fields[2];
217
    $current_table_2 = _field_sql_storage_tablename($field_2);
218
    $revision_table_2 = _field_sql_storage_revision_tablename($field_2);
219

    
220
    $this->assertTrue(isset($data[$current_table_2]));
221
    $this->assertTrue(isset($data[$revision_table_2]));
222
    // The second field should join against both node and users.
223
    $this->assertTrue(isset($data[$current_table_2]['table']['join']['node']));
224
    $this->assertTrue(isset($data[$revision_table_2]['table']['join']['node_revision']));
225
    $this->assertTrue(isset($data[$current_table_2]['table']['join']['users']));
226

    
227
    $expected_join = array(
228
      'left_field' => 'nid',
229
      'field' => 'entity_id',
230
      'extra' => array(
231
        array('field' => 'entity_type', 'value' => 'node'),
232
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
233
      )
234
    );
235
    $this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['node']);
236
    $expected_join = array(
237
      'left_field' => 'vid',
238
      'field' => 'revision_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[$revision_table_2]['table']['join']['node_revision']);
245
    $expected_join = array(
246
      'left_field' => 'uid',
247
      'field' => 'entity_id',
248
      'extra' => array(
249
        array('field' => 'entity_type', 'value' => 'user'),
250
        array('field' => 'deleted', 'value' => 0, 'numeric' => TRUE),
251
      )
252
    );
253
    $this->assertEqual($expected_join, $data[$current_table_2]['table']['join']['users']);
254

    
255
    // Check the fields
256
    // @todo
257

    
258
    // Check the arguments
259
    // @todo
260

    
261
    // Check the sort criterias
262
    // @todo
263

    
264
    // Check the relationships
265
    // @todo
266

    
267
  }
268
}
269

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

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

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

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

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

    
300
    $this->setUpInstances();
301

    
302
    $this->clearViewsCaches();
303

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

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

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

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

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

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

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

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

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

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

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

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

    
385
    $view->destroy();
386

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

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

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

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

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

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

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

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

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

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

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

    
493
}
494