HandlerFieldFieldTest.php 8.89 KB
Newer Older
merlinofchaos's avatar
merlinofchaos committed
1 2 3 4
<?php

/**
 * @file
5
 * Definition of Drupal\views\Test\Field\HandlerFieldFieldTest.
merlinofchaos's avatar
merlinofchaos committed
6 7
 */

8
namespace Drupal\views\Tests\Field;
merlinofchaos's avatar
merlinofchaos committed
9

10 11
use Drupal\views\ViewExecutable;

merlinofchaos's avatar
merlinofchaos committed
12 13 14 15 16 17 18 19
/**
 * Tests the field_field handler.
 * @TODO
 *   Check a entity-type with bundles
 *   Check a entity-type without bundles
 *   Check locale:disabled, locale:enabled and locale:enabled with another language
 *   Check revisions
 */
20
class HandlerFieldFieldTest extends FieldTestBase {
tim.plunkett's avatar
tim.plunkett committed
21

22 23 24 25 26 27 28
  /**
   * Views used by this test.
   *
   * @var array
   */
  public static $testViews = array('test_view_fieldapi');

merlinofchaos's avatar
merlinofchaos committed
29 30 31 32
  public $nodes;

  public static function getInfo() {
    return array(
33 34
      'name' => 'Field: Field handler',
      'description' => 'Tests the field itself of the Field integration.',
merlinofchaos's avatar
merlinofchaos committed
35 36 37 38
      'group' => 'Views Modules'
    );
  }

39 40 41
  /**
   * @todo.
   */
merlinofchaos's avatar
merlinofchaos committed
42 43 44 45 46 47 48 49
  protected function setUp() {
    parent::setUp();

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

    // Setup a field with cardinality > 1.
    $this->fields[3] = $field = field_create_field(array('field_name' => 'field_name_3', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
50 51
    // Setup a field that will have no value.
    $this->fields[4] = $field = field_create_field(array('field_name' => 'field_name_4', 'type' => 'text', 'cardinality' => FIELD_CARDINALITY_UNLIMITED));
merlinofchaos's avatar
merlinofchaos committed
52 53 54 55 56 57 58 59 60 61 62 63

    $this->setUpInstances();

    $this->clearViewsCaches();

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

      for ($key = 0; $key < 3; $key++) {
        $field = $this->fields[$key];
64
        $edit[$field['field_name']][LANGUAGE_NOT_SPECIFIED][0]['value'] = $this->randomName(8);
merlinofchaos's avatar
merlinofchaos committed
65 66
      }
      for ($j = 0; $j < 5; $j++) {
67
        $edit[$this->fields[3]['field_name']][LANGUAGE_NOT_SPECIFIED][$j]['value'] = $this->randomName(8);
merlinofchaos's avatar
merlinofchaos committed
68
      }
69
      // Set this field to be empty.
dawehner's avatar
dawehner committed
70
      $edit[$this->fields[4]['field_name']] = array(LANGUAGE_NOT_SPECIFIED => array(0 => array('value' => NULL)));
merlinofchaos's avatar
merlinofchaos committed
71 72 73

      $this->nodes[$i] = $this->drupalCreateNode($edit);
    }
74 75 76
  }

  /**
77 78 79 80
   * Sets up the testing view with random field data.
   *
   * @param \Drupal\views\ViewExecutable $view
   *   The view to add field data to.
81
   */
82 83 84 85 86 87 88
  protected function prepareView(ViewExecutable $view) {
    $view->initDisplay();
    foreach ($this->fields as $key => $field) {
      $view->display_handler->options['fields'][$field['field_name']]['id'] = $field['field_name'];
      $view->display_handler->options['fields'][$field['field_name']]['table'] = 'field_data_' . $field['field_name'];
      $view->display_handler->options['fields'][$field['field_name']]['field'] = $field['field_name'];
    }
merlinofchaos's avatar
merlinofchaos committed
89 90 91 92 93 94 95 96 97
  }

  public function testFieldRender() {
    $this->_testSimpleFieldRender();
    $this->_testFormatterSimpleFieldRender();
    $this->_testMultipleFieldRender();
  }

  public function _testSimpleFieldRender() {
98 99
    $view = views_get_view('test_view_fieldapi');
    $this->prepareView($view);
merlinofchaos's avatar
merlinofchaos committed
100 101 102 103 104 105 106
    $this->executeView($view);

    // Tests that the rendered fields match the actual value of the fields.
    for ($i = 0; $i < 3; $i++) {
      for ($key = 0; $key < 2; $key++) {
        $field = $this->fields[$key];
        $rendered_field = $view->style_plugin->get_field($i, $field['field_name']);
107
        $expected_field = $this->nodes[$i]->{$field['field_name']}[LANGUAGE_NOT_SPECIFIED][0]['value'];
merlinofchaos's avatar
merlinofchaos committed
108 109 110 111 112 113 114 115 116
        $this->assertEqual($rendered_field, $expected_field);
      }
    }
  }

  /**
   * Tests that fields with formatters runs as expected.
   */
  public function _testFormatterSimpleFieldRender() {
117 118
    $view = views_get_view('test_view_fieldapi');
    $this->prepareView($view);
119 120
    $view->displayHandlers['default']->options['fields'][$this->fields[0]['field_name']]['type'] = 'text_trimmed';
    $view->displayHandlers['default']->options['fields'][$this->fields[0]['field_name']]['settings'] = array(
merlinofchaos's avatar
merlinofchaos committed
121 122 123 124 125 126 127 128 129 130 131 132 133
      'trim_length' => 3,
    );
    $this->executeView($view);

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

  public function _testMultipleFieldRender() {
134
    $view = views_get_view('test_view_fieldapi');
135
    $field_name = $this->fields[3]['field_name'];
merlinofchaos's avatar
merlinofchaos committed
136 137

    // Test delta limit.
138
    $this->prepareView($view);
139 140
    $view->displayHandlers['default']->options['fields'][$field_name]['group_rows'] = TRUE;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_limit'] = 3;
merlinofchaos's avatar
merlinofchaos committed
141 142 143
    $this->executeView($view);

    for ($i = 0; $i < 3; $i++) {
144
      $rendered_field = $view->style_plugin->get_field($i, $field_name);
merlinofchaos's avatar
merlinofchaos committed
145
      $items = array();
146
      $pure_items = $this->nodes[$i]->{$field_name}[LANGUAGE_NOT_SPECIFIED];
merlinofchaos's avatar
merlinofchaos committed
147 148 149 150 151 152
      $pure_items = array_splice($pure_items, 0, 3);
      foreach ($pure_items as $j => $item) {
        $items[] = $pure_items[$j]['value'];
      }
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
    }
153 154 155 156

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

merlinofchaos's avatar
merlinofchaos committed
157 158 159
    $view->destroy();

    // Test delta limit + offset
160
    $this->prepareView($view);
161 162 163
    $view->displayHandlers['default']->options['fields'][$field_name]['group_rows'] = TRUE;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_limit'] = 3;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_offset'] = 1;
merlinofchaos's avatar
merlinofchaos committed
164 165 166
    $this->executeView($view);

    for ($i = 0; $i < 3; $i++) {
167
      $rendered_field = $view->style_plugin->get_field($i, $field_name);
merlinofchaos's avatar
merlinofchaos committed
168
      $items = array();
169
      $pure_items = $this->nodes[$i]->{$field_name}[LANGUAGE_NOT_SPECIFIED];
merlinofchaos's avatar
merlinofchaos committed
170 171 172 173 174 175 176 177 178
      $pure_items = array_splice($pure_items, 1, 3);
      foreach ($pure_items as $j => $item) {
        $items[] = $pure_items[$j]['value'];
      }
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
    }
    $view->destroy();

    // Test delta limit + reverse.
179
    $this->prepareView($view);
180 181 182 183
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_offset'] = 0;
    $view->displayHandlers['default']->options['fields'][$field_name]['group_rows'] = TRUE;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_limit'] = 3;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_reversed'] = TRUE;
merlinofchaos's avatar
merlinofchaos committed
184 185 186
    $this->executeView($view);

    for ($i = 0; $i < 3; $i++) {
187
      $rendered_field = $view->style_plugin->get_field($i, $field_name);
merlinofchaos's avatar
merlinofchaos committed
188
      $items = array();
189
      $pure_items = $this->nodes[$i]->{$field_name}[LANGUAGE_NOT_SPECIFIED];
merlinofchaos's avatar
merlinofchaos committed
190 191 192 193 194 195 196 197 198 199
      array_splice($pure_items, 0, -3);
      $pure_items = array_reverse($pure_items);
      foreach ($pure_items as $j => $item) {
        $items[] = $pure_items[$j]['value'];
      }
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
    }
    $view->destroy();

    // Test delta first last.
200
    $this->prepareView($view);
201 202 203 204
    $view->displayHandlers['default']->options['fields'][$field_name]['group_rows'] = TRUE;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_limit'] = 0;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_first_last'] = TRUE;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_reversed'] = FALSE;
merlinofchaos's avatar
merlinofchaos committed
205 206 207
    $this->executeView($view);

    for ($i = 0; $i < 3; $i++) {
208
      $rendered_field = $view->style_plugin->get_field($i, $field_name);
merlinofchaos's avatar
merlinofchaos committed
209
      $items = array();
210
      $pure_items = $this->nodes[$i]->{$field_name}[LANGUAGE_NOT_SPECIFIED];
merlinofchaos's avatar
merlinofchaos committed
211 212 213 214 215 216 217
      $items[] = $pure_items[0]['value'];
      $items[] = $pure_items[4]['value'];
      $this->assertEqual($rendered_field, implode(', ', $items), 'Take sure that the amount of items are limited.');
    }
    $view->destroy();

    // Test delta limit + custom seperator.
218
    $this->prepareView($view);
219 220 221 222
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_first_last'] = FALSE;
    $view->displayHandlers['default']->options['fields'][$field_name]['delta_limit'] = 3;
    $view->displayHandlers['default']->options['fields'][$field_name]['group_rows'] = TRUE;
    $view->displayHandlers['default']->options['fields'][$field_name]['separator'] = ':';
merlinofchaos's avatar
merlinofchaos committed
223 224 225
    $this->executeView($view);

    for ($i = 0; $i < 3; $i++) {
226
      $rendered_field = $view->style_plugin->get_field($i, $field_name);
merlinofchaos's avatar
merlinofchaos committed
227
      $items = array();
228
      $pure_items = $this->nodes[$i]->{$field_name}[LANGUAGE_NOT_SPECIFIED];
merlinofchaos's avatar
merlinofchaos committed
229 230 231 232 233 234 235 236 237
      $pure_items = array_splice($pure_items, 0, 3);
      foreach ($pure_items as $j => $item) {
        $items[] = $pure_items[$j]['value'];
      }
      $this->assertEqual($rendered_field, implode(':', $items), 'Take sure that the amount of items are limited.');
    }
  }

}