views_handler_field_boolean.test

Definition of ViewsHandlerFieldBooleanTest.

File

tests/handlers/views_handler_field_boolean.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsHandlerFieldBooleanTest.
  5. */
  6. /**
  7. * Tests the core views_handler_field_boolean handler.
  8. */
  9. class ViewsHandlerFieldBooleanTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Field: Boolean',
  13. 'description' => 'Test the core views_handler_field_boolean handler.',
  14. 'group' => 'Views Handlers',
  15. );
  16. }
  17. function dataSet() {
  18. // Use default dataset but remove the age from john and paul
  19. $data = parent::dataSet();
  20. $data[0]['age'] = 0;
  21. $data[3]['age'] = 0;
  22. return $data;
  23. }
  24. function viewsData() {
  25. $data = parent::viewsData();
  26. $data['views_test']['age']['field']['handler'] = 'views_handler_field_boolean';
  27. return $data;
  28. }
  29. public function testFieldBoolean() {
  30. $view = $this->getBasicView();
  31. $view->display['default']->handler->override_option('fields', array(
  32. 'age' => array(
  33. 'id' => 'age',
  34. 'table' => 'views_test',
  35. 'field' => 'age',
  36. 'relationship' => 'none',
  37. ),
  38. ));
  39. $this->executeView($view);
  40. // This is john, which has no age, there are no custom formats defined, yet.
  41. $this->assertEqual(t('No'), $view->field['age']->advanced_render($view->result[0]));
  42. $this->assertEqual(t('Yes'), $view->field['age']->advanced_render($view->result[1]));
  43. // Reverse the output.
  44. $view->field['age']->options['not'] = TRUE;
  45. $this->assertEqual(t('Yes'), $view->field['age']->advanced_render($view->result[0]));
  46. $this->assertEqual(t('No'), $view->field['age']->advanced_render($view->result[1]));
  47. unset($view->field['age']->options['not']);
  48. // Use another output format.
  49. $view->field['age']->options['type'] = 'true-false';
  50. $this->assertEqual(t('False'), $view->field['age']->advanced_render($view->result[0]));
  51. $this->assertEqual(t('True'), $view->field['age']->advanced_render($view->result[1]));
  52. // test awesome unicode.
  53. $view->field['age']->options['type'] = 'unicode-yes-no';
  54. $this->assertEqual('✖', $view->field['age']->advanced_render($view->result[0]));
  55. $this->assertEqual('✔', $view->field['age']->advanced_render($view->result[1]));
  56. // Set a custom output format programmatically.
  57. $view->field['age']->formats['test'] = array(t('Test-True'), t('Test-False'));
  58. $view->field['age']->options['type'] = 'test';
  59. $this->assertEqual(t('Test-False'), $view->field['age']->advanced_render($view->result[0]));
  60. $this->assertEqual(t('Test-True'), $view->field['age']->advanced_render($view->result[1]));
  61. // Set a custom output format through the UI using plain-text inputs.
  62. $view->field['age']->options['type'] = 'custom';
  63. $values = array(
  64. 'false' => 'Nay',
  65. 'true' => 'Yay',
  66. );
  67. $view->field['age']->options['type_custom_false'] = $values['false'];
  68. $view->field['age']->options['type_custom_true'] = $values['true'];
  69. $this->assertEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
  70. $this->assertEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));
  71. // Set a custom output format through the UI using valid HTML inputs.
  72. $view->field['age']->options['type'] = 'custom';
  73. $values = array(
  74. 'false' => '<div class="bar">Nay</div>',
  75. 'true' => '<div class="foo">Yay</div>',
  76. );
  77. $view->field['age']->options['type_custom_false'] = $values['false'];
  78. $view->field['age']->options['type_custom_true'] = $values['true'];
  79. $this->assertEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
  80. $this->assertEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));
  81. // Set a custom output format through the UI using unsafe inputs.
  82. $view->field['age']->options['type'] = 'custom';
  83. $values = array(
  84. 'false' => '<script>alert("Nay");</script>',
  85. 'true' => '<script>alert("Yay");</script>',
  86. );
  87. $view->field['age']->options['type_custom_false'] = $values['false'];
  88. $view->field['age']->options['type_custom_true'] = $values['true'];
  89. $this->assertNotEqual($values['false'], $view->field['age']->advanced_render($view->result[0]));
  90. $this->assertNotEqual($values['true'], $view->field['age']->advanced_render($view->result[1]));
  91. }
  92. }