views_handler_field_date.test

Definition of ViewsHandlerFieldDateTest.

File

tests/handlers/views_handler_field_date.test
View source
  1. <?php
  2. /**
  3. * @file
  4. * Definition of ViewsHandlerFieldDateTest.
  5. */
  6. /**
  7. * Tests the core views_handler_field_date handler.
  8. */
  9. class ViewsHandlerFieldDateTest extends ViewsSqlTest {
  10. public static function getInfo() {
  11. return array(
  12. 'name' => 'Field: Date',
  13. 'description' => 'Test the core views_handler_field_date handler.',
  14. 'group' => 'Views Handlers',
  15. );
  16. }
  17. function viewsData() {
  18. $data = parent::viewsData();
  19. $data['views_test']['created']['field']['handler'] = 'views_handler_field_date';
  20. return $data;
  21. }
  22. public function testFieldDate() {
  23. $view = $this->getBasicView();
  24. $view->display['default']->handler->override_option('fields', array(
  25. 'created' => array(
  26. 'id' => 'created',
  27. 'table' => 'views_test',
  28. 'field' => 'created',
  29. 'relationship' => 'none',
  30. // c is iso 8601 date format @see http://php.net/manual/en/function.date.php
  31. 'custom_date_format' => 'c',
  32. 'second_date_format' => 'custom',
  33. 'second_date_format_custom' => 'c',
  34. ),
  35. ));
  36. $time = gmmktime(0, 0, 0, 1, 1, 2000);
  37. $this->executeView($view);
  38. $timezones = array(
  39. NULL,
  40. 'UTC',
  41. 'America/New_York',
  42. );
  43. foreach ($timezones as $timezone) {
  44. $dates = array(
  45. 'small' => format_date($time, 'small', '', $timezone),
  46. 'medium' => format_date($time, 'medium', '', $timezone),
  47. 'large' => format_date($time, 'large', '', $timezone),
  48. 'custom' => format_date($time, 'custom', 'c', $timezone),
  49. 'today time ago custom' => format_date($time, 'custom', 'c', $timezone),
  50. 'today time ago' => t('%time ago', array('%time' => format_interval(120, 2))),
  51. );
  52. $this->assertRenderedDatesEqual($view, $dates, $timezone);
  53. }
  54. $intervals = array(
  55. 'raw time ago' => format_interval(REQUEST_TIME - $time, 2),
  56. 'time ago' => t('%time ago', array('%time' => format_interval(REQUEST_TIME - $time, 2))),
  57. // TODO write tests for them
  58. // 'raw time span' => format_interval(REQUEST_TIME - $time, 2),
  59. // 'time span' => t('%time hence', array('%time' => format_interval(REQUEST_TIME - $time, 2))),
  60. );
  61. $this->assertRenderedDatesEqual($view, $intervals);
  62. }
  63. protected function assertRenderedDatesEqual($view, $map, $timezone = NULL) {
  64. foreach ($map as $date_format => $expected_result) {
  65. $check_result_number = 0;
  66. // If it's "today time ago" format we have to check the 6th element.
  67. if ($date_format == 'today time ago') {
  68. $check_result_number = 5;
  69. }
  70. // Correct the date format.
  71. if ($date_format == 'today time ago custom') {
  72. $date_format = 'today time ago';
  73. }
  74. $view->field['created']->options['date_format'] = $date_format;
  75. $t_args = array(
  76. '%value' => $expected_result,
  77. '%format' => $date_format,
  78. );
  79. if (isset($timezone)) {
  80. $t_args['%timezone'] = $timezone;
  81. $message = t('Value %value in %format format for timezone %timezone matches.', $t_args);
  82. $view->field['created']->options['timezone'] = $timezone;
  83. }
  84. else {
  85. $message = t('Value %value in %format format matches.', $t_args);
  86. }
  87. $actual_result = $view->field['created']->advanced_render($view->result[$check_result_number]);
  88. $this->assertEqual($expected_result, $actual_result, $message);
  89. }
  90. }
  91. /**
  92. * Appends dataSet() with a data row for "today time ago" format testing.
  93. */
  94. protected function dataSet() {
  95. $data = parent::dataSet();
  96. $data[] = array(
  97. 'name' => 'David',
  98. 'age' => 25,
  99. 'job' => 'Singer',
  100. 'created' => REQUEST_TIME - 120,
  101. );
  102. return $data;
  103. }
  104. }