Project

General

Profile

Paste
Download (3.75 KB) Statistics
| Branch: | Revision:

root / drupal7 / sites / all / modules / views / tests / handlers / views_handler_field_date.test @ 4003efde

1
<?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

    
13
  /**
14
   *
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Field: Date',
19
      'description' => 'Test the core views_handler_field_date handler.',
20
      'group' => 'Views Handlers',
21
    );
22
  }
23

    
24
  /**
25
   *
26
   */
27
  function viewsData() {
28
    $data = parent::viewsData();
29
    $data['views_test']['created']['field']['handler'] = 'views_handler_field_date';
30
    return $data;
31
  }
32

    
33
  /**
34
   *
35
   */
36
  public function testFieldDate() {
37
    $view = $this->getBasicView();
38

    
39
    $view->display['default']->handler->override_option('fields', array(
40
      'created' => array(
41
        'id' => 'created',
42
        'table' => 'views_test',
43
        'field' => 'created',
44
        'relationship' => 'none',
45
        // c is iso 8601 date format.
46
        // @see http://php.net/manual/en/function.date.php
47
        'custom_date_format' => 'c',
48
        'second_date_format' => 'custom',
49
        'second_date_format_custom' => 'c',
50
      ),
51
    ));
52
    $time = gmmktime(0, 0, 0, 1, 1, 2000);
53

    
54
    $this->executeView($view);
55

    
56
    $timezones = array(
57
      NULL,
58
      'UTC',
59
      'America/New_York',
60
    );
61
    foreach ($timezones as $timezone) {
62
      $dates = array(
63
        'small' => format_date($time, 'small', '', $timezone),
64
        'medium' => format_date($time, 'medium', '', $timezone),
65
        'large' => format_date($time, 'large', '', $timezone),
66
        'custom' => format_date($time, 'custom', 'c', $timezone),
67
        'today time ago custom' => format_date($time, 'custom', 'c', $timezone),
68
        'today time ago' => t('%time ago', array('%time' => format_interval(120, 2))),
69
      );
70
      $this->assertRenderedDatesEqual($view, $dates, $timezone);
71
    }
72

    
73
    $intervals = array(
74
      'raw time ago' => format_interval(REQUEST_TIME - $time, 2),
75
      'time ago' => t('%time ago', array('%time' => format_interval(REQUEST_TIME - $time, 2))),
76
      // @todo write tests for them
77
      // 'raw time span' => format_interval(REQUEST_TIME - $time, 2),
78
      // 'time span' => t('%time hence',
79
      //   array('%time' => format_interval(REQUEST_TIME - $time, 2))),
80
    );
81
    $this->assertRenderedDatesEqual($view, $intervals);
82
  }
83

    
84
  /**
85
   *
86
   */
87
  protected function assertRenderedDatesEqual($view, $map, $timezone = NULL) {
88
    foreach ($map as $date_format => $expected_result) {
89
      $check_result_number = 0;
90

    
91
      // If it's "today time ago" format we have to check the 6th element.
92
      if ($date_format == 'today time ago') {
93
        $check_result_number = 5;
94
      }
95

    
96
      // Correct the date format.
97
      if ($date_format == 'today time ago custom') {
98
        $date_format = 'today time ago';
99
      }
100
      $view->field['created']->options['date_format'] = $date_format;
101
      $t_args = array(
102
        '%value' => $expected_result,
103
        '%format' => $date_format,
104
      );
105
      if (isset($timezone)) {
106
        $t_args['%timezone'] = $timezone;
107
        $message = t('Value %value in %format format for timezone %timezone matches.', $t_args);
108
        $view->field['created']->options['timezone'] = $timezone;
109
      }
110
      else {
111
        $message = t('Value %value in %format format matches.', $t_args);
112
      }
113
      $actual_result = $view->field['created']->advanced_render($view->result[$check_result_number]);
114
      $this->assertEqual($expected_result, $actual_result, $message);
115
    }
116
  }
117

    
118
  /**
119
   * Appends dataSet() with a data row for "today time ago" format testing.
120
   */
121
  protected function dataSet() {
122
    $data = parent::dataSet();
123
    $data[] = array(
124
      'name' => 'David',
125
      'age' => 25,
126
      'job' => 'Singer',
127
      'created' => REQUEST_TIME - 120,
128
    );
129

    
130
    return $data;
131
  }
132

    
133
}