You are here

public function FieldTest::testFieldTokens in Views (for Drupal 7) 8.3

Tests the field tokens, row level and field level.

File

lib/Drupal/views/Tests/Handler/FieldTest.php, line 479
Definition of Drupal\views\Tests\Handler\FieldTest.

Class

FieldTest
Tests the generic field handler.

Namespace

Drupal\views\Tests\Handler

Code

public function testFieldTokens() {
  $view = views_get_view('test_field_tokens');
  $this
    ->executeView($view);
  $name_field_0 = $view->field['name'];
  $name_field_1 = $view->field['name_1'];
  $name_field_2 = $view->field['name_2'];
  $row = $view->result[0];
  $name_field_0->options['alter']['alter_text'] = TRUE;
  $name_field_0->options['alter']['text'] = '[name]';
  $name_field_1->options['alter']['alter_text'] = TRUE;
  $name_field_1->options['alter']['text'] = '[name_1] [name]';
  $name_field_2->options['alter']['alter_text'] = TRUE;
  $name_field_2->options['alter']['text'] = '[name_2] [name_1]';
  foreach ($view->result as $row) {
    $expected_output_0 = $row->views_test_data_name;
    $expected_output_1 = "{$row->views_test_data_name} {$row->views_test_data_name}";
    $expected_output_2 = "{$row->views_test_data_name} {$row->views_test_data_name} {$row->views_test_data_name}";
    $output = $name_field_0
      ->advanced_render($row);
    $this
      ->assertEqual($output, $expected_output_0);
    $output = $name_field_1
      ->advanced_render($row);
    $this
      ->assertEqual($output, $expected_output_1);
    $output = $name_field_2
      ->advanced_render($row);
    $this
      ->assertEqual($output, $expected_output_2);
  }
  $job_field = $view->field['job'];
  $job_field->options['alter']['alter_text'] = TRUE;
  $job_field->options['alter']['text'] = '[test-token]';
  $random_text = $this
    ->randomName();
  $job_field
    ->setTestValue($random_text);
  $output = $job_field
    ->advanced_render($row);
  $this
    ->assertSubString($output, $random_text, format_string('Make sure the self token (!value) appears in the output (!output)' . array(
    '!value' => $random_text,
    '!output' => $output,
  )));
}