Skip to content

Commit

Permalink
Merge branch '2.4-develop' into L3-PR-2024-02-16
Browse files Browse the repository at this point in the history
  • Loading branch information
magento-devops-queue-mgr-svc authored Mar 19, 2024
2 parents c3615e1 + 12e071c commit 7bb84a1
Show file tree
Hide file tree
Showing 6 changed files with 278 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@
namespace Magento\CatalogGraphQl\DataProvider\Product\LayeredNavigation\Builder\Aggregations\Category;

use Magento\Catalog\Api\CategoryListInterface;
use Magento\Catalog\Model\Config\LayerCategoryConfig;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\App\ObjectManager;
use Magento\Framework\ObjectManager\ResetAfterRequestInterface;
use Magento\Framework\Search\Response\Aggregation;
use Magento\Framework\Search\Response\AggregationFactory;
Expand Down Expand Up @@ -61,25 +63,34 @@ class IncludeDirectChildrenOnly implements ResetAfterRequestInterface
*/
private $searchCriteriaBuilder;

/**
* @var LayerCategoryConfig|null
*/
private $layerCategoryConfig;

/**
* @param AggregationFactory $aggregationFactory
* @param BucketFactory $bucketFactory
* @param StoreManagerInterface $storeManager
* @param CategoryListInterface $categoryList
* @param SearchCriteriaBuilder $searchCriteriaBuilder
* @param LayerCategoryConfig|null $layerCategoryConfig
*/
public function __construct(
AggregationFactory $aggregationFactory,
BucketFactory $bucketFactory,
StoreManagerInterface $storeManager,
CategoryListInterface $categoryList,
SearchCriteriaBuilder $searchCriteriaBuilder
SearchCriteriaBuilder $searchCriteriaBuilder,
?LayerCategoryConfig $layerCategoryConfig = null
) {
$this->aggregationFactory = $aggregationFactory;
$this->bucketFactory = $bucketFactory;
$this->storeManager = $storeManager;
$this->categoryList = $categoryList;
$this->searchCriteriaBuilder = $searchCriteriaBuilder;
$this->layerCategoryConfig = $layerCategoryConfig ?? ObjectManager::getInstance()
->get(LayerCategoryConfig::class);
}

/**
Expand All @@ -91,28 +102,34 @@ public function __construct(
*/
public function filter(AggregationInterface $aggregation, ?int $storeId): Aggregation
{
$categoryIdsRequested = $this->filter['category'] ?? null;
if ($categoryIdsRequested === null) {
return $aggregation;
}
$buckets = $aggregation->getBuckets();
$categoryBucket = $buckets[self::CATEGORY_BUCKET] ?? null;
if ($categoryBucket === null || empty($categoryBucket->getValues())) {
return $aggregation;
if (!$this->layerCategoryConfig->isCategoryFilterVisibleInLayerNavigation()) {
$buckets = $aggregation->getBuckets();
unset($buckets[self::CATEGORY_BUCKET]);
} else {
$categoryIdsRequested = $this->filter['category'] ?? null;
if ($categoryIdsRequested === null) {
return $aggregation;
}
$buckets = $aggregation->getBuckets();
$categoryBucket = $buckets[self::CATEGORY_BUCKET] ?? null;
if ($categoryBucket === null || empty($categoryBucket->getValues())) {
return $aggregation;
}
$categoryIdsRequested = is_array($categoryIdsRequested) ? $categoryIdsRequested : [$categoryIdsRequested];
$bucketValuesFiltered = $this->filterBucketValues(
$categoryBucket->getValues(),
$categoryIdsRequested,
$storeId
);
$categoryBucketResolved = $this->bucketFactory->create(
[
'name' => self::CATEGORY_BUCKET,
'values' => $bucketValuesFiltered
]
);
$buckets[self::CATEGORY_BUCKET] = $categoryBucketResolved;
}
$categoryIdsRequested = is_array($categoryIdsRequested) ? $categoryIdsRequested : [$categoryIdsRequested];
$bucketValuesFiltered = $this->filterBucketValues(
$categoryBucket->getValues(),
$categoryIdsRequested,
$storeId
);
$categoryBucketResolved = $this->bucketFactory->create(
[
'name' => self::CATEGORY_BUCKET,
'values' => $bucketValuesFiltered
]
);
$buckets[self::CATEGORY_BUCKET] = $categoryBucketResolved;

return $this->aggregationFactory->create([self::BUCKETS_NAME => $buckets]);
}

Expand Down
27 changes: 24 additions & 3 deletions app/code/Magento/Sales/Model/ResourceModel/Order/Address.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
*/
namespace Magento\Sales\Model\ResourceModel\Order;

use Magento\Framework\Model\AbstractModel;
use Magento\Sales\Model\ResourceModel\EntityAbstract as SalesResource;
use Magento\Sales\Model\Spi\OrderAddressResourceInterface;
use Magento\Framework\Model\ResourceModel\Db\VersionControl\Snapshot;
Expand All @@ -15,7 +16,7 @@
class Address extends SalesResource implements OrderAddressResourceInterface
{
/**
* Event prefix
* Sales order address event prefix
*
* @var string
*/
Expand All @@ -33,10 +34,10 @@ class Address extends SalesResource implements OrderAddressResourceInterface

/**
* @param \Magento\Framework\Model\ResourceModel\Db\Context $context
* @param \Magento\Sales\Model\ResourceModel\Attribute $attribute
* @param \Magento\SalesSequence\Model\Manager $sequenceManager
* @param Snapshot $entitySnapshot
* @param \Magento\Framework\Model\ResourceModel\Db\VersionControl\RelationComposite $entityRelationComposite
* @param \Magento\Sales\Model\ResourceModel\Attribute $attribute
* @param \Magento\SalesSequence\Model\Manager $sequenceManager
* @param \Magento\Sales\Model\Order\Address\Validator $validator
* @param \Magento\Sales\Model\ResourceModel\GridPool $gridPool
* @param string $connectionName
Expand Down Expand Up @@ -122,4 +123,24 @@ protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
}
return $this;
}

/**
* @inheritdoc
*/
protected function isModified(AbstractModel $entity): bool
{
if (!$entity->getId()) {
return true;
}
$snapShotData = $this->entitySnapshot->getSnapshotData($entity);
foreach ($snapShotData as $field => $value) {
$fieldValue = $entity->getDataByKey($field);
if (is_numeric($fieldValue) && is_numeric($value)) {
if ($fieldValue !== $value) {
return true;
}
}
}
return parent::isModified($entity);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,6 @@ public function testSave()
->method('validate')
->with($this->addressMock)
->willReturn([]);
$this->entitySnapshotMock->expects($this->once())
->method('isModified')
->with($this->addressMock)
->willReturn(true);
$this->addressMock->expects($this->once())
->method('getParentId')
->willReturn(1);
Expand All @@ -116,10 +112,6 @@ public function testSaveValidationFailed()
{
$this->expectException('Magento\Framework\Exception\LocalizedException');
$this->expectExceptionMessage('We can\'t save the address:');
$this->entitySnapshotMock->expects($this->once())
->method('isModified')
->with($this->addressMock)
->willReturn(true);
$this->addressMock->expects($this->any())
->method('hasDataChanges')
->willReturn(true);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,92 @@ private function getGraphQlQuery(string $categoryList, string $includeDirectChil
}
}
}
QUERY;
}

/**
* Test the categories that appear in aggregation Layered Navigation > Display Category Filter => Yes (default).
*
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
* @throws \Exception
*/
public function testFetchCategoriesWhenDisplayCategoryEnabled(): void
{
$result = $this->aggregationWithDisplayCategorySetting();
$aggregationAttributeCode = [];
foreach ($result['products']['aggregations'] as $aggregation) {
$this->assertArrayHasKey('attribute_code', $aggregation);
$aggregationAttributeCode[] = $aggregation['attribute_code'];
}
$this->assertTrue(in_array('category_uid', $aggregationAttributeCode));
}

/**
* Test the categories not in aggregation when Layered Navigation > Display Category Filter => No.
*
* @magentoConfigFixture catalog/layered_navigation/display_category 0
* @magentoApiDataFixture Magento/Catalog/_files/categories.php
* @throws \Exception
*/
public function testDontFetchCategoriesWhenDisplayCategoryDisabled(): void
{
$result = $this->aggregationWithDisplayCategorySetting();
$aggregationAttributeCode = [];
foreach ($result['products']['aggregations'] as $aggregation) {
$this->assertArrayHasKey('attribute_code', $aggregation);
$aggregationAttributeCode[] = $aggregation['attribute_code'];
}
$this->assertFalse(in_array('category_uid', $aggregationAttributeCode));
}

/**
* @return array
* @throws \Exception
*/
private function aggregationWithDisplayCategorySetting(): array
{
$query = $this->getGraphQlQueryProductSearch();
$result = $this->graphQlQuery($query);

$this->assertArrayNotHasKey('errors', $result);
$this->assertArrayHasKey('aggregations', $result['products']);
return $result;
}

/**
* Get graphQl query.
*
* @return string
*/
private function getGraphQlQueryProductSearch(): string
{
return <<<QUERY
{
products(
search: "simple"
pageSize: 20
currentPage: 1
sort: { }
) {
items {
sku
canonical_url
categories{
name
path
}
}
aggregations (filter: {category: {includeDirectChildrenOnly: true}}) {
attribute_code
count
label
options {
label
value
}
}
}
}
QUERY;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
<?php
/**
* Copyright 2023 Adobe
* All Rights Reserved.
*
* NOTICE: All information contained herein is, and remains
* the property of Adobe and its suppliers, if any. The intellectual
* and technical concepts contained herein are proprietary to Adobe
* and its suppliers and are protected by all applicable intellectual
* property laws, including trade secret and copyright laws.
* Dissemination of this information or reproduction of this material
* is strictly forbidden unless prior written permission is obtained
* from Adobe.
*/
declare(strict_types=1);

namespace Magento\Sales\Model\ResourceModel\Order;

use Magento\Catalog\Test\Fixture\Product as ProductFixture;
use Magento\Checkout\Test\Fixture\PlaceOrder as PlaceOrderFixture;
use Magento\Checkout\Test\Fixture\SetBillingAddress;
use Magento\Checkout\Test\Fixture\SetDeliveryMethod as SetDeliveryMethodFixture;
use Magento\Checkout\Test\Fixture\SetPaymentMethod as SetPaymentMethodFixture;
use Magento\Checkout\Test\Fixture\SetShippingAddress;
use Magento\Customer\Test\Fixture\Customer;
use Magento\Framework\Exception\LocalizedException;
use Magento\Quote\Test\Fixture\AddProductToCart;
use Magento\Quote\Test\Fixture\CustomerCart;
use Magento\Sales\Api\OrderAddressRepositoryInterface;
use Magento\Sales\Api\OrderRepositoryInterface;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Fixture\DbIsolation;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\TestCase;

/**
* Test for order address
*
* @magentoAppArea adminhtml
*/
class AddressTest extends TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;

/**
* @var DataFixtureStorage
*/
private $fixtures;

/**
* @var OrderAddressRepositoryInterface
*/
private $orderAddressRepository;

/**
* @var OrderRepositoryInterface
*/
private $orderRepository;

protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->fixtures = $this->objectManager->get(DataFixtureStorageManager::class)->getStorage();
$this->orderAddressRepository = $this->objectManager->get(OrderAddressRepositoryInterface::class);
$this->orderRepository = $this->objectManager->get(OrderRepositoryInterface::class);
}

/**
* Check to see if leading zeros have been added to the number strings
* in the order address's phone field.
*
* @return void
* @throws LocalizedException
*/
#[
DataFixture(ProductFixture::class, as: 'product'),
DataFixture(Customer::class, as: 'customer'),
DataFixture(CustomerCart::class, ['customer_id' => '$customer.id$'], as: 'quote'),
DataFixture(AddProductToCart::class, ['cart_id' => '$quote.id$', 'product_id' => '$product.id$', 'qty' => 1]),
DataFixture(SetBillingAddress::class, [
'cart_id' => '$quote.id$',
'address' => [
'customer_id' => '$customer.id$',
'telephone' => '009999999999'
]
]),
DataFixture(SetShippingAddress::class, ['cart_id' => '$quote.id$']),
DataFixture(SetDeliveryMethodFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(SetPaymentMethodFixture::class, ['cart_id' => '$quote.id$']),
DataFixture(PlaceOrderFixture::class, ['cart_id' => '$quote.id$'], 'order')
]
public function testOrderAddressUpdateWithTelephone(): void
{
$telephoneValue = '9999999999';
$order = $this->fixtures->get('order');
$address = $this->orderAddressRepository->get($order->getBillingAddressId());
$address->setTelephone($telephoneValue);
$this->orderAddressRepository->save($address);
$updatedOrder = $this->orderRepository->get($order->getId());
$billingAddress = $updatedOrder->getBillingAddress();
$updatedTelephoneValue = $billingAddress->getTelephone();
$this->assertEquals($telephoneValue, $updatedTelephoneValue);
}
}
Loading

0 comments on commit 7bb84a1

Please sign in to comment.