You are here

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

File

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

Class

ProductAddonTest
Functional test of the formatter and form.

Namespace

Drupal\Tests\commerce_pado\Functional

Code

public function testAddonMultipleVariationOptionsAllowMultiple() {
  $variation_type = ProductVariationType::load($this->variation
    ->bundle());
  $variation_type
    ->setGenerateTitle(FALSE);
  $variation_type
    ->save();
  $this->productStorage
    ->resetCache([
    $this->variation
      ->getProductId(),
  ]);
  $product = $this->productStorage
    ->load($this->variation
    ->getProductId());

  // Use the add-on formatter.

  /** @var \Drupal\Core\Entity\Display\EntityViewDisplayInterface $view_display */
  $view_display = commerce_get_entity_display($product
    ->getEntityTypeId(), $product
    ->bundle(), 'view');
  $view_display
    ->setComponent('variations', [
    'type' => 'commerce_pado_add_to_cart',
    'label' => 'hidden',
    'settings' => [
      'add_on_field' => 'pado',
      'multiple' => 1,
    ],
  ]);
  $view_display
    ->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',
        ],
      ]),
    ],
  ]);
  $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('Tier 1');
  $this
    ->assertSession()
    ->checkboxNotChecked('Tier 2');
  $this
    ->assertSession()
    ->checkboxNotChecked('Tier 3');
  $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('Tier 2');
  $this
    ->getSession()
    ->getPage()
    ->checkField('Tier 3');
  $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());
}