You are here

public function PromotionTest::testDuplicatePromotion in Commerce Core 8.2

Tests duplicating a promotion.

File

modules/promotion/tests/src/FunctionalJavascript/PromotionTest.php, line 350

Class

PromotionTest
Tests the admin UI for promotions.

Namespace

Drupal\Tests\commerce_promotion\FunctionalJavascript

Code

public function testDuplicatePromotion() {
  $promotion = $this
    ->createEntity('commerce_promotion', [
    'name' => '10% off',
    'status' => TRUE,
    'offer' => [
      'target_plugin_id' => 'order_item_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.10',
      ],
    ],
    'conditions' => [
      [
        'target_plugin_id' => 'order_total_price',
        'target_plugin_configuration' => [
          'amount' => [
            'number' => '9.10',
            'currency_code' => 'USD',
          ],
        ],
      ],
    ],
  ]);
  $this
    ->drupalGet($promotion
    ->toUrl('duplicate-form'));

  // Check the integrity of the form.
  $this
    ->assertSession()
    ->fieldValueEquals('name[0][value]', '10% off');
  $this
    ->assertSession()
    ->fieldValueEquals('offer[0][target_plugin_id]', 'order_item_percentage_off');
  $this
    ->assertSession()
    ->fieldValueEquals('offer[0][target_plugin_configuration][order_item_percentage_off][percentage]', '10');
  $this
    ->assertSession()
    ->pageTextContains('Restricted');
  $this
    ->assertSession()
    ->checkboxChecked('Current order total');
  $this
    ->assertSession()
    ->fieldValueEquals('conditions[form][order][order_total_price][configuration][form][amount][number]', '9.10');
  $edit = [
    'name[0][value]' => '20% off',
    'offer[0][target_plugin_configuration][order_item_percentage_off][percentage]' => '20',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->pageTextContains('Saved the 20% off promotion.');

  // Confirm that the original promotion is unchanged.
  $promotion = $this
    ->reloadEntity($promotion);
  $this
    ->assertNotEmpty($promotion);
  $this
    ->assertEquals('10% off', $promotion
    ->label());

  // Confirm that the new promotion has the expected data.

  /** @var \Drupal\commerce_promotion\Entity\PromotionInterface $promotion */
  $promotion = Promotion::load($promotion
    ->id() + 1);
  $this
    ->assertNotEmpty($promotion);
  $this
    ->assertEquals('20% off', $promotion
    ->label());
  $offer = $promotion
    ->getOffer();
  $this
    ->assertEquals('0.20', $offer
    ->getConfiguration()['percentage']);
}