You are here

public function ProductTypeTest::testAdd in Commerce Core 8.2

Tests adding a product type.

File

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

Class

ProductTypeTest
Tests the product type UI.

Namespace

Drupal\Tests\commerce_product\Functional

Code

public function testAdd() {
  $user = $this
    ->drupalCreateUser([
    'administer commerce_product_type',
  ]);
  $this
    ->drupalLogin($user);
  $this
    ->drupalGet('admin/commerce/config/product-types/add');
  $variation_type_field = $this
    ->getSession()
    ->getPage()
    ->findField('variationType');
  $this
    ->assertFalse($variation_type_field
    ->hasAttribute('disabled'));
  $edit = [
    'id' => 'foo',
    'label' => 'Foo',
    'description' => 'My even more random product type',
    'variationType' => 'default',
  ];
  $this
    ->submitForm($edit, t('Save'));
  $this
    ->assertSession()
    ->pageTextContains('The product type Foo has been successfully saved.');
  $product_type = ProductType::load($edit['id']);
  $this
    ->assertNotEmpty($product_type);
  $this
    ->assertEquals($edit['label'], $product_type
    ->label());
  $this
    ->assertEquals($edit['description'], $product_type
    ->getDescription());
  $this
    ->assertEquals($edit['variationType'], $product_type
    ->getVariationTypeId());
  $this
    ->assertTrue($product_type
    ->allowsMultipleVariations());
  $this
    ->assertTrue($product_type
    ->shouldInjectVariationFields());
  $form_display = commerce_get_entity_display('commerce_product', $edit['id'], 'form');
  $this
    ->assertEmpty($form_display
    ->getComponent('variations'));

  // Automatic variation type creation option, single variation mode.
  $this
    ->drupalGet('admin/commerce/config/product-types/add');
  $edit = [
    'id' => 'foo2',
    'label' => 'Foo2',
    'description' => 'My even more random product type',
    'variationType' => '',
    'multipleVariations' => FALSE,
    'injectVariationFields' => FALSE,
  ];
  $this
    ->submitForm($edit, t('Save'));
  $product_type = ProductType::load($edit['id']);
  $this
    ->assertNotEmpty($product_type);
  $this
    ->assertEquals($edit['label'], $product_type
    ->label());
  $this
    ->assertEquals($edit['description'], $product_type
    ->getDescription());
  $this
    ->assertEquals($edit['id'], $product_type
    ->getVariationTypeId());
  $variation_type = ProductVariationType::load($edit['id']);
  $this
    ->assertNotEmpty($variation_type);
  $this
    ->assertEquals($variation_type
    ->label(), $edit['label']);
  $this
    ->assertEquals($variation_type
    ->getOrderItemTypeId(), 'default');
  $this
    ->assertFalse($product_type
    ->allowsMultipleVariations());
  $this
    ->assertFalse($product_type
    ->shouldInjectVariationFields());
  $form_display = commerce_get_entity_display('commerce_product', $edit['id'], 'form');
  $component = $form_display
    ->getComponent('variations');
  $this
    ->assertNotEmpty($component);
  $this
    ->assertEquals('commerce_product_single_variation', $component['type']);

  // Confirm that a conflicting product variation type ID is detected.
  $product_type_id = $product_type
    ->id();
  $product_type
    ->delete();
  $this
    ->drupalGet('admin/commerce/config/product-types/add');
  $edit = [
    'id' => $product_type_id,
    'label' => $this
      ->randomMachineName(),
    'description' => 'My even more random product type',
    'variationType' => '',
  ];
  $this
    ->submitForm($edit, t('Save'));
  $this
    ->assertSession()
    ->pageTextContains(t('A product variation type with the machine name @name already exists. Select an existing product variation type or change the machine name for this product type.', [
    '@name' => $product_type_id,
  ]));

  // Confirm that the form can't be submitted with no order item types.
  $default_order_item_type = OrderItemType::load('default');
  $this
    ->assertNotEmpty($default_order_item_type);
  $default_order_item_type
    ->delete();
  $this
    ->drupalGet('admin/commerce/config/product-types/add');
  $edit = [
    'id' => 'foo3',
    'label' => 'Foo3',
    'description' => 'Another random product type',
    'variationType' => '',
  ];
  $this
    ->submitForm($edit, t('Save'));
  $this
    ->assertSession()
    ->pageTextContains(t('A new product variation type cannot be created, because no order item types were found. Select an existing product variation type or retry after creating a new order item type.'));

  // Confirm that a non-default order item type can be selected.
  $default_order_item_type
    ->delete();
  OrderItemType::create([
    'id' => 'test',
    'label' => 'Test',
    'orderType' => 'default',
    'purchasableEntityType' => 'commerce_product_variation',
  ])
    ->save();
  $this
    ->drupalGet('admin/commerce/config/product-types/add');
  $edit = [
    'id' => 'foo4',
    'label' => 'Foo4',
    'description' => 'My even more random product type',
    'variationType' => '',
  ];
  $this
    ->submitForm($edit, t('Save'));
  $product_type = ProductType::load($edit['id']);
  $this
    ->assertNotEmpty($product_type);
  $this
    ->assertEquals($edit['label'], $product_type
    ->label());
  $this
    ->assertEquals($edit['description'], $product_type
    ->getDescription());
  $this
    ->assertEquals($edit['id'], $product_type
    ->getVariationTypeId());
  $variation_type = ProductVariationType::load($edit['id']);
  $this
    ->assertNotEmpty($variation_type);
  $this
    ->assertEquals($edit['label'], $variation_type
    ->label());
  $this
    ->assertEquals('test', $variation_type
    ->getOrderItemTypeId());
}