You are here

public function HandlerFieldFieldTest::_testMultipleFieldRender in Zircon Profile 8

Same name and namespace in other branches
  1. 8.0 core/modules/field/src/Tests/Views/HandlerFieldFieldTest.php \Drupal\field\Tests\Views\HandlerFieldFieldTest::_testMultipleFieldRender()
1 call to HandlerFieldFieldTest::_testMultipleFieldRender()
HandlerFieldFieldTest::testFieldRender in core/modules/field/src/Tests/Views/HandlerFieldFieldTest.php

File

core/modules/field/src/Tests/Views/HandlerFieldFieldTest.php, line 191
Contains \Drupal\field\Tests\Views\HandlerFieldFieldTest.

Class

HandlerFieldFieldTest
Tests the field itself of the Field integration.

Namespace

Drupal\field\Tests\Views

Code

public function _testMultipleFieldRender() {
  $view = Views::getView('test_view_fieldapi');
  $field_name = $this->fieldStorages[3]
    ->getName();

  // Test delta limit.
  $this
    ->prepareView($view);
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['group_rows'] = TRUE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_limit'] = 3;
  $this
    ->executeView($view);
  for ($i = 0; $i < 3; $i++) {
    $rendered_field = $view->style_plugin
      ->getField($i, $field_name);
    $items = array();
    $pure_items = $this->nodes[$i]->{$field_name}
      ->getValue();
    $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), 'The amount of items is limited.');
  }

  // Test that an empty field is rendered without error.
  $view->style_plugin
    ->getField(4, $this->fieldStorages[4]
    ->getName());
  $view
    ->destroy();

  // Test delta limit + offset
  $this
    ->prepareView($view);
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['group_rows'] = TRUE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_limit'] = 3;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_offset'] = 1;
  $this
    ->executeView($view);
  for ($i = 0; $i < 3; $i++) {
    $rendered_field = $view->style_plugin
      ->getField($i, $field_name);
    $items = array();
    $pure_items = $this->nodes[$i]->{$field_name}
      ->getValue();
    $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), 'The amount of items is limited and the offset is correct.');
  }
  $view
    ->destroy();

  // Test delta limit + reverse.
  $this
    ->prepareView($view);
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_offset'] = 0;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['group_rows'] = TRUE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_limit'] = 3;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_reversed'] = TRUE;
  $this
    ->executeView($view);
  for ($i = 0; $i < 3; $i++) {
    $rendered_field = $view->style_plugin
      ->getField($i, $field_name);
    $items = array();
    $pure_items = $this->nodes[$i]->{$field_name}
      ->getValue();
    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), 'The amount of items is limited and they are reversed.');
  }
  $view
    ->destroy();

  // Test delta first last.
  $this
    ->prepareView($view);
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['group_rows'] = TRUE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_limit'] = 0;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_first_last'] = TRUE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_reversed'] = FALSE;
  $this
    ->executeView($view);
  for ($i = 0; $i < 3; $i++) {
    $rendered_field = $view->style_plugin
      ->getField($i, $field_name);
    $items = array();
    $pure_items = $this->nodes[$i]->{$field_name}
      ->getValue();
    $items[] = $pure_items[0]['value'];
    $items[] = $pure_items[4]['value'];
    $this
      ->assertEqual($rendered_field, implode(', ', $items), 'Items are limited to first and last.');
  }
  $view
    ->destroy();

  // Test delta limit + custom separator.
  $this
    ->prepareView($view);
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_first_last'] = FALSE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_limit'] = 3;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['group_rows'] = TRUE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['separator'] = ':';
  $this
    ->executeView($view);
  for ($i = 0; $i < 3; $i++) {
    $rendered_field = $view->style_plugin
      ->getField($i, $field_name);
    $items = array();
    $pure_items = $this->nodes[$i]->{$field_name}
      ->getValue();
    $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), 'The amount of items is limited and the custom separator is correct.');
  }
  $view
    ->destroy();

  // Test separator with HTML, ensure it is escaped.
  $this
    ->prepareView($view);
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['group_rows'] = TRUE;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['delta_limit'] = 3;
  $view->displayHandlers
    ->get('default')->options['fields'][$field_name]['separator'] = '<h2>test</h2>';
  $this
    ->executeView($view);
  for ($i = 0; $i < 3; $i++) {
    $rendered_field = $view->style_plugin
      ->getField($i, $field_name);
    $items = [];
    $pure_items = $this->nodes[$i]->{$field_name}
      ->getValue();
    $pure_items = array_splice($pure_items, 0, 3);
    foreach ($pure_items as $j => $item) {
      $items[] = $pure_items[$j]['value'];
    }
    $this
      ->assertEqual($rendered_field, implode('<h2>test</h2>', $items), 'The custom separator is correctly escaped.');
  }
  $view
    ->destroy();
}