You are here

public function CommerceTaxUIAdminTest::testCommerceTaxUICreateTaxRate in Commerce Core 7

Test the creation of a tax rate.

File

modules/tax/tests/commerce_tax_ui.test, line 109
Functional tests for the commerce tax UI module.

Class

CommerceTaxUIAdminTest
Functional tests for the commerce tax UI module.

Code

public function testCommerceTaxUICreateTaxRate() {

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

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

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

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

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

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

  // Check the integrity of the tax rate form.
  $this
    ->pass(t('Test the integrity of the tax rate add form:'));
  $this
    ->assertFieldByXPath('//input[@id="edit-tax-rate-title" and contains(@class, "required")]', NULL, t('Tax rate title field is present and is required'));
  $this
    ->assertFieldById('edit-tax-rate-display-title', NULL, t('Tax rate display title field is present'));
  $this
    ->assertFieldById('edit-tax-rate-description', NULL, t('Tax rate description is present'));
  $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'));
  $this
    ->assertFieldByXPath('//select[@id="edit-tax-rate-type" and contains(@class, "required")]', NULL, t('Tax rate type field is present and is required'));
  $tax_select_types = $this
    ->xpath('//select[@id="edit-tax-rate-type"]//option');
  foreach (commerce_tax_types() as $tax_type) {
    $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'],
    )));
  }
  $this
    ->assertFieldById('edit-submit', t('Save tax rate'), t('\'Save tax rate\' button is present'));
  $this
    ->assertRaw(l(t('Cancel'), 'admin/commerce/config/taxes'), t('Cancel link is present'));

  // Fill the tax rate information and save tax rate.
  $edit = array(
    'tax_rate[title]' => 'Example tax rate',
    'tax_rate[name]' => 'example_tax_rate',
    'tax_rate[display_title]' => 'Example tax rate',
    'tax_rate[description]' => 'Example tax rate for testing',
    'tax_rate[rate]' => rand(1, 100) / 1000,
    'tax_rate[type]' => 'example_tax_type',
  );
  $this
    ->drupalPost(NULL, $edit, t('Save tax rate'));

  // Check the url after creation and if the values have been saved.
  $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'));
  $this
    ->assertText($edit['tax_rate[title]'], t('Title of the tax rate is present in the tax rates listing'));
  $this
    ->assertText($edit['tax_rate[name]'], t('Machine name of the tax rate is present in the tax rates listing'));
  $this
    ->assertText($edit['tax_rate[description]'], t('Description of the tax rate is present in the tax rates listing'));
  $this
    ->assertText(trim($edit['tax_rate[rate]']), t('Rate value of the tax rate is present in the tax rates listing'));

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