ltsecurityinc安防产品在线商城代码
This commit is contained in:
@@ -0,0 +1,175 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Swatches;
|
||||
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Test layered navigation filter.
|
||||
*
|
||||
* @package Magento\GraphQl\Swatches
|
||||
*/
|
||||
class ProductSearchTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* Verify that layered navigation filters are returned for product query
|
||||
*
|
||||
* @magentoApiDataFixture Magento/Swatches/_files/products_with_layered_navigation_swatch.php
|
||||
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
|
||||
*/
|
||||
public function testFilterLn()
|
||||
{
|
||||
$query = <<<QUERY
|
||||
{
|
||||
products (
|
||||
filter:{
|
||||
sku:{
|
||||
in:["simple1", "simple2", "simple3"]
|
||||
}
|
||||
}
|
||||
pageSize: 4
|
||||
currentPage: 1
|
||||
sort: {
|
||||
name: DESC
|
||||
}
|
||||
)
|
||||
{
|
||||
items {
|
||||
sku
|
||||
}
|
||||
filters {
|
||||
name
|
||||
filter_items_count
|
||||
request_var
|
||||
filter_items {
|
||||
label
|
||||
value_string
|
||||
items_count
|
||||
... on SwatchLayerFilterItemInterface {
|
||||
swatch_data {
|
||||
type
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
/**
|
||||
* @var ProductRepositoryInterface $productRepository
|
||||
*/
|
||||
$response = $this->graphQlQuery($query);
|
||||
|
||||
$this->assertArrayHasKey(
|
||||
'filters',
|
||||
$response['products'],
|
||||
'Filters are missing in product query result.'
|
||||
);
|
||||
$this->assertFilters(
|
||||
$response,
|
||||
$this->getExpectedFiltersDataSet(),
|
||||
'Returned filters data set does not match the expected value'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get array with expected data for layered navigation filters
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
private function getExpectedFiltersDataSet()
|
||||
{
|
||||
/** @var \Magento\Eav\Model\Config $eavConfig */
|
||||
$eavConfig = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(\Magento\Eav\Model\Config::class);
|
||||
$attribute = $eavConfig->getAttribute('catalog_product', 'color_swatch');
|
||||
/** @var \Magento\Eav\Api\Data\AttributeOptionInterface[] $options */
|
||||
$options = $attribute->getOptions();
|
||||
// Fetching option ID is required for continuous debug as of autoincrement IDs.
|
||||
return [
|
||||
[
|
||||
'name' => 'Category',
|
||||
'filter_items_count' => 1,
|
||||
'request_var' => 'cat',
|
||||
'filter_items' => [
|
||||
[
|
||||
'label' => 'Category 1',
|
||||
'value_string' => '333',
|
||||
'items_count' => 3,
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'Test Swatch',
|
||||
'filter_items_count' => 1,
|
||||
'request_var' => 'color_swatch',
|
||||
'filter_items' => [
|
||||
[
|
||||
'label' => 'option 1',
|
||||
'value_string' => $options[1]->getValue(),
|
||||
'items_count' => 2,
|
||||
'swatch_data' => [
|
||||
'type' => '1',
|
||||
'value' => '#555555',
|
||||
],
|
||||
],
|
||||
],
|
||||
],
|
||||
[
|
||||
'name' => 'Price',
|
||||
'filter_items_count' => 3,
|
||||
'request_var' => 'price',
|
||||
'filter_items' => [
|
||||
[
|
||||
'label' => '<span class="price">$0.00</span> - <span class="price">$9.99</span>',
|
||||
'value_string' => '-10',
|
||||
'items_count' => 1,
|
||||
],
|
||||
[
|
||||
'label' => '<span class="price">$10.00</span> - <span class="price">$19.99</span>',
|
||||
'value_string' => '10-20',
|
||||
'items_count' => 1,
|
||||
],
|
||||
[
|
||||
'label' => '<span class="price">$20.00</span> and above',
|
||||
'value_string' => '20-',
|
||||
'items_count' => 1,
|
||||
],
|
||||
],
|
||||
],
|
||||
];
|
||||
}
|
||||
|
||||
/**
|
||||
* Assert filters data.
|
||||
*
|
||||
* @param array $response
|
||||
* @param array $expectedFilters
|
||||
* @param string $message
|
||||
*/
|
||||
private function assertFilters($response, $expectedFilters, $message = '')
|
||||
{
|
||||
$this->assertArrayHasKey('filters', $response['products'], 'Product has filters');
|
||||
$this->assertIsArray(($response['products']['filters']), 'Product filters is array');
|
||||
$this->assertTrue(count($response['products']['filters']) > 0, 'Product filters is not empty');
|
||||
foreach ($expectedFilters as $expectedFilter) {
|
||||
$found = false;
|
||||
foreach ($response['products']['filters'] as $responseFilter) {
|
||||
if ($responseFilter['name'] == $expectedFilter['name']
|
||||
&& $responseFilter['request_var'] == $expectedFilter['request_var']) {
|
||||
$found = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (!$found) {
|
||||
$this->fail($message);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
+154
@@ -0,0 +1,154 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\GraphQl\Swatches;
|
||||
|
||||
use Magento\Swatches\Helper\Media as SwatchesMedia;
|
||||
use Magento\Swatches\Model\Swatch;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\TestCase\GraphQlAbstract;
|
||||
|
||||
/**
|
||||
* Test for configurable product option swatch data
|
||||
*/
|
||||
class ProductSwatchDataTest extends GraphQlAbstract
|
||||
{
|
||||
/**
|
||||
* @var SwatchesMedia
|
||||
*/
|
||||
private $swatchMediaHelper;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->swatchMediaHelper = $objectManager->get(SwatchesMedia::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Swatches/_files/configurable_product_text_swatch_attribute.php
|
||||
*/
|
||||
public function testTextSwatchDataValues()
|
||||
{
|
||||
$productSku = 'configurable';
|
||||
$query = <<<QUERY
|
||||
{
|
||||
products(filter: {sku: {eq: "$productSku"}}) {
|
||||
items {
|
||||
... on ConfigurableProduct{
|
||||
configurable_options{
|
||||
values {
|
||||
swatch_data{
|
||||
value
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
$response = $this->graphQlQuery($query);
|
||||
|
||||
$this->assertArrayHasKey('products', $response);
|
||||
$this->assertArrayHasKey('items', $response['products']);
|
||||
$this->assertArrayHasKey(0, $response['products']['items']);
|
||||
|
||||
$product = $response['products']['items'][0];
|
||||
$this->assertArrayHasKey('configurable_options', $product);
|
||||
$this->assertArrayHasKey(0, $product['configurable_options']);
|
||||
|
||||
$option = $product['configurable_options'][0];
|
||||
$this->assertArrayHasKey('values', $option);
|
||||
$length = count($option['values']);
|
||||
$swatchData = ['Swatch 1', 'Swatch 2', 'Swatch 3'];
|
||||
for ($i = 0; $i < $length; $i++) {
|
||||
$swatchValue = $option['values'][$i]['swatch_data']['value'];
|
||||
$this->assertContains($swatchValue, $swatchData);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoApiDataFixture Magento/Swatches/_files/configurable_product_with_visual_swatch_attribute.php
|
||||
*/
|
||||
public function testVisualSwatchDataValues()
|
||||
{
|
||||
$productSku = 'configurable';
|
||||
$imageName = '/visual_swatch_attribute_option_type_image.jpg';
|
||||
$color = '#000000';
|
||||
$query = <<<QUERY
|
||||
{
|
||||
products(filter: { sku: { eq: "$productSku" } }) {
|
||||
items {
|
||||
... on ConfigurableProduct {
|
||||
configurable_options {
|
||||
values {
|
||||
swatch_data {
|
||||
value
|
||||
... on ImageSwatchData {
|
||||
thumbnail
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
configurable_product_options_selection {
|
||||
configurable_options {
|
||||
values {
|
||||
label
|
||||
swatch {
|
||||
value
|
||||
... on ImageSwatchData {
|
||||
thumbnail
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
QUERY;
|
||||
$response = $this->graphQlQuery($query);
|
||||
|
||||
$this->assertArrayHasKey('products', $response);
|
||||
$this->assertArrayHasKey('items', $response['products']);
|
||||
$this->assertArrayHasKey(0, $response['products']['items']);
|
||||
|
||||
$product = $response['products']['items'][0];
|
||||
$this->assertArrayHasKey('configurable_options', $product);
|
||||
$this->assertArrayHasKey(0, $product['configurable_options']);
|
||||
|
||||
$option = $product['configurable_options'][0];
|
||||
$this->assertArrayHasKey('values', $option);
|
||||
$this->assertEquals($color, $option['values'][0]['swatch_data']['value']);
|
||||
$this->assertStringContainsString(
|
||||
$option['values'][1]['swatch_data']['value'],
|
||||
$this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_IMAGE_NAME, $imageName)
|
||||
);
|
||||
$this->assertEquals(
|
||||
$option['values'][1]['swatch_data']['thumbnail'],
|
||||
$this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_THUMBNAIL_NAME, $imageName)
|
||||
);
|
||||
|
||||
$configurableProductOptionsSelection =
|
||||
$product['configurable_product_options_selection']['configurable_options'][0];
|
||||
|
||||
$this->assertArrayHasKey('values', $configurableProductOptionsSelection);
|
||||
$this->assertEquals($color, $configurableProductOptionsSelection['values'][0]['swatch']['value']);
|
||||
$this->assertStringContainsString(
|
||||
$configurableProductOptionsSelection['values'][1]['swatch']['value'],
|
||||
$this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_IMAGE_NAME, $imageName)
|
||||
);
|
||||
$this->assertEquals(
|
||||
$configurableProductOptionsSelection['values'][1]['swatch']['thumbnail'],
|
||||
$this->swatchMediaHelper->getSwatchAttributeImage(Swatch::SWATCH_THUMBNAIL_NAME, $imageName)
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user