|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | + |
| 7 | +use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory; |
| 8 | +use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory; |
| 9 | + |
| 10 | +\Magento\TestFramework\Helper\Bootstrap::getInstance()->reinitialize(); |
| 11 | + |
| 12 | +/** @var \Magento\TestFramework\ObjectManager $objectManager */ |
| 13 | +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 14 | + |
| 15 | +/** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */ |
| 16 | +$categoryLinkManagement = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class); |
| 17 | + |
| 18 | +$tierPrices = []; |
| 19 | +/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory */ |
| 20 | +$tierPriceFactory = $objectManager->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class); |
| 21 | +/** @var $tpExtensionAttributes */ |
| 22 | +$tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class); |
| 23 | +/** @var $productExtensionAttributes */ |
| 24 | +$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class); |
| 25 | + |
| 26 | +$adminWebsite = $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class)->get('admin'); |
| 27 | +$tierPriceExtensionAttributes1 = $tpExtensionAttributesFactory->create() |
| 28 | + ->setWebsiteId($adminWebsite->getId()); |
| 29 | +$productExtensionAttributesWebsiteIds = $productExtensionAttributesFactory->create( |
| 30 | + ['website_ids' => $adminWebsite->getId()] |
| 31 | +); |
| 32 | + |
| 33 | +$tierPrices[] = $tierPriceFactory->create( |
| 34 | + [ |
| 35 | + 'data' => [ |
| 36 | + 'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL, |
| 37 | + 'qty' => 2, |
| 38 | + 'value' => 8 |
| 39 | + ] |
| 40 | + ] |
| 41 | +)->setExtensionAttributes($tierPriceExtensionAttributes1); |
| 42 | + |
| 43 | +$tierPrices[] = $tierPriceFactory->create( |
| 44 | + [ |
| 45 | + 'data' => [ |
| 46 | + 'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL, |
| 47 | + 'qty' => 5, |
| 48 | + 'value' => 5 |
| 49 | + ] |
| 50 | + ] |
| 51 | +)->setExtensionAttributes($tierPriceExtensionAttributes1); |
| 52 | + |
| 53 | +$tierPrices[] = $tierPriceFactory->create( |
| 54 | + [ |
| 55 | + 'data' => [ |
| 56 | + 'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, |
| 57 | + 'qty' => 3, |
| 58 | + 'value' => 5 |
| 59 | + ] |
| 60 | + ] |
| 61 | +)->setExtensionAttributes($tierPriceExtensionAttributes1); |
| 62 | + |
| 63 | +$tierPrices[] = $tierPriceFactory->create( |
| 64 | + [ |
| 65 | + 'data' => [ |
| 66 | + 'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, |
| 67 | + 'qty' => 3.2, |
| 68 | + 'value' => 6, |
| 69 | + ] |
| 70 | + ] |
| 71 | +)->setExtensionAttributes($tierPriceExtensionAttributes1); |
| 72 | + |
| 73 | +$tierPriceExtensionAttributes2 = $tpExtensionAttributesFactory->create() |
| 74 | + ->setWebsiteId($adminWebsite->getId()) |
| 75 | + ->setPercentageValue(50); |
| 76 | + |
| 77 | +$tierPrices[] = $tierPriceFactory->create( |
| 78 | + [ |
| 79 | + 'data' => [ |
| 80 | + 'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, |
| 81 | + 'qty' => 10 |
| 82 | + ] |
| 83 | + ] |
| 84 | +)->setExtensionAttributes($tierPriceExtensionAttributes2); |
| 85 | + |
| 86 | +/** @var $product \Magento\Catalog\Model\Product */ |
| 87 | +$product = $objectManager->create(\Magento\Catalog\Model\Product::class); |
| 88 | +$product->isObjectNew(true); |
| 89 | +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) |
| 90 | + ->setId(1) |
| 91 | + ->setAttributeSetId(4) |
| 92 | + ->setWebsiteIds([1]) |
| 93 | + ->setName('Simple Product') |
| 94 | + ->setSku('simple') |
| 95 | + ->setPrice(10) |
| 96 | + ->setWeight(1) |
| 97 | + ->setShortDescription("Short description") |
| 98 | + ->setTaxClassId(0) |
| 99 | + ->setTierPrices($tierPrices) |
| 100 | + ->setDescription('Description with <b>html tag</b>') |
| 101 | + ->setExtensionAttributes($productExtensionAttributesWebsiteIds) |
| 102 | + ->setMetaTitle('meta title') |
| 103 | + ->setMetaKeyword('meta keyword') |
| 104 | + ->setMetaDescription('meta description') |
| 105 | + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) |
| 106 | + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_DISABLED) |
| 107 | + ->setStockData( |
| 108 | + [ |
| 109 | + 'use_config_manage_stock' => 1, |
| 110 | + 'qty' => 100, |
| 111 | + 'is_qty_decimal' => 0, |
| 112 | + 'is_in_stock' => 1, |
| 113 | + ] |
| 114 | + )->setCanSaveCustomOptions(true) |
| 115 | + ->setHasOptions(true); |
| 116 | + |
| 117 | +$oldOptions = [ |
| 118 | + [ |
| 119 | + 'previous_group' => 'text', |
| 120 | + 'title' => 'Test Field', |
| 121 | + 'type' => 'field', |
| 122 | + 'is_require' => 1, |
| 123 | + 'sort_order' => 0, |
| 124 | + 'price' => 1, |
| 125 | + 'price_type' => 'fixed', |
| 126 | + 'sku' => '1-text', |
| 127 | + 'max_characters' => 100, |
| 128 | + ], |
| 129 | + [ |
| 130 | + 'previous_group' => 'date', |
| 131 | + 'title' => 'Test Date and Time', |
| 132 | + 'type' => 'date_time', |
| 133 | + 'is_require' => 1, |
| 134 | + 'sort_order' => 0, |
| 135 | + 'price' => 2, |
| 136 | + 'price_type' => 'fixed', |
| 137 | + 'sku' => '2-date', |
| 138 | + ], |
| 139 | + [ |
| 140 | + 'previous_group' => 'select', |
| 141 | + 'title' => 'Test Select', |
| 142 | + 'type' => 'drop_down', |
| 143 | + 'is_require' => 1, |
| 144 | + 'sort_order' => 0, |
| 145 | + 'values' => [ |
| 146 | + [ |
| 147 | + 'option_type_id' => null, |
| 148 | + 'title' => 'Option 1', |
| 149 | + 'price' => 3, |
| 150 | + 'price_type' => 'fixed', |
| 151 | + 'sku' => '3-1-select', |
| 152 | + ], |
| 153 | + [ |
| 154 | + 'option_type_id' => null, |
| 155 | + 'title' => 'Option 2', |
| 156 | + 'price' => 3, |
| 157 | + 'price_type' => 'fixed', |
| 158 | + 'sku' => '3-2-select', |
| 159 | + ], |
| 160 | + ] |
| 161 | + ], |
| 162 | + [ |
| 163 | + 'previous_group' => 'select', |
| 164 | + 'title' => 'Test Radio', |
| 165 | + 'type' => 'radio', |
| 166 | + 'is_require' => 1, |
| 167 | + 'sort_order' => 0, |
| 168 | + 'values' => [ |
| 169 | + [ |
| 170 | + 'option_type_id' => null, |
| 171 | + 'title' => 'Option 1', |
| 172 | + 'price' => 3, |
| 173 | + 'price_type' => 'fixed', |
| 174 | + 'sku' => '4-1-radio', |
| 175 | + ], |
| 176 | + [ |
| 177 | + 'option_type_id' => null, |
| 178 | + 'title' => 'Option 2', |
| 179 | + 'price' => 3, |
| 180 | + 'price_type' => 'fixed', |
| 181 | + 'sku' => '4-2-radio', |
| 182 | + ], |
| 183 | + ] |
| 184 | + ] |
| 185 | +]; |
| 186 | + |
| 187 | +$options = []; |
| 188 | + |
| 189 | +/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory $customOptionFactory */ |
| 190 | +$customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class); |
| 191 | + |
| 192 | +foreach ($oldOptions as $option) { |
| 193 | + /** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $option */ |
| 194 | + $option = $customOptionFactory->create(['data' => $option]); |
| 195 | + $option->setProductSku($product->getSku()); |
| 196 | + |
| 197 | + $options[] = $option; |
| 198 | +} |
| 199 | + |
| 200 | +$product->setOptions($options); |
| 201 | + |
| 202 | +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ |
| 203 | +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); |
| 204 | +$productRepository->save($product); |
| 205 | + |
| 206 | +$categoryLinkManagement->assignProductToCategories( |
| 207 | + $product->getSku(), |
| 208 | + [2] |
| 209 | +); |
| 210 | + |
| 211 | +/** |
| 212 | + * We need to remember that automatic reindexation is not working properly in integration tests |
| 213 | + * Reindexation is sitting on top of afterCommit callbacks: |
| 214 | + * \Magento\Catalog\Model\Product::priceReindexCallback |
| 215 | + * |
| 216 | + * However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however |
| 217 | + * integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests |
| 218 | + */ |
| 219 | +/** @var \Magento\Indexer\Model\Indexer $indexer */ |
| 220 | +$indexer = $objectManager->create(\Magento\Indexer\Model\Indexer::class); |
| 221 | +$indexer->load('catalog_product_price'); |
| 222 | +$indexer->reindexRow($product->getId()); |
0 commit comments