You are here

public function PromotionStorageTest::testLoadAvailable in Commerce Core 8.2

Tests loadAvailable().

File

modules/promotion/tests/src/Kernel/PromotionStorageTest.php, line 88

Class

PromotionStorageTest
Tests promotion storage.

Namespace

Drupal\Tests\commerce_promotion\Kernel

Code

public function testLoadAvailable() {

  // Starts now. No end date.
  $promotion1 = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      $this->orderType,
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_fixed_amount_off',
      'target_plugin_configuration' => [
        'amount' => [
          'number' => '25.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'start_date' => '2019-11-15T10:14:00',
    'status' => TRUE,
  ]);
  $promotion1
    ->save();
  $promotion1 = $this
    ->reloadEntity($promotion1);

  // Past start date, no end date.
  $promotion2 = Promotion::create([
    'name' => 'Promotion 2',
    'order_types' => [
      $this->orderType,
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.20',
      ],
    ],
    'start_date' => '2019-01-01T00:00:00',
    'status' => TRUE,
  ]);
  $promotion2
    ->save();
  $promotion2 = $this
    ->reloadEntity($promotion2);

  // Past start date, no end date. Disabled.
  $promotion3 = Promotion::create([
    'name' => 'Promotion32',
    'order_types' => [
      $this->orderType,
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.30',
      ],
    ],
    'start_date' => '2014-01-01T00:00:00',
    'status' => FALSE,
  ]);
  $promotion3
    ->save();

  // Past start date, ends now.
  $promotion4 = Promotion::create([
    'name' => 'Promotion 4',
    'order_types' => [
      $this->orderType,
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.40',
      ],
    ],
    'start_date' => '2019-01-01T00:00:00',
    'end_date' => '2019-11-15T10:14:00',
    'status' => TRUE,
  ]);
  $promotion4
    ->save();

  // Past start date, future end date.
  $promotion5 = Promotion::create([
    'name' => 'Promotion 5',
    'order_types' => [
      $this->orderType,
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.50',
      ],
    ],
    'start_date' => '2019-01-01T00:00:00',
    'end_date' => '2020-01-01T00:00:00',
    'status' => TRUE,
    'weight' => -10,
  ]);
  $promotion5
    ->save();
  $promotion5 = $this
    ->reloadEntity($promotion5);

  // Past start date, past end date.
  $promotion6 = Promotion::create([
    'name' => 'Promotion 6',
    'order_types' => [
      $this->orderType,
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_percentage_off',
      'target_plugin_configuration' => [
        'percentage' => '0.60',
      ],
    ],
    'start_date' => '2019-01-01T00:00:00',
    'end_date' => '2019-10-15T10:14:00',
    'status' => TRUE,
  ]);
  $promotion6
    ->save();

  // Confirm that the promotions were filtered by date and status,
  // and sorted by weight.
  $promotions = $this->promotionStorage
    ->loadAvailable($this->order);
  $this
    ->assertCount(3, $promotions);
  $this
    ->assertEquals([
    $promotion5
      ->id(),
    $promotion1
      ->id(),
    $promotion2
      ->id(),
  ], array_keys($promotions));

  // Test filtering by offer ID.
  $promotions = $this->promotionStorage
    ->loadAvailable($this->order, [
    'order_fixed_amount_off',
    'order_percentage_off',
  ]);
  $this
    ->assertCount(3, $promotions);
  $this
    ->assertEquals([
    $promotion5
      ->id(),
    $promotion1
      ->id(),
    $promotion2
      ->id(),
  ], array_keys($promotions));
  $promotions = $this->promotionStorage
    ->loadAvailable($this->order, [
    'order_fixed_amount_off',
  ]);
  $this
    ->assertCount(1, $promotions);
  $this
    ->assertEquals([
    $promotion1
      ->id(),
  ], array_keys($promotions));
  $promotions = $this->promotionStorage
    ->loadAvailable($this->order, [
    'order_percentage_off',
  ]);
  $this
    ->assertCount(2, $promotions);
  $this
    ->assertEquals([
    $promotion5
      ->id(),
    $promotion2
      ->id(),
  ], array_keys($promotions));
}