forked from asmallwebfirm/scale_addressfield
-
Notifications
You must be signed in to change notification settings - Fork 0
/
scale_addressfield.test
447 lines (368 loc) · 18.1 KB
/
scale_addressfield.test
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
<?php
/**
* @file
* Scale Address Field tests.
*/
/**
* Common parent class containing common helpers.
*/
abstract class ScaleAddressfieldWebTestCase extends DrupalWebTestCase {
/**
* Machine name of the field.
*/
protected $field_name = 'field_address';
/**
* Form element name for the country field.
*/
protected $country_name = '';
/**
* Form element name for the administrative area field.
*/
protected $state_name = '';
/**
* Sets up a fully configured addressfield instance.
*/
protected function setUpAddressfield() {
// Create an Addressfield field.
$field = $this->createAddressfield($this->field_name);
// Attach the field to the User entity and require it on registration.
$field['settings'] = array('user_register_form' => 1);
$field['entity_type'] = 'user';
$field['label'] = 'Address';
$field['bundle'] = 'user';
$field['required'] = TRUE;
$this->createAddressfieldInstance($field);
// Set the US to the site's default country.
variable_set('site_default_country', 'US');
// Define some useful triggering elements and form input names.
$country = array(LANGUAGE_NONE, 0, 'country');
$this->country_name = $this->field_name . '[' . implode('][', $country) . ']';
$state = array(LANGUAGE_NONE, 0, 'administrative_area');
$this->state_name = $this->field_name . '[' . implode('][', $state) . ']';
}
/**
* Tears down the addressfield created in setUpAddressfield().
*/
protected function tearDownAddressfield() {
// Delete the field generated in setUp().
$instance = field_info_instance('user', $this->field_name, 'user');
field_delete_instance($instance, TRUE);
}
/**
* Creates a new Address Field instance.
*/
protected function createAddressfield($name) {
$field = array(
'field_name' => $name,
'type' => 'addressfield',
'module' => 'addressfield',
);
return field_create_field($field);
}
/**
* Attaches an Addressfield field to the user entity.
*/
protected function createAddressfieldInstance($instance) {
$instance['widget'] = array(
'weight' => 7,
'type' => 'addressfield_standard',
'module' => 'addressfield',
'active' => 1,
'settings' => array(
'available_countries' => array(),
'format_handlers' => array('address' => 'address'),
),
);
return field_create_instance($instance);
}
/**
* Asserts that a select option exists.
*/
protected function assertOption($value, $message) {
$this->assertRaw('<option value="' . $value . '"', $message);
}
/**
* Asserts that a select option does not exist.
*/
protected function assertNoOption($value, $message) {
$this->assertNoRaw('<option value="' . $value . '"', $message);
}
/**
* Asserts that a given select field does not contain any of the provided
* options.
*/
protected function assertNoOptions($field, $values) {
$options = $this->getAllOptions($field);
$attributes = $field->attributes();
foreach ($values as $value) {
$this->assertFalse(in_array($value, $options), format_string('%value option not found in the %name field.', array(
'%value' => $value,
'%name' => $attributes['name'],
)));
}
}
/**
* Asserts that elements for a given XPath have a given class.
*
* @param string $xpath
* The XPath used to find the element(s).
*
* @param $class
* The HTML class whose existence you are asserting.
*
* @param $message
* The message to be used for this assertion.
*
* @param string $group
* (Optional) The group to which this message belongs.
*
* @return bool
* TRUE on pass, FALSE on failure.
*/
protected function assertHasClassByXpath($xpath, $class, $message, $group = 'Other') {
$elements = $this->xpath($xpath);
$has_class = FALSE;
$class = ' ' . $class . ' ';
if ($elements) {
foreach ($elements as $element) {
// Add extra padding (in case the class comes first or last).
$element['class'] = ' ' . $element['class'] . ' ';
if (isset($element['class']) && strpos($element['class'], $class) !== FALSE) {
$has_class = TRUE;
}
else {
$has_class = FALSE;
}
}
}
return $this->assertTrue($has_class, $message);
}
}
/**
* Test form render functionality.
*/
class ScaleAddressfieldFormRenderTestCase extends ScaleAddressfieldWebTestCase {
/**
* An array of field names that should exist on the page by default, keyed by
* their xNAL name (which should exist as a class / Drupal.settings item).
*/
protected $fields = array(
'thoroughfare' => 'field_address[und][0][thoroughfare]',
'premise' => 'field_address[und][0][premise]',
'street_block' => NULL,
'localityname' => 'field_address[und][0][locality]',
'administrativearea' => 'field_address[und][0][administrative_area]',
'postalcode' => 'field_address[und][0][postal_code]',
'locality' => NULL,
);
public static function getInfo() {
return array(
'name' => 'Scale Address Field Form Render',
'description' => 'Tests Scale Address Field form render functionality.',
'group' => 'Scale Address Field',
);
}
function setUp() {
parent::setUp('scale_addressfield');
// Set up a reasonable addressfield instance.
$this->setUpAddressfield();
// Enable the page cache.
variable_set('cache', 1);
variable_set('cache_lifetime', 3600);
}
function tearDown() {
$this->tearDownAddressfield();
// Disable the page cache.
variable_set('cache', 0);
variable_set('cache_lifetime', 0);
drupal_flush_all_caches();
parent::tearDown();
}
/**
* Tests basic form rendering and error handling.
*/
public function testFormRender() {
// Visit the user registration page; this will be a cache MISS.
$this->drupalGet('user/register');
$cache = $this->drupalGetHeader('x-drupal-cache');
$this->assertTrue($cache == 'MISS', 'Page returned a cache MISS.');
// Get our Drupal.settings object.
$settings = $this->drupalGetSettings();
$settings = $settings['scale_addressfield'];
// Ensure that no cache_form entry was generated on render.
$count = (int) db_query('SELECT COUNT(*) FROM {cache_form}')->fetchField();
$this->assertTrue($count === 0, 'No cache_form entry was created on render');
// Ensure that all of the JS that's expected is included on the page.
$this->assertRaw('modules/scale_addressfield/js/addressfield.js', 'Found addressfield.js inclusion on the page.');
$this->assertRaw('modules/scale_addressfield/js/jquery.addressfield/jquery.addressfield.min.js', 'Found the address field jQuery plugin inclusion on the page.');
// Ensure that the callback is configured properly.
$this->assertTrue(strpos($settings['config_json'], '/addressfield/config.json') !== FALSE, 'JSON configuration callback set properly.');
// Iterate through all expected fields and assert their existence.
foreach ($this->fields as $xnal => $input_name) {
// Ensure they're in the Drupal.settings configuration.
$this->assertEqual($settings['enabled']['addressfield-wrapper'][$xnal], '.' . $xnal, format_string('The %field field was properly added to Drupal.settings.', array(
'%field' => $xnal,
)));
// Ensure that real fields exist.
if (!empty($input_name)) {
$this->assertFieldByName($input_name, NULL, format_string('The %field field was properly rendered.', array(
'%field' => $input_name,
)));
// Also ensure that the field also uses the appropriate class name.
$this->assertHasClassByXpath($this->constructFieldXpath('name', $input_name), $xnal, format_string('The %class class was properly applied to field %field.', array(
'%class' => $xnal,
'%field' => $input_name,
)));
}
}
// Ensure the localityname field does not have a "locality" class.
$field = $this->xpath($this->constructFieldXpath('name', $this->fields['localityname']));
$this->assertTrue(strpos('locality ', $field[0]['class']) === FALSE, 'Locality class not found on localityname element.');
// Ensure that the administrative area field is rendered as a textfield.
$this->assertFieldByXpath($this->buildXPathQuery('//input[@name=:name]', array(
':name' => $this->fields['administrativearea'],
)), NULL, 'Administrative area field is rendered as a text field.');
// POST an empty form, ensure that error messages exist for state/postal.
$this->drupalPost(NULL, array(), 'Create new account');
$this->assertText('State field is required.', 'Administrative area field validation error shown when required.');
$this->assertText('ZIP code field is required.', 'Postal code field validation error shown when required.');
// Post a form with an invalid zip code.
$this->drupalPost(NULL, array($this->fields['postalcode'] => 'xxxxx'), 'Create new account');
$this->assertText('Please check your formatting.', 'Postal code field validation error shown when invalid postal code is provided.');
// Swap to defaulting to France.
variable_set('site_default_country', 'AF');
$this->drupalGet('user/register', array('query' => array('cb' => 1)));
$cache = $this->drupalGetHeader('x-drupal-cache');
$this->assertTrue($cache == 'MISS', 'Page returned a cache MISS.');
// Ensure that the default country is now Afghanistan.
$this->assertFieldByName('field_address[und][0][country]', 'AF', 'Default country successfully configured as Afghanistan.');
// Ensure that the administrative area and postal code fields exist (even
// though they will be hidden via JS for Afghanistan).
$this->assertFieldByXpath($this->buildXPathQuery('//input[@name=:name]', array(
':name' => $this->fields['administrativearea'],
)), NULL, 'Administrative area field is rendered as a text field.');
$this->assertFieldByXpath($this->buildXPathQuery('//input[@name=:name]', array(
':name' => $this->fields['postalcode'],
)), NULL, 'Postal code field is rendered as a text field.');
// POST an empty form, ensure that error messages do not exist for state
// or postal, despite the fact that they were rendered above.
$this->assertNoText('State field is required.', 'Administrative area field validation error hidden when irrelevant.');
$this->assertNoText('ZIP code field is required.', 'Postal code field validation error hidden when irrelevant.');
}
/**
* Tests form render overrides (provided by a test module).
*/
public function testFormRenderOverrides() {
// Enable the override module.
module_enable(array('test_scale_addressfield'));
// Load the page and check for initial alterations.
$this->drupalGet('user/register');
// Ensure that hook_countries_alter() is respected (no France).
$this->assertNoOption('FR', 'The hook_countries_alter() hook was respected');
// Ensure that hook_addressfield_config_pre_alter() is respected (multiple).
$this->assertText('Altered country', 'Country label successfully altered via hook_addressfield_config_pre_alter().');
$this->assertText('State, Yo', 'Administrative area label successfully altered via hook_addressfield_config_pre_alter().');
$this->assertOption('CA*', 'New country successfully added via hook_addressfield_config_post_alter().');
// Ensure the config can be overridden by swapping out the file location.
$alt_config = drupal_get_path('module', 'test_scale_addressfield') . '/alternate-config.json';
variable_set('scale_addressfield_config_json', $alt_config);
cache_clear_all('*', 'cache', TRUE);
// Ensure our config file was loaded.
$this->drupalGet('user/register', array('query' => array('cb' => 1)));
$cache = $this->drupalGetHeader('x-drupal-cache');
$this->assertTrue($cache == 'MISS', 'Page returned a cache MISS.');
// Try a smattering of countries (all of which should not be there).
$this->assertNoOption('DE', 'Germany successfully removed via alternate default config file.');
$this->assertNoOption('BR', 'Brazil successfully removed via alternate default config file.');
// Ensure that "nega canada" exists.
$this->assertRaw('Nega Canada', 'Found country provided via alternate default config file.');
}
}
/**
* Tests JSON rendering of address field configurations.
*/
class ScaleAddressfieldJsonTestCase extends ScaleAddressfieldWebTestCase {
public static function getInfo() {
return array(
'name' => 'Scale Address Field JSON Render',
'description' => 'Tests Scale Address Field JSON render functionality.',
'group' => 'Scale Address Field',
);
}
function setUp() {
parent::setUp('scale_addressfield');
// Enable the page cache.
variable_set('cache', 1);
variable_set('cache_lifetime', 3600);
}
function tearDown() {
// Disable the page cache.
variable_set('cache', 0);
variable_set('cache_lifetime', 0);
drupal_flush_all_caches();
parent::tearDown();
}
public function testDefaultJson() {
// Return and store the config json.
$this->drupalGet('addressfield/config.json');
$config = drupal_json_decode($this->drupalGetContent());
// Assert the right headers.
$this->assertEqual($this->drupalGetHeader('content-type'), 'application/json; charset=utf-8', 'Correctly returned application/JSON content-type.');
$this->assertTrue($this->drupalGetHeader('content-language'), 'en', 'Correctly returned English content-language.');
$this->assertTrue($this->drupalGetHeader('x-drupal-cache') == 'MISS', 'JSON returned a cache MISS.');
// Assert that the response matches the cached value.
$cid = 'scale_addressfield:address_config:en';
$cached_config = cache_get($cid);
$cached_config = $cached_config->data;
$this->assertIdentical($config, $cached_config['php'], 'Returned configuration as PHP matches cached configuration.');
$this->assertIdentical($this->drupalGetContent(), $cached_config['json'], 'Returned configuration as JSON matches cached configuration');
// Assert that the basic structure is as expected.
$this->assertTrue(isset($config['options']), 'Returned JSON matches expected output: options key.');
$this->assertTrue(isset($config['label']), 'Returned JSON matches expected output: label key.');
$this->assertTrue(isset($config['options'][0]['fields']), 'Returned JSON matches expected format.');
// Retry all tests, but now cached.
$this->drupalGet('addressfield/config.json');
$this->assertTrue($this->drupalGetHeader('x-drupal-cache') == 'HIT', 'JSON response was cached successfully.');
$this->assertEqual($this->drupalGetHeader('content-type'), 'application/json; charset=utf-8', 'Correctly returned application/JSON content-type.');
$this->assertTrue($this->drupalGetHeader('content-language'), 'en', 'Correctly returned English content-language.');
// Assert that the response matches the cached value.
$cid = 'scale_addressfield:address_config:en';
$cached_config = cache_get($cid);
$cached_config = $cached_config->data;
$this->assertIdentical($config, $cached_config['php'], 'Returned configuration as PHP matches cached configuration.');
$this->assertIdentical($this->drupalGetContent(), $cached_config['json'], 'Returned configuration as JSON matches cached configuration');
// Assert that the basic structure is as expected.
$this->assertTrue(isset($config['options']), 'Returned JSON matches expected output: options key.');
$this->assertTrue(isset($config['label']), 'Returned JSON matches expected output: label key.');
$this->assertTrue(isset($config['options'][0]['fields']), 'Returned JSON matches expected format.');
}
public function testJsonOverrides() {
module_enable(array('test_scale_addressfield'));
// Return and store the config json.
$this->drupalGet('addressfield/config.json');
$config = drupal_json_decode($this->drupalGetContent());
$this->assertTrue($this->drupalGetHeader('x-drupal-cache') == 'MISS', 'JSON returned a cache MISS.');
// Ensure that hook_countries_alter() is respected (no France).
$this->assertFalse(array_key_exists(74, $config['options']), 'The hook_countries_alter() hook was respected.');
// Ensure that hook_addressfield_config_pre_alter() is respected (multiple).
$this->assertEqual($config['label'], 'Altered country', 'Country label successfully altered via hook_addressfield_config_pre_alter().');
$this->assertEqual($config['options'][234]['fields'][2]['locality'][1]['administrativearea']['label'], 'State, Yo', 'Administrative area label successfully altered via hook_addressfield_config_pre_alter().');
$this->assertEqual($config['options'][234]['fields'][2]['locality'][1]['administrativearea']['options'][38]['OR'], 'Portland', 'Specific administrative area successfully altered via hook_addressfield_config_pre_alter().');
$canada_star = $config['options'][39];
$canada_star['iso'] = 'CA*';
$this->assertIdentical($config['options'][247], $canada_star, 'New country successfully added via hook_addressfield_config_post_alter().');
// Ensure the config can be overriden by swapping out the file location.
$alt_config = drupal_get_path('module', 'test_scale_addressfield') . '/alternate-config.json';
variable_set('scale_addressfield_config_json', $alt_config);
cache_clear_all('*', 'cache', TRUE);
// Ensure our config file was loaded.
$this->drupalGet('addressfield/config.json', array('query' => array('cb' => 1)));
$config = drupal_json_decode($this->drupalGetContent());
$this->assertTrue($this->drupalGetHeader('x-drupal-cache') == 'MISS', 'JSON returned a cache MISS.');
// Try a smattering of countries (all of which should not be there).
$this->assertFalse(isset($config['options'][81]), 'Germany successfully removed via alternate default config file.');
$this->assertFalse(isset($config['options'][30]), 'Brazil successfully removed via alternate default config file.');
// Ensure that "nega canada" exists.
$this->assertEqual($config['options'][0]['label'], 'Nega Canada', 'Found country provided via alternate default config file.');
}
}