Projet

Général

Profil

Paste
Télécharger (36,7 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / commerce / modules / tax / tests / commerce_tax_ui.test @ b858700c

1
<?php
2

    
3
/**
4
 * @file
5
 ** Functional tests for the commerce tax UI module.
6
 */
7

    
8
/**
9
 * Functional tests for the commerce tax UI module.
10
 */
11
class CommerceTaxUIAdminTest extends CommerceBaseTestCase {
12
  /**
13
   * Normal user (without admin or store permissions) for testing.
14
   */
15
  protected $normal_user;
16

    
17
  /**
18
   * Tax type.
19
   */
20
  protected $tax_type;
21

    
22
  /**
23
   * Implementation of getInfo().
24
   */
25
  public static function getInfo() {
26
    return array(
27
      'name' => 'Tax administration',
28
      'description' => 'Test creating, editing and deleting tax rates and tax types using the tax administration user interface.',
29
      'group' => 'Drupal Commerce',
30
    );
31
  }
32

    
33
  /**
34
   * Implementation of setUp().
35
   */
36
  function setUp() {
37
    $modules = parent::setUpHelper('all');
38
    parent::setUp($modules);
39

    
40
    // User creation for different operations.
41
    $this->store_admin = $this->createStoreAdmin();
42
    $this->normal_user = $this->drupalCreateUser(array('access checkout', 'view own commerce_order entities'));
43

    
44
    // Create a dummy tax type for testing.
45
    $this->tax_type = $this->createDummyTaxType();
46
  }
47

    
48
  /**
49
   * Go through the checkout process.
50
   */
51
  protected function commerceTaxHelperCompleteCheckout() {
52
    // Get the checkout url and navigate to it.
53
    $links = commerce_line_item_summary_links();
54
    $this->checkout_url = $links['checkout']['href'];
55
    $this->drupalGet($this->checkout_url);
56

    
57
    // The rule that sends a mail after checkout completion should be disabled
58
    //  as it returns an error caused by how mail messages are stored.
59
    $rules_config = rules_config_load('commerce_checkout_order_email');
60
    $rules_config->active = FALSE;
61
    $rules_config->save();
62

    
63
    // Complete the order process.
64
    $billing_pane = $this->xpath("//select[starts-with(@name, 'customer_profile_billing[commerce_customer_address]')]");
65
    $this->drupalPostAJAX(NULL, array((string) $billing_pane[0]['name'] => 'US'), (string) $billing_pane[0]['name']);
66
    $info = array(
67
      'customer_profile_billing[commerce_customer_address][und][0][name_line]' => $this->randomName(),
68
      'customer_profile_billing[commerce_customer_address][und][0][thoroughfare]' => $this->randomName(),
69
      'customer_profile_billing[commerce_customer_address][und][0][locality]' => $this->randomName(),
70
      'customer_profile_billing[commerce_customer_address][und][0][administrative_area]' => 'KY',
71
      'customer_profile_billing[commerce_customer_address][und][0][postal_code]' => rand(00000, 99999),
72
    );
73
    $this->drupalPost(NULL, $info, t('Continue to next step'));
74
    $this->drupalPost(NULL, array(), t('Continue to next step'));
75
  }
76

    
77
  /**
78
   * Test access to tax rates listing.
79
   */
80
  public function testCommerceTaxUIAccessTaxRates() {
81
    // Login with normal user.
82
    $this->drupalLogin($this->normal_user);
83

    
84
    // Access the Tax rates listing.
85
    $this->drupalGet('admin/commerce/config/taxes');
86

    
87
    // It should return a 403.
88
    $this->assertResponse(403, t('Normal user is not able to access the tax rates listing page'));
89

    
90
    // Login with store admin user.
91
    $this->drupalLogin($this->store_admin);
92

    
93
    // Access the Tax rates listing.
94
    $this->drupalGet('admin/commerce/config/taxes');
95

    
96
    // It should return a 200.
97
    $this->assertResponse(200, t('Store admin user can access the tax rates listing page'));
98

    
99
    // Check if the add link is there.
100
    $this->assertText(t('Add a tax rate'), t('%link link is present in the tax rates listing page', array('%link' => t('Add a tax rate'))));
101

    
102
    // There shouldn't be any tax rate yet.
103
    $this->assertRaw(t('There are no tax rates yet. <a href="@link">Add a tax rate</a>.', array('@link' => url('admin/commerce/config/taxes/rates/add'))), t('Empty tax rate listing message is displayed'));
104
  }
105

    
106
  /**
107
   * Test the creation of a tax rate.
108
   */
109
  public function testCommerceTaxUICreateTaxRate() {
110
    // Login with normal user.
111
    $this->drupalLogin($this->normal_user);
112

    
113
    // Access the creation page for tax rates.
114
    $this->drupalGet('admin/commerce/config/taxes/rates/add');
115

    
116
    // It should return a 403.
117
    $this->assertResponse(403, t('Normal user is not able to access the creation page for tax rates'));
118

    
119
    // Login with store admin user.
120
    $this->drupalLogin($this->store_admin);
121

    
122
    // Access the creation page for tax rates.
123
    $this->drupalGet('admin/commerce/config/taxes/rates/add');
124

    
125
    // It should return a 200.
126
    $this->assertResponse(200, t('Store admin user can access the creation page for tax rates'));
127

    
128
    // Check the integrity of the tax rate form.
129
    $this->pass(t('Test the integrity of the tax rate add form:'));
130
    $this->assertFieldByXPath('//input[@id="edit-tax-rate-title" and contains(@class, "required")]', NULL, t('Tax rate title field is present and is required'));
131
    $this->assertFieldById('edit-tax-rate-display-title', NULL, t('Tax rate display title field is present'));
132
    $this->assertFieldById('edit-tax-rate-description', NULL, t('Tax rate description is present'));
133
    $this->assertFieldByXPath('//input[@id="edit-tax-rate-rate" and contains(@class, "required")]', 0, t('Tax rate rate field is present, has 0 as default value and is required'));
134
    $this->assertFieldByXPath('//select[@id="edit-tax-rate-type" and contains(@class, "required")]', NULL, t('Tax rate type field is present and is required'));
135

    
136
    $tax_select_types = $this->xpath('//select[@id="edit-tax-rate-type"]//option');
137
    foreach (commerce_tax_types() as $tax_type) {
138
      $this->assertTrue(in_array($tax_type['display_title'], (array)$tax_select_types), t('Tax rate type %type is available for the rate', array('%type' => $tax_type['display_title'])));
139
    }
140

    
141
    $this->assertFieldById('edit-submit', t('Save tax rate'), t('\'Save tax rate\' button is present'));
142
    $this->assertRaw(l(t('Cancel'), 'admin/commerce/config/taxes'), t('Cancel link is present'));
143

    
144
    // Fill the tax rate information and save tax rate.
145
    $edit = array(
146
      'tax_rate[title]' => 'Example tax rate',
147
    	'tax_rate[name]' => 'example_tax_rate',
148
    	'tax_rate[display_title]' => 'Example tax rate',
149
    	'tax_rate[description]' => 'Example tax rate for testing',
150
    	'tax_rate[rate]' => rand(1,100)/1000,
151
    	'tax_rate[type]' => 'example_tax_type',
152
    );
153
    $this->drupalPost(NULL, $edit, t('Save tax rate'));
154

    
155
    // Check the url after creation and if the values have been saved.
156
    $this->assertTrue($this->url == url('admin/commerce/config/taxes', array('absolute' => TRUE)), t('After saving a tax rate we are in the list of tax rates'));
157
    $this->assertText($edit['tax_rate[title]'], t('Title of the tax rate is present in the tax rates listing'));
158
    $this->assertText($edit['tax_rate[name]'], t('Machine name of the tax rate is present in the tax rates listing'));
159
    $this->assertText($edit['tax_rate[description]'], t('Description of the tax rate is present in the tax rates listing'));
160
    $this->assertText(trim($edit['tax_rate[rate]']), t('Rate value of the tax rate is present in the tax rates listing'));
161

    
162
    // Check in database if the tax rate has been created.
163
    commerce_tax_rates_reset();
164
    $tax_rate = commerce_tax_rate_load($edit['tax_rate[name]']);
165
    $this->assertFalse(empty($tax_rate), t('Tax is stored in database'));
166
  }
167

    
168
  /**
169
   * Test editing a tax rate.
170
   */
171
  public function testCommerceTaxUIEditTaxRate() {
172
    // Create a tax rate.
173
    $tax_rate = $this->createDummyTaxRate();
174

    
175
    // Login with normal user.
176
    $this->drupalLogin($this->normal_user);
177

    
178
    // Access the edit page for tax rates.
179
    $this->drupalGet('admin/commerce/config/taxes/rates/' . strtr($tax_rate['name'], '_', '-') . '/edit');
180

    
181
    // It should return a 403.
182
    $this->assertResponse(403, t('Normal user is not able to access the tax rate edit page'));
183

    
184
    // Login with store admin user.
185
    $this->drupalLogin($this->store_admin);
186

    
187
    // Access the edit page for tax rates.
188
    $this->drupalGet('admin/commerce/config/taxes/rates/' . strtr($tax_rate['name'], '_', '-') . '/edit');
189

    
190
    // It should return a 200.
191
    $this->assertResponse(200, t('Store admin user can access the tax rate edit page'));
192

    
193
    // Check if the data loaded in the edit form matches with the tax rate.
194
    $this->pass(t('Test if the rate is correctly loaded in the edit form:'));
195
    $this->assertFieldById('edit-tax-rate-title', $tax_rate['title'], t('Title field corresponds with tax rate'));
196
    $this->assertText($tax_rate['name'], t('Machine name field corresponds with tax rate'));
197
    $this->assertFieldById('edit-tax-rate-display-title', $tax_rate['display_title'], t('Display title field corresponds with tax rate'));
198
    $this->assertFieldById('edit-tax-rate-rate', $tax_rate['rate'], t('Rate field corresponds with tax rate'));
199
    $this->assertOptionSelected('edit-tax-rate-type', $tax_rate['type'], t('Type select value corresponds with tax rate'));
200
    $this->assertFieldById('edit-submit', t('Save tax rate'), t('\'Save tax rate\' button is present'));
201
    $this->assertFieldById('edit-delete', t('Delete tax rate'), t('Delete button is present'));
202
    $this->assertRaw(l(t('Cancel'), 'admin/commerce/config/taxes'), t('Cancel link is present'));
203

    
204
    // Modify tax rate information and save the form.
205
    $edit = array(
206
      'tax_rate[title]' => 'Altered tax rate',
207
    	'tax_rate[display_title]' => 'Altered tax rate',
208
    	'tax_rate[description]' => 'Altered tax rate for testing',
209
    	'tax_rate[rate]' => $tax_rate['rate'] + rand(1,100)/1000,
210
    	'tax_rate[type]' => 'vat',
211
    );
212
    $this->drupalPost(NULL, $edit, t('Save tax rate'));
213

    
214
    // Check the url after edit and if the values have been loaded.
215
    $this->assertTrue($this->url == url('admin/commerce/config/taxes', array('absolute' => TRUE)), t('After saving a tax rate we are in the list of tax rates'));
216
    $this->assertText($edit['tax_rate[title]'], t('Title of the tax rate is present in the tax rates listing'));
217
    $this->assertText($edit['tax_rate[description]'], t('Description of the tax rate is present in the tax rates listing'));
218
    $this->assertText(trim($edit['tax_rate[rate]']), t('Rate value of the tax rate is present in the tax rates listing'));
219
    $this->assertText(t('Tax rate saved.'), t('\'Tax rate saved\' message is displayed'));
220

    
221
    // Check in database if the tax rate has been correctly modified.
222
    commerce_tax_rates_reset();
223
    $tax_rate = commerce_tax_rate_load($tax_rate['name']);
224
    $this->assertFalse(empty($tax_rate), t('Tax is present in database'));
225
    $this->assertTrue($tax_rate['title'] = $edit['tax_rate[title]'], t('Tax title is correctly saved in database'));
226
    $this->assertTrue($tax_rate['display_title'] = $edit['tax_rate[display_title]'], t('Tax display title is correctly saved in database'));
227
    $this->assertTrue($tax_rate['description'] = $edit['tax_rate[description]'], t('Tax description is correctly saved in database'));
228
    $this->assertTrue($tax_rate['rate'] = $edit['tax_rate[rate]'], t('Tax rate is correctly saved in database'));
229
    $this->assertTrue($tax_rate['type'] = $edit['tax_rate[type]'], t('Tax type is correctly saved in database'));
230
  }
231

    
232
  /**
233
   * Test deleting a tax rate.
234
   */
235
  public function testCommerceTaxUIDeleteTaxRate() {
236
    // Create a tax rate.
237
    $tax_rate = $this->createDummyTaxRate();
238

    
239
    // Login with normal user.
240
    $this->drupalLogin($this->normal_user);
241

    
242
    // Access the deletion page for tax rates.
243
    $this->drupalGet('admin/commerce/config/taxes/rates/' . strtr($tax_rate['name'], '_', '-') . '/delete');
244

    
245
    // It should return a 403.
246
    $this->assertResponse(403, t('Normal user is not able to access the delete form of a tax rate'));
247

    
248
    // Login with store admin user.
249
    $this->drupalLogin($this->store_admin);
250

    
251
    // Access the deletion page for tax rates.
252
    $this->drupalGet('admin/commerce/config/taxes/rates/' . strtr($tax_rate['name'], '_', '-') . '/delete');
253

    
254
    // It should return a 200.
255
    $this->assertResponse(200, t('Store admin user can access the delete form of a tax rate'));
256

    
257
    // Check the integrity of the tax rate delete confirmation form.
258
    $this->pass('Test the tax rate delete confirmation form:');
259
    $this->assertTitle(t('Are you sure you want to delete the !title tax rate?', array('!title' => $tax_rate['title'])) . ' | Drupal', t('The confirmation message is displayed'));
260
    $this->assertText(t('This action cannot be undone'), t('A warning notifying the user about the action can\'t be undone is displayed.'));
261
    $this->assertFieldById('edit-submit', t('Delete'), t('Delete button is present'));
262
    $this->assertText(t('Cancel'), t('Cancel is present'));
263

    
264
    // Confirm delete.
265
    $this->drupalPost(NULL, array(), t('Delete'));
266

    
267
    // Check the url after deleting and if the tax rate has been deleted in
268
    // database.
269
    $this->assertTrue($this->url == url('admin/commerce/config/taxes', array('absolute' => TRUE)), t('Landing page after deleting a tax rate is the tax rates listing page'));
270
    $this->assertRaw(t('The tax rate %title has been deleted.', array('%title' => $tax_rate['title'])), t('\'Tax rate has been deleted\' message is displayed'));
271
    $this->assertRaw(t('There are no tax rates yet. <a href="@link">Add a tax rate</a>.', array('@link' => url('admin/commerce/config/taxes/rates/add'))), t('Empty tax rate listing message is displayed'));
272

    
273
    commerce_tax_rates_reset();
274
    $tax_rate = commerce_tax_rate_load($tax_rate['name']);
275
    $this->assertTrue(empty($tax_rate), t('Tax is correctly deleted from database'));
276
  }
277

    
278
  /**
279
   * Test configuring a tax rate.
280
   */
281
  public function testCommerceTaxUIConfigureTaxRate() {
282
    // Create a tax rate.
283
    $tax_rate = $this->createDummyTaxRate();
284

    
285
    // Login with normal user.
286
    $this->drupalLogin($this->normal_user);
287

    
288
    // Access the configure component page for tax rates.
289
    $this->drupalGet('admin/config/workflow/rules/components/manage/' . $tax_rate['rules_component']);
290

    
291
    // It should return a 403.
292
    $this->assertResponse(403, t('Normal user is not able to access the component configure page for a tax rate'));
293

    
294
    // Login with store admin user.
295
    $this->drupalLogin($this->store_admin);
296

    
297
    // Access the configure component page for tax rates.
298
    $this->drupalGet('admin/config/workflow/rules/components/manage/' . $tax_rate['rules_component']);
299

    
300
    // It should return a 200.
301
    $this->assertResponse(200, t('Store admin user can access the component configure page for a tax rate'));
302

    
303
    // Add a condition
304
    $this->assertLink(t('Add condition'), 0, t('Add condition link is present'));
305
    $this->clickLink(t('Add condition'));
306

    
307
    // Select a basic condition as at this step we're only testing if the
308
    // saving process is working.
309
    $this->drupalPost(NULL, array('element_name' => 'entity_is_of_type'), t('Continue'));
310

    
311
    $this->drupalPost(NULL, array('parameter[entity][settings][entity:select]' => 'commerce-line-item', 'parameter[type][settings][type]' => 'commerce_line_item'), t('Save'));
312

    
313
    // Check that after saving the url is correct and the condition has been
314
    // added.
315
    $this->assertTrue($this->url == url('admin/config/workflow/rules/components/manage/' . $tax_rate['rules_component'], array('absolute' => TRUE)), t('After adding a new condition component the landing page is the edit components one'));
316
    $this->assertText(t('Entity is of type'), t('Condition was added correctly'));
317
  }
318

    
319
  /**
320
   * Test access to tax types listing.
321
   */
322
  public function testCommerceTaxUIAccessTaxTypes() {
323
    // Login with normal user.
324
    $this->drupalLogin($this->normal_user);
325

    
326
    // Access the tax rate types listing.
327
    $this->drupalGet('admin/commerce/config/taxes/types');
328

    
329
    // It should return a 403.
330
    $this->assertResponse(403, t('Normal user is not able to access the tax types listing'));
331

    
332
    // Login with store admin user.
333
    $this->drupalLogin($this->store_admin);
334

    
335
    // Access the tax rate types listing.
336
    $this->drupalGet('admin/commerce/config/taxes/types');
337

    
338
    // It should return a 200.
339
    $this->assertResponse(200, t('Store admin user can access the tax types listing'));
340

    
341
    // Load the tax rate types and check that all of them are in the page.
342
    $tax_types = commerce_tax_types();
343
    foreach ($tax_types as $tax_type) {
344
      $this->assertText($tax_type['display_title'], t('Tax type !name is present in the tax types listing page', array('!name' => $tax_type['display_title'])));
345
    }
346

    
347
    // Look for the Add tax type link.
348
    $this->assertText(t('Add a tax type'), t('%link link is present in the tax rates listing page', array('%link' => t('Add a tax type'))));
349
  }
350

    
351
  /**
352
   * Test the creation of a tax type.
353
   */
354
  public function testCommerceTaxUICreateTaxType() {
355
    // Login with normal user.
356
    $this->drupalLogin($this->normal_user);
357

    
358
    // Access the tax rate types create page.
359
    $this->drupalGet('admin/commerce/config/taxes/types/add');
360

    
361
    // It should return a 403.
362
    $this->assertResponse(403, t('Normal user is not able to access the tax type creation'));
363

    
364
    // Login with store admin user.
365
    $this->drupalLogin($this->store_admin);
366

    
367
    // Access the tax rate types create page.
368
    $this->drupalGet('admin/commerce/config/taxes/types/add');
369

    
370
    // It should return a 200.
371
    $this->assertResponse(200, t('Store admin user can access the tax type creation'));
372

    
373
    // Check the integrity of the creation form.
374
    $this->pass(t('Test the integrity of the tax type add form:'));
375
    $this->assertFieldByXPath('//input[@id="edit-tax-type-title" and contains(@class, "required")]', NULL, t('Tax type title field is present and is required'));
376
    $this->assertFieldById('edit-tax-type-display-title', NULL, t('Tax type display title is present'));
377
    $this->assertFieldById('edit-tax-type-description', NULL, t('Tax type description is present'));
378
    $this->assertFieldById('edit-tax-type-display-inclusive', NULL, t('Tax type checkbox for configure inclusive taxes is present'));
379
    $this->assertFieldById('edit-submit', t('Save tax type'), t('\'Save tax type\' button is present'));
380
    $this->assertRaw(l(t('Cancel'), 'admin/commerce/config/taxes/types'), t('Cancel link is present'));
381

    
382
    // Save the tax type.
383
    $edit = array(
384
      'tax_type[title]' => 'Additional tax type',
385
    	'tax_type[name]' => 'additional_tax_rate',
386
    	'tax_type[display_title]' => 'Additional tax rate',
387
    	'tax_type[description]' => 'Additional tax rate for testing',
388
    	'tax_type[display_inclusive]' => 1,
389
    );
390
    $this->drupalPost(NULL, $edit, t('Save tax type'));
391

    
392
    // Check the url after saving and if the tax type loads in the form.
393
    $this->assertTrue($this->url == url('admin/commerce/config/taxes/types', array('absolute' => TRUE)), t('After saving a tax type we are in the list of tax types'));
394
    $this->assertText($edit['tax_type[title]'], t('Title of the tax type is present in the tax rates listing'));
395
    $this->assertText($edit['tax_type[name]'], t('Machine name of the tax type is present in the tax rates listing'));
396
    $this->assertText($edit['tax_type[description]'], t('Description of the tax type is present in the tax rates listing'));
397

    
398
    // Check in database if the tax rate has been created.
399
    commerce_tax_types_reset();
400
    $tax_type = commerce_tax_type_load($edit['tax_type[name]']);
401
    $this->assertFalse(empty($tax_type), t('Tax type is stored in database'));
402

    
403
    // Create a tax rate and check that the new tax type is there.
404
    $this->drupalGet('admin/commerce/config/taxes/rates/add');
405
    $tax_select_types = $this->xpath('//select[@id="edit-tax-rate-type"]//option');
406
    $this->assertTrue(in_array($this->tax_type['display_title'], $tax_select_types), t('Tax type is available in the tax rate creation form'));
407
  }
408

    
409
  /**
410
   * Test editing a tax type.
411
   */
412
  public function testCommerceTaxUIEditTaxType() {
413
    // Login with normal user.
414
    $this->drupalLogin($this->normal_user);
415

    
416
    // Access the tax rate type edit page.
417
    $this->drupalGet('admin/commerce/config/taxes/types/' . strtr($this->tax_type['name'], '_', '-') . '/edit');
418

    
419
    // It should return a 403.
420
    $this->assertResponse(403, t('Normal user is not able to access the tax type edit page'));
421

    
422
    // Login with store admin user.
423
    $this->drupalLogin($this->store_admin);
424

    
425
    // Access the tax rate type edit page.
426
    $this->drupalGet('admin/commerce/config/taxes/types/' . strtr($this->tax_type['name'], '_', '-') . '/edit');
427

    
428
    // It should return a 200.
429
    $this->assertResponse(200, t('Store admin user can access the tax type edit page'));
430

    
431
    // Check if the data loaded in the edit form matches with the tax type.
432
    $this->pass(t('Test if the type is correctly loaded in the edit form:'));
433
    $this->assertFieldById('edit-tax-type-title', $this->tax_type['title'], t('Title field corresponds with tax type'));
434
    $this->assertText($this->tax_type['name'], t('Machine name field corresponds with tax type'));
435
    $this->assertFieldById('edit-tax-type-display-title', $this->tax_type['display_title'], t('Display title field corresponds with tax type'));
436
    $this->assertFieldById('edit-submit', t('Save tax type'), t('\'Save tax rate\' button is present'));
437
    $this->assertFieldById('edit-delete', t('Delete tax type'), t('Delete button is present'));
438
    $this->assertRaw(l(t('Cancel'), 'admin/commerce/config/taxes/types'), t('Cancel link is present'));
439

    
440
    // Modify the tax type and save it.
441
    $edit = array(
442
      'tax_type[title]' => 'Additional tax type',
443
    	'tax_type[display_title]' => 'Additional tax rate',
444
    	'tax_type[description]' => 'Additional tax rate for testing',
445
    	'tax_type[display_inclusive]' => 1,
446
    );
447
    $this->drupalPost(NULL, $edit, t('Save tax type'));
448

    
449
    // Check the url after edit and if the values have been loaded.
450
    $this->assertTrue($this->url == url('admin/commerce/config/taxes/types', array('absolute' => TRUE)), t('After saving a tax type we are in the list of tax types'));
451
    $this->assertText($edit['tax_type[title]'], t('Title of the tax type is present in the tax types listing'));
452
    $this->assertText($edit['tax_type[description]'], t('Description of the tax type is present in the tax types listing'));
453
    $this->assertText(t('Tax type saved.'), t('\'Tax type saved\' message is displayed'));
454

    
455
    // Check in database if the tax rate has been correctly modified.
456
    commerce_tax_types_reset();
457
    $tax_type = commerce_tax_type_load($this->tax_type['name']);
458
    $this->assertTrue($tax_type['title'] == $edit['tax_type[title]'], t('Title of the tax type has been correctly modified in the database'));
459
    $this->assertTrue($tax_type['display_title'] == $edit['tax_type[display_title]'], t('Display title of the tax type has been correctly modified in the database'));
460
    $this->assertTrue($tax_type['description'] == $edit['tax_type[description]'], t('Description of the tax type has been correctly modified in the database'));
461
    $this->assertTrue($tax_type['display_inclusive'] == $edit['tax_type[display_inclusive]'], t('Display inclusive option of the tax type has been correctly modified in the database'));
462
  }
463

    
464
  /**
465
   * Test adding a condition to a tax type.
466
   */
467
  public function testCommerceTaxUIConfigureTaxType() {
468
    // Login with normal user.
469
    $this->drupalLogin($this->normal_user);
470

    
471
    // Access the tax rate type configure rule page.
472
    $this->drupalGet('admin/config/workflow/rules/reaction/manage/commerce_tax_type_' . $this->tax_type['name']);
473

    
474
    // It should return a 403.
475
    $this->assertResponse(403, t('Normal user is not able to access the'));
476

    
477
    // Login with store admin user.
478
    $this->drupalLogin($this->store_admin);
479

    
480
    // Access the tax rate type configure rule page.
481
    $this->drupalGet('admin/config/workflow/rules/reaction/manage/commerce_tax_type_' . $this->tax_type['name']);
482

    
483
    // It should return a 200.
484
    $this->assertResponse(200, t('Store admin user can access the'));
485

    
486
    // Add a condition
487
    $this->assertLink(t('Add condition'), 0, t('Add condition link is present'));
488
    $this->clickLink(t('Add condition'));
489

    
490
    // Select a basic condition as at this step we're only testing if the
491
    // saving process is working.
492
    $this->drupalPost(NULL, array('element_name' => 'entity_is_of_type'), t('Continue'));
493

    
494
    $this->drupalPost(NULL, array('parameter[entity][settings][entity:select]' => 'commerce-line-item', 'parameter[type][settings][type]' => 'commerce_line_item'), t('Save'));
495

    
496
    // Check that after saving the url is correct and the condition has been
497
    // added.
498
    $this->assertTrue($this->url == url('admin/config/workflow/rules/reaction/manage/commerce_tax_type_' . $this->tax_type['name'], array('absolute' => TRUE)), t('After adding a new condition component the landing page is the edit components one'));
499
    $this->assertText(t('Entity is of type'), t('Condition was added correctly'));
500
  }
501

    
502
  /**
503
   * Test deleting a tax type.
504
   */
505
  public function testCommerceTaxUIDeleteTaxType() {
506
    // Login with normal user.
507
    $this->drupalLogin($this->normal_user);
508

    
509
    // Access the tax rate type delete page.
510
    $this->drupalGet('admin/commerce/config/taxes/types/' . strtr($this->tax_type['name'], '_', '-') . '/delete');
511

    
512
    // It should return a 403.
513
    $this->assertResponse(403, t('Normal user is not able to access the tax type delete page'));
514

    
515
    // Login with store admin user.
516
    $this->drupalLogin($this->store_admin);
517

    
518
    // Access the tax rate type delete page.
519
    $this->drupalGet('admin/commerce/config/taxes/types/' . strtr($this->tax_type['name'], '_', '-') . '/delete');
520

    
521
    // It should return a 200.
522
    $this->assertResponse(200, t('Store admin user can access the tax type delete page'));
523

    
524
    // Check the integrity of the tax type delete confirmation form.
525
    $this->pass('Test the tax type delete confirmation form:');
526
    $this->assertTitle(t('Are you sure you want to delete the !title tax type?', array('!title' => $this->tax_type['title'])) . ' | Drupal', t('The confirmation message for deleting a tax type is displayed'));
527
    $this->assertText(t('This action cannot be undone'), t('A warning notifying the user about the action can\'t be undone is displayed.'));
528
    $this->assertFieldById('edit-submit', t('Delete'), t('Delete button is present'));
529
    $this->assertText(t('Cancel'), t('Cancel is present'));
530

    
531
    // Delete the tax type.
532
    $this->drupalPost(NULL, array(), t('Delete'));
533

    
534
   // Check the url after deleting and if the tax type has been deleted in
535
    // database.
536
    $this->assertTrue($this->url == url('admin/commerce/config/taxes/types', array('absolute' => TRUE)), t('Landing page after deleting a tax type is the tax types listing page'));
537
    $this->assertRaw(t('The tax type %title has been deleted.', array('%title' => $this->tax_type['title'])), t('\'Tax type has been deleted\' message is displayed'));
538

    
539
    commerce_tax_types_reset();
540
    $tax_type = commerce_tax_rate_load($this->tax_type['name']);
541
    $this->assertTrue(empty($tax_type), t('Tax type is correctly deleted from database'));
542
  }
543

    
544
  /**
545
   * Text the deletion of a tax type that already has rates.
546
   */
547
  public function testCommerceTaxUIDeleteTaxTypeWithRates() {
548
    // Create a tax rate associated with the type.
549
    $tax_rate = $this->createDummyTaxRate();
550

    
551
    // Login with store admin user.
552
    $this->drupalLogin($this->store_admin);
553

    
554
    // Access the tax rate type delete page.
555
    $this->drupalGet('admin/commerce/config/taxes/types/' . strtr($this->tax_type['name'], '_', '-') . '/delete');
556

    
557
    // Check that the tax can't be deleted as it has a rate associated to it.
558
    $this->assertTitle(t('Cannot delete the !title tax type', array('!title' => $this->tax_type['title'])) . ' | Drupal', t('Page title for tax type deletion with rates associated to it is correct'));
559
    $this->assertText(t('There is a tax rate of this type. It cannot be deleted.'), t('A message that prevents user from deleting a tax type with rates associated to it is displayed'));
560
  }
561

    
562
  /**
563
   * Check if a 'Salex tax' rate is correctly applied in a given order.
564
   */
565
  public function testCommerceTaxUIApplySalesTax() {
566
    // Create a tax rate of Salex Type.
567
    $tax_rate = $this->createDummyTaxRate(array('type' => 'sales_tax'));
568

    
569
    // Create a dummy order in cart status.
570
    $order = $this->createDummyOrder($this->normal_user->uid);
571

    
572
    // Login with normal user.
573
    $this->drupalLogin($this->normal_user);
574

    
575
    // Get the checkout url and navigate to it.
576
    $links = commerce_line_item_summary_links();
577
    $this->checkout_url = $links['checkout']['href'];
578
    $this->drupalGet($this->checkout_url);
579

    
580
    // Check in database if the tax is applied.
581
    $order = commerce_order_load_multiple(array($order->order_id), array(), TRUE);
582
    $order_wrapper = entity_metadata_wrapper('commerce_order', reset($order));
583
    $order_data = $order_wrapper->commerce_order_total->value();
584
    $components = commerce_price_component_load($order_data, $tax_rate['price_component']);
585
    $tax_component = reset($components);
586
    $this->assertFalse(empty($tax_component), t('Tax is applied in the order'));
587

    
588
    // Tax should be applied in Checkout Review with the correct quantity.
589
    $this->assertText($tax_rate['display_title'], t('Tax appears in the order summary'));
590
    $this->assertText(trim(commerce_currency_format($order_data['amount'], $order_data['currency_code'])), t('Tax amount applied appears in the order summary'));
591
  }
592

    
593
  /**
594
   * Check if a 'VAT' tax type is correctly applied in a given product.
595
   */
596
  public function testCommerceTaxUIApplyVAT() {
597
    // Create a tax rate VAT Type.
598
    $tax_rate = $this->createDummyTaxRate(array('type' => 'vat'));
599

    
600
    // Create a new product and product display.
601
    $this->createDummyProductDisplayContentType();
602
    $product = $this->createDummyProduct();
603
    $product_wrapper = entity_metadata_wrapper('commerce_product', $product);
604
    $product_node = $this->createDummyProductNode(array($product->product_id));
605

    
606
    // Login with normal user.
607
    $this->drupalLogin($this->normal_user);
608

    
609
    // Go to the product display and check if the tax is applied.
610
    $this->drupalGet('node/' . $product_node->nid);
611

    
612
    // Check the tax applied in database and product display.
613
    $products = commerce_product_load_multiple(array($product->product_id), array(), TRUE);
614
    $product = reset($products);
615
    $price_component = commerce_product_calculate_sell_price($product);
616
    $this->assertText(trim(commerce_currency_format($price_component['amount'], $price_component['currency_code'])), t('Amount with taxes corresponds with the amount displayed in the product display page'));
617
    $components = commerce_price_component_load($price_component, $tax_rate['price_component']);
618
    $tax_component = reset($components);
619
    $this->assertFalse(empty($tax_component), t('Tax component is set in the product.'));
620
  }
621

    
622
  /**
623
   * Check if a 'VAT' tax type is correctly applied in a given product.
624
   */
625
  public function testCommerceTaxUIApplyVATInclusive() {
626
    // Create a tax rate VAT Type.
627
    $tax_rate = $this->createDummyTaxRate(array('type' => 'vat'));
628

    
629
    // Create a new product and product display.
630
    $this->createDummyProductDisplayContentType();
631
    $product = $this->createDummyProduct();
632
    $product_node = $this->createDummyProductNode(array($product->product_id));
633

    
634
    // Login with store admin user.
635
    $this->drupalLogin($this->store_admin);
636

    
637
    // Edit the product to be VAT tax inclusive.
638
    $this->drupalGet('admin/commerce/products/' . $product->product_id . '/edit');
639
    $this->drupalPost(NULL, array('commerce_price[und][0][include_tax]' => $tax_rate['name']), t('Save product'));
640

    
641
    // Login with normal user.
642
    $this->drupalLogin($this->normal_user);
643

    
644
    // Go to the product display and check if the tax is applied.
645
    $this->drupalGet('node/' . $product_node->nid);
646

    
647
    // Check the tax applied in database and in the product display.
648
    $products = commerce_product_load_multiple(array($product->product_id), array(), TRUE);
649
    $product = reset($products);
650
    $price_component = commerce_product_calculate_sell_price($product);
651
    $this->assertText(trim(commerce_currency_format($price_component['amount'], $price_component['currency_code'])), t('Amount with taxes included corresponds with the amount displayed in the product display page'));
652
    $components = commerce_price_component_load($price_component, $tax_rate['price_component']);
653
    $tax_component = reset($components);
654
    $this->assertFalse(empty($tax_component), t('Tax component is set in the product.'));
655
    $this->assertTrue($tax_component['included'], t('Tax component is configured to be included in the price'));
656
  }
657

    
658
  /**
659
   * A tax rate with no matching condition doesn't get applied.
660
   */
661
  public function testCommerceTaxUITaxNoMatchingCondition() {
662
    // Create a tax rate of Salex Type.
663
    $tax_rate = $this->createDummyTaxRate(array('type' => 'sales_tax'));
664

    
665
    // Create a dummy order in cart status.
666
    $order = $this->createDummyOrder($this->normal_user->uid);
667

    
668
    // Login with store admin user.
669
    $this->drupalLogin($this->store_admin);
670

    
671
    // Modify the tax rate to no match condition.
672
    $this->drupalGet('admin/config/workflow/rules/components/manage/' . $tax_rate['rules_component']);
673
    $this->clickLink(t('Add condition'));
674
    $this->drupalPost(NULL, array('element_name' => 'data_is'), t('Continue'));
675
    $this->drupalPost(NULL, array('parameter[data][settings][data:select]' => 'commerce-line-item:commerce-total:amount'), t('Continue'));
676
    $this->drupalPost(NULL, array('parameter[op][settings][op]' => '<', 'parameter[value][settings][value]' => '0'), t('Save'));
677

    
678
    // Login with normal user.
679
    $this->drupalLogin($this->normal_user);
680

    
681
    // Get the checkout url and navigate to it.
682
    $links = commerce_line_item_summary_links();
683
    $this->checkout_url = $links['checkout']['href'];
684
    $this->drupalGet($this->checkout_url);
685

    
686
    // As the condition is impossible to match, tax rate shouldn't be applied.
687
    $this->assertNoText($tax_rate['display_title'], t('Tax rate doesn\'t match the conditions and is not present in the cart review pane.'));
688
    // Also check it at database level.
689
    $orders = commerce_order_load_multiple(array($order->order_id), array(), TRUE);
690
    $order = reset($orders);
691
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
692
    $components = commerce_price_component_load($order_wrapper->commerce_order_total->value(), $tax_rate['price_component']);
693
    $tax_component = reset($components);
694
    $this->assertTrue(empty($tax_component), t('Tax component is not set in the order.'));
695
  }
696

    
697
  /**
698
   * Check the taxes applied in the order that a normal user can view.
699
   */
700
  public function testCommerceTaxUIUserOrderView() {
701
    // Create a tax rate.
702
    $tax_rate = $this->createDummyTaxRate(array('type' => 'sales_tax'));
703

    
704
    // Create new order and products associated to it.
705
    $order = $this->createDummyOrder($this->normal_user->uid);
706

    
707
    // Login with normal user.
708
    $this->drupalLogin($this->normal_user);
709

    
710
    // Go through the complete order process.
711
    $this->commerceTaxHelperCompleteCheckout();
712

    
713
    // Access the View orders page and view the order just created.
714
    $this->drupalGet('user/' . $this->normal_user->uid . '/orders/' . $order->order_id);
715

    
716
    // Reload the order directly from db.
717
    $orders = commerce_order_load_multiple(array($order->order_id), array(), TRUE);
718
    $order = reset($orders);
719
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
720
    $components = commerce_price_component_load($order_wrapper->commerce_order_total->value(), $tax_rate['price_component']);
721
    $tax_component = reset($components);
722

    
723
    // Check the taxes applied.
724
    $this->assertText($tax_rate['display_title'], t('Tax display title is displayed in the user view of an order.'));
725
    $this->assertText(trim(commerce_currency_format($tax_component['price']['amount'], $tax_component['price']['currency_code'])), t('Tax amount is displayed in the user view of an order.'));
726
  }
727

    
728
  /**
729
   * Check the taxes applied in the order admin view.
730
   */
731
  public function testCommerceTaxUIAdminOrder() {
732
    // Create a tax rate.
733
    $tax_rate = $this->createDummyTaxRate(array('type' => 'sales_tax'));
734

    
735
    // Create new order and products associated to it.
736
    $order = $this->createDummyOrder($this->normal_user->uid);
737

    
738
    // Login with normal user.
739
    $this->drupalLogin($this->normal_user);
740

    
741
    // Go through the complete order process.
742
    $this->commerceTaxHelperCompleteCheckout();
743

    
744
    // Login with store admin user.
745
    $this->drupalLogin($this->store_admin);
746

    
747
    // Access the admin order edit page.
748
    $this->drupalGet('admin/commerce/orders/' . $order->order_id);
749

    
750
    // Reload the order directly from db.
751
    $orders = commerce_order_load_multiple(array($order->order_id), array(), TRUE);
752
    $order = reset($orders);
753
    $order_wrapper = entity_metadata_wrapper('commerce_order', $order);
754
    $components = commerce_price_component_load($order_wrapper->commerce_order_total->value(), $tax_rate['price_component']);
755
    $tax_component = reset($components);
756

    
757
    // Check the taxes applied.
758
    $this->assertText($tax_rate['display_title'], t('Tax display title is displayed in the admin view of an order.'));
759
    $this->assertText(trim(commerce_currency_format($tax_component['price']['amount'], $tax_component['price']['currency_code'])), t('Tax amount is displayed in the admin view of an order.'));
760
  }
761

    
762
}