Skip to content
This repository was archived by the owner on Apr 29, 2019. It is now read-only.

Commit 601ff22

Browse files
committed
MAGETWO-89458: Cover changed and new logic with automated tests
1 parent 8b3ec37 commit 601ff22

File tree

4 files changed

+421
-1
lines changed

4 files changed

+421
-1
lines changed

dev/tests/api-functional/testsuite/Magento/GraphQl/Catalog/ProductSearchTest.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66

77
namespace Magento\GraphQl\Catalog;
88

9-
use Magento\Catalog\Api\CategoryLinkManagementInterface;
109
use Magento\Catalog\Api\Data\ProductInterface;
1110
use Magento\Catalog\Api\ProductRepositoryInterface;
1211
use Magento\Framework\EntityManager\MetadataPool;
Lines changed: 159 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,159 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
namespace Magento\GraphQl\Swatches;
8+
9+
use Magento\Catalog\Api\ProductRepositoryInterface;
10+
use Magento\TestFramework\TestCase\GraphQlAbstract;
11+
12+
/**
13+
* Test layered navigation filter.
14+
*
15+
* @package Magento\GraphQl\Swatches
16+
*/
17+
class ProductSearchTest extends GraphQlAbstract
18+
{
19+
/**
20+
* Verify that layered navigation filters are returned for product query
21+
*
22+
* @magentoApiDataFixture Magento/Swatches/_files/products_with_layered_navigation_swatch.php
23+
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
24+
*/
25+
public function testFilterLn()
26+
{
27+
$query = <<<QUERY
28+
{
29+
products (
30+
filter:{
31+
sku:{
32+
like:"%simple%"
33+
}
34+
}
35+
pageSize: 4
36+
currentPage: 1
37+
sort: {
38+
name: DESC
39+
}
40+
)
41+
{
42+
items {
43+
sku
44+
}
45+
filters {
46+
name
47+
filter_items_count
48+
request_var
49+
filter_items {
50+
label
51+
value_string
52+
items_count
53+
... on SwatchLayerFilterItemInterface {
54+
swatch_data {
55+
type
56+
value
57+
}
58+
}
59+
}
60+
}
61+
}
62+
}
63+
QUERY;
64+
/**
65+
* @var ProductRepositoryInterface $productRepository
66+
*/
67+
$response = $this->graphQlQuery($query);
68+
69+
$this->assertArrayHasKey(
70+
'filters',
71+
$response['products'],
72+
'Filters are missing in product query result.'
73+
);
74+
$this->assertFilters(
75+
$response,
76+
$this->getExpectedFiltersDataSet(),
77+
'Returned filters data set does not match the expected value'
78+
);
79+
}
80+
81+
/**
82+
* Get array with expected data for layered navigation filters
83+
*
84+
* @return array
85+
*/
86+
private function getExpectedFiltersDataSet()
87+
{
88+
/** @var \Magento\Eav\Model\Config $eavConfig */
89+
$eavConfig = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
90+
$attribute = $eavConfig->getAttribute('catalog_product', 'color_swatch');
91+
/** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $options */
92+
$options = $attribute->getOptions();
93+
// Fetching option ID is required for continuous debug as of autoincrement IDs.
94+
return [
95+
[
96+
'name' => 'Category',
97+
'filter_items_count' => 1,
98+
'request_var' => 'cat',
99+
'filter_items' => [
100+
[
101+
'label' => 'Category 1',
102+
'value_string' => '333',
103+
'items_count' => 3,
104+
],
105+
],
106+
],
107+
[
108+
'name' => 'Test Swatch',
109+
'filter_items_count' => 1,
110+
'request_var' => 'color_swatch',
111+
'filter_items' => [
112+
[
113+
'label' => 'option 1',
114+
'value_string' => $options[1]->getValue(),
115+
'items_count' => 2,
116+
'swatch_data' => [
117+
'type' => '1',
118+
'value' => '#555555',
119+
],
120+
],
121+
],
122+
],
123+
[
124+
'name' => 'Price',
125+
'filter_items_count' => 3,
126+
'request_var' => 'price',
127+
'filter_items' => [
128+
[
129+
'label' => '<span class="price">$0.00</span> - <span class="price">$9.99</span>',
130+
'value_string' => '-10',
131+
'items_count' => 1,
132+
],
133+
[
134+
'label' => '<span class="price">$10.00</span> - <span class="price">$19.99</span>',
135+
'value_string' => '10-20',
136+
'items_count' => 1,
137+
],
138+
[
139+
'label' => '<span class="price">$20.00</span> and above',
140+
'value_string' => '20-',
141+
'items_count' => 1,
142+
],
143+
],
144+
],
145+
];
146+
}
147+
148+
/**
149+
* Assert filters data.
150+
*
151+
* @param array $response
152+
* @param array $expectedFilters
153+
* @param string $message
154+
*/
155+
private function assertFilters($response, $expectedFilters, $message = '')
156+
{
157+
$this->assertEquals($expectedFilters, $response['products']['filters'], $message);
158+
}
159+
}
Lines changed: 213 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,213 @@
1+
<?php
2+
/**
3+
* Copyright © Magento, Inc. All rights reserved.
4+
* See COPYING.txt for license details.
5+
*/
6+
7+
use Magento\TestFramework\Helper\Bootstrap;
8+
use Magento\Eav\Api\Data\AttributeOptionInterface;
9+
10+
/** @var \Magento\Framework\ObjectManagerInterface $objectManager */
11+
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
12+
/** @var $installer \Magento\Catalog\Setup\CategorySetup */
13+
$installer = Bootstrap::getObjectManager()->create(\Magento\Catalog\Setup\CategorySetup::class);
14+
15+
$data = [
16+
'attribute_code' => 'color_swatch',
17+
'entity_type_id' => $installer->getEntityTypeId('catalog_product'),
18+
'is_global' => 1,
19+
'is_user_defined' => 1,
20+
'frontend_input' => 'select',
21+
'is_unique' => 0,
22+
'is_required' => 0,
23+
'is_searchable' => 1,
24+
'is_visible_in_advanced_search' => 1,
25+
'is_comparable' => 1,
26+
'is_filterable' => 1,
27+
'is_filterable_in_search' => 1,
28+
'is_used_for_promo_rules' => 0,
29+
'is_html_allowed_on_front' => 1,
30+
'is_visible_on_front' => 1,
31+
'used_in_product_listing' => 1,
32+
'used_for_sort_by' => 1,
33+
'frontend_label' => ['Test Swatch'],
34+
'backend_type' => 'int',
35+
'use_product_image_for_swatch' => 0,
36+
'update_product_preview_image' => 0,
37+
];
38+
$optionsPerAttribute = 3;
39+
40+
$data['swatch_input_type'] = 'visual';
41+
$data['swatchvisual']['value'] = array_reduce(
42+
range(1, $optionsPerAttribute),
43+
function ($values, $index) use ($optionsPerAttribute) {
44+
$values['option_' . $index] = '#'
45+
. str_repeat(
46+
dechex(255 * $index / $optionsPerAttribute),
47+
3
48+
);
49+
return $values;
50+
},
51+
[]
52+
);
53+
$data['optionvisual']['value'] = array_reduce(
54+
range(1, $optionsPerAttribute),
55+
function ($values, $index) use ($optionsPerAttribute) {
56+
$values['option_' . $index] = ['option ' . $index];
57+
return $values;
58+
},
59+
[]
60+
);
61+
62+
$data['options']['option'] = array_reduce(
63+
range(1, $optionsPerAttribute),
64+
function ($values, $index) use ($optionsPerAttribute) {
65+
$values[] = [
66+
'label' => 'option ' . $index,
67+
'value' => 'option_' . $index,
68+
];
69+
return $values;
70+
},
71+
[]
72+
);
73+
74+
$options = [];
75+
foreach ($data['options']['option'] as $optionData) {
76+
$options[] = $objectManager->get(AttributeOptionInterface::class)
77+
->setLabel($optionData['label'])
78+
->setValue($optionData['value']);
79+
}
80+
81+
$attribute = $objectManager->create(
82+
\Magento\Catalog\Api\Data\ProductAttributeInterface::class,
83+
['data' => $data]
84+
);
85+
$attribute->setOptions($options);
86+
$attribute->save();
87+
88+
/* Assign attribute to attribute set */
89+
$installer->addAttributeToGroup('catalog_product', 'Default', 'General', $attribute->getId());
90+
91+
/** @var \Magento\Eav\Model\Config $eavConfig */
92+
$eavConfig = Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
93+
$eavConfig->clear();
94+
95+
$attribute = $eavConfig->getAttribute('catalog_product', 'color_swatch');
96+
$options = $attribute->getOptions();
97+
98+
99+
// workaround for saved attribute
100+
$attribute->setDefaultValue($options[1]->getValue());
101+
102+
$attribute->save();
103+
$eavConfig->clear();
104+
105+
/** @var $product \Magento\Catalog\Model\Product */
106+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
107+
$product->isObjectNew(true);
108+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
109+
->setId(10)
110+
->setAttributeSetId(4)
111+
->setName('Simple Product1')
112+
->setSku('simple1')
113+
->setTaxClassId('none')
114+
->setDescription('description')
115+
->setShortDescription('short description')
116+
->setOptionsContainer('container1')
117+
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_IN_CART)
118+
->setPrice(10)
119+
->setWeight(1)
120+
->setMetaTitle('meta title')
121+
->setMetaKeyword('meta keyword')
122+
->setMetaDescription('meta description')
123+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
124+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
125+
->setWebsiteIds([1])
126+
->setCateroryIds([])
127+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 100, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
128+
->setSpecialPrice('5.99')
129+
->save();
130+
131+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
132+
$product->isObjectNew(true);
133+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
134+
->setId(11)
135+
->setAttributeSetId(4)
136+
->setName('Simple Product2')
137+
->setSku('simple2')
138+
->setTaxClassId('none')
139+
->setDescription('description')
140+
->setShortDescription('short description')
141+
->setOptionsContainer('container1')
142+
->setMsrpDisplayActualPriceType(\Magento\Msrp\Model\Product\Attribute\Source\Type::TYPE_ON_GESTURE)
143+
->setPrice(20)
144+
->setWeight(1)
145+
->setMetaTitle('meta title')
146+
->setMetaKeyword('meta keyword')
147+
->setMetaDescription('meta description')
148+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
149+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
150+
->setWebsiteIds([1])
151+
->setCateroryIds([])
152+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 50, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
153+
->setSpecialPrice('15.99')
154+
->save();
155+
156+
$product = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Product::class);
157+
$product->isObjectNew(true);
158+
$product->setTypeId(\Magento\Catalog\Model\Product\Type::TYPE_SIMPLE)
159+
->setId(12)
160+
->setAttributeSetId(4)
161+
->setName('Simple Product3')
162+
->setSku('simple3')
163+
->setTaxClassId('none')
164+
->setDescription('description')
165+
->setShortDescription('short description')
166+
->setPrice(30)
167+
->setWeight(1)
168+
->setVisibility(\Magento\Catalog\Model\Product\Visibility::VISIBILITY_BOTH)
169+
->setStatus(\Magento\Catalog\Model\Product\Attribute\Source\Status::STATUS_ENABLED)
170+
->setWebsiteIds([1])
171+
->setCateroryIds([])
172+
->setStockData(['use_config_manage_stock' => 1, 'qty' => 140, 'is_qty_decimal' => 0, 'is_in_stock' => 1])
173+
->setSpecialPrice('25.99')
174+
->save();
175+
176+
177+
178+
$category = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(\Magento\Catalog\Model\Category::class);
179+
$category->isObjectNew(true);
180+
$category->setId(
181+
333
182+
)->setCreatedAt(
183+
'2014-06-23 09:50:07'
184+
)->setName(
185+
'Category 1'
186+
)->setParentId(
187+
2
188+
)->setPath(
189+
'1/2/333'
190+
)->setLevel(
191+
2
192+
)->setAvailableSortBy(
193+
['position', 'name']
194+
)->setDefaultSortBy(
195+
'name'
196+
)->setIsActive(
197+
true
198+
)->setPosition(
199+
1
200+
)->setPostedProducts(
201+
[10 => 10, 11 => 11, 12 => 12]
202+
)->save();
203+
204+
205+
206+
/** @var \Magento\Indexer\Model\Indexer\Collection $indexerCollection */
207+
$indexerCollection = Bootstrap::getObjectManager()->get(\Magento\Indexer\Model\Indexer\Collection::class);
208+
$indexerCollection->load();
209+
/** @var \Magento\Indexer\Model\Indexer $indexer */
210+
foreach ($indexerCollection->getItems() as $indexer) {
211+
$indexer->reindexAll();
212+
}
213+

0 commit comments

Comments
 (0)