Skip to content

Commit 7f560ab

Browse files
committed
[17] Read API Integration tests Magento StorefrontGraphQl #63 [17] Read API Integ
1 parent 8513dfd commit 7f560ab

File tree

8 files changed

+312
-8
lines changed

8 files changed

+312
-8
lines changed

dev/tests/integration/framework/Magento/TestFramework/TestCase/AbstractBackendController.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,12 @@ protected function setUp(): void
5858
parent::setUp();
5959

6060
$this->_objectManager->get(\Magento\Backend\Model\UrlInterface::class)->turnOffSecretKey();
61-
61+
/**
62+
* Authorization can be created on test bootstrap...
63+
* If it will be created on test bootstrap we will have invalid RoleLocator object.
64+
* As tests by default are run not from adminhtml area...
65+
*/
66+
\Magento\TestFramework\ObjectManager::getInstance()->removeSharedInstance(\Magento\Framework\Authorization::class);
6267
$this->_auth = $this->_objectManager->get(\Magento\Backend\Model\Auth::class);
6368
$this->_session = $this->_auth->getAuthStorage();
6469
$credentials = $this->_getAdminCredentials();

dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple.php

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -207,3 +207,16 @@
207207
$product->getSku(),
208208
[2]
209209
);
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());
Lines changed: 222 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,222 @@
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());

dev/tests/integration/testsuite/Magento/Catalog/_files/product_simple_xss.php

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,17 @@
3131
$product->getSku(),
3232
[2]
3333
);
34+
35+
36+
/**
37+
* We need to remember that automatic reindexation is not working properly in integration tests
38+
* Reindexation is sitting on top of afterCommit callbacks:
39+
* \Magento\Catalog\Model\Product::priceReindexCallback
40+
*
41+
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
42+
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
43+
*/
44+
/** @var \Magento\Indexer\Model\Indexer $indexer */
45+
$indexer = $objectManager->create(\Magento\Indexer\Model\Indexer::class);
46+
$indexer->load('catalog_product_price');
47+
$indexer->reindexRow($product->getId());

dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/product_configurable.php

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
$attributeValues = [];
3636
$attributeSetId = $installer->getAttributeSetId(Product::ENTITY, 'Default');
3737
$associatedProductIds = [];
38-
$productIds = [10, 20];
38+
$idsToReindex = $productIds = [10, 20];
3939
array_shift($options); //remove the first option which is empty
4040

4141
foreach ($options as $option) {
@@ -142,3 +142,17 @@
142142
$product->getSku(),
143143
[2]
144144
);
145+
146+
/**
147+
* We need to remember that automatic reindexation is not working properly in integration tests
148+
* Reindexation is sitting on top of afterCommit callbacks:
149+
* \Magento\Catalog\Model\Product::priceReindexCallback
150+
*
151+
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
152+
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
153+
*/
154+
/** @var \Magento\Indexer\Model\Indexer $indexer */
155+
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
156+
$indexer->load('catalog_product_price');
157+
$indexer->reindexRow($product->getId());
158+
$indexer->reindexList($idsToReindex);

dev/tests/integration/testsuite/Magento/ConfigurableProduct/_files/product_configurable_sku.php

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@
3636
$attributeValues = [];
3737
$attributeSetId = $installer->getAttributeSetId('catalog_product', 'Default');
3838
$associatedProductIds = [];
39-
$productIds = [10, 20];
39+
$idsToReindex = $productIds = [10, 20];
4040
array_shift($options); //remove the first option which is empty
4141

4242
foreach ($options as $option) {
@@ -142,3 +142,16 @@
142142
$product->getSku(),
143143
[2]
144144
);
145+
146+
/**
147+
* We need to remember that automatic reindexation is not working properly in integration tests
148+
* Reindexation is sitting on top of afterCommit callbacks:
149+
* \Magento\Catalog\Model\Product::priceReindexCallback
150+
*
151+
* However, callbacks are applied only when transaction_level = 0 (when transaction is commited), however
152+
* integration tests are not committing transactions, so we need to reindex data manually in order to reuse it in tests
153+
*/
154+
/** @var \Magento\Indexer\Model\Indexer $indexer */
155+
$indexer = Bootstrap::getObjectManager()->create(\Magento\Indexer\Model\Indexer::class);
156+
$indexer->load('catalog_product_price');
157+
$indexer->reindexList($idsToReindex);

dev/tests/integration/testsuite/Magento/Wishlist/Model/WishlistTest.php

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,17 +116,13 @@ public function testGetItemCollection(): void
116116
}
117117

118118
/**
119-
* @magentoDataFixture Magento/Wishlist/_files/wishlist.php
119+
* @magentoDataFixture Magento/Wishlist/_files/wishlist_with_disabled_simple_product.php
120120
*
121121
* @return void
122122
*/
123123
public function testGetItemCollectionWithDisabledProduct(): void
124124
{
125-
$productSku = 'simple';
126125
$customerId = 1;
127-
$product = $this->productRepository->get($productSku);
128-
$product->setStatus(ProductStatus::STATUS_DISABLED);
129-
$this->productRepository->save($product);
130126
$this->assertEmpty($this->getWishlistByCustomerId->execute($customerId)->getItemCollection()->getItems());
131127
}
132128

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\Catalog\Api\ProductRepositoryInterface;
8+
use Magento\Customer\Model\CustomerRegistry;
9+
use Magento\Framework\DataObject;
10+
use Magento\TestFramework\Helper\Bootstrap;
11+
use Magento\TestFramework\Workaround\Override\Fixture\Resolver;
12+
use Magento\Wishlist\Model\Wishlist;
13+
14+
Resolver::getInstance()->requireDataFixture('Magento/Customer/_files/customer.php');
15+
Resolver::getInstance()->requireDataFixture('Magento/Catalog/_files/product_simple_disabled.php');
16+
17+
$objectManager = Bootstrap::getObjectManager();
18+
/** @var CustomerRegistry $customerRegistry */
19+
$customerRegistry = Bootstrap::getObjectManager()->create(CustomerRegistry::class);
20+
$customer = $customerRegistry->retrieve(1);
21+
/** @var ProductRepositoryInterface $productRepository */
22+
$productRepository = $objectManager->create(ProductRepositoryInterface::class);
23+
$product = $productRepository->get('simple');
24+
$wishlist = Bootstrap::getObjectManager()->create(Wishlist::class);
25+
$wishlist->loadByCustomerId($customer->getId(), true);
26+
$item = $wishlist->addNewItem($product, new DataObject([]));
27+
$wishlist->setSharingCode('fixture_unique_code')->save();

0 commit comments

Comments
 (0)