Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[17] Read API Integration tests Magento StorefrontGraphQl #63 [… #28746

Merged
merged 2 commits into from
Jun 16, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ protected function setUp(): void
parent::setUp();

$this->_objectManager->get(\Magento\Backend\Model\UrlInterface::class)->turnOffSecretKey();

/**
* Authorization can be created on test bootstrap...
* If it will be created on test bootstrap we will have invalid RoleLocator object.
* As tests by default are run not from adminhtml area...
*/
\Magento\TestFramework\ObjectManager::getInstance()->removeSharedInstance(\Magento\Framework\Authorization::class);
$this->_auth = $this->_objectManager->get(\Magento\Backend\Model\Auth::class);
$this->_session = $this->_auth->getAuthStorage();
$credentials = $this->_getAdminCredentials();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -207,3 +207,16 @@
$product->getSku(),
[2]
);

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = $objectManager->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexRow($product->getId());
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

use Magento\Catalog\Api\Data\ProductTierPriceExtensionFactory;
use Magento\Catalog\Api\Data\ProductExtensionInterfaceFactory;

\Magento\TestFramework\Helper\Bootstrap::getInstance()->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);

$tierPrices[] = $tierPriceFactory->create(
[
'data' => [
'customer_group_id' => \Magento\Customer\Model\Group::NOT_LOGGED_IN_ID,
'qty' => 3.2,
'value' => 6,
]
]
)->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 <b>html tag</b>')
->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_DISABLED)
->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]
);

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = $objectManager->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexRow($product->getId());
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,17 @@
$product->getSku(),
[2]
);


/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = $objectManager->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexRow($product->getId());
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
$attributeValues = [];
$attributeSetId = $installer->getAttributeSetId(Product::ENTITY, 'Default');
$associatedProductIds = [];
$productIds = [10, 20];
$idsToReindex = $productIds = [10, 20];
array_shift($options); //remove the first option which is empty

foreach ($options as $option) {
Expand Down Expand Up @@ -142,3 +142,17 @@
$product->getSku(),
[2]
);

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexRow($product->getId());
$indexer->reindexList($idsToReindex);
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
$attributeValues = [];
$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
$associatedProductIds = [];
$productIds = [10, 20];
$idsToReindex = $productIds = [10, 20];
array_shift($options); //remove the first option which is empty

foreach ($options as $option) {
Expand Down Expand Up @@ -142,3 +142,16 @@
$product->getSku(),
[2]
);

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexList($idsToReindex);
Original file line number Diff line number Diff line change
Expand Up @@ -116,17 +116,13 @@ public function testGetItemCollection(): void
}

/**
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_disabled_simple_product.php
*
* @return void
*/
public function testGetItemCollectionWithDisabledProduct(): void
{
$productSku = 'simple';
$customerId = 1;
$product = $this->productRepository->get($productSku);
$product->setStatus(ProductStatus::STATUS_DISABLED);
$this->productRepository->save($product);
$this->assertEmpty($this->getWishlistByCustomerId->execute($customerId)->getItemCollection()->getItems());
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@
$wishlistForSecondCustomer->loadByCustomerId($secondCustomerIdFromFixture, true);
$item = $wishlistForSecondCustomer->addNewItem($product, new \Magento\Framework\DataObject([]));
$wishlistForSecondCustomer->save();

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexList([$product->getId()]);
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,17 @@
$wishlist->loadByCustomerId($customer->getId(), true);
$item = $wishlist->addNewItem($product, new DataObject([]));
$wishlist->setSharingCode('fixture_unique_code')->save();

/**
* We need to remember that automatic reindexation is not working properly in integration tests
* Reindexation is sitting on top of afterCommit callbacks:
* \Magento\Catalog\Model\Product::priceReindexCallback
*
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
*/
/** @var \Magento\Indexer\Model\Indexer $indexer */
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
$indexer->load('catalog_product_price');
$indexer->reindexList([$product->getId()]);

Loading