Skip to content

Commit

Permalink
Merge pull request #4149 from magento-honey-badgers/hb-graphql-pr-2.3.2
Browse files Browse the repository at this point in the history
[honey] MC-15941, MC-15882
  • Loading branch information
cpartica authored May 3, 2019
2 parents 0fede7b + de1da5f commit 76a0af9
Show file tree
Hide file tree
Showing 8 changed files with 154 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function calculate(int $rootCategoryId) : int
$connection = $this->resourceConnection->getConnection();
$select = $connection->select()
->from($this->resourceConnection->getTableName('catalog_category_entity'), 'level')
->where($this->resourceCategory->getLinkField() . " = ?", $rootCategoryId);
->where($this->resourceCategory->getEntityIdField() . " = ?", $rootCategoryId);

return (int) $connection->fetchOne($select);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,9 +86,6 @@ public function getList(
$collection->load();

// Methods that perform extra fetches post-load
if (in_array('media_gallery_entries', $attributes)) {
$collection->addMediaGalleryData();
}
if (in_array('options', $attributes)) {
$collection->addOptionsToResult();
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor;

use Magento\Catalog\Model\ResourceModel\Product\Collection;
use Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessorInterface;
use Magento\Framework\Api\SearchCriteriaInterface;
use Magento\Catalog\Model\Product\Media\Config as MediaConfig;

/**
* Add attributes required for every GraphQL product resolution process.
*
* {@inheritdoc}
*/
class MediaGalleryProcessor implements CollectionProcessorInterface
{
/**
* @var MediaConfig
*/
private $mediaConfig;

/**
* Add media gallery attributes to collection
*
* @param MediaConfig $mediaConfig
*/
public function __construct(MediaConfig $mediaConfig)
{
$this->mediaConfig = $mediaConfig;
}

/**
* @inheritdoc
*/
public function process(
Collection $collection,
SearchCriteriaInterface $searchCriteria,
array $attributeNames
): Collection {
if (in_array('media_gallery_entries', $attributeNames)) {
$mediaAttributes = $this->mediaConfig->getMediaAttributeCodes();
foreach ($mediaAttributes as $mediaAttribute) {
if (!in_array($mediaAttribute, $attributeNames)) {
$collection->addAttributeToSelect($mediaAttribute);
}
}
$collection->addMediaGalleryData();
}

return $collection;
}
}
1 change: 1 addition & 0 deletions app/code/Magento/CatalogGraphQl/etc/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
<item name="search" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\SearchCriteriaProcessor</item>
<item name="stock" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\StockProcessor</item>
<item name="visibility" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\VisibilityStatusProcessor</item>
<item name="mediaGallery" xsi:type="object">Magento\CatalogGraphQl\Model\Resolver\Products\DataProvider\Product\CollectionProcessor\MediaGalleryProcessor</item>
</argument>
</arguments>
</type>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@

use Magento\TestFramework\TestCase\GraphQlAbstract;

/**
* Test media gallery queries
*/
class MediaGalleryTest extends GraphQlAbstract
{
/**
Expand Down Expand Up @@ -45,6 +48,43 @@ public function testProductSmallImageUrlWithExistingImage()
self::assertTrue($this->checkImageExists($response['products']['items'][0]['small_image']['url']));
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/product_with_multiple_images.php
*/
public function testMediaGalleryTypesAreCorrect()
{
$productSku = 'simple';
$query = <<<QUERY
{
products(filter: {sku: {eq: "{$productSku}"}}) {
items {
media_gallery_entries {
label
media_type
file
types
}
}
}
}
QUERY;
$response = $this->graphQlQuery($query);

$this->assertNotEmpty($response['products']['items'][0]['media_gallery_entries']);
$mediaGallery = $response['products']['items'][0]['media_gallery_entries'];
$this->assertCount(2, $mediaGallery);

$this->assertEquals('Image Alt Text', $mediaGallery[0]['label']);
$this->assertEquals('image', $mediaGallery[0]['media_type']);
$this->assertContains('magento_image', $mediaGallery[0]['file']);
$this->assertEquals(['image', 'small_image'], $mediaGallery[0]['types']);

$this->assertEquals('Thumbnail Image', $mediaGallery[1]['label']);
$this->assertEquals('image', $mediaGallery[1]['media_type']);
$this->assertContains('magento_thumbnail', $mediaGallery[1]['file']);
$this->assertEquals(['thumbnail', 'swatch_image'], $mediaGallery[1]['types']);
}

/**
* @param string $url
* @return bool
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,14 @@
$mediaDirectory->create($targetDirPath);
$mediaDirectory->create($targetTmpDirPath);

$targetTmpFilePath = $mediaDirectory->getAbsolutePath() . DIRECTORY_SEPARATOR . $targetTmpDirPath
. DIRECTORY_SEPARATOR . 'magento_image.jpg';
copy(__DIR__ . '/magento_image.jpg', $targetTmpFilePath);
// Copying the image to target dir is not necessary because during product save, it will be moved there from tmp dir
$images = ['magento_image.jpg', 'magento_small_image.jpg', 'magento_thumbnail.jpg'];

foreach ($images as $image) {
$targetTmpFilePath = $mediaDirectory->getAbsolutePath() . DIRECTORY_SEPARATOR . $targetTmpDirPath
. DIRECTORY_SEPARATOR . $image;

$sourceFilePath = __DIR__ . DIRECTORY_SEPARATOR . $image;

copy($sourceFilePath, $targetTmpFilePath);
// Copying the image to target dir is not necessary because during product save, it will be moved there from tmp dir
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

require __DIR__ . '/product_image.php';
require __DIR__ . '/product_simple.php';

$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$productRepository = $objectManager->create(\Magento\Catalog\Api\ProductRepositoryInterface::class);
$product = $productRepository->get('simple');

/** @var $product \Magento\Catalog\Model\Product */
$product->setStoreId(0)
->setImage('/m/a/magento_image.jpg')
->setSmallImage('/m/a/magento_image.jpg')
->setThumbnail('/m/a/magento_thumbnail.jpg')
->setSwatchImage('/m/a/magento_thumbnail.jpg')
->setData('media_gallery', ['images' => [
[
'file' => '/m/a/magento_image.jpg',
'position' => 1,
'label' => 'Image Alt Text',
'disabled' => 0,
'media_type' => 'image'
],
[
'file' => '/m/a/magento_thumbnail.jpg',
'position' => 2,
'label' => 'Thumbnail Image',
'disabled' => 0,
'media_type' => 'image'
],
]])
->setCanSaveCustomOptions(true)
->save();
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

require __DIR__ . '/product_with_image_rollback.php';

0 comments on commit 76a0af9

Please sign in to comment.