You are here

public function ProductTypeTest::testDelete in Commerce Core 8.2

Tests deleting a product type.

File

modules/product/tests/src/Functional/ProductTypeTest.php, line 226

Class

ProductTypeTest
Tests the product type UI.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testDelete() {
  $variation_type = $this
    ->createEntity('commerce_product_variation_type', [
    'id' => 'foo',
    'label' => 'foo',
  ]);

  /** @var \Drupal\commerce_product\Entity\ProductTypeInterface $product_type */
  $product_type = $this
    ->createEntity('commerce_product_type', [
    'id' => 'foo',
    'label' => 'foo',
    'variationType' => $variation_type
      ->id(),
  ]);
  $product = $this
    ->createEntity('commerce_product', [
    'type' => $product_type
      ->id(),
    'title' => $this
      ->randomMachineName(),
  ]);

  // Confirm that the type can't be deleted while there's a product.
  $this
    ->drupalGet($product_type
    ->toUrl('delete-form'));
  $this
    ->assertSession()
    ->pageTextContains(t('@type is used by 1 product on your site. You cannot remove this product type until you have removed all of the @type products.', [
    '@type' => $product_type
      ->label(),
  ]));
  $this
    ->assertSession()
    ->pageTextNotContains(t('This action cannot be undone.'));

  // Confirm that the delete page is not available when the type is locked.
  $product_type
    ->lock();
  $product_type
    ->save();
  $this
    ->drupalGet($product_type
    ->toUrl('delete-form'));
  $this
    ->assertSession()
    ->statusCodeEquals('403');

  // Delete the product, unlock the type, confirm that deletion works.
  $product
    ->delete();
  $product_type
    ->unlock();
  $product_type
    ->save();
  $this
    ->drupalGet($product_type
    ->toUrl('delete-form'));
  $this
    ->assertSession()
    ->pageTextContains(t('Are you sure you want to delete the product type @type?', [
    '@type' => $product_type
      ->label(),
  ]));
  $this
    ->assertSession()
    ->pageTextContains(t('This action cannot be undone.'));
  $this
    ->submitForm([], 'Delete');
  $exists = (bool) ProductType::load($product_type
    ->id());
  $this
    ->assertEmpty($exists);
}