You are here

public function UsageTest::testOrderIntegration in Commerce Core 8.2

Tests the order integration.

@covers ::register @covers ::delete @covers ::deleteByCoupon @covers ::load @covers ::loadMultiple

File

modules/promotion/tests/src/Kernel/UsageTest.php, line 206

Class

UsageTest
Tests the usage tracking of promotions.

Namespace

Drupal\Tests\commerce_promotion\Kernel

Code

public function testOrderIntegration() {
  $first_promotion = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.10',
      ],
    ],
    'start_date' => '2017-01-01',
    'status' => TRUE,
  ]);
  $first_promotion
    ->save();
  $coupon = Coupon::create([
    'code' => $this
      ->randomMachineName(),
    'status' => TRUE,
  ]);
  $coupon
    ->save();
  $second_promotion = $first_promotion
    ->createDuplicate();
  $second_promotion
    ->addCoupon($coupon);
  $second_promotion
    ->save();
  $this->order
    ->get('coupons')
    ->appendItem($coupon);
  $this->order
    ->save();
  $this->container
    ->get('commerce_order.order_refresh')
    ->refresh($this->order);
  $this
    ->assertEquals(2, count($this->order
    ->collectAdjustments()));
  $this->order
    ->save();
  $this->order
    ->getState()
    ->applyTransitionById('place');
  $this->order
    ->save();
  $this
    ->assertEquals(1, $this->usage
    ->load($first_promotion));
  $this
    ->assertEquals(1, $this->usage
    ->load($second_promotion));
  $this
    ->assertEquals([
    1 => 1,
    2 => 1,
  ], $this->usage
    ->loadMultiple([
    $first_promotion,
    $second_promotion,
  ]));

  // Deleting a coupon should delete its usage.
  $second_promotion
    ->delete();
  $this
    ->assertEquals(0, $this->usage
    ->load($second_promotion));

  // Deleting a promotion should delete its usage.
  $first_promotion
    ->delete();
  $this
    ->assertEquals(0, $this->usage
    ->load($first_promotion));
  $this
    ->assertEquals([
    1 => 0,
    2 => 0,
  ], $this->usage
    ->loadMultiple([
    $first_promotion,
    $second_promotion,
  ]));
}