1 views_handler_field_boolean.test public ViewsHandlerFieldBooleanTest::testFieldBoolean()

File

core/modules/views/tests/handlers/views_handler_field_boolean.test, line 27
Definition of ViewsHandlerFieldBooleanTest.

Class

ViewsHandlerFieldBooleanTest
Tests the core views_handler_field_boolean handler.

Code

public function testFieldBoolean() {
  $view = $this->getBasicView();

  $view->display['default']->handler->override_option('fields', array(
    'age' => array(
      'id' => 'age',
      'table' => 'views_test',
      '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']->advanced_render($view->result[0]));
  $this->assertEqual(t('Yes'), $view->field['age']->advanced_render($view->result[1]));

  // Reverse the output.
  $view->field['age']->options['not'] = TRUE;
  $this->assertEqual(t('Yes'), $view->field['age']->advanced_render($view->result[0]));
  $this->assertEqual(t('No'), $view->field['age']->advanced_render($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']->advanced_render($view->result[0]));
  $this->assertEqual(t('True'), $view->field['age']->advanced_render($view->result[1]));

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

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

  // Set a custom output format through the UI using plain-text inputs.
  $view->field['age']->options['type'] = 'custom';
  $values = array(
    'false' => 'Nay',
    'true' => 'Yay',
  );
  $view->field['age']->options['type_custom_false'] = $values['false'];
  $view->field['age']->options['type_custom_true'] = $values['true'];
  $this->assertEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
  $this->assertEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));

  // Set a custom output format through the UI using valid HTML inputs.
  $view->field['age']->options['type'] = 'custom';
  $values = array(
    'false' => '<div class="bar">Nay</div>',
    'true' => '<div class="foo">Yay</div>',
  );
  $view->field['age']->options['type_custom_false'] = $values['false'];
  $view->field['age']->options['type_custom_true'] = $values['true'];
  $this->assertEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
  $this->assertEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));

  // Set a custom output format through the UI using unsafe inputs.
  $view->field['age']->options['type'] = 'custom';
  $values = array(
    'false' => '<script>alert("Nay");</script>',
    'true' => '<script>alert("Yay");</script>',
  );
  $view->field['age']->options['type_custom_false'] = $values['false'];
  $view->field['age']->options['type_custom_true'] = $values['true'];
  $this->assertNotEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
  $this->assertNotEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));
}