You are here

public function ViewFieldUnitTest::testFieldHandlerMethods in Views Field View 8

Test field handler methods in a unit test like way.

File

tests/src/Kernel/ViewFieldUnitTest.php, line 68

Class

ViewFieldUnitTest
Tests the views field view handler methods.

Namespace

Drupal\Tests\views_field_view\Kernel

Code

public function testFieldHandlerMethods() {
  $view = Views::getView('views_field_view_test_parent_normal');
  $view
    ->initDisplay();
  $view
    ->initHandlers();
  $view
    ->setArguments([
    'Hey jude',
  ]);
  $view
    ->execute();
  $view->style_plugin
    ->render();

  /** @var \Drupal\views_field_view\Plugin\views\field\View $field_handler */
  $field_handler = $view->field['view'];
  $this
    ->assertTrue($field_handler instanceof ViewField);

  // Test the split_tokens() method.
  $result = $field_handler
    ->splitTokens('{{ raw_fields.uid }},{{ fields.nid }}');
  $expected = [
    '{{ raw_fields.uid }}',
    '{{ fields.nid }}',
  ];
  $this
    ->assertEquals($result, $expected, 'The token string has been split correctly (",").');
  $result = $field_handler
    ->splitTokens('{{ raw_fields.uid }}/{{ fields.nid }}');
  $this
    ->assertEquals($result, $expected, 'The token string has been split correctly ("/").');

  // Test the get_token_argument() method.
  $result = $field_handler
    ->getTokenValue('{{ raw_fields.id }}', $view->result[0], $view);
  $this
    ->assertEquals(2, $result);
  $result = $field_handler
    ->getTokenValue('{{ fields.id }}', $view->result[0], $view);
  $this
    ->assertEquals(3, $result);
  $result = $field_handler
    ->getTokenValue('{{ raw_fields.id_1 }}', $view->result[0], $view);
  $this
    ->assertEquals(2, $result);
  $result = $field_handler
    ->getTokenValue('{{ fields.id_1 }}', $view->result[0], $view);
  $this
    ->assertEquals(3, $result);
  $result = $field_handler
    ->getTokenValue('{{ raw_fields.name }}', $view->result[0], $view);
  $this
    ->assertEquals('George', $result);
  $result = $field_handler
    ->getTokenValue('{{ fields.name }}', $view->result[0], $view);
  $this
    ->assertEquals('Ringo', $result);
  $result = $field_handler
    ->getTokenValue('{{ raw_arguments.null }}', $view->result[0], $view);
  $this
    ->assertEquals('Hey jude', $result);
  $result = $field_handler
    ->getTokenValue('{{ arguments.null }}', $view->result[0], $view);
  $this
    ->assertEquals('Hey jude', $result);
}