function HandlerFieldFieldTest::doTestMultipleFieldRender

Same name in other branches
  1. 9 core/modules/field/tests/src/Kernel/Views/HandlerFieldFieldTest.php \Drupal\Tests\field\Kernel\Views\HandlerFieldFieldTest::doTestMultipleFieldRender()
  2. 8.9.x core/modules/field/tests/src/Kernel/Views/HandlerFieldFieldTest.php \Drupal\Tests\field\Kernel\Views\HandlerFieldFieldTest::doTestMultipleFieldRender()
  3. 11.x core/modules/field/tests/src/Kernel/Views/HandlerFieldFieldTest.php \Drupal\Tests\field\Kernel\Views\HandlerFieldFieldTest::doTestMultipleFieldRender()

Tests multi-value field rendering.

1 call to HandlerFieldFieldTest::doTestMultipleFieldRender()
HandlerFieldFieldTest::testFieldRender in core/modules/field/tests/src/Kernel/Views/HandlerFieldFieldTest.php
Tests fields rendering in views.

File

core/modules/field/tests/src/Kernel/Views/HandlerFieldFieldTest.php, line 175

Class

HandlerFieldFieldTest
Tests the field rendering in views.

Namespace

Drupal\Tests\field\Kernel\Views

Code

public function doTestMultipleFieldRender() {
    $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;
    $view->preview();
    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'];
        }
        // Check that the amount of items is limited.
        $this->assertEquals(implode(', ', $items), $rendered_field);
    }
    // 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;
    $view->preview();
    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, 1, 3);
        foreach ($pure_items as $j => $item) {
            $items[] = $pure_items[$j]['value'];
        }
        // Check that the amount of items is limited and the offset is correct.
        $this->assertEquals(implode(', ', $items), $rendered_field);
    }
    $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;
    $view->preview();
    for ($i = 0; $i < 3; $i++) {
        $rendered_field = $view->style_plugin
            ->getField($i, $field_name);
        $items = [];
        $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'];
        }
        // Check that the amount of items is limited and they are reversed.
        $this->assertEquals(implode(', ', $items), $rendered_field);
    }
    $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;
    $view->preview();
    for ($i = 0; $i < 3; $i++) {
        $rendered_field = $view->style_plugin
            ->getField($i, $field_name);
        $items = [];
        $pure_items = $this->nodes[$i]->{$field_name}
            ->getValue();
        $items[] = $pure_items[0]['value'];
        $items[] = $pure_items[4]['value'];
        // Check that items are limited to first and last.
        $this->assertEquals(implode(', ', $items), $rendered_field);
    }
    $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'] = ':';
    $view->preview();
    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'];
        }
        // Check that the amount of items is limited and the custom separator is
        // correct.
        $this->assertEquals(implode(':', $items), $rendered_field);
    }
    $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>';
    $view->preview();
    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'];
        }
        // Check that the custom separator is correctly escaped.
        $this->assertSame(implode('<h2>test</h2>', $items), (string) $rendered_field);
    }
}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.