Skip to content

Commit

Permalink
Merge pull request #6302 from magento-honey-badgers/HB-PR-delivery-Oct
Browse files Browse the repository at this point in the history
[honey] PR
  • Loading branch information
zakdma committed Nov 3, 2020
2 parents a7bde28 + 8311af5 commit 632a7c6
Show file tree
Hide file tree
Showing 15 changed files with 393 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function build(AggregationInterface $aggregation, ?int $storeId): array
);
}

return [$result];
return [self::PRICE_BUCKET => $result];
}

/**
Expand Down
27 changes: 24 additions & 3 deletions app/code/Magento/CatalogGraphQl/Model/Resolver/Aggregations.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@

namespace Magento\CatalogGraphQl\Model\Resolver;

use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\LayerBuilder;
use Magento\Directory\Model\PriceCurrency;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\LayerBuilder;
use Magento\Store\Api\Data\StoreInterface;

/**
Expand All @@ -28,16 +30,24 @@ class Aggregations implements ResolverInterface
*/
private $layerBuilder;

/**
* @var PriceCurrency
*/
private $priceCurrency;

/**
* @param \Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider
* @param LayerBuilder $layerBuilder
* @param PriceCurrency $priceCurrency
*/
public function __construct(
\Magento\CatalogGraphQl\Model\Resolver\Layer\DataProvider\Filters $filtersDataProvider,
LayerBuilder $layerBuilder
LayerBuilder $layerBuilder,
PriceCurrency $priceCurrency = null
) {
$this->filtersDataProvider = $filtersDataProvider;
$this->layerBuilder = $layerBuilder;
$this->priceCurrency = $priceCurrency ?: ObjectManager::getInstance()->get(PriceCurrency::class);
}

/**
Expand All @@ -60,7 +70,18 @@ public function resolve(
/** @var StoreInterface $store */
$store = $context->getExtensionAttributes()->getStore();
$storeId = (int)$store->getId();
return $this->layerBuilder->build($aggregations, $storeId);
$results = $this->layerBuilder->build($aggregations, $storeId);
if (isset($results['price_bucket'])) {
foreach ($results['price_bucket']['options'] as &$value) {
list($from, $to) = explode('-', $value['label']);
$newLabel = $this->priceCurrency->convertAndRound($from)
. '-'
. $this->priceCurrency->convertAndRound($to);
$value['label'] = $newLabel;
$value['value'] = str_replace('-', '_', $newLabel);
}
}
return $results;
} else {
return [];
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Magento\CatalogGraphQl\Model\Resolver\Category\DataProvider;

use Magento\Catalog\Api\Data\CategoryInterface;
use Magento\Catalog\Model\ResourceModel\Category\CollectionFactory;

/**
Expand Down Expand Up @@ -46,6 +47,7 @@ public function getData(string $categoryPath): array
$collection = $this->collectionFactory->create();
$collection->addAttributeToSelect(['name', 'url_key', 'url_path']);
$collection->addAttributeToFilter('entity_id', $parentCategoryIds);
$collection->addAttributeToFilter(CategoryInterface::KEY_IS_ACTIVE, 1);

foreach ($collection as $category) {
$breadcrumbsData[] = [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

namespace Magento\CatalogGraphQl\Model\Resolver\Category;

use Magento\Catalog\Model\Category;
use Magento\Catalog\Model\Category\FileInfo;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\Framework\Exception\LocalizedException;
use Magento\Framework\UrlInterface;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Framework\Filesystem\DirectoryList;
use Magento\Catalog\Model\Category\FileInfo;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;

/**
* Resolve category image to a fully qualified URL
Expand Down Expand Up @@ -52,15 +54,15 @@ public function resolve(
if (!isset($value['model'])) {
throw new LocalizedException(__('"model" value should be specified'));
}
/** @var \Magento\Catalog\Model\Category $category */
/** @var Category $category */
$category = $value['model'];
$imagePath = $category->getData('image');
if (empty($imagePath)) {
return null;
}
/** @var StoreInterface $store */
$store = $context->getExtensionAttributes()->getStore();
$baseUrl = $store->getBaseUrl();
$baseUrl = $store->getBaseUrl(UrlInterface::URL_TYPE_WEB);

$filenameWithMedia = $this->fileInfo->isBeginsWithMediaDirectoryPath($imagePath)
? $imagePath : $this->formatFileNameWithMediaCategoryFolder($imagePath);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ public function getList(
$searchResults = $this->searchResultsFactory->create();
$searchResults->setSearchCriteria($searchCriteriaForCollection);
$searchResults->setItems($collection->getItems());
$searchResults->setTotalCount($searchResult->getTotalCount());
$searchResults->setTotalCount($collection->getSize());
return $searchResults;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ public function __construct(
*/
public function apply(Filter $filter, AbstractDb $collection)
{
$conditionType = $filter->getConditionType();
if ($conditionType !== 'eq') {
return true;
}

$categoryIds = $filter->getValue();
if (!is_array($categoryIds)) {
$categoryIds = [$categoryIds];
Expand Down
1 change: 1 addition & 0 deletions app/code/Magento/CatalogGraphQl/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"magento/module-eav": "*",
"magento/module-catalog": "*",
"magento/module-catalog-inventory": "*",
"magento/module-directory": "*",
"magento/module-search": "*",
"magento/module-store": "*",
"magento/module-eav-graph-ql": "*",
Expand Down
90 changes: 74 additions & 16 deletions app/code/Magento/QuoteGraphQl/Model/Resolver/MergeCarts.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,24 @@

namespace Magento\QuoteGraphQl\Model\Resolver;

use Magento\Framework\App\ObjectManager;
use Magento\Framework\Exception\CouldNotSaveException;
use Magento\Framework\GraphQl\Config\Element\Field;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Framework\GraphQl\Exception\GraphQlInputException;
use Magento\Framework\GraphQl\Exception\GraphQlNoSuchEntityException;
use Magento\Framework\GraphQl\Query\ResolverInterface;
use Magento\Framework\GraphQl\Schema\Type\ResolveInfo;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\GraphQl\Model\Query\ContextInterface;
use Magento\Framework\GraphQl\Exception\GraphQlAuthorizationException;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Cart\CustomerCartResolver;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\QuoteGraphQl\Model\Cart\GetCartForUser;

/**
* Merge Carts Resolver
*
* @SuppressWarnings(PHPMD.LongVariable)
*/
class MergeCarts implements ResolverInterface
{
Expand All @@ -31,44 +38,95 @@ class MergeCarts implements ResolverInterface
*/
private $cartRepository;

/**
* @var CustomerCartResolver
*/
private $customerCartResolver;

/**
* @var QuoteIdToMaskedQuoteIdInterface
*/
private $quoteIdToMaskedQuoteId;

/**
* @param GetCartForUser $getCartForUser
* @param CartRepositoryInterface $cartRepository
* @param CustomerCartResolver|null $customerCartResolver
* @param QuoteIdToMaskedQuoteIdInterface|null $quoteIdToMaskedQuoteId
*/
public function __construct(
GetCartForUser $getCartForUser,
CartRepositoryInterface $cartRepository
CartRepositoryInterface $cartRepository,
CustomerCartResolver $customerCartResolver = null,
QuoteIdToMaskedQuoteIdInterface $quoteIdToMaskedQuoteId = null
) {
$this->getCartForUser = $getCartForUser;
$this->cartRepository = $cartRepository;
$this->customerCartResolver = $customerCartResolver
?: ObjectManager::getInstance()->get(CustomerCartResolver::class);
$this->quoteIdToMaskedQuoteId = $quoteIdToMaskedQuoteId
?: ObjectManager::getInstance()->get(QuoteIdToMaskedQuoteIdInterface::class);
}

/**
* @inheritdoc
*/
public function resolve(Field $field, $context, ResolveInfo $info, array $value = null, array $args = null)
{
public function resolve(
Field $field,
$context,
ResolveInfo $info,
array $value = null,
array $args = null
) {
if (empty($args['source_cart_id'])) {
throw new GraphQlInputException(__('Required parameter "source_cart_id" is missing'));
}

if (empty($args['destination_cart_id'])) {
throw new GraphQlInputException(__('Required parameter "destination_cart_id" is missing'));
throw new GraphQlInputException(__(
'Required parameter "source_cart_id" is missing'
));
}

/** @var ContextInterface $context */
if (false === $context->getExtensionAttributes()->getIsCustomer()) {
throw new GraphQlAuthorizationException(__('The current customer isn\'t authorized.'));
throw new GraphQlAuthorizationException(__(
'The current customer isn\'t authorized.'
));
}
$currentUserId = $context->getUserId();

if (!isset($args['destination_cart_id'])) {
try {
$cart = $this->customerCartResolver->resolve($currentUserId);
} catch (CouldNotSaveException $exception) {
throw new GraphQlNoSuchEntityException(
__('Could not create empty cart for customer'),
$exception
);
}
$customerMaskedCartId = $this->quoteIdToMaskedQuoteId->execute(
(int) $cart->getId()
);
} else {
if (empty($args['destination_cart_id'])) {
throw new GraphQlInputException(__(
'The parameter "destination_cart_id" cannot be empty'
));
}
}

$guestMaskedCartId = $args['source_cart_id'];
$customerMaskedCartId = $args['destination_cart_id'];
$customerMaskedCartId = $customerMaskedCartId ?? $args['destination_cart_id'];

$currentUserId = $context->getUserId();
$storeId = (int)$context->getExtensionAttributes()->getStore()->getId();
// passing customerId as null enforces source cart should always be a guestcart
$guestCart = $this->getCartForUser->execute($guestMaskedCartId, null, $storeId);
$customerCart = $this->getCartForUser->execute($customerMaskedCartId, $currentUserId, $storeId);
$guestCart = $this->getCartForUser->execute(
$guestMaskedCartId,
null,
$storeId
);
$customerCart = $this->getCartForUser->execute(
$customerMaskedCartId,
$currentUserId,
$storeId
);
$customerCart->merge($guestCart);
$guestCart->setIsActive(false);
$this->cartRepository->save($customerCart);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/QuoteGraphQl/etc/schema.graphqls
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ type Mutation {
setPaymentMethodOnCart(input: SetPaymentMethodOnCartInput): SetPaymentMethodOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentMethodOnCart")
setGuestEmailOnCart(input: SetGuestEmailOnCartInput): SetGuestEmailOnCartOutput @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\SetGuestEmailOnCart")
setPaymentMethodAndPlaceOrder(input: SetPaymentMethodAndPlaceOrderInput): PlaceOrderOutput @deprecated(reason: "Should use setPaymentMethodOnCart and placeOrder mutations in single request.") @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\SetPaymentAndPlaceOrder")
mergeCarts(source_cart_id: String!, destination_cart_id: String!): Cart! @doc(description:"Merges the source cart into the destination cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
mergeCarts(source_cart_id: String!, destination_cart_id: String): Cart! @doc(description:"Merges the source cart into the destination cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\MergeCarts")
placeOrder(input: PlaceOrderInput): PlaceOrderOutput @resolver(class: "\\Magento\\QuoteGraphQl\\Model\\Resolver\\PlaceOrder")
addProductsToCart(cartId: String!, cartItems: [CartItemInput!]!): AddProductsToCartOutput @doc(description:"Add any type of product to the cart") @resolver(class: "Magento\\QuoteGraphQl\\Model\\Resolver\\AddProductsToCart")
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -395,8 +395,9 @@ public function testMinimumMatchQueryLength()
* Test category image full name is returned
*
* @magentoApiDataFixture Magento/Catalog/_files/catalog_category_with_long_image_name.php
* @magentoConfigFixture default_store web/seo/use_rewrites 0
*/
public function testCategoryImageName()
public function testCategoryImageNameAndSeoDisabled()
{
/** @var CategoryCollection $categoryCollection */
$categoryCollection = Bootstrap::getObjectManager()->get(CategoryCollection::class);
Expand Down Expand Up @@ -427,14 +428,13 @@ public function testCategoryImageName()
$categories = $response['categories'];
$this->assertArrayNotHasKey('errors', $response);
$this->assertNotEmpty($response['categories']['items']);
$expectedImageUrl = str_replace('index.php/', '', $expectedImageUrl);
$categories['items'][0]['image'] = str_replace('index.php/', '', $categories['items'][0]['image']);
$this->assertEquals('Parent Image Category', $categories['items'][0]['name']);
$this->assertEquals($expectedImageUrl, $categories['items'][0]['image']);
}

/**
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
* @magentoConfigFixture default_store web/seo/use_rewrites 1
*/
public function testFilterByUrlPathTopLevelCategory()
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -660,6 +660,45 @@ public function testCategoryImage(?string $imagePrefix)
$this->assertEquals($expectedImageUrl, $childCategory['image']);
}

/**
* Testing breadcrumbs that shouldn't include disabled parent categories
*
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
*/
public function testBreadCrumbsWithDisabledParentCategory()
{
$parentCategoryId = 4;
$childCategoryId = 5;
$category = $this->categoryRepository->get($parentCategoryId);
$category->setIsActive(false);
$this->categoryRepository->save($category);

$query = <<<QUERY
{
category(id: {$childCategoryId}) {
name
breadcrumbs {
category_id
category_name
}
}
}
QUERY;
$response = $this->graphQlQuery($query);
$expectedResponse = [
'category' => [
'name' => 'Category 1.1.1',
'breadcrumbs' => [
[
'category_id' => 3,
'category_name' => "Category 1",
]
]
]
];
$this->assertEquals($expectedResponse, $response);
}

/**
* @return array
*/
Expand Down
Loading

0 comments on commit 632a7c6

Please sign in to comment.