From ad1f1bf338b495997d048a354e908aa0f3fcf5bd Mon Sep 17 00:00:00 2001 From: Alexandr Voronoy Date: Sat, 24 Nov 2018 19:59:01 +0200 Subject: [PATCH 1/3] Api-functional test with fixtures and integration --- .../GraphQl/SendFriend/SendFriendTest.php | 469 ++++++++++++++++++ .../Fixtures/process_config_data.php | 22 + .../Fixtures/sendfriend_configuration.php | 24 + .../sendfriend_configuration_rollback.php | 22 + .../SendFriend/_files/product_simple.php | 212 ++++++++ .../_files/product_simple_rollback.php | 26 + 6 files changed, 775 insertions(+) create mode 100644 dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php create mode 100644 dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/process_config_data.php create mode 100644 dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration.php create mode 100644 dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration_rollback.php create mode 100644 dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php create mode 100644 dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple_rollback.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php new file mode 100644 index 000000000000..57e61895e6d1 --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php @@ -0,0 +1,469 @@ +dataObjectFactory = Bootstrap::getObjectManager()->get(DataObjectFactory::class); + $this->sendFriendFactory = Bootstrap::getObjectManager()->get(SendFriendFactory::class); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + */ + public function testSendFriend() + { + $query = + <<graphQlQuery($query); + self::assertEquals('Name', $response['sendEmailToFriend']['sender']['name']); + self::assertEquals('e@mail.com', $response['sendEmailToFriend']['sender']['email']); + self::assertEquals('Lorem Ipsum', $response['sendEmailToFriend']['sender']['message']); + self::assertEquals('Recipient Name 1', $response['sendEmailToFriend']['recipients'][0]['name']); + self::assertEquals('recipient1@mail.com', $response['sendEmailToFriend']['recipients'][0]['email']); + self::assertEquals('Recipient Name 2', $response['sendEmailToFriend']['recipients'][1]['name']); + self::assertEquals('recipient2@mail.com', $response['sendEmailToFriend']['recipients'][1]['email']); + } + + public function testSendWithoutExistProduct() + { + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage('The product that was requested doesn\'t exist. Verify the product and try again.'); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + */ + public function testMaxSendEmailToFriend() + { + /** @var SendFriend $sendFriend */ + $sendFriend = $this->sendFriendFactory->create(); + + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage("No more than {$sendFriend->getMaxRecipients()} emails can be sent at a time."); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + */ + public function testSendWithoutRecipentsName() + { + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage('Please provide Name for all of recipients.'); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + */ + public function testSendWithoutRecipentsEmail() + { + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage('Please provide Email for all of recipients.'); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + */ + public function testSendWithoutSenderName() + { + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage('Please provide Name of sender.'); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + */ + public function testSendWithoutSenderEmail() + { + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage('Please provide Email of sender.'); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + */ + public function testSendWithoutSenderMessage() + { + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage('Please provide Message.'); + $this->graphQlQuery($query); + } + + /** + * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + * @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php + */ + public function testLimitMessagesPerHour() + { + + /** @var SendFriend $sendFriend */ + $sendFriend = $this->sendFriendFactory->create(); + + $query = + <<expectException(\Exception::class); + $this->expectExceptionMessage("You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour."); + + for ($i = 0; $i <= $sendFriend->getMaxSendsToFriend() + 1; $i++) { + $this->graphQlQuery($query); + } + } +} diff --git a/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/process_config_data.php b/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/process_config_data.php new file mode 100644 index 000000000000..2c672378fb83 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/process_config_data.php @@ -0,0 +1,22 @@ + $value) { + $config->setDataByPath($key, $value); + $config->save(); + } +}; + +$deleteConfigData = function (WriterInterface $writer, array $configData, string $scope, int $scopeId) { + foreach ($configData as $path) { + $writer->delete($path, $scope, $scopeId); + } +}; diff --git a/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration.php b/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration.php new file mode 100644 index 000000000000..229d6eddb496 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration.php @@ -0,0 +1,24 @@ + 1, + 'sendfriend/email/check_by' => 1, + +]; +$objectManager = Bootstrap::getObjectManager(); +$defConfig = $objectManager->create(Config::class); +$defConfig->setScope(ScopeConfigInterface::SCOPE_TYPE_DEFAULT); +$processConfigData($defConfig, $configData); diff --git a/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration_rollback.php b/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration_rollback.php new file mode 100644 index 000000000000..9265e032bbc4 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/SendFriend/Fixtures/sendfriend_configuration_rollback.php @@ -0,0 +1,22 @@ +get(WriterInterface::class); +$deleteConfigData($configWriter, $configData, ScopeConfigInterface::SCOPE_TYPE_DEFAULT, 0); diff --git a/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php b/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php new file mode 100644 index 000000000000..c27599600081 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php @@ -0,0 +1,212 @@ +reinitialize(); + +/** @var \Magento\TestFramework\ObjectManager $objectManager */ +$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + +/** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */ +$categoryLinkManagement = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class); + +$tierPrices = []; +/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory */ +$tierPriceFactory = $objectManager->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class); +/** @var $tpExtensionAttributes */ +$tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class); +/** @var $productExtensionAttributes */ +$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class); + +$adminWebsite = $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class)->get('admin'); +$tierPriceExtensionAttributes1 = $tpExtensionAttributesFactory->create() + ->setWebsiteId($adminWebsite->getId()); +$productExtensionAttributesWebsiteIds = $productExtensionAttributesFactory->create( + ['website_ids' => $adminWebsite->getId()] +); + +$tierPrices[] = $tierPriceFactory->create( + [ + 'data' => [ + 'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL, + 'qty' => 2, + 'value' => 8 + ] + ] +)->setExtensionAttributes($tierPriceExtensionAttributes1); + +$tierPrices[] = $tierPriceFactory->create( + [ + 'data' => [ + 'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL, + 'qty' => 5, + 'value' => 5 + ] + ] +)->setExtensionAttributes($tierPriceExtensionAttributes1); + +$tierPrices[] = $tierPriceFactory->create( + [ + 'data' => [ + 'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, + 'qty' => 3, + 'value' => 5 + ] + ] +)->setExtensionAttributes($tierPriceExtensionAttributes1); + +$tierPriceExtensionAttributes2 = $tpExtensionAttributesFactory->create() + ->setWebsiteId($adminWebsite->getId()) + ->setPercentageValue(50); + +$tierPrices[] = $tierPriceFactory->create( + [ + 'data' => [ + 'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, + 'qty' => 10 + ] + ] +)->setExtensionAttributes($tierPriceExtensionAttributes2); + +/** @var $product \Magento\Catalog\Model\Product */ +$product = $objectManager->create(\Magento\Catalog\Model\Product::class); +$product->isObjectNew(true); +$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) + ->setId(1) + ->setAttributeSetId(4) + ->setWebsiteIds([1]) + ->setName('Simple Product') + ->setSku('simple') + ->setPrice(10) + ->setWeight(1) + ->setShortDescription("Short description") + ->setTaxClassId(0) + ->setTierPrices($tierPrices) + ->setDescription('Description with html tag') + ->setExtensionAttributes($productExtensionAttributesWebsiteIds) + ->setMetaTitle('meta title') + ->setMetaKeyword('meta keyword') + ->setMetaDescription('meta description') + ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) + ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) + ->setStockData( + [ + 'use_config_manage_stock' => 1, + 'qty' => 100, + 'is_qty_decimal' => 0, + 'is_in_stock' => 1, + ] + )->setCanSaveCustomOptions(true) + ->setHasOptions(true); + +$oldOptions = [ + [ + 'previous_group' => 'text', + 'title' => 'Test Field', + 'type' => 'field', + 'is_require' => 1, + 'sort_order' => 0, + 'price' => 1, + 'price_type' => 'fixed', + 'sku' => '1-text', + 'max_characters' => 100, + ], + [ + 'previous_group' => 'date', + 'title' => 'Test Date and Time', + 'type' => 'date_time', + 'is_require' => 1, + 'sort_order' => 0, + 'price' => 2, + 'price_type' => 'fixed', + 'sku' => '2-date', + ], + [ + 'previous_group' => 'select', + 'title' => 'Test Select', + 'type' => 'drop_down', + 'is_require' => 1, + 'sort_order' => 0, + 'values' => [ + [ + 'option_type_id' => null, + 'title' => 'Option 1', + 'price' => 3, + 'price_type' => 'fixed', + 'sku' => '3-1-select', + ], + [ + 'option_type_id' => null, + 'title' => 'Option 2', + 'price' => 3, + 'price_type' => 'fixed', + 'sku' => '3-2-select', + ], + ] + ], + [ + 'previous_group' => 'select', + 'title' => 'Test Radio', + 'type' => 'radio', + 'is_require' => 1, + 'sort_order' => 0, + 'values' => [ + [ + 'option_type_id' => null, + 'title' => 'Option 1', + 'price' => 3, + 'price_type' => 'fixed', + 'sku' => '4-1-radio', + ], + [ + 'option_type_id' => null, + 'title' => 'Option 2', + 'price' => 3, + 'price_type' => 'fixed', + 'sku' => '4-2-radio', + ], + ] + ] +]; + +$options = []; + +/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory $customOptionFactory */ +$customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class); + +foreach ($oldOptions as $option) { + /** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $option */ + $option = $customOptionFactory->create(['data' => $option]); + $option->setProductSku($product->getSku()); + + $options[] = $option; +} + +$product->setOptions($options); + +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); +$productRepository->save($product); + +$categoryLinkManagement->assignProductToCategories( + $product->getSku(), + [2] +); +///** +// * @var $configWriter \Magento\Framework\App\Config\Storage\WriterInterface +// */ +//$configWriter = $objectManager->get(\Magento\Framework\App\Config\Storage\WriterInterface::class); +///** +// * @var $cacheManager \Magento\Framework\App\Cache\Manager +// */ +//$cacheManager = $objectManager->get(\Magento\Framework\App\Cache\Manager::class); +//$configWriter->save('sendfriend/email/max_per_hour', 1); +//$configWriter->save('sendfriend/email/check_by', 1); +//$types = $cacheManager->getAvailableTypes(); +//$cacheManager->clean($types); + diff --git a/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple_rollback.php b/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple_rollback.php new file mode 100644 index 000000000000..ed98732fc870 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple_rollback.php @@ -0,0 +1,26 @@ +get(\Magento\Framework\Registry::class); + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', true); + +/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ +$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); + +try { + $product = $productRepository->get('simple', false, null, true); + $productRepository->delete($product); +} catch (\Magento\Framework\Exception\NoSuchEntityException $exception) { + //Product already removed +} + +$registry->unregister('isSecureArea'); +$registry->register('isSecureArea', false); From c3845db335eea03a2d234042910e6e0d106a21f5 Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Wed, 6 Feb 2019 17:55:00 +0200 Subject: [PATCH 2/3] graphQl-263: removed unused param and static fixes --- .../GraphQl/SendFriend/SendFriendTest.php | 17 ++++++++--------- 1 file changed, 8 insertions(+), 9 deletions(-) diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php index 57e61895e6d1..5d84b5846fce 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php @@ -20,14 +20,9 @@ class SendFriendTest extends GraphQlAbstract * @var SendFriendFactory */ private $sendFriendFactory; - /** - * @var DataObjectFactory - */ - private $dataObjectFactory; protected function setUp() { - $this->dataObjectFactory = Bootstrap::getObjectManager()->get(DataObjectFactory::class); $this->sendFriendFactory = Bootstrap::getObjectManager()->get(SendFriendFactory::class); } @@ -120,7 +115,9 @@ public function testSendWithoutExistProduct() } QUERY; $this->expectException(\Exception::class); - $this->expectExceptionMessage('The product that was requested doesn\'t exist. Verify the product and try again.'); + $this->expectExceptionMessage( + 'The product that was requested doesn\'t exist. Verify the product and try again.' + ); $this->graphQlQuery($query); } @@ -191,7 +188,7 @@ public function testMaxSendEmailToFriend() /** * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php */ - public function testSendWithoutRecipentsName() + public function testSendWithoutRecipientName() { $query = <<expectException(\Exception::class); - $this->expectExceptionMessage("You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour."); + $this->expectExceptionMessage( + "You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour." + ); for ($i = 0; $i <= $sendFriend->getMaxSendsToFriend() + 1; $i++) { $this->graphQlQuery($query); From fdaa3ada5456a1c88d3d99f29008bc3100b1358f Mon Sep 17 00:00:00 2001 From: Vitaliy Boyko Date: Wed, 6 Feb 2019 22:18:31 +0200 Subject: [PATCH 3/3] graphQl-167: removed redundant fixture, optimised tests --- .../GraphQl/SendFriend/SendFriendTest.php | 245 +++++------------- .../SendFriend/_files/product_simple.php | 212 --------------- 2 files changed, 67 insertions(+), 390 deletions(-) delete mode 100644 dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php diff --git a/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php index 5d84b5846fce..05e3e608c5e5 100644 --- a/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php +++ b/dev/tests/api-functional/testsuite/Magento/GraphQl/SendFriend/SendFriendTest.php @@ -7,7 +7,6 @@ namespace Magento\GraphQl\SendFriend; -use Magento\Framework\DataObjectFactory; use Magento\SendFriend\Model\SendFriend; use Magento\SendFriend\Model\SendFriendFactory; use Magento\TestFramework\Helper\Bootstrap; @@ -27,7 +26,7 @@ protected function setUp() } /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php */ public function testSendFriend() { @@ -122,7 +121,7 @@ public function testSendWithoutExistProduct() } /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php */ public function testMaxSendEmailToFriend() { @@ -186,31 +185,19 @@ public function testMaxSendEmailToFriend() } /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * @dataProvider sendFriendsErrorsDataProvider + * @param string $input + * @param string $errorMessage */ - public function testSendWithoutRecipientName() + public function testErrors(string $input, string $errorMessage) { $query = <<expectException(\Exception::class); - $this->expectExceptionMessage('Please provide Name for all of recipients.'); + $this->expectExceptionMessage($errorMessage); $this->graphQlQuery($query); } /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php + * TODO: use magentoApiConfigFixture (to be merged https://github.com/magento/graphql-ce/pull/351) + * @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php */ - public function testSendWithoutRecipientEmail() + public function testLimitMessagesPerHour() { + + /** @var SendFriend $sendFriend */ + $sendFriend = $this->sendFriendFactory->create(); + $query = <<expectException(\Exception::class); - $this->expectExceptionMessage('Please provide Email for all of recipients.'); - $this->graphQlQuery($query); + $this->expectExceptionMessage( + "You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour." + ); + + for ($i = 0; $i <= $sendFriend->getMaxSendsToFriend() + 1; $i++) { + $this->graphQlQuery($query); + } } /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php + * @return array */ - public function testSendWithoutSenderName() + public function sendFriendsErrorsDataProvider() { - $query = - <<expectException(\Exception::class); - $this->expectExceptionMessage('Please provide Name of sender.'); - $this->graphQlQuery($query); - } - - /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php - */ - public function testSendWithoutSenderEmail() - { - $query = - <<expectException(\Exception::class); - $this->expectExceptionMessage('Please provide Email of sender.'); - $this->graphQlQuery($query); - } - - /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php - */ - public function testSendWithoutSenderMessage() - { - $query = - <<expectException(\Exception::class); - $this->expectExceptionMessage('Please provide Message.'); - $this->graphQlQuery($query); - } - - /** - * @magentoApiDataFixture Magento/SendFriend/_files/product_simple.php - * @magentoApiDataFixture Magento/SendFriend/Fixtures/sendfriend_configuration.php - */ - public function testLimitMessagesPerHour() - { - - /** @var SendFriend $sendFriend */ - $sendFriend = $this->sendFriendFactory->create(); - - $query = - <<expectException(\Exception::class); - $this->expectExceptionMessage( - "You can't send messages more than {$sendFriend->getMaxSendsToFriend()} times an hour." - ); - - for ($i = 0; $i <= $sendFriend->getMaxSendsToFriend() + 1; $i++) { - $this->graphQlQuery($query); - } + } + ]', 'Please provide Message.' + ] + ]; } } diff --git a/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php b/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php deleted file mode 100644 index c27599600081..000000000000 --- a/dev/tests/integration/testsuite/Magento/SendFriend/_files/product_simple.php +++ /dev/null @@ -1,212 +0,0 @@ -reinitialize(); - -/** @var \Magento\TestFramework\ObjectManager $objectManager */ -$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); - -/** @var \Magento\Catalog\Api\CategoryLinkManagementInterface $categoryLinkManagement */ -$categoryLinkManagement = $objectManager->get(\Magento\Catalog\Api\CategoryLinkManagementInterface::class); - -$tierPrices = []; -/** @var \Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory $tierPriceFactory */ -$tierPriceFactory = $objectManager->get(\Magento\Catalog\Api\Data\ProductTierPriceInterfaceFactory::class); -/** @var $tpExtensionAttributes */ -$tpExtensionAttributesFactory = $objectManager->get(ProductTierPriceExtensionFactory::class); -/** @var $productExtensionAttributes */ -$productExtensionAttributesFactory = $objectManager->get(ProductExtensionInterfaceFactory::class); - -$adminWebsite = $objectManager->get(\Magento\Store\Api\WebsiteRepositoryInterface::class)->get('admin'); -$tierPriceExtensionAttributes1 = $tpExtensionAttributesFactory->create() - ->setWebsiteId($adminWebsite->getId()); -$productExtensionAttributesWebsiteIds = $productExtensionAttributesFactory->create( - ['website_ids' => $adminWebsite->getId()] -); - -$tierPrices[] = $tierPriceFactory->create( - [ - 'data' => [ - 'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL, - 'qty' => 2, - 'value' => 8 - ] - ] -)->setExtensionAttributes($tierPriceExtensionAttributes1); - -$tierPrices[] = $tierPriceFactory->create( - [ - 'data' => [ - 'customer_group_id' => \Magento\Customer\Model\Group::CUST_GROUP_ALL, - 'qty' => 5, - 'value' => 5 - ] - ] -)->setExtensionAttributes($tierPriceExtensionAttributes1); - -$tierPrices[] = $tierPriceFactory->create( - [ - 'data' => [ - 'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, - 'qty' => 3, - 'value' => 5 - ] - ] -)->setExtensionAttributes($tierPriceExtensionAttributes1); - -$tierPriceExtensionAttributes2 = $tpExtensionAttributesFactory->create() - ->setWebsiteId($adminWebsite->getId()) - ->setPercentageValue(50); - -$tierPrices[] = $tierPriceFactory->create( - [ - 'data' => [ - 'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID, - 'qty' => 10 - ] - ] -)->setExtensionAttributes($tierPriceExtensionAttributes2); - -/** @var $product \Magento\Catalog\Model\Product */ -$product = $objectManager->create(\Magento\Catalog\Model\Product::class); -$product->isObjectNew(true); -$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE) - ->setId(1) - ->setAttributeSetId(4) - ->setWebsiteIds([1]) - ->setName('Simple Product') - ->setSku('simple') - ->setPrice(10) - ->setWeight(1) - ->setShortDescription("Short description") - ->setTaxClassId(0) - ->setTierPrices($tierPrices) - ->setDescription('Description with html tag') - ->setExtensionAttributes($productExtensionAttributesWebsiteIds) - ->setMetaTitle('meta title') - ->setMetaKeyword('meta keyword') - ->setMetaDescription('meta description') - ->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH) - ->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED) - ->setStockData( - [ - 'use_config_manage_stock' => 1, - 'qty' => 100, - 'is_qty_decimal' => 0, - 'is_in_stock' => 1, - ] - )->setCanSaveCustomOptions(true) - ->setHasOptions(true); - -$oldOptions = [ - [ - 'previous_group' => 'text', - 'title' => 'Test Field', - 'type' => 'field', - 'is_require' => 1, - 'sort_order' => 0, - 'price' => 1, - 'price_type' => 'fixed', - 'sku' => '1-text', - 'max_characters' => 100, - ], - [ - 'previous_group' => 'date', - 'title' => 'Test Date and Time', - 'type' => 'date_time', - 'is_require' => 1, - 'sort_order' => 0, - 'price' => 2, - 'price_type' => 'fixed', - 'sku' => '2-date', - ], - [ - 'previous_group' => 'select', - 'title' => 'Test Select', - 'type' => 'drop_down', - 'is_require' => 1, - 'sort_order' => 0, - 'values' => [ - [ - 'option_type_id' => null, - 'title' => 'Option 1', - 'price' => 3, - 'price_type' => 'fixed', - 'sku' => '3-1-select', - ], - [ - 'option_type_id' => null, - 'title' => 'Option 2', - 'price' => 3, - 'price_type' => 'fixed', - 'sku' => '3-2-select', - ], - ] - ], - [ - 'previous_group' => 'select', - 'title' => 'Test Radio', - 'type' => 'radio', - 'is_require' => 1, - 'sort_order' => 0, - 'values' => [ - [ - 'option_type_id' => null, - 'title' => 'Option 1', - 'price' => 3, - 'price_type' => 'fixed', - 'sku' => '4-1-radio', - ], - [ - 'option_type_id' => null, - 'title' => 'Option 2', - 'price' => 3, - 'price_type' => 'fixed', - 'sku' => '4-2-radio', - ], - ] - ] -]; - -$options = []; - -/** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory $customOptionFactory */ -$customOptionFactory = $objectManager->create(\Magento\Catalog\Api\Data\ProductCustomOptionInterfaceFactory::class); - -foreach ($oldOptions as $option) { - /** @var \Magento\Catalog\Api\Data\ProductCustomOptionInterface $option */ - $option = $customOptionFactory->create(['data' => $option]); - $option->setProductSku($product->getSku()); - - $options[] = $option; -} - -$product->setOptions($options); - -/** @var \Magento\Catalog\Api\ProductRepositoryInterface $productRepository */ -$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class); -$productRepository->save($product); - -$categoryLinkManagement->assignProductToCategories( - $product->getSku(), - [2] -); -///** -// * @var $configWriter \Magento\Framework\App\Config\Storage\WriterInterface -// */ -//$configWriter = $objectManager->get(\Magento\Framework\App\Config\Storage\WriterInterface::class); -///** -// * @var $cacheManager \Magento\Framework\App\Cache\Manager -// */ -//$cacheManager = $objectManager->get(\Magento\Framework\App\Cache\Manager::class); -//$configWriter->save('sendfriend/email/max_per_hour', 1); -//$configWriter->save('sendfriend/email/check_by', 1); -//$types = $cacheManager->getAvailableTypes(); -//$cacheManager->clean($types); -