You are here

protected function BuyXGetYTest::setUp in Commerce Core 8.2

Overrides OrderKernelTestBase::setUp

File

modules/promotion/tests/src/Kernel/Plugin/Commerce/PromotionOffer/BuyXGetYTest.php, line 57

Class

BuyXGetYTest
Tests the "Buy X Get Y" offer.

Namespace

Drupal\Tests\commerce_promotion\Kernel\Plugin\Commerce\PromotionOffer

Code

protected function setUp() : void {
  parent::setUp();
  $this
    ->installEntitySchema('commerce_promotion');
  $this
    ->installConfig([
    'commerce_promotion',
  ]);
  $this
    ->installSchema('commerce_promotion', [
    'commerce_promotion_usage',
  ]);
  $product_type = ProductType::create([
    'id' => 'test',
    'label' => 'Test',
    'variationType' => 'default',
  ]);
  $product_type
    ->save();
  for ($i = 0; $i < 4; $i++) {
    $this->variations[$i] = ProductVariation::create([
      'type' => 'default',
      'sku' => $this
        ->randomMachineName(),
      'price' => [
        'number' => Calculator::multiply('10', $i + 1),
        'currency_code' => 'USD',
      ],
    ]);
    $this->variations[$i]
      ->save();
  }
  $first_product = Product::create([
    'type' => 'test',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $this->variations[0],
    ],
  ]);
  $first_product
    ->save();
  $second_product = Product::create([
    'type' => 'default',
    'title' => $this
      ->randomMachineName(),
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $this->variations[1],
    ],
  ]);
  $second_product
    ->save();
  $third_product = Product::create([
    'type' => 'default',
    'title' => 'Hat 1',
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $this->variations[2],
    ],
  ]);
  $third_product
    ->save();
  $fourth_product = Product::create([
    'type' => 'default',
    'title' => 'Hat 2',
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $this->variations[3],
    ],
  ]);
  $fourth_product
    ->save();
  $this->order = Order::create([
    'type' => 'default',
    'state' => 'completed',
    'mail' => 'test@example.com',
    'ip_address' => '127.0.0.1',
    'order_number' => '6',
    'uid' => $this
      ->createUser(),
    'store_id' => $this->store,
    'order_items' => [],
  ]);

  // Buy 6 "test" products, get 4 hats.
  $promotion = Promotion::create([
    'name' => 'Promotion 1',
    'order_types' => [
      $this->order
        ->bundle(),
    ],
    'stores' => [
      $this->store
        ->id(),
    ],
    'offer' => [
      'target_plugin_id' => 'order_buy_x_get_y',
      'target_plugin_configuration' => [
        'buy_quantity' => 6,
        'buy_conditions' => [
          [
            'plugin' => 'order_item_product_type',
            'configuration' => [
              'product_types' => [
                'test',
              ],
            ],
          ],
        ],
        'get_quantity' => 4,
        'get_conditions' => [
          [
            'plugin' => 'order_item_product',
            'configuration' => [
              'products' => [
                [
                  'product' => $third_product
                    ->uuid(),
                ],
                [
                  'product' => $fourth_product
                    ->uuid(),
                ],
              ],
            ],
          ],
        ],
        'offer_type' => 'fixed_amount',
        'offer_amount' => [
          'number' => '1.00',
          'currency_code' => 'USD',
        ],
      ],
    ],
    'status' => TRUE,
  ]);
  $promotion
    ->save();
  $this->promotion = $this
    ->reloadEntity($promotion);
}