Projet

Général

Profil

Paste
Télécharger (6,68 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / date / tests / DateValidationTestCase.test @ 599a39cd

1
<?php
2

    
3
/**
4
 * @file
5
 * Validate date field values.
6
 */
7

    
8
/**
9
 * Validate date field values.
10
 */
11
class DateValidationTestCase extends DateFieldTestBase {
12

    
13
  /**
14
   * @todo.
15
   */
16
  public static function getInfo() {
17
    return array(
18
      'name' => 'Date Validation',
19
      'description' => 'Test date validation.',
20
      'group' => 'Date',
21
    );
22
  }
23

    
24
  /**
25
   * @todo.
26
   */
27
  public function testValidation() {
28
    // Attempts to create text date field stored as a date with default settings
29
    // (from input which is not valid).
30
    foreach (array('date', 'datestamp', 'datetime') as $field_type) {
31
      foreach (array('date_select', 'date_text') as $widget_type) {
32
        $this->checkDateField($field_type, $widget_type, TRUE);
33
        return;
34
      }
35
    }
36
  }
37

    
38
  /**
39
   * {@inheritdoc}
40
   */
41
  protected function checkDateField($field_type, $widget_type, $delete_when_done = TRUE) {
42
    $field_name = 'field_test';
43
    $label = 'Test';
44
    $options = array(
45
      'label' => $label,
46
      'field_name' => $field_name,
47
      'field_type' => $field_type,
48
      'widget_type' => $widget_type,
49
      'input_format' => 'm/d/Y - H:i',
50
    );
51
    $this->createDateField($options);
52

    
53
    // Malformed date test won't work on date_select, which won't allow
54
    // invalid input.
55
    if ($widget_type != 'date_select') {
56
      $this->malFormedDate($field_name, $field_type, $widget_type);
57
    }
58

    
59
    $this->wrongGranularity($field_name, $field_type, $widget_type);
60

    
61
    if ($delete_when_done) {
62
      $this->deleteDateField($label);
63
    }
64
  }
65

    
66
  /**
67
   * @todo.
68
   */
69
  function malFormedDate($field_name, $field_type, $widget_type) {
70
    // Tests that date field filters improper dates.
71
    $edit = array();
72
    $edit['title'] = $this->randomName(8);
73
    $edit['body[und][0][value]'] = $this->randomName(16);
74
    if ($widget_type == 'date_select') {
75
      $edit[$field_name . '[und][0][value][year]'] = '2011';
76
      $edit[$field_name . '[und][0][value][month]'] = '15';
77
      $edit[$field_name . '[und][0][value][day]'] = '49';
78
      $edit[$field_name . '[und][0][value][hour]'] = '10';
79
      $edit[$field_name . '[und][0][value][minute]'] = '30';
80
    }
81
    elseif ($widget_type == 'date_text') {
82
      $edit[$field_name . '[und][0][value][date]'] = '15/49/2011 - 10:30';
83
    }
84
    elseif ($widget_type == 'date_popup') {
85
      $edit[$field_name . '[und][0][value][date]'] = '15/49/2011';
86
      $edit[$field_name . '[und][0][value][time]'] = '10:30';
87
    }
88
    $this->drupalPost('node/add/story', $edit, t('Save'));
89
    $should_not_be = $edit['title'] . "has been created";
90
    $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid month and day for a $field_type field using the $widget_type widget.");
91
    $this->assertText('The month is invalid.', "Correctly blocked invalid month for a $field_type field using the $widget_type widget.");
92
    $this->assertText('The day is invalid.', "Correctly blocked invalid day for a $field_type field using the $widget_type widget.");
93

    
94
    // Test two-digit entry for year where 4-digit is expected.
95
    if ($widget_type == 'date_select') {
96
      $edit[$field_name . '[und][0][value][year]'] = '11';
97
      $edit[$field_name . '[und][0][value][month]'] = '12';
98
      $edit[$field_name . '[und][0][value][day]'] = '10';
99
      $edit[$field_name . '[und][0][value][hour]'] = '10';
100
      $edit[$field_name . '[und][0][value][minute]'] = '30';
101
    }
102
    elseif ($widget_type == 'date_text') {
103
      $edit[$field_name . '[und][0][value][date]'] = '12/10/11 - 10:30';
104
    }
105
    elseif ($widget_type == 'date_popup') {
106
      $edit[$field_name . '[und][0][value][date]'] = '12/10/11';
107
      $edit[$field_name . '[und][0][value][time]'] = '10:30';
108
    }
109
    $this->drupalPost('node/add/story', $edit, t('Save'));
110
    $should_not_be = $edit['title'] . " has been created";
111
    $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid year for a $field_type field using the $widget_type widget.");
112
    $should_be = 'The year is invalid. Please check that entry includes four digits.';
113
    $this->assertText($should_be, "Correctly blocked two digit year for a $field_type field using the $widget_type widget.");
114

    
115
    // Test invalid hour/minute entry for time.
116
    if ($widget_type == 'date_select') {
117
      $edit[$field_name . '[und][0][value][year]'] = '2011';
118
      $edit[$field_name . '[und][0][value][month]'] = '12';
119
      $edit[$field_name . '[und][0][value][day]'] = '10';
120
      $edit[$field_name . '[und][0][value][hour]'] = '29';
121
      $edit[$field_name . '[und][0][value][minute]'] = '95';
122
    }
123
    elseif ($widget_type == 'date_text') {
124
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011 - 29:95';
125
    }
126
    elseif ($widget_type == 'date_popup') {
127
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
128
      $edit[$field_name . '[und][0][value][time]'] = '29:95';
129
    }
130
    $this->drupalPost('node/add/story', $edit, t('Save'));
131
    $should_not_be = $edit['title'] . " has been created";
132
    $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid time for a $field_type field using the $widget_type widget.");
133
    $should_be = 'The hour is invalid.';
134
    $this->assertText($should_be, "Correctly blocked invalid hour for a $field_type field using the $widget_type widget.");
135
    $should_be = 'The minute is invalid.';
136
    $this->assertText($should_be, "Correctly blocked invalid minute for a $field_type field using the $widget_type widget.");
137
  }
138

    
139
  /**
140
   * @todo.
141
   */
142
  public function wrongGranularity($field_name, $field_type, $widget_type) {
143
    // Create a node with incorrect granularity -- missing time.
144
    $edit = array();
145
    $edit['title'] = $this->randomName(8);
146
    $edit['body[und][0][value]'] = $this->randomName(16);
147
    if ($widget_type == 'date_select') {
148
      $edit[$field_name . '[und][0][value][year]'] = '2011';
149
      $edit[$field_name . '[und][0][value][month]'] = '12';
150
      $edit[$field_name . '[und][0][value][day]'] = '10';
151
      $edit[$field_name . '[und][0][value][hour]'] = '';
152
      $edit[$field_name . '[und][0][value][minute]'] = '';
153
    }
154
    elseif ($widget_type == 'date_text') {
155
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
156
    }
157
    elseif ($widget_type == 'date_popup') {
158
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
159
      $edit[$field_name . '[und][0][value][time]'] = '';
160
    }
161
    $this->drupalPost('node/add/story', $edit, t('Save'));
162
    $should_not_be = $edit['title'] . " has been created";
163
    $this->assertNoText($should_not_be, "Correctly blocked creation of node with missing time for a $field_type field using the $widget_type widget.");
164
    $this->assertText('invalid', "Marked form with missing time as invalid for a $field_type field using the $widget_type widget.");
165
  }
166

    
167
}