Projet

Général

Profil

Paste
Télécharger (33,1 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / customer / tests / commerce_customer_ui.test @ b858700c

1
<?php
2

    
3
/**
4
 * @file
5
 * Commerce customer profile tests.
6
 */
7

    
8
/**
9
 * Functional tests for the commerce customer UI module.
10
 */
11
class CommerceCustomerUITest extends CommerceBaseTestCase {
12
  /**
13
   * Implementation of getInfo().
14
   */
15
  public static function getInfo() {
16
    return array(
17
      'name' => 'Customer user interface',
18
      'description' => 'Test creating, editing, deleting cusomer profiles and how they interact with other components, like orders.',
19
      'group' => 'Drupal Commerce',
20
    );
21
  }
22

    
23
  /**
24
   * Implementation of setUp().
25
   */
26
  function setUp() {
27
    $modules = parent::setUpHelper('all');
28
    parent::setUp($modules);
29

    
30
    // User creation for different operations.
31
    $this->store_admin = $this->createStoreAdmin();
32
    $this->store_customer = $this->createStoreCustomer();
33

    
34
    // Set the default country to US.
35
    variable_set('site_default_country', 'US');
36
  }
37

    
38
  /**
39
   * Load a customer profile basing in field conditions.
40
   */
41
  protected function loadCustomerProfile($conditions) {
42
    $query = db_select('commerce_customer_profile', 'cp');
43
    $query = new EntityFieldQuery();
44
    $query
45
      ->entityCondition('entity_type', 'commerce_customer_profile', '=');
46

    
47
    foreach ($conditions as $condition) {
48
      $operation = !empty($condition['operation']) ? $condition['operation'] : '=';
49
      $query->fieldCondition($condition['field'], $condition['column'], $condition['value'], $operation);
50
    }
51

    
52
    $results = $query->execute();
53
    return $results['commerce_customer_profile'];
54
  }
55

    
56
  /**
57
   * Access to the customer profiles listing.
58
   */
59
  public function testCommerceCustomerUIAccessCustomerProfilesListing() {
60
    // Login with customer.
61
    $this->drupalLogin($this->store_customer);
62
    // Check the access to the profiles listing.
63
    $this->drupalGet('admin/commerce/customer-profiles');
64
    $this->assertResponse(403, t('The store customer has no access to the administration listing of customer profiles'));
65

    
66
    // Login with store admin.
67
    $this->drupalLogin($this->store_admin);
68
    // Check the access to the profiles listing.
69
    $this->drupalGet('admin/commerce/customer-profiles');
70
    $this->assertResponse(200, t('The store customer has access to the administration listing of customer profiles'));
71

    
72
    // Check the message of no profiles available.
73
    $this->assertText(t('No customer profiles have been created yet.'), t('\'No customer profiles have been created yet\' message is displayed'));
74
    // Check the add customer profile link.
75
    $this->assertRaw(l('Add a customer profile', 'admin/commerce/customer-profiles/add'), t('\'Add a customer profile\' link is present in the page'));
76
  }
77

    
78
  /**
79
   * Access to the customer profile types listing.
80
   */
81
  public function testCommerceCustomerUIAccessCustomerProfileTypesListing() {
82
    // Login with customer.
83
    $this->drupalLogin($this->store_customer);
84
    // Check the access to the profile types listing.
85
    $this->drupalGet('admin/commerce/customer-profiles/types');
86
    $this->assertResponse(403, t('The store customer has no access to the administration listing of customer profile types'));
87

    
88
    // Login with store admin.
89
    $this->drupalLogin($this->store_admin);
90
    // Check the access to the profile types listing.
91
    $this->drupalGet('admin/commerce/customer-profiles/types');
92
    $this->assertResponse(200, t('The store customer has access to the administration listing of customer profile types'));
93

    
94
    // Check if all the profiles defined by default are there.
95
    $types = commerce_customer_profile_types();
96
    foreach ($types as $type) {
97
      $this->assertText($type['name'], t('!type customer profile type is found in the listing', array('!type' => $type['name'])));
98
    }
99
  }
100

    
101
  /**
102
   * Add a customer profile.
103
   */
104
  public function testCommerceCustomerUIAddCustomerProfile() {
105
    // Login with customer.
106
    $this->drupalLogin($this->store_customer);
107
    // Check the access to the profile add page.
108
    $this->drupalGet('admin/commerce/customer-profiles/add');
109

    
110
    // Login with store admin.
111
    $this->drupalLogin($this->store_admin);
112
    // Check the access to the profile add page.
113
    $this->drupalGet('admin/commerce/customer-profiles/add');
114

    
115
    // As The billing information is the only profile shipped by default at
116
    // the moment, the destination url is the billing information creation
117
    // form.
118
    $this->assertTrue($this->url = url('admin/commerce/customer-profiles/add/billing', array('absolute => TRUE')));
119

    
120
    // Get the default values for the address.
121
    $address = addressfield_default_values();
122

    
123
    // Check the integrity of the add form.
124
    $this->pass(t('Test the integrity of the add customer profile form:'));
125
    $billing_country = $this->xpath("//select[starts-with(@name, 'commerce_customer_address')]");
126
    $this->drupalPostAJAX(NULL, array((string) $billing_country[0]['name'] => $address['country']), (string) $billing_country[0]['name']);
127

    
128
    $this->assertFieldByXPath("//select[starts-with(@id, 'edit-commerce-customer-address-und-0-country')]", $address['country'], t('Country field exists and it has the default country selected'));
129
    $this->assertFieldByXPath("//input[starts-with(@id, 'edit-commerce-customer-address-und-0-name-line')]", NULL, t('Field !field exists in the customer profile form', array('!field' => 'Name line')));
130

    
131
    // Also check for the buttons and cancel link.
132
    $this->assertFieldById('edit-submit', t('Save profile'), t('\'Save profile\' button is present'));
133
    $this->assertFieldById('edit-save-continue', t('Save and add another'), t('\'Save an add another\' button is present'));
134
    $this->assertRaw(l(t('Cancel'), 'admin/commerce/customer-profiles'), t('Cancel link is present'));
135

    
136
    // Generate random information, as city, postal code, etc.
137
    $address_info = $this->generateAddressInformation();
138

    
139
    // Fill the profile information and Save.
140
    $info = array(
141
      'commerce_customer_address[und][0][name_line]' => $address_info['name_line'],
142
    	'commerce_customer_address[und][0][thoroughfare]' => $address_info['thoroughfare'],
143
    	'commerce_customer_address[und][0][locality]' => $address_info['locality'],
144
    	'commerce_customer_address[und][0][administrative_area]' => $address_info['administrative_area'],
145
    	'commerce_customer_address[und][0][postal_code]' => $address_info['postal_code'],
146
    );
147
    $this->drupalPost(NULL, $info, t('Save profile'));
148

    
149
    // Check in database if the profile got created.
150
    $conditions = array();
151
    foreach ($address_info as $id => $element) {
152
      $conditions[] = array(
153
        'field' => 'commerce_customer_address',
154
      	'column' => $id,
155
      	'value' => $element,
156
      );
157
    }
158
    $profile = $this->loadCustomerProfile($conditions);
159
    $this->assertFalse(empty($profile), t('Profile has been created in database'));
160

    
161
    // Check the landing url and if the profile is in the listing.
162
    $this->assertTrue($this->url == url('admin/commerce/customer-profiles', array('absolute' => TRUE)), t('Landing page after save the profile is the profile listing page'));
163
    $this->assertText(t('Profile saved'), t('\'Profile saved\' message is displayed after saving a customer profile'));
164
    $this->assertText($address_info['name_line'], t('Profile name line value: !value is present in the customer profile listing', array('!value' => $address_info['name_line'])));
165
  }
166

    
167
  /**
168
   * Save and add another customer profile.
169
   */
170
  public function testCommerceCustomerUIAddCustomerProfileSaveAndAddAnother() {
171
    // Login with store admin.
172
    $this->drupalLogin($this->store_admin);
173
    // Check the access to the profile add page.
174
    $this->drupalGet('admin/commerce/customer-profiles/add');
175

    
176
    // Fill the profile information and click on Save and add another.
177
    $billing_country = $this->xpath("//select[starts-with(@name, 'commerce_customer_address')]");
178
    $this->drupalPostAJAX(NULL, array((string) $billing_country[0]['name'] => variable_get('site_default_country', 'US')), (string) $billing_country[0]['name']);
179

    
180
    // Generate random information, as city, postal code, etc.
181
    $address_info = $this->generateAddressInformation();
182

    
183
    // Fill the profile information and Save.
184
    $info = array(
185
      'commerce_customer_address[und][0][name_line]' => $address_info['name_line'],
186
    	'commerce_customer_address[und][0][thoroughfare]' => $address_info['thoroughfare'],
187
    	'commerce_customer_address[und][0][locality]' => $address_info['locality'],
188
    	'commerce_customer_address[und][0][administrative_area]' => $address_info['administrative_area'],
189
    	'commerce_customer_address[und][0][postal_code]' => $address_info['postal_code'],
190
    );
191
    $this->drupalPost(NULL, $info, t('Save and add another'));
192

    
193
    // Check the landing url and if the profile got created.
194
    $this->assertTrue($this->url == url('admin/commerce/customer-profiles/add/billing', array('absolute' => TRUE)), t('Landing page after save and add another for profiles is the profile creation page'));
195
    $this->assertText(t('Profile saved'), t('\'Profile saved\' message is displayed after saving a customer profile'));
196
    $this->assertFieldById('edit-commerce-customer-address-und-0-name-line', '', t('\'Name line\' field is present and empty'));
197

    
198
    $conditions = array();
199
    foreach ($address_info as $id => $element) {
200
      $conditions[] = array(
201
        'field' => 'commerce_customer_address',
202
      	'column' => $id,
203
      	'value' => $element,
204
      );
205
    }
206
    $profile = $this->loadCustomerProfile($conditions);
207
    $this->assertFalse(empty($profile), t('Profile has been created in database'));
208
  }
209

    
210

    
211
  /**
212
   * Add extra fields to a profile type.
213
   */
214
  public function testCommerceCustomerUIProfileWithExtraFields() {
215
    // Login with store admin.
216
    $this->drupalLogin($this->store_admin);
217
    // Access to the profile billing type manage fields.
218
    $this->drupalGet('admin/commerce/customer-profiles/types/billing/fields');
219
    $this->assertResponse(200, t('Store admin user is able to access the customer profile type manage fields screen'));
220

    
221
    // Create an extra field for the profile.
222
    $edit = array(
223
      'fields[_add_new_field][label]' => $this->randomName(),
224
      'fields[_add_new_field][field_name]' => strtolower($this->randomName()),
225
      'fields[_add_new_field][type]' => 'text',
226
      'fields[_add_new_field][widget_type]' => 'text_textfield',
227
    );
228
    $this->drupalPost(NULL, $edit, t('Save'));
229
    $this->drupalPost(NULL, array(), t('Save field settings'));
230
    $this->drupalPost(NULL, array(), t('Save settings'));
231

    
232
    // Add a new profile, check that the field is there.
233
    $this->drupalGet('admin/commerce/customer-profiles/add');
234

    
235
    // Assert that the field exists in the profile add form.
236

    
237
    $address_info = $this->generateAddressInformation();
238
    // Fill the profile information and Save.
239
    $info = array(
240
      'commerce_customer_address[und][0][name_line]' => $address_info['name_line'],
241
    	'commerce_customer_address[und][0][thoroughfare]' => $address_info['thoroughfare'],
242
    	'commerce_customer_address[und][0][locality]' => $address_info['locality'],
243
    	'commerce_customer_address[und][0][administrative_area]' => $address_info['administrative_area'],
244
    	'commerce_customer_address[und][0][postal_code]' => $address_info['postal_code'],
245
    );
246

    
247
    // Also add the new field value.
248
    $field_value = $this->randomName();
249
    $info['field_' . $edit['fields[_add_new_field][field_name]'] . '[und][0][value]'] = $field_value;
250

    
251
    $this->drupalPost(NULL, $info, t('Save profile'));
252

    
253
    // Check that the profile got created and if the field is filled.
254
    $this->assertText(t('Profile saved'), t('\'Profile saved\' message is displayed after saving a customer profile'));
255

    
256
    // Check also in database.
257
    foreach ($address_info as $id => $element) {
258
      $conditions[] = array(
259
        'field' => 'commerce_customer_address',
260
      	'column' => $id,
261
      	'value' => $element,
262
      );
263
    }
264

    
265
    // Load the profile and check if the field is filled.
266
    $profile = commerce_customer_profile_load(reset($this->loadCustomerProfile($conditions))->profile_id);
267
    $this->assertTrue($profile->{'field_' . $edit['fields[_add_new_field][field_name]']}[LANGUAGE_NONE][0]['value'] == $field_value, t('The extra field !field created for the customer profile exists and it has the correct value: !value', array('!field' => $edit['fields[_add_new_field][field_name]'], '!value' => $field_value)));
268
  }
269

    
270
  /**
271
   * Edit a previously existing customer profile.
272
   */
273
  public function testCommerceCustomerUIEditCustomerProfile() {
274
    // Create a new customer profile.
275
    $profile = $this->createDummyCustomerProfile('billing', $this->store_customer->uid);
276
    // Login with store admin.
277
    $this->drupalLogin($this->store_admin);
278
    // Edit the customer profile.
279
    $this->drupalGet('admin/commerce/customer-profiles/'. $profile->profile_id .'/edit');
280

    
281
    $address = $profile->commerce_customer_address[LANGUAGE_NONE][0];
282
    // Check the integrity of the edit form.
283
    $this->pass(t('Test the integrity of the edit customer profile form:'));
284
    $this->assertFieldById('edit-commerce-customer-address-und-0-country', $address['country'], t('Country field exists and it has the default country selected'));
285
    $this->assertFieldById('edit-commerce-customer-address-und-0-name-line', $address['name_line'], t('Field !field exists in the customer profile form and has the correct value !value', array('!field' => 'Name line', '!value' => $address['name_line'])));
286

    
287
    // Also check for the buttons and cancel link.
288
    $this->assertFieldById('edit-submit', t('Save profile'), t('\'Save profile\' button is present'));
289
    $this->assertRaw(l(t('Cancel'), 'admin/commerce/customer-profiles'), t('Cancel link is present'));
290

    
291
    // Change some fields and save.
292
    $edit = array(
293
      'commerce_customer_address[und][0][name_line]' => 'Example Name line',
294
      'commerce_customer_address[und][0][locality]' => 'Example Locality',
295
      'name' => '',
296
    );
297
    $this->drupalPost(NULL, $edit, t('Save profile'));
298

    
299
    // Assert fields after saving the profile.
300
    $this->pass(t('Assert the field values after saving the profile form:'));
301
    $this->assertTrue($this->url == url('admin/commerce/customer-profiles/'. $profile->profile_id .'/edit', array('absolute' => TRUE)), t('Landing page after save the profile is the profile edit page'));
302
    $this->assertText(t('Profile saved'), t('\'Profile saved\' message is displayed after saving a customer profile'));
303
    $this->assertFieldById('edit-commerce-customer-address-und-0-name-line', $edit['commerce_customer_address[und][0][name_line]'], t('Field !field exists in the customer profile form and has the correct value !value', array('!field' => 'Name line', '!value' => $edit['commerce_customer_address[und][0][name_line]'])));
304
    $this->assertFieldById('edit-commerce-customer-address-und-0-locality', $edit['commerce_customer_address[und][0][locality]'], t('Field !field exists in the customer profile form and has the correct value !value', array('!field' => 'Locality', '!value' => $edit['commerce_customer_address[und][0][locality]'])));
305
    $this->assertFieldByName('name', NULL, t('Name field is present and empty'));
306

    
307
    // Check at database level.
308
    $profile = reset(commerce_customer_profile_load_multiple(array($profile->profile_id), array(), TRUE));
309
    $this->assertTrue($profile->commerce_customer_address[LANGUAGE_NONE][0]['name_line'] == $edit['commerce_customer_address[und][0][name_line]'], t('\'Name line\' field has been correctly modified in the customer profile'));
310
    $this->assertTrue($profile->commerce_customer_address[LANGUAGE_NONE][0]['locality'] == $edit['commerce_customer_address[und][0][locality]'], t('\'Locality\' field has been correctly modified in the customer profile'));
311
    $this->assertTrue($profile->uid == 0, t('Profile owner is now anonymous user'));
312
  }
313

    
314
  /**
315
   * Disable a customer profile.
316
   * @TODO: Probably this test should be completed when it is possible to
317
   * select older profiles for the orders.
318
   */
319
  public function testCommerceCustomerUIDisableCustomerProfile() {
320
    // Create a new customer profile.
321
    $profile = $this->createDummyCustomerProfile('billing', $this->store_customer->uid);
322
    // Login with store admin.
323
    $this->drupalLogin($this->store_admin);
324
    // Edit the customer profile.
325
    $this->drupalPost('admin/commerce/customer-profiles/'. $profile->profile_id .'/edit', array('status' => 0), t('Save profile'));
326

    
327
    $this->drupalGet('admin/commerce/customer-profiles');
328
    $this->assertText(t('Disabled'), t('\'Disabled\' text for the profile appears in the profile listing page'));
329
    $profile = reset(commerce_customer_profile_load_multiple(array($profile->profile_id), array(), TRUE));
330
    $this->assertTrue($profile->status == 0, t('Profile status is Disabled'));
331
  }
332

    
333
  /**
334
   * Delete a customer profile.
335
   */
336
  public function testCommerceCustomerUIDeleteCustomerProfile() {
337
    // Create a new customer profile.
338
    $profile = $this->createDummyCustomerProfile('billing', $this->store_customer->uid);
339
    // Login with customer.
340
    $this->drupalLogin($this->store_customer);
341
    // Check the access to the profile delete.
342
    $this->drupalGet('admin/commerce/customer-profiles/'. $profile->profile_id .'/delete');
343
    $this->assertResponse(403, t('Store customer is not able to access the admin deletion page for a customer profile'));
344

    
345
    // Login with store admin.
346
    $this->drupalLogin($this->store_admin);
347
    // Check the access to the profile delete.
348
    $this->drupalGet('admin/commerce/customer-profiles/'. $profile->profile_id .'/delete');
349
    $this->assertResponse(200, t('Store customer is able to access the admin deletion page for a customer profile'));
350

    
351
    // Check the integrity of the delete form.
352
    $this->pass(t('Test the integrity of the delete customer profile form:'));
353
    $this->assertTitle(t('Are you sure you want to delete this profile?') . ' | Drupal', t('The title of the deletion page is correct'));
354
    $this->assertText(t('Deleting this profile cannot be undone'), t('A warning message for deleting the profile is displayed'));
355
    $this->assertFieldById('edit-submit', t('Delete'), '\'Delete\' button is present');
356
    $this->assertLink(t('Cancel'), 0, t('Cancel link is present'));
357

    
358
    // Delete the profile.
359
    $this->drupalPost(NULL, array(), t('Delete'));
360

    
361
    // Assert the landing page and confirmation messages.
362
    $this->assertTrue($this->url == url('admin/commerce/customer-profiles', array('absolute' => TRUE)), t('Landing page after deleting the profile is the profile listing page'));
363
    $this->assertText(t('The profile has been deleted'), t('Confirmation message after deleting the profile is displayed'));
364
    $this->assertText(t('No customer profiles have been created yet.'), t('\'No customer profiles have been created yet\' message is displayed'));
365

    
366
    // Check at database level.
367
    $profile = reset(commerce_customer_profile_load_multiple(array($profile->profile_id), array(), TRUE));
368
    $this->assertTrue(empty($profile), t('Profile can\'t be loaded from database after deleting it'));
369
  }
370

    
371
  /**
372
   * Create a customer profile in the process of order creation.
373
   */
374
  public function testCommerceCustomerUIAddProfileViaCheckout() {
375
    // The rule that sends a mail after checkout completion should be disabled
376
    //  as it returns an error caused by how mail messages are stored.
377
    $rules_config = rules_config_load('commerce_checkout_order_email');
378
    $rules_config->active = FALSE;
379
    $rules_config->save();
380

    
381
    // Create an order.
382
    $order = $this->createDummyOrder($this->store_customer->uid);
383
    // Login with customer.
384
    $this->drupalLogin($this->store_customer);
385
    // Access checkout.
386
    $this->drupalGet($this->getCommerceUrl('checkout'));
387

    
388
    // Generate random information, as city, postal code, etc.
389
    $address_info = $this->generateAddressInformation();
390

    
391
    // Fill in the billing address information
392
    $billing_pane = $this->xpath("//select[starts-with(@name, 'customer_profile_billing[commerce_customer_address]')]");
393
    $this->drupalPostAJAX(NULL, array((string) $billing_pane[0]['name'] => 'US'), (string) $billing_pane[0]['name']);
394

    
395
    // Check if the country has been selected correctly, this uses XPath as the
396
    //  ajax call replaces the element and the id may change
397
    $this->assertFieldByXPath("//select[starts-with(@id, 'edit-customer-profile-billing-commerce-customer-address')]//option[@selected='selected']", 'US', t('Country selected'));
398

    
399
    // Fill in the required information for billing pane, with a random State.
400
    $info = array(
401
      'customer_profile_billing[commerce_customer_address][und][0][name_line]' => $address_info['name_line'],
402
      'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => $address_info['thoroughfare'],
403
      'customer_profile_billing[commerce_customer_address][und][0][locality]' => $address_info['locality'],
404
      'customer_profile_billing[commerce_customer_address][und][0][administrative_area]' => 'KY',
405
      'customer_profile_billing[commerce_customer_address][und][0][postal_code]' => $address_info['postal_code'],
406
    );
407
    $this->drupalPost(NULL, $info, t('Continue to next step'));
408

    
409
    // Finish checkout process
410
    $this->drupalPost(NULL, array(), t('Continue to next step'));
411

    
412
    // Login with store admin.
413
    $this->drupalLogin($this->store_admin);
414

    
415
    // Check the customer profile at database level.
416
    $order = reset(commerce_order_load_multiple(array($order->order_id), array(), TRUE));
417
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
418
    $profile = $order_wrapper->commerce_customer_billing->value();
419
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
420
    $address = $profile_wrapper->commerce_customer_address->value();
421

    
422
    $this->assertTrue(array_intersect($address_info, $address) == $address_info, t('The address info for the checkout is stored in the customer profile'));
423

    
424
    // Check the customer profile in the listing.
425
    $this->drupalGet('admin/commerce/customer-profiles');
426
    $this->assertTrue($address['name_line'], t('\'Name line\' text is present with the correct value: !value', array('!value' => $address['name_line'])));
427
  }
428

    
429
  /**
430
   * Add a customer profile using the Order interface.
431
   */
432
  public function testCommerceCustomerUIAddProfileViaOrderUI() {
433
    // Create an order for store customer.
434
    $order = $this->createDummyOrder($this->store_customer->uid, array(), 'pending');
435
    // Login with store admin.
436
    $this->drupalLogin($this->store_admin);
437
    // Access the order and fill customer profile information.
438
    $this->drupalGet('admin/commerce/orders/' . $order->order_id . '/edit');
439

    
440
    $address_info = $this->generateAddressInformation();
441

    
442
    $billing_country = $this->xpath("//select[starts-with(@name, 'commerce_customer_billing')]");
443
    $this->drupalPostAJAX(NULL, array((string) $billing_country[0]['name'] => variable_get('site_default_country', 'US')), (string) $billing_country[0]['name']);
444

    
445
    // Fill the profile information and Save.
446
    $info = array(
447
      'commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][name_line]' => $address_info['name_line'],
448
    	'commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][thoroughfare]' => $address_info['thoroughfare'],
449
    	'commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][locality]' => $address_info['locality'],
450
    	'commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][administrative_area]' => $address_info['administrative_area'],
451
    	'commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][postal_code]' => $address_info['postal_code'],
452
    );
453
    $this->drupalPost(NULL, $info, t('Save order'));
454

    
455
    $this->assertText(t('Order saved'), t('\'Order saved\' message is displayed'));
456
    // Check the customer profile in the listing.
457
    $this->drupalGet('admin/commerce/customer-profiles');
458
    $this->assertTrue($address_info['name_line'], t('\'Name line\' text is present with the correct value: !value', array('!value' => $address_info['name_line'])));
459

    
460
    // Check the customer profile at database level.
461
    $conditions = array();
462
    foreach ($address_info as $id => $element) {
463
      $conditions[] = array(
464
        'field' => 'commerce_customer_address',
465
      	'column' => $id,
466
      	'value' => $element,
467
      );
468
    }
469
    $profile = commerce_customer_profile_load(reset($this->loadCustomerProfile($conditions))->profile_id);
470
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
471
    $this->assertFalse(empty($profile), t('Profile has been created in database'));
472
    foreach ($address_info as $name => $info) {
473
      $this->assertEqual($profile_wrapper->commerce_customer_address->{$name}->value(), $info, t('!name is present in the profile with value !value', array('!name' => $name, '!value' => $info)));
474
    }
475
  }
476

    
477
  /**
478
   * Edit a customer profile through the order UI.
479
   */
480
  public function testCommerceCustomerUIEditProfileViaOrderUI() {
481
    // Create a new customer profile.
482
    $profile = $this->createDummyCustomerProfile('billing', $this->store_customer->uid);
483
    // Create an order for store customer.
484
    $order = $this->createDummyOrder($this->store_customer->uid, array(), 'pending', $profile->profile_id);
485
    // Login with store admin.
486
    $this->drupalLogin($this->store_admin);
487

    
488
    // Change some profile fields in the order and save.
489
    $edit = array(
490
      'commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][name_line]' => 'Example Name line',
491
      'commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][locality]' => 'Example Locality',
492
    );
493
    $this->drupalPost('admin/commerce/orders/' . $order->order_id . '/edit', $edit, t('Save order'));
494

    
495
    $this->assertText(t('Order saved'), t('\'Order saved\' message is displayed'));
496
    // Check the customer profile in the listing.
497
    $this->drupalGet('admin/commerce/customer-profiles');
498
    $this->assertTrue($edit['commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][name_line]'], t('\'Name line\' text is present with the correct value: !value', array('!value' => $edit['commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][name_line]'])));
499

    
500
    // Check the customer profile at database level.
501
    $profile = reset(commerce_customer_profile_load_multiple(array($profile->profile_id), array(), TRUE));
502
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
503
    $this->assertEqual($profile_wrapper->commerce_customer_address->name_line->value(), $edit['commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][name_line]'], t('\'Name line\' property value !value match', array('!value' => $edit['commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][name_line]'])));
504
    $this->assertEqual($profile_wrapper->commerce_customer_address->locality->value(), $edit['commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][locality]'], t('\'Locality\' property value !value match', array('!value' => $edit['commerce_customer_billing[und][profiles][0][commerce_customer_address][und][0][locality]'])));
505
  }
506

    
507
  /**
508
   * Delete a customer profile through the order UI.
509
   */
510
  public function testCommerceCustomerUIDeleteProfileViaOrderUI() {
511
    // Create a new customer profile.
512
    $profile = $this->createDummyCustomerProfile('billing', $this->store_customer->uid);
513
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
514
    // Create an order for store customer.
515
    $order = $this->createDummyOrder($this->store_customer->uid, array(), 'pending', $profile->profile_id);
516
    // Login with store admin.
517
    $this->drupalLogin($this->store_admin);
518
    // Access the order and check delete customer profile information.
519
    $this->drupalPost('admin/commerce/orders/' . $order->order_id . '/edit', array('commerce_customer_billing[und][profiles][0][remove]' => 1), t('Save order'));
520

    
521
    // Check the customer profile is not present in the listing.
522
    $this->drupalGet('admin/commerce/customer-profiles');
523
    $this->assertNoText($profile_wrapper->commerce_customer_address->name_line->value(), t('\'Name line\' for the profile is not present in the customer profiles listing'));
524

    
525
    // Check the customer profile has been deleted at database level.
526
    $profile = reset(commerce_customer_profile_load_multiple(array($profile->profile_id), array(), TRUE));
527
    $this->assertTrue(empty($profile), t('Profile has been delete from database'));
528
  }
529

    
530
  /**
531
   * Create a custom profile type form an helper module and test it.
532
   */
533
  public function testCommerceCustomerUINewProfileType() {
534
    // Enable the helper module that creates a new profile type.
535
    module_enable(array('commerce_customer_profile_dummy_type'));
536

    
537
    // Login with store admin.
538
    $this->drupalLogin($this->store_admin);
539

    
540
    // Check the customer profile types.
541
    $this->drupalGet('admin/commerce/customer-profiles/types');
542
    $this->assertText(t('Dummy profile type'), t('Dummy profile type is available in the profile types listing page'));
543

    
544
    // Check the order fields.
545
    $this->drupalGet('admin/commerce/config/order/fields');
546
    $this->assertText(t('Dummy profile type'), t('Dummy profile type is present in the order reference fields'));
547

    
548
    // Check the checkout panes.
549
    $this->drupalGet('admin/commerce/config/checkout/form');
550
    $this->assertText(t('Dummy profile type'), t('Dummy profile type is present as checkout pane'));
551

    
552
    // Create an order for store customer.
553
    $order = $this->createDummyOrder($this->store_customer->uid, array(), 'pending');
554

    
555
    // Check if the profile type is present.
556
    $this->drupalGet('admin/commerce/orders/' . $order->order_id . '/edit');
557
    $this->assertText(t('Dummy profile type'), t('Dummy profile type is present in the order edit form'));
558
  }
559

    
560
  /**
561
   * Test the copying of one profile's fields to another (disabled by default).
562
   */
563
  public function testCommerceCustomerUIProfileCopy() {
564
    $this->_testCommerceCustomerUIProfileCopy();
565
  }
566

    
567
  /**
568
   * Test the copying of one profile's fields to another (enabled by default).
569
   */
570
  public function testCommerceCustomerUIProfileCopyDefaultEnabled() {
571
    $this->_testCommerceCustomerUIProfileCopy(TRUE);
572
  }
573

    
574
  /**
575
   * Test the copying of one profile's fields to another.
576
   */
577
  public function _testCommerceCustomerUIProfileCopy($default_enabled = FALSE) {
578
    // Enable the helper module that creates a new profile type.
579
    module_enable(array('commerce_customer_profile_dummy_type'));
580

    
581
    // Configure the dummy profile type's checkout pane to allow copying from
582
    // the billing information customer profile type.
583
    variable_set('commerce_customer_profile_dummy_profile_copy', TRUE);
584
    variable_set('commerce_customer_profile_dummy_profile_copy_source', 'billing');
585
    variable_set('commerce_customer_profile_dummy_profile_copy_default', $default_enabled);
586

    
587
    // Create an order.
588
    $order = $this->createDummyOrder($this->store_customer->uid);
589
    // Login with customer.
590
    $this->drupalLogin($this->store_customer);
591
    // Access checkout.
592
    $this->drupalGet($this->getCommerceUrl('checkout'));
593

    
594
    // Generate random information, as city, postal code, etc.
595
    $address_info = $this->generateAddressInformation();
596

    
597
    // Fill in the billing address information if the copying isn't enabled by default.
598
    if (!$default_enabled) {
599
      $billing_pane = $this->xpath("//select[starts-with(@name, 'customer_profile_billing[commerce_customer_address]')]");
600
      $this->drupalPostAJAX(NULL, array((string) $billing_pane[0]['name'] => 'US'), (string) $billing_pane[0]['name']);
601
    }
602

    
603
    // Fill in the required information for billing pane, with a random State.
604
    $info = array(
605
      'customer_profile_billing[commerce_customer_address][und][0][name_line]' => $address_info['name_line'],
606
      'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => $address_info['thoroughfare'],
607
      'customer_profile_billing[commerce_customer_address][und][0][locality]' => $address_info['locality'],
608
      'customer_profile_billing[commerce_customer_address][und][0][administrative_area]' => 'KY',
609
      'customer_profile_billing[commerce_customer_address][und][0][postal_code]' => $address_info['postal_code'],
610
      'customer_profile_dummy[commerce_customer_profile_copy]' => 1,
611
    );
612
    $this->drupalPostAJAX(NULL, $info, 'customer_profile_dummy[commerce_customer_profile_copy]');
613
    $this->drupalPost(NULL, $info, t('Continue to next step'));
614

    
615
    // Check the customer profile at database level.
616
    $order = reset(commerce_order_load_multiple(array($order->order_id), array(), TRUE));
617
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
618

    
619
    // Extract the address field value from the billing profile.
620
    $profile = $order_wrapper->commerce_customer_billing->value();
621
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
622
    $billing_address = $profile_wrapper->commerce_customer_address->value();
623

    
624
    // And extract the address field value from the dummy profile.
625
    $profile = $order_wrapper->commerce_customer_dummy->value();
626
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
627
    $dummy_address = $profile_wrapper->commerce_customer_address->value();
628

    
629
    $this->assertTrue(array_intersect($billing_address, $dummy_address) == $billing_address, t('A billing information customer profile was successfully copied to a dummy customer profile during checkout.'));
630
  }
631
}