You are here

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

File

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

Class

ProductAddonTest
Functional test of the formatter and form.

Namespace

Drupal\Tests\commerce_pado\Functional

Code

public function testAddonOneOption() {
  $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',
        'sku' => 'MEMBERSHIP',
        'price' => [
          'number' => '10.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()
    ->pageTextContains(new FormattableMarkup('Add @product', [
    '@product' => $add_on_product
      ->label(),
  ]));
  $this
    ->assertSession()
    ->checkboxNotChecked(new FormattableMarkup('@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
    ->getSession()
    ->getPage()
    ->checkField(new FormattableMarkup('@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(2, $this->cart
    ->getItems());
}