You are here

public function ProductVariationTypeTest::testDelete in Commerce Core 8.2

Tests deleting a product variation type.

File

modules/product/tests/src/Functional/ProductVariationTypeTest.php, line 136

Class

ProductVariationTypeTest
Tests the product variation type UI.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testDelete() {

  /** @var \Drupal\commerce_product\Entity\ProductTypeInterface $variation_type */
  $variation_type = $this
    ->createEntity('commerce_product_variation_type', [
    'id' => 'foo',
    'label' => 'foo',
  ]);
  $variation = $this
    ->createEntity('commerce_product_variation', [
    'type' => $variation_type
      ->id(),
    'sku' => $this
      ->randomMachineName(),
    'title' => $this
      ->randomMachineName(),
  ]);

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

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

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