|
2 | 2 |
|
3 | 3 | namespace Drupal\Tests\commerce_product\Functional; |
4 | 4 |
|
| 5 | +use Drupal\commerce_order\Entity\OrderItemType; |
5 | 6 | use Drupal\commerce_product\Entity\ProductType; |
| 7 | +use Drupal\commerce_product\Entity\ProductVariationType; |
6 | 8 |
|
7 | 9 | /** |
8 | 10 | * Ensure the product type works correctly. |
@@ -54,10 +56,84 @@ public function testProductTypeCreation() { |
54 | 56 | ]; |
55 | 57 | $this->submitForm($edit, t('Save')); |
56 | 58 | $product_type = ProductType::load($edit['id']); |
57 | | - $this->assertNotEmpty(!empty($product_type), 'The new product type has been created.'); |
58 | | - $this->assertEquals($product_type->label(), $edit['label'], 'The new product type has the correct label.'); |
59 | | - $this->assertEquals($product_type->getDescription(), $edit['description'], 'The new product type has the correct label.'); |
60 | | - $this->assertEquals($product_type->getVariationTypeId(), $edit['variationType'], 'The new product type has the correct associated variation type.'); |
| 59 | + $this->assertNotEmpty($product_type); |
| 60 | + $this->assertEquals($product_type->label(), $edit['label']); |
| 61 | + $this->assertEquals($product_type->getDescription(), $edit['description']); |
| 62 | + $this->assertEquals($product_type->getVariationTypeId(), $edit['variationType']); |
| 63 | + |
| 64 | + // Automatic variation type creation option. |
| 65 | + $this->drupalGet('admin/commerce/config/product-types/add'); |
| 66 | + $edit = [ |
| 67 | + 'id' => strtolower($this->randomMachineName(8)), |
| 68 | + 'label' => $this->randomMachineName(), |
| 69 | + 'description' => 'My even more random product type', |
| 70 | + 'variationType' => '', |
| 71 | + ]; |
| 72 | + $this->submitForm($edit, t('Save')); |
| 73 | + $product_type = ProductType::load($edit['id']); |
| 74 | + $this->assertNotEmpty($product_type); |
| 75 | + $this->assertEquals($product_type->label(), $edit['label']); |
| 76 | + $this->assertEquals($product_type->getDescription(), $edit['description']); |
| 77 | + $this->assertEquals($product_type->getVariationTypeId(), $edit['id']); |
| 78 | + $product_variation_type = ProductVariationType::load($edit['id']); |
| 79 | + $this->assertNotEmpty($product_variation_type); |
| 80 | + $this->assertEquals($product_variation_type->label(), $edit['label']); |
| 81 | + $this->assertEquals($product_variation_type->getOrderItemTypeId(), 'default'); |
| 82 | + |
| 83 | + // Confirm that a conflicting product variation type ID is detected. |
| 84 | + $product_type_id = $product_type->id(); |
| 85 | + $product_type->delete(); |
| 86 | + $this->drupalGet('admin/commerce/config/product-types/add'); |
| 87 | + $edit = [ |
| 88 | + 'id' => $product_type_id, |
| 89 | + 'label' => $this->randomMachineName(), |
| 90 | + 'description' => 'My even more random product type', |
| 91 | + 'variationType' => '', |
| 92 | + ]; |
| 93 | + $this->submitForm($edit, t('Save')); |
| 94 | + $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])); |
| 95 | + |
| 96 | + // Confirm that the form can't be submitted with no order item types. |
| 97 | + $default_order_item_type = OrderItemType::load('default'); |
| 98 | + $this->assertNotEmpty(!empty($default_order_item_type), 'The default order item type has been loaded.'); |
| 99 | + $default_order_item_type->delete(); |
| 100 | + |
| 101 | + $this->drupalGet('admin/commerce/config/product-types/add'); |
| 102 | + $edit = [ |
| 103 | + 'id' => strtolower($this->randomMachineName(8)), |
| 104 | + 'label' => $this->randomMachineName(), |
| 105 | + 'description' => 'Another random product type', |
| 106 | + 'variationType' => '', |
| 107 | + ]; |
| 108 | + $this->submitForm($edit, t('Save')); |
| 109 | + $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.')); |
| 110 | + |
| 111 | + // Confirm that a non-default order item type can be selected. |
| 112 | + $default_order_item_type->delete(); |
| 113 | + OrderItemType::create([ |
| 114 | + 'id' => 'test', |
| 115 | + 'label' => 'Test', |
| 116 | + 'orderType' => 'default', |
| 117 | + 'purchasableEntityType' => 'commerce_product_variation', |
| 118 | + ])->save(); |
| 119 | + |
| 120 | + $this->drupalGet('admin/commerce/config/product-types/add'); |
| 121 | + $edit = [ |
| 122 | + 'id' => strtolower($this->randomMachineName(8)), |
| 123 | + 'label' => $this->randomMachineName(), |
| 124 | + 'description' => 'My even more random product type', |
| 125 | + 'variationType' => '', |
| 126 | + ]; |
| 127 | + $this->submitForm($edit, t('Save')); |
| 128 | + $product_type = ProductType::load($edit['id']); |
| 129 | + $this->assertNotEmpty($product_type); |
| 130 | + $this->assertEquals($product_type->label(), $edit['label']); |
| 131 | + $this->assertEquals($product_type->getDescription(), $edit['description']); |
| 132 | + $this->assertEquals($product_type->getVariationTypeId(), $edit['id']); |
| 133 | + $product_variation_type = ProductVariationType::load($edit['id']); |
| 134 | + $this->assertNotEmpty($product_variation_type); |
| 135 | + $this->assertEquals($product_variation_type->label(), $edit['label']); |
| 136 | + $this->assertEquals($product_variation_type->getOrderItemTypeId(), 'test'); |
61 | 137 | } |
62 | 138 |
|
63 | 139 | /** |
|
0 commit comments