You are here

public function ProductAddonTest::testAddonMultipleVariationOptions in Commerce Product Add-on 8

File

tests/src/Functional/ProductAddonTest.php, line 121

Class

ProductAddonTest
Functional test of the formatter and form.

Namespace

Drupal\Tests\commerce_pado\Functional

Code

public function testAddonMultipleVariationOptions() {
  $variation_type = ProductVariationType::load($this->variation
    ->bundle());
  $variation_type
    ->setGenerateTitle(FALSE);
  $variation_type
    ->save();
  $add_on_product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'Membership',
    'stores' => [
      $this->store,
    ],
    'body' => [
      'value' => 'This is a membership addon',
    ],
    'variations' => [
      $this
        ->createEntity('commerce_product_variation', [
        'type' => 'default',
        'title' => 'Tier 1',
        'sku' => 'MEMBERSHIP',
        'price' => [
          'number' => '10.99',
          'currency_code' => 'USD',
        ],
      ]),
      $this
        ->createEntity('commerce_product_variation', [
        'type' => 'default',
        'title' => 'Tier 2',
        'sku' => 'MEMBERSHIP',
        'price' => [
          'number' => '14.99',
          'currency_code' => 'USD',
        ],
      ]),
      $this
        ->createEntity('commerce_product_variation', [
        'type' => 'default',
        'title' => 'Tier 3',
        'sku' => 'MEMBERSHIP',
        'price' => [
          'number' => '19.99',
          'currency_code' => 'USD',
        ],
      ]),
      $this
        ->createEntity('commerce_product_variation', [
        'type' => 'default',
        'title' => 'Unpublish variation',
        'sku' => 'MEMBERSHIP',
        'status' => 0,
        'price' => [
          'number' => '29.99',
          'currency_code' => 'USD',
        ],
      ]),
    ],
  ]);
  $this->productStorage
    ->resetCache([
    $this->variation
      ->getProductId(),
  ]);
  $product = $this->productStorage
    ->load($this->variation
    ->getProductId());
  $product
    ->get('pado')
    ->appendItem($add_on_product
    ->id());
  $product
    ->save();
  $this
    ->drupalGet($product
    ->toUrl());
  $this
    ->assertSession()
    ->selectExists(new FormattableMarkup('Add @product', [
    '@product' => $add_on_product
      ->label(),
  ]));
  $this
    ->submitForm([], 'Add to cart');
  $this->orderStorage
    ->resetCache([
    $this->cart
      ->id(),
  ]);
  $this->cart = $this->orderStorage
    ->load($this->cart
    ->id());
  $this
    ->assertCount(1, $this->cart
    ->getItems());
  $this->cartManager
    ->emptyCart($this->cart);
  $this
    ->drupalGet($product
    ->toUrl());
  $this
    ->assertSession()
    ->optionNotExists('add_ons[items][add_ons_2]', 'Add Unpublish variation');
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption(new FormattableMarkup('Add @product', [
    '@product' => $add_on_product
      ->label(),
  ]), 'Tier 3');
  $this
    ->submitForm([], 'Add to cart');
  $this->orderStorage
    ->resetCache([
    $this->cart
      ->id(),
  ]);
  $this->cart = $this->orderStorage
    ->load($this->cart
    ->id());
  $this
    ->assertCount(2, $this->cart
    ->getItems());
}