/**
  * Test the variation type setting.
  */
 public function testTitleGenerationSetting()
 {
     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $field_definitions */
     $this->assertFalse($this->variationType->shouldGenerateTitle());
     $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('commerce_product_variation', $this->variationType->id());
     $this->assertTrue($field_definitions['title']->isRequired());
     // Enable generation.
     $this->variationType->setGenerateTitle(TRUE);
     $this->variationType->save();
     /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
     $variation_type = ProductVariationType::load($this->variationType->id());
     $this->assertTrue($variation_type->shouldGenerateTitle());
     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $field_definitions */
     $field_definitions = \Drupal::service('entity_field.manager')->getFieldDefinitions('commerce_product_variation', $this->variationType->id());
     $this->assertFalse($field_definitions['title']->isRequired());
 }
 /**
  * Tests deleting a product variation type via a form.
  */
 public function testProductVariationTypeDeletion()
 {
     $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()]);
     // @todo Make sure $variation_type->delete() also does nothing if there's
     // a variation of that type. Right now the check is done on the form level.
     $this->drupalGet('admin/commerce/config/product-variation-types/' . $variation_type->id() . '/delete');
     $this->assertRaw(t('%type is used by 1 product variation on your site. You can not remove this product variation type until you have removed all of the %type product variations.', ['%type' => $variation_type->label()]), 'The product variation type will not be deleted until all variations of that type are deleted.');
     $this->assertNoText(t('This action cannot be undone.'), 'The product variation type deletion confirmation form is not available');
     $variation->delete();
     $this->drupalGet('admin/commerce/config/product-variation-types/' . $variation_type->id() . '/delete');
     $this->assertRaw(t('Are you sure you want to delete the product variation type %type?', ['%type' => $variation_type->label()]), 'The product variation type is available for deletion');
     $this->assertText(t('This action cannot be undone.'), 'The product variation type deletion confirmation form is available');
     $this->drupalPostForm(NULL, NULL, t('Delete'));
     $exists = (bool) ProductVariationType::load($variation_type->id());
     $this->assertFalse($exists, 'The new product variation type has been deleted from the database.');
 }
 /**
  * {@inheritdoc}
  */
 public static function bundleFieldDefinitions(EntityTypeInterface $entity_type, $bundle, array $base_field_definitions)
 {
     /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
     $variation_type = ProductVariationType::load($bundle);
     /** @var \Drupal\Core\Field\BaseFieldDefinition[] $fields */
     $fields = [];
     $fields['title'] = clone $base_field_definitions['title'];
     if ($variation_type->shouldGenerateTitle()) {
         // The title is always generated, the field needs to be hidden.
         // The widget is hidden in commerce_product_field_widget_form_alter()
         // since setDisplayOptions() can't affect existing form displays.
         $fields['title']->setRequired(FALSE);
         $fields['title']->setDisplayConfigurable('form', FALSE);
     }
     return $fields;
 }
 /**
  * Tests adding a product to the cart when there are multiple variations.
  */
 public function testMultipleVariationsMultipleAttributes()
 {
     /** @var \Drupal\commerce_product\Entity\ProductVariationTypeInterface $variation_type */
     $variation_type = ProductVariationType::load($this->variation->bundle());
     $this->createAttributeField($variation_type, 'test_size_attribute');
     /** @var \Drupal\taxonomy\TermInterface[] $size_attributes */
     $size_attributes = ['small' => $this->createAttributeOption('test_color_attribute', 'Small'), 'medium' => $this->createAttributeOption('test_color_attribute', 'Medium')];
     $this->createAttributeField($variation_type, 'test_color_attribute');
     /** @var \Drupal\taxonomy\TermInterface[] $color_attributes */
     $color_attributes = ['red' => $this->createAttributeOption('test_color_attribute', 'Red'), 'blue' => $this->createAttributeOption('test_color_attribute', 'Blue')];
     // Reload the variation since we have a new fields.
     $this->variation = ProductVariation::load($this->variation->id());
     // Get the product so we can append new variations
     $product = $this->variation->getProduct();
     /**
      * +--------------------+------------------+
      * | Variation          | Attributes       |
      * +--------------------+------------------+
      * | 1                  | Small, Red       |
      * | 2                  | Medium, Red      |
      * | 3                  | Medium, Blue     |
      * +--------------------+------------------+
      */
     // Update first variation to have the attribute's value.
     $this->variation->set('test_size_attribute', $size_attributes['small']->id());
     $this->variation->set('test_color_attribute', $color_attributes['red']->id());
     $this->variation->save();
     $variation2 = $this->createEntity('commerce_product_variation', ['type' => $variation_type->id(), 'sku' => $this->randomMachineName(), 'price' => ['amount' => 999, 'currency_code' => 'USD'], 'test_size_attribute' => $size_attributes['medium']->id(), 'test_color_attribute' => $color_attributes['red']->id()]);
     $variation2->save();
     $variation3 = $this->createEntity('commerce_product_variation', ['type' => $variation_type->id(), 'sku' => $this->randomMachineName(), 'price' => ['amount' => 999, 'currency_code' => 'USD'], 'test_size_attribute' => $size_attributes['medium']->id(), 'test_color_attribute' => $color_attributes['blue']->id()]);
     $variation3->save();
     $product->variations->appendItem($variation2);
     $product->variations->appendItem($variation3);
     $product->save();
     $this->postAddToCart($this->variation->getProduct());
     // Trigger AJAX by changing size attribute
     $this->drupalPostAjaxForm(NULL, ['purchased_entity[0][attributes][test_size_attribute]' => $size_attributes['medium']->id()], 'purchased_entity[0][attributes][test_size_attribute]');
     // Trigger AJAX by changing color attribute
     $this->drupalPostAjaxForm(NULL, ['purchased_entity[0][attributes][test_color_attribute]' => $color_attributes['blue']->id()], 'purchased_entity[0][attributes][test_color_attribute]');
     // We can't assert an option doesn't exist using AssertContentTrait, since
     // our ID is dynamic. Version of assertNoOption using data-drupal-selector.
     // @see \Drupal\simpletest\AssertContentTrait::assertNoOption
     $selects = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]', [':data_drupal_selector' => 'edit-purchased-entity-0-attributes-test-size-attribute']);
     $options = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector]//option[@value=:option]', [':data_drupal_selector' => 'edit-purchased-entity-0-attributes-test-size-attribute', ':option' => $size_attributes['small']->id()]);
     $this->assertTrue(isset($selects[0]) && !isset($options[0]), NULL, 'Browser');
     $selects = $this->xpath('//select[@data-drupal-selector=:data_drupal_selector and @disabled]', [':data_drupal_selector' => 'edit-purchased-entity-0-attributes-test-size-attribute']);
     $this->assertTrue(isset($selects[0]));
     // Since we do not have a Small, Blue. Should only see variation 3.
     $this->postAddToCart($product, ['purchased_entity[0][attributes][test_color_attribute]' => $color_attributes['blue']->id(), 'purchased_entity[0][attributes][test_size_attribute]' => $size_attributes['medium']->id()]);
     // Check if the quantity was increased for the existing line item.
     $this->cart = Order::load($this->cart->id());
     $line_items = $this->cart->getLineItems();
     /** @var \Drupal\commerce_order\Entity\LineItemInterface $line_item */
     $line_item = $line_items[0];
     $this->assertEqual($line_item->getTitle(), $this->variation->getLineItemTitle());
     $this->assertTrue($line_item->getQuantity() == 1, t('The product @product has been added to cart.', ['@product' => $line_item->getTitle()]));
     $line_item = $line_items[1];
     $this->assertEqual($line_item->getTitle(), $variation3->getLineItemTitle());
     $this->assertTrue($line_item->getQuantity() == 1, t('The product @product has been added to cart.', ['@product' => $line_item->getTitle()]));
 }