You are here

public function ProductTypeTest::testEdit in Commerce Core 8.2

Tests editing a product type.

File

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

Class

ProductTypeTest
Tests the product type UI.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testEdit() {
  $this
    ->drupalGet('admin/commerce/config/product-types/default/edit');
  $variation_type_field = $this
    ->getSession()
    ->getPage()
    ->findField('variationType');
  $this
    ->assertFalse($variation_type_field
    ->hasAttribute('disabled'));
  $edit = [
    'label' => 'Default!',
    'description' => 'New description.',
    'multipleVariations' => FALSE,
    'injectVariationFields' => FALSE,
  ];
  $this
    ->submitForm($edit, t('Save'));
  $this
    ->assertSession()
    ->pageTextContains('The product type Default! has been successfully saved.');
  $product_type = ProductType::load('default');
  $this
    ->assertEquals($edit['label'], $product_type
    ->label());
  $this
    ->assertEquals($edit['description'], $product_type
    ->getDescription());
  $this
    ->assertFalse($product_type
    ->allowsMultipleVariations());
  $this
    ->assertFalse($product_type
    ->shouldInjectVariationFields());

  // Confirm that the product display was updated.
  $form_display = commerce_get_entity_display('commerce_product', 'default', 'form');
  $component = $form_display
    ->getComponent('variations');
  $this
    ->assertNotEmpty($component);
  $this
    ->assertEquals('commerce_product_single_variation', $component['type']);

  // Re-enable multiple variations.
  $this
    ->drupalGet('admin/commerce/config/product-types/default/edit');
  $edit = [
    'multipleVariations' => TRUE,
  ];
  $this
    ->submitForm($edit, t('Save'));
  \Drupal::entityTypeManager()
    ->getStorage('commerce_product_type')
    ->resetCache();
  $product_type = ProductType::load('default');
  $this
    ->assertTrue($product_type
    ->allowsMultipleVariations());

  // Confirm that the product display was updated.
  $form_display = commerce_get_entity_display('commerce_product', 'default', 'form');
  $this
    ->assertEmpty($form_display
    ->getComponent('variations'));

  // Cannot change the variation type once a product has been created.
  $product = $this
    ->createEntity('commerce_product', [
    'type' => 'default',
    'title' => 'Test product',
  ]);
  $this
    ->drupalGet('admin/commerce/config/product-types/default/edit');
  $variation_type_field = $this
    ->getSession()
    ->getPage()
    ->findField('variationType');
  $this
    ->assertTrue($variation_type_field
    ->hasAttribute('disabled'));
}