1
|
<?php
|
2
|
|
3
|
/**
|
4
|
* @file
|
5
|
* Basic functions for Date tests.
|
6
|
*/
|
7
|
abstract class DateFieldBasic extends DrupalWebTestCase {
|
8
|
protected $privileged_user;
|
9
|
|
10
|
/**
|
11
|
* @todo.
|
12
|
*/
|
13
|
protected function setUp() {
|
14
|
// Load the date_api module.
|
15
|
parent::setUp('field', 'field_ui', 'date_api', 'date', 'date_popup', 'date_tools');
|
16
|
|
17
|
// Create and log in our privileged user.
|
18
|
$this->privileged_user = $this->drupalCreateUser(
|
19
|
array('administer content types', 'administer nodes', 'bypass node access', 'administer date tools')
|
20
|
);
|
21
|
$this->drupalLogin($this->privileged_user);
|
22
|
|
23
|
variable_set('date_popup_timepicker', 'none');
|
24
|
|
25
|
module_load_include('inc', 'node', 'content_types');
|
26
|
module_load_include('inc', 'node', 'node.pages');
|
27
|
module_load_include('inc', 'field', 'field.crud');
|
28
|
module_load_include('inc', 'date', 'date_admin');
|
29
|
|
30
|
$edit = array();
|
31
|
$edit['name'] = 'Story';
|
32
|
$edit['type'] = 'story';
|
33
|
$this->drupalPost('admin/structure/types/add', $edit, t('Save content type'));
|
34
|
$this->assertText('The content type Story has been added.', 'Content type added.');
|
35
|
|
36
|
}
|
37
|
|
38
|
/**
|
39
|
* Creates a date field from an array of settings values.
|
40
|
*
|
41
|
* All values have defaults, only need to specify values that need to be
|
42
|
* different.
|
43
|
*/
|
44
|
protected function createDateField($values = array()) {
|
45
|
extract($values);
|
46
|
|
47
|
$field_name = !empty($field_name) ? $field_name : 'field_test';
|
48
|
$entity_type = !empty($entity_type) ? $entity_type : 'node';
|
49
|
$bundle = !empty($bundle) ? $bundle : 'story';
|
50
|
$label = !empty($label) ? $label : 'Test';
|
51
|
$field_type = !empty($field_type) ? $field_type : 'datetime';
|
52
|
$repeat = !empty($repeat) ? $repeat : 0;
|
53
|
$todate = !empty($todate) ? $todate : 'optional';
|
54
|
$widget_type = !empty($widget_type) ? $widget_type : 'date_select';
|
55
|
$tz_handling = !empty($tz_handing) ? $tz_handling : 'site';
|
56
|
$granularity = !empty($granularity) ? $granularity : array('year', 'month', 'day', 'hour', 'minute');
|
57
|
$year_range = !empty($year_range) ? $year_range : '2010:+1';
|
58
|
$input_format = !empty($input_format) ? $input_format : date_default_format($widget_type);
|
59
|
$input_format_custom = !empty($input_format_custom) ? $input_format_custom : '';
|
60
|
$text_parts = !empty($text_parts) ? $text_parts : array();
|
61
|
$increment = !empty($increment) ? $increment : 15;
|
62
|
$default_value = !empty($default_value) ? $default_value : 'now';
|
63
|
$default_value2 = !empty($default_value2) ? $default_value2 : 'blank';
|
64
|
$default_format = !empty($default_format) ? $default_format : 'long';
|
65
|
$cache_enabled = !empty($cache_enabled);
|
66
|
$cache_count = !empty($cache_count) ? $cache_count : 4;
|
67
|
|
68
|
$field = array(
|
69
|
'field_name' => $field_name,
|
70
|
'type' => $field_type,
|
71
|
'cardinality' => !empty($repeat) ? FIELD_CARDINALITY_UNLIMITED : 1,
|
72
|
'settings' => array(
|
73
|
'granularity' => $granularity,
|
74
|
'tz_handling' => $tz_handling,
|
75
|
'timezone_db' => date_get_timezone_db($tz_handling),
|
76
|
'repeat' => $repeat,
|
77
|
'todate' => $todate,
|
78
|
'cache_enabled' => $cache_enabled,
|
79
|
'cache_count' => $cache_count,
|
80
|
),
|
81
|
);
|
82
|
$instance = array(
|
83
|
'entity_type' => $entity_type,
|
84
|
'field_name' => $field_name,
|
85
|
'label' => $label,
|
86
|
'bundle' => $bundle,
|
87
|
// Move the date right below the title.
|
88
|
'weight' => -4,
|
89
|
'widget' => array(
|
90
|
'type' => $widget_type,
|
91
|
// Increment for minutes and seconds, can be 1, 5, 10, 15, or 30.
|
92
|
'settings' => array(
|
93
|
'increment' => $increment,
|
94
|
// The number of years to go back and forward in drop-down year
|
95
|
// selectors.
|
96
|
'year_range' => $year_range,
|
97
|
'input_format' => $input_format,
|
98
|
'input_format_custom' => $input_format_custom,
|
99
|
'text_parts' => $text_parts,
|
100
|
'label_position' => 'above',
|
101
|
'repeat_collapsed' => 0,
|
102
|
),
|
103
|
'weight' => -4,
|
104
|
),
|
105
|
'settings' => array(
|
106
|
'default_value' => $default_value,
|
107
|
'default_value2' => $default_value2,
|
108
|
),
|
109
|
);
|
110
|
|
111
|
$instance['display'] = array(
|
112
|
'default' => array(
|
113
|
'label' => 'above',
|
114
|
'type' => 'date_default',
|
115
|
'settings' => array(
|
116
|
'format_type' => $default_format,
|
117
|
'show_repeat_rule' => 'show',
|
118
|
'multiple_number' => '',
|
119
|
'multiple_from' => '',
|
120
|
'multiple_to' => '',
|
121
|
'fromto' => 'both',
|
122
|
),
|
123
|
'module' => 'date',
|
124
|
'weight' => 0 ,
|
125
|
),
|
126
|
'teaser' => array(
|
127
|
'label' => 'above',
|
128
|
'type' => 'date_default',
|
129
|
'weight' => 0,
|
130
|
'settings' => array(
|
131
|
'format_type' => $default_format,
|
132
|
'show_repeat_rule' => 'show',
|
133
|
'multiple_number' => '',
|
134
|
'multiple_from' => '',
|
135
|
'multiple_to' => '',
|
136
|
'fromto' => 'both',
|
137
|
),
|
138
|
'module' => 'date',
|
139
|
),
|
140
|
);
|
141
|
|
142
|
$field = field_create_field($field);
|
143
|
$instance = field_create_instance($instance);
|
144
|
|
145
|
field_info_cache_clear(TRUE);
|
146
|
field_cache_clear(TRUE);
|
147
|
|
148
|
// Look at how the field got configured.
|
149
|
$this->drupalGet("admin/structure/types/manage/$bundle/fields/$field_name");
|
150
|
$this->drupalGet("admin/structure/types/manage/$bundle/display");
|
151
|
|
152
|
}
|
153
|
|
154
|
/**
|
155
|
* @todo.
|
156
|
*/
|
157
|
protected function deleteDateField($label, $bundle = 'story', $bundle_name = 'Story') {
|
158
|
$this->drupalGet("admin/structure/types/manage/$bundle/fields");
|
159
|
$this->clickLink('delete');
|
160
|
$this->drupalPost(NULL, NULL, t('Delete'));
|
161
|
$this->assertText("The field $label has been deleted from the $bundle_name content type.", 'Removed date field.');
|
162
|
}
|
163
|
|
164
|
}
|
165
|
|
166
|
class DateFieldTestCase extends DateFieldBasic {
|
167
|
|
168
|
/**
|
169
|
* @todo.
|
170
|
*/
|
171
|
public static function getInfo() {
|
172
|
return array(
|
173
|
'name' => 'Date Field',
|
174
|
'description' => 'Test date field settings and Start/End date interaction.',
|
175
|
'group' => 'Date',
|
176
|
);
|
177
|
}
|
178
|
|
179
|
/**
|
180
|
* @todo.
|
181
|
*/
|
182
|
public function testField() {
|
183
|
// Create a date fields with simple values.
|
184
|
foreach (array('date', 'datestamp', 'datetime') as $field_type) {
|
185
|
foreach (array('date_select', 'date_popup', 'date_text') as $widget_type) {
|
186
|
$field_name = "field_test_$widget_type";
|
187
|
$label = 'Test';
|
188
|
$options = array(
|
189
|
'label' => $label,
|
190
|
'widget_type' => $widget_type,
|
191
|
'field_name' => $field_name,
|
192
|
'field_type' => $field_type,
|
193
|
'input_format' => 'm/d/Y - H:i',
|
194
|
);
|
195
|
$this->createDateField($options);
|
196
|
$this->dateForm($field_name, $field_type, $widget_type);
|
197
|
$this->deleteDateField($label);
|
198
|
}
|
199
|
}
|
200
|
}
|
201
|
|
202
|
/**
|
203
|
* @todo.
|
204
|
*/
|
205
|
public function dateForm($field_name, $field_type, $widget_type, $todate = TRUE) {
|
206
|
// Tests that date field functions properly.
|
207
|
$edit = array();
|
208
|
$edit['title'] = $this->randomName(8);
|
209
|
if ($widget_type == 'date_select') {
|
210
|
$edit[$field_name . '[und][0][value][year]'] = '2010';
|
211
|
$edit[$field_name . '[und][0][value][month]'] = '10';
|
212
|
$edit[$field_name . '[und][0][value][day]'] = '7';
|
213
|
$edit[$field_name . '[und][0][value][hour]'] = '10';
|
214
|
$edit[$field_name . '[und][0][value][minute]'] = '30';
|
215
|
if ($todate) {
|
216
|
$edit[$field_name . '[und][0][show_todate]'] = '1';
|
217
|
$edit[$field_name . '[und][0][value2][year]'] = '2010';
|
218
|
$edit[$field_name . '[und][0][value2][month]'] = '10';
|
219
|
$edit[$field_name . '[und][0][value2][day]'] = '7';
|
220
|
$edit[$field_name . '[und][0][value2][hour]'] = '11';
|
221
|
$edit[$field_name . '[und][0][value2][minute]'] = '30';
|
222
|
}
|
223
|
}
|
224
|
elseif ($widget_type == 'date_text') {
|
225
|
$edit[$field_name . '[und][0][value][date]'] = '10/07/2010 - 10:30';
|
226
|
if ($todate) {
|
227
|
$edit[$field_name . '[und][0][show_todate]'] = '1';
|
228
|
$edit[$field_name . '[und][0][value2][date]'] = '10/07/2010 - 11:30';
|
229
|
}
|
230
|
}
|
231
|
elseif ($widget_type == 'date_popup') {
|
232
|
$edit[$field_name . '[und][0][value][date]'] = '10/07/2010';
|
233
|
$edit[$field_name . '[und][0][value][time]'] = '10:30';
|
234
|
if ($todate) {
|
235
|
$edit[$field_name . '[und][0][show_todate]'] = '1';
|
236
|
$edit[$field_name . '[und][0][value2][date]'] = '10/07/2010';
|
237
|
$edit[$field_name . '[und][0][value2][time]'] = '11:30';
|
238
|
}
|
239
|
}
|
240
|
// Test that the date is displayed correctly using both the 'short' and
|
241
|
// 'long' date types.
|
242
|
//
|
243
|
// For the short type, save an explicit format and assert that is the one
|
244
|
// which is displayed.
|
245
|
variable_set('date_format_short', 'l, m/d/Y - H:i:s');
|
246
|
$instance = field_info_instance('node', $field_name, 'story');
|
247
|
$instance['display']['default']['settings']['format_type'] = 'short';
|
248
|
field_update_instance($instance);
|
249
|
$this->drupalPost('node/add/story', $edit, t('Save'));
|
250
|
$this->assertText($edit['title'], "Node has been created");
|
251
|
$should_be = $todate ? 'Thursday, 10/07/2010 - 10:30 to 11:30' : 'Thursday, 10/07/2010 - 10:30';
|
252
|
$this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the short date format.");
|
253
|
// For the long format, do not save anything, and assert that the displayed
|
254
|
// date uses the expected default value of this format provided by Drupal
|
255
|
// core ('l, F j, Y - H:i').
|
256
|
$instance = field_info_instance('node', $field_name, 'story');
|
257
|
$instance['display']['default']['settings']['format_type'] = 'long';
|
258
|
field_update_instance($instance);
|
259
|
$this->drupalPost('node/add/story', $edit, t('Save'));
|
260
|
$this->assertText($edit['title'], "Node has been created");
|
261
|
$should_be = $todate ? 'Thursday, October 7, 2010 - 10:30 to 11:30' : 'Thursday, October 7, 2010 - 10:30';
|
262
|
$this->assertText($should_be, "Found the correct date for a $field_type field using the $widget_type widget displayed using the long date format.");
|
263
|
}
|
264
|
}
|