You are here

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

File

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

Class

ProductAddonTest
Functional test of the formatter and form.

Namespace

Drupal\Tests\commerce_pado\Functional

Code

public function testMultipleAddonProducts() {
  $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',
        ],
      ]),
    ],
  ]);
  $add_on_product_2 = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'USB Stick',
    'stores' => [
      $this->store,
    ],
    'variations' => [
      $this
        ->createEntity('commerce_product_variation', [
        'type' => 'default',
        'sku' => 'MEMBERSHIP-USB',
        'title' => 'USB Stick',
        'price' => [
          'number' => '3.99',
          'currency_code' => 'USD',
        ],
      ]),
    ],
  ]);
  $this->productStorage
    ->resetCache([
    $this->variation
      ->getProductId(),
  ]);

  /** @var \Drupal\commerce_product\Entity\Product $product */
  $product = $this->productStorage
    ->load($this->variation
    ->getProductId());
  $product
    ->get('pado')
    ->setValue([
    $add_on_product
      ->id(),
    $add_on_product_2
      ->id(),
  ]);
  $product
    ->save();
  $this
    ->drupalGet($product
    ->toUrl());
  $this
    ->assertSession()
    ->selectExists(new FormattableMarkup('Add @product', [
    '@product' => $add_on_product
      ->label(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains(new FormattableMarkup('Add @product', [
    '@product' => $add_on_product_2
      ->label(),
  ]));
  $this
    ->getSession()
    ->getPage()
    ->selectFieldOption(new FormattableMarkup('Add @product', [
    '@product' => $add_on_product
      ->label(),
  ]), 'Tier 2');
  $this
    ->getSession()
    ->getPage()
    ->checkField('USB Stick');
  $this
    ->submitForm([], 'Add to cart');
  $this->orderStorage
    ->resetCache([
    $this->cart
      ->id(),
  ]);
  $this->cart = $this->orderStorage
    ->load($this->cart
    ->id());
  $this
    ->assertCount(3, $this->cart
    ->getItems());
}