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