Projet

Général

Profil

Paste
Télécharger (15,2 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / payment / tests / commerce_payment_ui.test @ b858700c

1
<?php
2

    
3
/**
4
 * @file
5
 * Functional tests for the commerce payment module user interface.
6
 */
7

    
8
/**
9
 * Test payment user interface.
10
 */
11
class CommercePaymentUITest extends CommerceBaseTestCase {
12
  /**
13
   * Order object.
14
   */
15
  protected $order;
16

    
17
  /**
18
   * Implementation of getInfo().
19
   */
20
  public static function getInfo() {
21
    return array(
22
      'name' => 'Payment user interface',
23
      'description' => 'Test the payment user and administrator interface.',
24
      'group' => 'Drupal Commerce',
25
    );
26
  }
27

    
28
  /**
29
   * Implementation of setUp().
30
   */
31
  function setUp() {
32
    $modules = parent::setUpHelper('all');
33
    parent::setUp($modules);
34

    
35
    // User creation for different operations.
36
    $this->store_admin = $this->createStoreAdmin();
37
    $this->store_customer = $this->createStoreCustomer();
38

    
39
    // The rule that sends a mail after checkout completion should be disabled
40
    //  as it returns an error caused by how mail messages are stored.
41
    $rules_config = rules_config_load('commerce_checkout_order_email');
42
    $rules_config->active = FALSE;
43
    $rules_config->save();
44
  }
45

    
46
  /**
47
   * Create a dummy order and go to checkout payment page.
48
   */
49
  protected function createOrderAndGoToPayment($user = NULL, $products = array()) {
50
    if (empty($user)) {
51
      $user = $this->store_customer;
52
    }
53

    
54
    // Log in as normal user.
55
    $this->drupalLogin($user);
56

    
57
    // Order creation, in cart status.
58
    $this->order = $this->createDummyOrder($user->uid, $products);
59

    
60
    // Go to checkout page.
61
    $this->drupalGet($this->getCommerceUrl('checkout'));
62

    
63
    // Check if the page resolves and if the default panes are present.
64
    $this->assertResponse(200, t('Store customer user is able to access the checkout page'));
65
    $this->assertTitle(t('Checkout') . ' | Drupal', t('Checkout page successfully loaded'));
66

    
67
    // Generate random information, as city, postal code, etc.
68
    $address_info = $this->generateAddressInformation();
69

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

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

    
78
    // Fill in the required information for billing pane, with a random State.
79
    $info = array(
80
      'customer_profile_billing[commerce_customer_address][und][0][name_line]' => $address_info['name_line'],
81
      'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => $address_info['thoroughfare'],
82
      'customer_profile_billing[commerce_customer_address][und][0][locality]' => $address_info['locality'],
83
      'customer_profile_billing[commerce_customer_address][und][0][administrative_area]' => $address_info['administrative_area'],
84
      'customer_profile_billing[commerce_customer_address][und][0][postal_code]' => $address_info['postal_code'],
85
    );
86
    $this->drupalPost(NULL, $info, t('Continue to next step'));
87

    
88
    // Check for default panes and information in this checkout phase.
89
    $this->assertTitle(t('Review order') . ' | Drupal', t('Review order page successfully loaded'));
90
  }
91

    
92
  /**
93
   * Test the payment on checkout process using an authenticated user.
94
   */
95
  public function testCommercePaymentCheckout() {
96
    $this->createOrderAndGoToPayment();
97

    
98
    $this->assertText('Example payment', t('Example payment method pane is present'));
99

    
100
    // Finish checkout process.
101
    $this->drupalPost(NULL, array(), t('Continue to next step'));
102

    
103
    // Load payment to check its status.
104
    $payment = commerce_payment_transaction_load_multiple(array(), array('order_id' => $this->order->order_id), TRUE);
105

    
106
    // Order status should be pending when completing checkout process.
107
    $this->assertEqual(reset($payment)->status, 'success', t('Payment was successfully processed'));
108

    
109
    // Check if the completion message has been displayed.
110
    $this->assertTitle(t('Checkout complete') . ' | Drupal', t('Checkout process completed successfully'));
111
  }
112

    
113
  /**
114
   * Test the adding payments using administration pages.
115
   */
116
  public function testCommercePaymentAdministration() {
117
    // Order creation, in cart status.
118
    $this->order = $this->createDummyOrder($this->store_customer->uid);
119

    
120
    // Log in as administrator user.
121
    $this->drupalLogin($this->store_admin);
122

    
123
    // Go to payment administration page.
124
    $this->drupalGet('admin/commerce/orders/' . $this->order->order_id . '/payment');
125

    
126
    $this->assertText(t('Payment'), t('Payment text found on the page.'));
127

    
128
    // Check order balance.
129
    $balance = commerce_payment_order_balance($this->order);
130
    $this->assertRaw('<td class="label">' . t('Order balance') . '</td><td class="balance">' . commerce_currency_format($balance['amount'], $balance['currency_code']) . '</td>', t('Order balance is equal to order amount'));
131

    
132
    // Add a payment for half of balance.
133
    $this->drupalPostAJAX(NULL, array('payment_method' => 'commerce_payment_example|commerce_payment_commerce_payment_example'), array('op' => t('Add payment')));
134
    $this->assertFieldByXPath("//input[starts-with(@id, 'edit-amount')]", NULL, t('Amount field is present'));
135
    $this->assertFieldByXPath("//select[starts-with(@id, 'edit-currency-code')]", NULL, t('Currency code field is present'));
136
    $this->assertFieldByXPath("//input[starts-with(@id, 'edit-payment-details-credit-card-number')]", NULL, t('Credit card number field from payment example module is present'));
137
    $this->assertFieldByXPath("//input[starts-with(@id, 'edit-submit')]", NULL, t('Save button is present'));
138
    $payment_amount = intval($balance['amount'] / 2);
139
    $post_data = array(
140
      'amount' => (commerce_currency_amount_to_decimal($payment_amount, $balance['currency_code'])),
141
      'currency_code' => 'USD',
142
    );
143
    $this->drupalPost(NULL, $post_data, t('Save'));
144

    
145
    // Reload the order.
146
    $order = commerce_order_load_multiple(array($this->order->order_id), array(), TRUE);
147
    // Reset the cache as we don't want to keep the lock.
148
    entity_get_controller('commerce_order')->resetCache();
149

    
150
    // Check order balance, it should be half of total now.
151
    $new_balance = commerce_payment_order_balance(reset($order));
152
    $this->assertEqual($new_balance['amount'], $balance['amount'] - $payment_amount, t('After half payment order balance is correct'));
153
    $this->assertRaw('<td class="label">' . t('Total paid') . '</td><td class="total">' . commerce_currency_format($payment_amount, $post_data['currency_code']) . '</td>', t('Total paid reflects the payment'));
154
    $this->assertRaw('<td class="label">' . t('Order balance') . '</td><td class="balance">' . commerce_currency_format($new_balance['amount'], $new_balance['currency_code']) . '</td>', t('Order balance is displayed correctly'));
155

    
156
    // Add a payment for the remainder.
157
    $this->drupalPostAJAX(NULL, array('payment_method' => 'commerce_payment_example|commerce_payment_commerce_payment_example'), array('op' => t('Add payment')));
158
    $this->assertFieldByXPath("//input[starts-with(@id, 'edit-amount')]", NULL, t('Amount field is present'));
159
    $this->assertFieldByXPath("//select[starts-with(@id, 'edit-currency-code')]", NULL, t('Currency code field is present'));
160
    $this->assertFieldByXPath("//input[starts-with(@id, 'edit-payment-details-credit-card-number')]", NULL, t('Credit card number field from payment example module is present'));
161
    $this->assertFieldByXPath("//input[starts-with(@id, 'edit-submit')]", NULL, t('Save button is present'));
162
    $post_data = array(
163
      'amount' => commerce_currency_amount_to_decimal($new_balance['amount'], $new_balance['currency_code']),
164
      'currency_code' => 'USD',
165
    );
166
    $this->drupalPost(NULL, $post_data, t('Save'));
167

    
168
    // Reload the order.
169
    $order = commerce_order_load_multiple(array($this->order->order_id), array(), TRUE);
170

    
171
    // Check order balance, it should be zero now.
172
    $new_balance = commerce_payment_order_balance(reset($order));
173
    $this->assertEqual($new_balance['amount'], 0, t('Order balance is now zero'));
174
    $this->assertRaw('<td class="label">' . t('Total paid') . '</td><td class="total">' . commerce_currency_format($balance['amount'], $balance['currency_code']) . '</td>', t('Total paid is now equal to order total'));
175
    $this->assertRaw('<td class="label">' . t('Order balance') . '</td><td class="balance">' . commerce_currency_format(0, $new_balance['currency_code']) . '</td>', t('Balance is displayed as zero'));
176
  }
177

    
178
  /**
179
   * Test payment method rules conditions.
180
   */
181
  public function testCommercePaymentMethodsAdministration() {
182
    // Log in as store administrator user.
183
    $this->drupalLogin($this->store_admin);
184

    
185
    // Go to payment methods page.
186
    $this->drupalGet('admin/commerce/config/payment-methods');
187

    
188
    $this->assertTitle(t('Payment methods') . ' | Drupal', t('We are now in the payment methods page'));
189
    $this->assertText(t('Example payment'), t('Example payment rule is present'));
190

    
191
    // Go to edit example payment rule.
192
    $this->clickLink(t('Example payment'));
193

    
194
    // Adding a new condition.
195
    $this->clickLink(t('Add condition'));
196

    
197
    // Create new data comparison condition for amount > $50.
198
    $this->drupalPost(NULL, array('element_name' => 'data_is'), t('Continue'));
199
    $this->assertText(t('Compare two data values of the same type with each other.'), t('Second step page for adding a condition was successfully loaded'));
200
    $this->drupalPost(NULL, array('parameter[data][settings][data:select]' => 'commerce-order:commerce-order-total:amount'), t('Continue'));
201
    $this->assertText(t('The data to be compared, specified by using a data selector, e.g. "node:author:name".'), t('Third step page for adding a condition was successfully loaded'));
202
    $this->drupalPost(NULL, array('parameter[op][settings][op]' => '>', 'parameter[value][settings][value]' => 50), t('Save'));
203
    $this->assertText(t('Your changes have been saved.'), t('New condition was successfully added'));
204

    
205
    // Adding a new action to enable the payment method if conditions are met.
206
    $this->clickLink(t('Add action'));
207
    $this->drupalPost(NULL, array('element_name' => 'commerce_payment_enable_commerce_payment_example'), t('Continue'));
208
    $this->drupalPost(NULL, array('parameter[commerce_order][settings][commerce_order:select]' => 'commerce-order'), t('Save'));
209
    $this->assertText(t('Your changes have been saved.'), t('New action was successfully added'));
210

    
211
    // Create a less than $50 order (20 products $2 each).
212
    $product = $this->createDummyProduct($this->randomName(), $this->randomName(), 2, 'USD', $this->store_admin->uid);
213
    $this->createOrderAndGoToPayment($this->store_customer, array($product->product_id => 20));
214
    // Check that the payment method example is *not* there.
215
    $this->assertNoText('Example payment', t('Example payment method panel is not present'));
216

    
217
    // Create a more than $50 order (40 products $2 each).
218
    $product = $this->createDummyProduct($this->randomName(), $this->randomName(), 2, 'USD', $this->store_admin->uid);
219
    $this->createOrderAndGoToPayment($this->store_customer, array($product->product_id => 40));
220
    // Check that the payment method example is there.
221
    $this->assertText('Example payment', t('Example payment method panel is present'));
222
  }
223

    
224
  /**
225
   * Test the access to the payment/payment methods administration pages.
226
   */
227
  public function testCommercePaymentAccessPaymentAdministration() {
228
    // Login with normal user.
229
    $this->drupalLogin($this->store_customer);
230

    
231
    // Order creation, in cart status.
232
    $this->order = $this->createDummyOrder($this->store_customer->uid);
233

    
234
    // Access payment administration page.
235
    $this->drupalGet('admin/commerce/orders/' . $this->order->order_id . '/payment');
236

    
237
    $this->assertResponse(403, t('Normal user is not able to access the payment administration page'));
238

    
239
    // Access to the payment methods administration page.
240
    $this->drupalGet('admin/commerce/config/payment-methods');
241

    
242
    $this->assertResponse(403, t('Normal user is not able to access the payment methods administration page'));
243

    
244
    // Login with store admin.
245
    $this->drupalLogin($this->store_admin);
246

    
247
    // Access payment administration page.
248
    $this->drupalGet('admin/commerce/orders/' . $this->order->order_id . '/payment');
249

    
250
    $this->assertResponse(200, t('Store admin user can access the payment administration page'));
251

    
252
    // Access to the payment methods administration page
253
    $this->drupalGet('admin/commerce/config/payment-methods');
254

    
255
    $this->assertResponse(200, t('Store admin user can access the payment methods administration page'));
256
  }
257

    
258
}
259

    
260
/**
261
 * Test payment user interface.
262
 */
263
class CommercePaymentOffsiteTest extends CommerceBaseTestCase {
264
  /**
265
   * Order object.
266
   */
267
  protected $order;
268

    
269
  /**
270
   * Implementation of getInfo().
271
   */
272
  public static function getInfo() {
273
    return array(
274
      'name' => 'Payment Offsite',
275
      'description' => 'Test the payment offsite process.',
276
      'group' => 'Drupal Commerce',
277
    );
278
  }
279

    
280
  /**
281
   * Implementation of setUp().
282
   */
283
  function setUp() {
284
    $modules = parent::setUpHelper('all');
285
    $modules[] = 'commerce_payment_dummy_offsite';
286
    parent::setUp($modules);
287

    
288
    // User creation for different operations.
289
    $this->store_admin = $this->createStoreAdmin();
290
    $this->store_customer = $this->createStoreCustomer();
291
  }
292

    
293
  /**
294
   * Test an Offsite payment method.
295
   */
296
  public function testCommercePaymentOffsitePayment() {
297
    // Create a new customer profile.
298
    $profile = $this->createDummyCustomerProfile('billing', $this->store_customer->uid);
299
    $profile_wrapper = entity_metadata_wrapper('commerce_customer_profile', $profile);
300
    // Create an order for store customer.
301
    $order = $this->createDummyOrder($this->store_customer->uid, array(), 'cart', $profile->profile_id);
302

    
303
    // Login with store admin.
304
    $this->drupalLogin($this->store_admin);
305

    
306
    // Access to the payment methods administration page.
307
    $this->drupalGet('admin/commerce/config/payment-methods');
308

    
309
    // Check if the payment method exists and it's listed.
310
    $this->assertText(t('Dummy Payment Method Offsite'), t('Offsite example payment method is listed in the payment methods administration page'));
311

    
312
    // Login with store customer and access to checkout.
313
    $this->drupalLogin($this->store_customer);
314
    $this->drupalGet($this->getCommerceUrl('checkout'));
315

    
316
    // Process the order and check if the offsite payment is working.
317
    $this->drupalPost(NULL, array(), t('Continue to next step'));
318
    $this->assertText(t('Dummy Payment Method Offsite'), t('Offsite example payment method is listed in the checkout process form'));
319
    $this->drupalPostAJAX(NULL, array('commerce_payment[payment_method]' => 'commerce_payment_dummy_offsite|commerce_payment_commerce_payment_dummy_offsite'), 'commerce_payment[payment_method]');
320
    $this->drupalPost(NULL, array(), t('Continue to next step'));
321

    
322
    $this->assertFieldById('edit-submit', t('Redirect to Offsite platform'), t('Redirection button to offsite payment platform is present'));
323
    // We can't really test further than this.
324
  }
325

    
326
}