You are here

public function FieldBooleanTest::testFieldBoolean in Drupal 8

Same name and namespace in other branches
  1. 9 core/modules/views/tests/src/Kernel/Handler/FieldBooleanTest.php \Drupal\Tests\views\Kernel\Handler\FieldBooleanTest::testFieldBoolean()

File

core/modules/views/tests/src/Kernel/Handler/FieldBooleanTest.php, line 36

Class

FieldBooleanTest
Tests the core Drupal\views\Plugin\views\field\Boolean handler.

Namespace

Drupal\Tests\views\Kernel\Handler

Code

public function testFieldBoolean() {
  $view = Views::getView('test_view');
  $view
    ->setDisplay();
  $view->displayHandlers
    ->get('default')
    ->overrideOption('fields', [
    'age' => [
      'id' => 'age',
      'table' => 'views_test_data',
      'field' => 'age',
      'relationship' => 'none',
    ],
  ]);
  $this
    ->executeView($view);

  // This is john, which has no age, there are no custom formats defined, yet.
  $this
    ->assertEqual(t('No'), $view->field['age']
    ->advancedRender($view->result[0]));
  $this
    ->assertEqual(t('Yes'), $view->field['age']
    ->advancedRender($view->result[1]));

  // Reverse the output.
  $view->field['age']->options['not'] = TRUE;
  $this
    ->assertEqual(t('Yes'), $view->field['age']
    ->advancedRender($view->result[0]));
  $this
    ->assertEqual(t('No'), $view->field['age']
    ->advancedRender($view->result[1]));
  unset($view->field['age']->options['not']);

  // Use another output format.
  $view->field['age']->options['type'] = 'true-false';
  $this
    ->assertEqual(t('False'), $view->field['age']
    ->advancedRender($view->result[0]));
  $this
    ->assertEqual(t('True'), $view->field['age']
    ->advancedRender($view->result[1]));

  // test awesome unicode.
  $view->field['age']->options['type'] = 'unicode-yes-no';
  $this
    ->assertEqual('✖', $view->field['age']
    ->advancedRender($view->result[0]));
  $this
    ->assertEqual('✔', $view->field['age']
    ->advancedRender($view->result[1]));

  // Set a custom output format.
  $view->field['age']->formats['test'] = [
    t('Test-True'),
    t('Test-False'),
  ];
  $view->field['age']->options['type'] = 'test';
  $this
    ->assertEqual(t('Test-False'), $view->field['age']
    ->advancedRender($view->result[0]));
  $this
    ->assertEqual(t('Test-True'), $view->field['age']
    ->advancedRender($view->result[1]));
}