88use Drupal \commerce_product \Entity \ProductVariation ;
99use Drupal \commerce_product \Entity \ProductVariationType ;
1010use Drupal \commerce_product \Entity \ProductVariationTypeInterface ;
11+ use Drupal \field \Entity \FieldConfig ;
1112use Drupal \Tests \commerce \Kernel \CommerceKernelTestBase ;
1213
1314/**
@@ -122,7 +123,7 @@ protected function setUp() {
122123 '1tb ' => '1TB ' ,
123124 '2tb ' => '2TB ' ,
124125 '3tb ' => '3TB ' ,
125- ]);
126+ ], FALSE );
126127 }
127128
128129 /**
@@ -288,7 +289,7 @@ public function testPrepareAttributesOptional() {
288289 $ this ->assertEquals ('disk2 ' , $ disk2_attribute ->getId ());
289290 $ this ->assertEquals ('Disk2 ' , $ disk2_attribute ->getLabel ());
290291 $ this ->assertEquals ('select ' , $ disk2_attribute ->getElementType ());
291- $ this ->assertTrue ($ disk2_attribute ->isRequired ());
292+ $ this ->assertFalse ($ disk2_attribute ->isRequired ());
292293 $ this ->assertEquals (['_none ' => '' ], $ disk2_attribute ->getValues ());
293294
294295 // Test from the 16GB x 1TB x None variation.
@@ -312,7 +313,7 @@ public function testPrepareAttributesOptional() {
312313 $ this ->assertEquals ('disk2 ' , $ disk2_attribute ->getId ());
313314 $ this ->assertEquals ('Disk2 ' , $ disk2_attribute ->getLabel ());
314315 $ this ->assertEquals ('select ' , $ disk2_attribute ->getElementType ());
315- $ this ->assertTrue ($ disk2_attribute ->isRequired ());
316+ $ this ->assertFalse ($ disk2_attribute ->isRequired ());
316317 $ this ->assertEquals (['_none ' => '' , '17 ' => '1TB ' ], $ disk2_attribute ->getValues ());
317318 }
318319
@@ -375,7 +376,7 @@ public function testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo() {
375376 $ this ->assertEquals ('disk2 ' , $ disk2_attribute ->getId ());
376377 $ this ->assertEquals ('Disk2 ' , $ disk2_attribute ->getLabel ());
377378 $ this ->assertEquals ('select ' , $ disk2_attribute ->getElementType ());
378- $ this ->assertTrue ($ disk2_attribute ->isRequired ());
379+ $ this ->assertFalse ($ disk2_attribute ->isRequired ());
379380 $ this ->assertEquals (['18 ' => '2TB ' ], $ disk2_attribute ->getValues ());
380381
381382 // Test 8GB x 1TB x 2TB.
@@ -401,7 +402,7 @@ public function testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo() {
401402 $ this ->assertEquals ('disk2 ' , $ disk2_attribute ->getId ());
402403 $ this ->assertEquals ('Disk2 ' , $ disk2_attribute ->getLabel ());
403404 $ this ->assertEquals ('select ' , $ disk2_attribute ->getElementType ());
404- $ this ->assertTrue ($ disk2_attribute ->isRequired ());
405+ $ this ->assertFalse ($ disk2_attribute ->isRequired ());
405406 // There should only be one Disk 2 option, since the other 8GB RAM option
406407 // has a Disk 1 value of 2TB.
407408 $ this ->assertEquals (['18 ' => '2TB ' ], $ disk2_attribute ->getValues ());
@@ -427,7 +428,7 @@ public function testMutuallyExclusiveAttributeMatrixTwoByTwoByTwo() {
427428 $ this ->assertEquals ('disk2 ' , $ disk2_attribute ->getId ());
428429 $ this ->assertEquals ('Disk2 ' , $ disk2_attribute ->getLabel ());
429430 $ this ->assertEquals ('select ' , $ disk2_attribute ->getElementType ());
430- $ this ->assertTrue ($ disk2_attribute ->isRequired ());
431+ $ this ->assertFalse ($ disk2_attribute ->isRequired ());
431432 // There should only be one Disk 2 option, since the other 8GB RAM option
432433 // has a Disk 1 value of 2TB.
433434 $ this ->assertEquals (['17 ' => '1TB ' ], $ disk2_attribute ->getValues ());
@@ -635,17 +636,25 @@ protected function generateThreeByTwoOptionalScenario() {
635636 * The attribute field name.
636637 * @param array $options
637638 * Associative array of key name values. [red => Red].
639+ * @param bool $required
640+ * Whether the created attribute should be required.
638641 *
639642 * @return \Drupal\commerce_product\Entity\ProductAttributeValueInterface[]
640643 * Array of attribute entities.
641644 */
642- protected function createAttributeSet (ProductVariationTypeInterface $ variation_type , $ name , array $ options ) {
645+ protected function createAttributeSet (ProductVariationTypeInterface $ variation_type , $ name , array $ options, $ required = TRUE ) {
643646 $ attribute = ProductAttribute::create ([
644647 'id ' => $ name ,
645648 'label ' => ucfirst ($ name ),
646649 ]);
647650 $ attribute ->save ();
648651 $ this ->attributeFieldManager ->createField ($ attribute , $ variation_type ->id ());
652+ // The field is always created as required by default.
653+ if (!$ required ) {
654+ $ field = FieldConfig::loadByName ('commerce_product_variation ' , $ variation_type ->id (), 'attribute_ ' . $ name );
655+ $ field ->setRequired (FALSE );
656+ $ field ->save ();
657+ }
649658
650659 $ attribute_set = [];
651660 foreach ($ options as $ key => $ value ) {
0 commit comments