init
This commit is contained in:
+65
@@ -0,0 +1,65 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogGraphQl\Model\Config;
|
||||
|
||||
use Magento\Framework\GraphQl\Schema\Type\Entity\MapperInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class AttributeReaderTest extends TestCase
|
||||
{
|
||||
/** @var AttributeReader */
|
||||
private $attributeReader;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$mapper = $this->createMock(MapperInterface::class);
|
||||
$mapper->expects($this->any())
|
||||
->method('getMappedTypes')
|
||||
->willReturn([
|
||||
'product' => 'ProductInterface',
|
||||
'simple' => 'SimpleProduct',
|
||||
'virtual' => 'VirtualProduct',
|
||||
'downloadable' => 'DownloadableProduct',
|
||||
'bundle' => 'BundleProduct',
|
||||
'grouped' => 'GroupedProduct',
|
||||
'configurable' => 'ConfigurableProduct',
|
||||
]);
|
||||
$this->attributeReader = $objectManager->create(AttributeReader::class, ['mapper' => $mapper]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store web_api/graphql/include_dynamic_attributes_as_entity_type_fields 1
|
||||
*/
|
||||
public function testReadWithIncludeDynamicAttributesOptionEnabled()
|
||||
{
|
||||
$result = $this->attributeReader->read();
|
||||
$this->assertCount(7, $result);
|
||||
|
||||
//Adding custom attribute dynamically to the schema is deprecated.
|
||||
foreach ($result as $typeName) {
|
||||
if (!isset($typeName['fields'])) {
|
||||
$this->fail('Invalid config');
|
||||
}
|
||||
|
||||
array_map(fn ($attribute) => $this->assertArrayHasKey('deprecated', $attribute), $typeName['fields']);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoConfigFixture current_store web_api/graphql/include_dynamic_attributes_as_entity_type_fields 0
|
||||
*/
|
||||
public function testReadWithIncludeDynamicAttributesOptionDisabled()
|
||||
{
|
||||
$this->assertEmpty($this->attributeReader->read());
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogGraphQl\Model\Config;
|
||||
|
||||
use Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection as ProductsAttributesCollection;
|
||||
use Magento\Framework\Search\Request\Config as SearchRequestConfig;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use Magento\TestFramework\Helper\CacheCleaner;
|
||||
|
||||
/**
|
||||
* @magentoDbIsolation disabled
|
||||
* @magentoAppArea graphql
|
||||
*/
|
||||
class ConfigTest extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
* @var bool|null $isFixtureLoaded
|
||||
*/
|
||||
private ?bool $isFixtureLoaded = null;
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function setUp(): void
|
||||
{
|
||||
$this->isFixtureLoaded = false;
|
||||
CacheCleaner::cleanAll();
|
||||
}
|
||||
|
||||
/**
|
||||
* @inheritDoc
|
||||
*/
|
||||
protected function tearDown(): void
|
||||
{
|
||||
if ($this->isFixtureLoaded) {
|
||||
$rollBackPath = __DIR__
|
||||
. '/../../../Catalog/_files/products_with_layered_navigation_attribute_store_options_rollback.php';
|
||||
require $rollBackPath;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to confirm that we don't load cached configuration before attribute existed
|
||||
*
|
||||
* @covers \Magento\Framework\Search\Request\Config
|
||||
* @return void
|
||||
* @magentoApiDataFixture Magento/Store/_files/store.php
|
||||
*/
|
||||
public function testAttributesChangeCleansSearchRequestConfigCache(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
/** @var SearchRequestConfig $configInstance1 */
|
||||
$configInstance1 = $objectManager->create(SearchRequestConfig::class);
|
||||
$aggregations1 = $configInstance1->get('graphql_product_search_with_aggregation/aggregations');
|
||||
$this->assertArrayNotHasKey('test_configurable_bucket', $aggregations1);
|
||||
require __DIR__ . '/../../../Catalog/_files/products_with_layered_navigation_attribute_store_options.php';
|
||||
$this->isFixtureLoaded = true;
|
||||
/** @var SearchRequestConfig $configInstance2 */
|
||||
$configInstance2 = $objectManager->create(SearchRequestConfig::class);
|
||||
$aggregations2 = $configInstance2->get('graphql_product_search_with_aggregation/aggregations');
|
||||
$this->assertArrayHasKey('test_configurable_bucket', $aggregations2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to confirm that we don't load cached configuration before attribute existed
|
||||
*
|
||||
* @return void
|
||||
* @magentoApiDataFixture Magento/Store/_files/store.php
|
||||
*/
|
||||
public function testAttributesChangeCleansGraphQlConfigCache(): void
|
||||
{
|
||||
$objectManager = Bootstrap::getObjectManager();
|
||||
$this->resetStateProductsAttributesCollection($objectManager);
|
||||
$configInstance1 = $objectManager->create('Magento\Framework\GraphQl\Config\Data');
|
||||
$aggregations1 = $configInstance1->get('SimpleProduct/fields');
|
||||
$this->assertArrayNotHasKey('test_configurable', $aggregations1);
|
||||
require __DIR__ . '/../../../Catalog/_files/products_with_layered_navigation_attribute_store_options.php';
|
||||
$this->isFixtureLoaded = true;
|
||||
$this->resetStateProductsAttributesCollection($objectManager);
|
||||
$configInstance2 = $objectManager->create('Magento\Framework\GraphQl\Config\Data');
|
||||
$aggregations2 = $configInstance2->get('SimpleProduct/fields');
|
||||
$this->assertArrayHasKey('test_configurable', $aggregations2);
|
||||
}
|
||||
|
||||
/**
|
||||
* Test to confirm that we don't load cached configuration before attribute existed
|
||||
*
|
||||
* @return void
|
||||
* @magentoApiDataFixture Magento/Store/_files/store.php
|
||||
* @magentoAppArea global
|
||||
*/
|
||||
public function testGraphQlConfigCacheShouldntBreakWhenLoadedByGlobalArea(): void
|
||||
{
|
||||
/*
|
||||
* When Magento\Framework\GraphQl\Config\Data is loaded from area outside of graphql, and its cache doesn't
|
||||
* currently exist, then it will load incorrect data and save it in its cache, which will then be used by
|
||||
* Magento\Framework\GraphQl\Config\Data when it actually is in the graphql area.
|
||||
* See AC-10465 for more details on this bug.
|
||||
*/
|
||||
$this->markTestSkipped('Fix this in AC-10465');
|
||||
$this->testAttributesChangeCleansGraphQlConfigCache();
|
||||
}
|
||||
|
||||
/**
|
||||
* Magento\CatalogGraphQl\Model\Config\AttributeReader has mutable state in
|
||||
* Magento\CatalogGraphQl\Model\Resolver\Products\Attributes\Collection $collection, so we must reset it.
|
||||
*
|
||||
* @param $objectManager
|
||||
* @return void
|
||||
*/
|
||||
private function resetStateProductsAttributesCollection($objectManager) : void
|
||||
{
|
||||
/** @var ProductsAttributesCollection $productsAttributesCollection */
|
||||
$productsAttributesCollection = $objectManager->get(ProductsAttributesCollection::class);
|
||||
$productsAttributesCollection->_resetState();
|
||||
}
|
||||
}
|
||||
+88
@@ -0,0 +1,88 @@
|
||||
<?php
|
||||
/**
|
||||
* Copyright © Magento, Inc. All rights reserved.
|
||||
* See COPYING.txt for license details.
|
||||
*/
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace Magento\CatalogGraphQl\Model\Resolver\Cache\Product\MediaGallery;
|
||||
|
||||
use Magento\Catalog\Api\Data\ProductInterface;
|
||||
use Magento\Catalog\Api\ProductRepositoryInterface;
|
||||
use Magento\Framework\Serialize\SerializerInterface;
|
||||
use Magento\TestFramework\Helper\Bootstrap;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
|
||||
class ProductModelHydratorDehydratorTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @var \Magento\TestFramework\ObjectManager
|
||||
*/
|
||||
private $objectManager;
|
||||
|
||||
/**
|
||||
* @var ProductRepositoryInterface
|
||||
*/
|
||||
private $productRepository;
|
||||
|
||||
/**
|
||||
* @var SerializerInterface
|
||||
*/
|
||||
private $serializer;
|
||||
|
||||
public function setUp(): void
|
||||
{
|
||||
$this->objectManager = Bootstrap::getObjectManager();
|
||||
$this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
|
||||
$this->serializer = $this->objectManager->get(SerializerInterface::class);
|
||||
}
|
||||
|
||||
/**
|
||||
* @magentoDataFixture Magento/Catalog/_files/product_with_media_gallery.php
|
||||
*/
|
||||
public function testModelHydration(): void
|
||||
{
|
||||
$productModel = $this->productRepository->get('simple_product_with_media');
|
||||
$resolverData = $this->extractResolverData($productModel);
|
||||
$originalResolverData = $resolverData;
|
||||
|
||||
/** @var ProductModelDehydrator $dehydrator */
|
||||
$dehydrator = $this->objectManager->get(ProductModelDehydrator::class);
|
||||
$dehydrator->dehydrate($resolverData);
|
||||
$mediaGalleryEntity = $resolverData[0];
|
||||
$this->assertArrayNotHasKey('model', $mediaGalleryEntity);
|
||||
$this->assertArrayHasKey('model_info', $mediaGalleryEntity);
|
||||
|
||||
$serializedData = $this->serializer->serialize($resolverData);
|
||||
$resolverData = $this->serializer->unserialize($serializedData);
|
||||
|
||||
/** @var ProductModelHydrator $hydrator */
|
||||
$hydrator = $this->objectManager->get(ProductModelHydrator::class);
|
||||
$resolverDataEntityOne = $resolverData[0];
|
||||
$hydrator->hydrate($resolverDataEntityOne);
|
||||
$hydratedModel = $resolverDataEntityOne['model'];
|
||||
$this->assertInstanceOf(ProductInterface::class, $hydratedModel);
|
||||
$originalModel = $originalResolverData[0]['model'];
|
||||
$this->assertEquals($originalModel->getId(), $hydratedModel->getId());
|
||||
}
|
||||
|
||||
/**
|
||||
* Extract media gallery resolver data
|
||||
*
|
||||
* @param ProductInterface $product
|
||||
* @return array
|
||||
*/
|
||||
private function extractResolverData(ProductInterface $product)
|
||||
{
|
||||
$mediaGalleryEntries = [];
|
||||
foreach ($product->getMediaGalleryEntries() ?? [] as $key => $entry) {
|
||||
$mediaGalleryEntries[$key] = $entry->getData();
|
||||
$mediaGalleryEntries[$key]['model'] = $product;
|
||||
if ($entry->getExtensionAttributes() && $entry->getExtensionAttributes()->getVideoContent()) {
|
||||
$mediaGalleryEntries[$key]['video_content']
|
||||
= $entry->getExtensionAttributes()->getVideoContent()->getData();
|
||||
}
|
||||
}
|
||||
return $mediaGalleryEntries;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user