Projet

Général

Profil

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

root / drupal7 / sites / all / modules / date / tests / date_validation.test @ db9ffd17

1
<?php
2
/**
3
 * @file
4
 * Date validation tests.
5
 */
6
class DateValidationTestCase extends DateFieldBasic {
7

    
8
  /**
9
   * @todo.
10
   */
11
  public static function getInfo() {
12
    return array(
13
      'name' => 'Date Validation',
14
      'description' => 'Test date validation.',
15
      'group' => 'Date',
16
    );
17
  }
18

    
19
  /**
20
   * @todo.
21
   */
22
  public function testValidation() {
23
    // Attempts to create text date field stored as a date with default settings
24
    // (from input which is not valid).
25
    foreach (array('date', 'datestamp', 'datetime') as $field_type) {
26
      foreach (array('date_select', 'date_popup', 'date_text') as $widget_type) {
27
        $field_name = 'field_test';
28
        $label = 'Test';
29
        $options = array(
30
          'label' => $label,
31
          'field_name' => $field_name,
32
          'field_type' => $field_type,
33
          'widget_type' => $widget_type,
34
          'input_format' => 'm/d/Y - H:i',
35
        );
36
        $this->createDateField($options);
37

    
38
        // Malformed date test won't work on date_select, which won't allow
39
        // invalid input.
40
        if ($widget_type != 'date_select') {
41
          $this->malFormedDate($field_name, $field_type, $widget_type);
42
        }
43

    
44
        $this->wrongGranularity($field_name, $field_type, $widget_type);
45
        $this->deleteDateField($label);
46
      }
47
    }
48
  }
49

    
50
  /**
51
   * @todo.
52
   */
53
  function malFormedDate($field_name, $field_type, $widget_type) {
54
    // Tests that date field filters improper dates.
55
    $edit = array();
56
    $edit['title'] = $this->randomName(8);
57
    $edit['body[und][0][value]'] = $this->randomName(16);
58
    if ($widget_type == 'date_select') {
59
      $edit[$field_name . '[und][0][value][year]'] = '2011';
60
      $edit[$field_name . '[und][0][value][month]'] = '15';
61
      $edit[$field_name . '[und][0][value][day]'] = '49';
62
      $edit[$field_name . '[und][0][value][hour]'] = '10';
63
      $edit[$field_name . '[und][0][value][minute]'] = '30';
64
    }
65
    elseif ($widget_type == 'date_text') {
66
      $edit[$field_name . '[und][0][value][date]'] = '15/49/2011 - 10:30';
67
    }
68
    elseif ($widget_type == 'date_popup') {
69
      $edit[$field_name . '[und][0][value][date]'] = '15/49/2011';
70
      $edit[$field_name . '[und][0][value][time]'] = '10:30';
71
    }
72
    $this->drupalPost('node/add/story', $edit, t('Save'));
73
    $should_not_be = $edit['title'] . "has been created";
74
    $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.");
75
    $this->assertText('The month is invalid.', "Correctly blocked invalid month for a $field_type field using the $widget_type widget.");
76
    $this->assertText('The day is invalid.', "Correctly blocked invalid day for a $field_type field using the $widget_type widget.");
77

    
78
    // Test two-digit entry for year where 4-digit is expected.
79
    if ($widget_type == 'date_select') {
80
      $edit[$field_name . '[und][0][value][year]'] = '11';
81
      $edit[$field_name . '[und][0][value][month]'] = '12';
82
      $edit[$field_name . '[und][0][value][day]'] = '10';
83
      $edit[$field_name . '[und][0][value][hour]'] = '10';
84
      $edit[$field_name . '[und][0][value][minute]'] = '30';
85
    }
86
    elseif ($widget_type == 'date_text') {
87
      $edit[$field_name . '[und][0][value][date]'] = '12/10/11 - 10:30';
88
    }
89
    elseif ($widget_type == 'date_popup') {
90
      $edit[$field_name . '[und][0][value][date]'] = '12/10/11';
91
      $edit[$field_name . '[und][0][value][time]'] = '10:30';
92
    }
93
    $this->drupalPost('node/add/story', $edit, t('Save'));
94
    $should_not_be = $edit['title'] . " has been created";
95
    $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid year for a $field_type field using the $widget_type widget.");
96
    $should_be = 'The year is invalid. Please check that entry includes four digits.';
97
    $this->assertText($should_be, "Correctly blocked two digit year for a $field_type field using the $widget_type widget.");
98

    
99
    // Test invalid hour/minute entry for time.
100
    if ($widget_type == 'date_select') {
101
      $edit[$field_name . '[und][0][value][year]'] = '2011';
102
      $edit[$field_name . '[und][0][value][month]'] = '12';
103
      $edit[$field_name . '[und][0][value][day]'] = '10';
104
      $edit[$field_name . '[und][0][value][hour]'] = '29';
105
      $edit[$field_name . '[und][0][value][minute]'] = '95';
106
    }
107
    elseif ($widget_type == 'date_text') {
108
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011 - 29:95';
109
    }
110
    elseif ($widget_type == 'date_popup') {
111
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
112
      $edit[$field_name . '[und][0][value][time]'] = '29:95';
113
    }
114
    $this->drupalPost('node/add/story', $edit, t('Save'));
115
    $should_not_be = $edit['title'] . " has been created";
116
    $this->assertNoText($should_not_be, "Correctly blocked creation of node with invalid time for a $field_type field using the $widget_type widget.");
117
    $should_be = 'The hour is invalid.';
118
    $this->assertText($should_be, "Correctly blocked invalid hour for a $field_type field using the $widget_type widget.");
119
    $should_be = 'The minute is invalid.';
120
    $this->assertText($should_be, "Correctly blocked invalid minute for a $field_type field using the $widget_type widget.");
121

    
122
  }
123

    
124
  /**
125
   * @todo.
126
   */
127
  public function wrongGranularity($field_name, $field_type, $widget_type) {
128
    // Create a node with incorrect granularity -- missing time.
129
    $edit = array();
130
    $edit['title'] = $this->randomName(8);
131
    $edit['body[und][0][value]'] = $this->randomName(16);
132
    if ($widget_type == 'date_select') {
133
      $edit[$field_name . '[und][0][value][year]'] = '2011';
134
      $edit[$field_name . '[und][0][value][month]'] = '12';
135
      $edit[$field_name . '[und][0][value][day]'] = '10';
136
      $edit[$field_name . '[und][0][value][hour]'] = '';
137
      $edit[$field_name . '[und][0][value][minute]'] = '';
138
    }
139
    elseif ($widget_type == 'date_text') {
140
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
141
    }
142
    elseif ($widget_type == 'date_popup') {
143
      $edit[$field_name . '[und][0][value][date]'] = '12/10/2011';
144
      $edit[$field_name . '[und][0][value][time]'] = '';
145
    }
146
    $this->drupalPost('node/add/story', $edit, t('Save'));
147
    $should_not_be = $edit['title'] . " has been created";
148
    $this->assertNoText($should_not_be, "Correctly blocked creation of node with missing time for a $field_type field using the $widget_type widget.");
149
    $this->assertText('invalid', "Marked form with missing time as invalid for a $field_type field using the $widget_type widget.");
150
  }
151
}