Skip to content

Commit

Permalink
Merge branch '2.4-develop' of https://github.com/mage-os/mirror-magento2
Browse files Browse the repository at this point in the history
 into 2.4-develop
  • Loading branch information
mage-os-ci committed Jun 14, 2024
2 parents 5192a8c + 2e08b17 commit fe6755b
Show file tree
Hide file tree
Showing 3 changed files with 128 additions and 139 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,25 @@
use Magento\Cms\Api\Data\BlockInterface;
use Magento\Cms\Model\Block;
use Magento\CmsGraphQl\Model\Resolver\Blocks;
use Magento\Framework\Exception\LocalizedException;
use Magento\GraphQlResolverCache\Model\Resolver\Result\CacheKey\Calculator\ProviderInterface;
use Magento\GraphQlResolverCache\Model\Resolver\Result\Type as GraphQlResolverCache;
use Magento\Store\Model\StoreManagerInterface;
use Magento\Store\Test\Fixture\Store;
use Magento\Cms\Test\Fixture\Block as BlockFixture;
use Magento\TestFramework\Fixture\Config;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\ObjectManager;
use Magento\TestFramework\TestCase\GraphQl\ResolverCacheAbstract;
use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException;
use Magento\Widget\Model\Template\FilterEmulate;

/**
* Test for cms block resolver cache
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class BlockTest extends ResolverCacheAbstract
{
Expand All @@ -44,24 +53,29 @@ class BlockTest extends ResolverCacheAbstract
*/
private $storeManager;

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

protected function setUp(): void
{
$objectManager = ObjectManager::getInstance();
$this->blockRepository = $objectManager->get(BlockRepositoryInterface::class);
$this->graphQlResolverCache = $objectManager->get(GraphQlResolverCache::class);
$this->widgetFilter = $objectManager->get(FilterEmulate::class);
$this->storeManager = $objectManager->get(StoreManagerInterface::class);
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();

parent::setUp();
}

/**
* @magentoConfigFixture default_store web/seo/use_rewrites 1
* @magentoDataFixture Magento/Cms/_files/blocks.php
*/
#[
DataFixture(BlockFixture::class, as: 'guest_block')
]
public function testCmsSingleBlockResolverCacheAndInvalidationAsGuest()
{
$block = $this->blockRepository->getById('enabled_block');
$block = $this->fixtures->get('guest_block');

$query = $this->getQuery([
$block->getIdentifier(),
Expand Down Expand Up @@ -94,15 +108,13 @@ public function testCmsSingleBlockResolverCacheAndInvalidationAsGuest()
);
}

/**
* @magentoConfigFixture default_store web/seo/use_rewrites 1
* @magentoDataFixture Magento/Cms/_files/block.php
* @magentoDataFixture Magento/Cms/_files/blocks.php
*/
#[
DataFixture(BlockFixture::class, as: 'block', count: 2)
]
public function testCmsMultipleBlockResolverCacheAndInvalidationAsGuest()
{
$block1 = $this->blockRepository->getById('enabled_block');
$block2 = $this->blockRepository->getById('fixture_block');
$block1 = $this->fixtures->get('block1');
$block2 = $this->fixtures->get('block2');

$query = $this->getQuery([
$block1->getIdentifier(),
Expand Down Expand Up @@ -139,13 +151,12 @@ public function testCmsMultipleBlockResolverCacheAndInvalidationAsGuest()
);
}

/**
* @magentoConfigFixture default_store web/seo/use_rewrites 1
* @magentoDataFixture Magento/Cms/_files/block.php
*/
#[
DataFixture(BlockFixture::class, as: 'deleted_block')
]
public function testCmsBlockResolverCacheInvalidatesWhenBlockGetsDeleted()
{
$block = $this->blockRepository->getById('fixture_block');
$block = $this->fixtures->get('deleted_block');

$query = $this->getQuery([
$block->getIdentifier(),
Expand Down Expand Up @@ -177,13 +188,12 @@ public function testCmsBlockResolverCacheInvalidatesWhenBlockGetsDeleted()
);
}

/**
* @magentoConfigFixture default_store web/seo/use_rewrites 1
* @magentoDataFixture Magento/Cms/_files/blocks.php
*/
#[
DataFixture(BlockFixture::class, as: 'enabled_block')
]
public function testCmsBlockResolverCacheInvalidatesWhenBlockGetsDisabled()
{
$block = $this->blockRepository->getById('enabled_block');
$block = $this->fixtures->get('enabled_block');

$query = $this->getQuery([
$block->getIdentifier(),
Expand Down Expand Up @@ -217,14 +227,20 @@ public function testCmsBlockResolverCacheInvalidatesWhenBlockGetsDisabled()
}

/**
* @magentoConfigFixture default_store web/seo/use_rewrites 1
* @magentoDataFixture Magento/Cms/_files/block.php
* @magentoDataFixture Magento/Store/_files/second_store.php
* @throws LocalizedException
* @throws \Zend_Cache_Exception
*/
#[
DataFixture(BlockFixture::class, as: 'block'),
DataFixture(Store::class, as: 'second_store'),
]
public function testCmsBlockResolverCacheIsInvalidatedAfterChangingItsStoreView()
{
/** @var Block $block */
$block = $this->blockRepository->getById('fixture_block');
$block = $this->fixtures->get('block');

/** @var \Magento\Store\Model\Store $store */
$store = $this->fixtures->get('second_store');

$query = $this->getQuery([
$block->getIdentifier(),
Expand All @@ -238,17 +254,17 @@ public function testCmsBlockResolverCacheIsInvalidatedAfterChangingItsStoreView(
$cacheEntryDecoded = json_decode($cacheEntry, true);

$this->assertEqualsCanonicalizing(
$this->generateExpectedDataFromBlocks([$block]),
$cacheEntryDecoded
$cacheEntryDecoded,
$this->generateExpectedDataFromBlocks([$block])
);

$this->assertTagsByCacheIdentityAndBlocks(
$cacheKey,
(string)$cacheKey,
[$block]
);

// assert that cache is invalidated after changing block's store view
$secondStoreViewId = $this->storeManager->getStore('fixture_second_store')->getId();
$secondStoreViewId = $store->getId();
$block->setStoreId($secondStoreViewId);
$this->blockRepository->save($block);

Expand Down Expand Up @@ -283,15 +299,14 @@ public function testCmsBlockResolverCacheDoesNotSaveNonExistentCmsBlock()
);
}

/**
* @magentoConfigFixture default/system/full_page_cache/caching_application 2
* @magentoDataFixture Magento/Cms/_files/block.php
* @magentoDataFixture Magento/Cms/_files/blocks.php
*/
#[
Config('system/full_page_cache/caching_application', '2'),
DataFixture(BlockFixture::class, as: 'block', count: 2)
]
public function testCmsBlockResolverCacheRetainsEntriesThatHaveNotBeenUpdated()
{
// query block1
$block1 = $this->blockRepository->getById('fixture_block');
$block1 = $this->fixtures->get('block1');

$queryBlock1 = $this->getQuery([
$block1->getIdentifier(),
Expand All @@ -302,7 +317,7 @@ public function testCmsBlockResolverCacheRetainsEntriesThatHaveNotBeenUpdated()
$cacheKeyBlock1 = $this->getResolverCacheKeyFromBlocks([$block1]);

// query block2
$block2 = $this->blockRepository->getById('enabled_block');
$block2 = $this->fixtures->get('block2');

$queryBlock2 = $this->getQuery([
$block2->getIdentifier(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,22 @@

namespace Magento\GraphQl\Quote\Customer;

use Magento\Catalog\Test\Fixture\Product;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Test\Fixture\Customer;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\GraphQl\Quote\GetMaskedQuoteIdByReservedOrderId;
use Magento\Integration\Api\CustomerTokenServiceInterface;
use Magento\Quote\Model\Quote;
use Magento\Quote\Model\QuoteFactory;
use Magento\Quote\Model\QuoteIdToMaskedQuoteIdInterface;
use Magento\Quote\Model\ResourceModel\Quote as QuoteResource;
use Magento\Quote\Test\Fixture\AddProductToCart;
use Magento\Quote\Test\Fixture\GuestCart;
use Magento\TestFramework\Fixture\DataFixture;
use Magento\TestFramework\Fixture\DataFixtureStorage;
use Magento\TestFramework\Fixture\DataFixtureStorageManager;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\TestCase\GraphQlAbstract;

Expand Down Expand Up @@ -61,6 +69,11 @@ class SetBillingAddressOnCartTest extends GraphQlAbstract
*/
private $customerRepository;

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

protected function setUp(): void
{
$objectManager = Bootstrap::getObjectManager();
Expand All @@ -72,6 +85,7 @@ protected function setUp(): void
$this->customerAddressRepository = $objectManager->get(AddressRepositoryInterface::class);
$this->searchCriteriaBuilder = $objectManager->get(SearchCriteriaBuilder::class);
$this->customerRepository = $objectManager->get(CustomerRepositoryInterface::class);
$this->fixtures = $objectManager->get(DataFixtureStorageManager::class)->getStorage();
}

/**
Expand Down Expand Up @@ -715,25 +729,45 @@ public function testSetBillingAddressToAnotherCustomerCart()
* Tests that a logged-in customer cannot use a saved customer address that is not their own.
*
* _security
* @magentoApiDataFixture Magento/Customer/_files/three_customers.php
* @magentoApiDataFixture Magento/Customer/_files/customer_address.php
* @magentoApiDataFixture Magento/Checkout/_files/quote_with_simple_product_saved.php
*
*/
#[
DataFixture(Customer::class, ['addresses' => [['postcode' => '12345']]], as: 'customer', count: 2),
DataFixture(Product::class, as: 'product'),
DataFixture(GuestCart::class, ['customer_email' => '$customer2.email$'], as: 'cart'),
DataFixture(AddProductToCart::class, [
'cart_id' => '$cart.id$',
'product_id' => '$product.id$',
'qty' => 1
])
]
public function testSetBillingAddressIfCustomerIsNotOwnerOfAddress()
{
$this->expectException(\Exception::class);
$this->expectExceptionMessage('Current customer does not have permission to address with ID "1"');
/** @var \Magento\Customer\Model\Customer $customer1 */
$customer1 = $this->fixtures->get('customer1');
$customer1AddressId = current($customer1->getAddresses())->getId();
$customer2 = $this->fixtures->get('customer2');
$customer2 = $this->customerRepository->getById($customer2->getId());

$maskedQuoteId = $this->assignQuoteToCustomer('test_order_with_simple_product_without_address', 2);
/** @var Quote $quote */
$quote = $this->fixtures->get('cart');
$maskedQuoteId = $this->quoteIdToMaskedId->execute((int)$quote->getId());

$quote->setCustomer($customer2);
$quote->setCustomerIsGuest(false);
$quote->save();

$this->expectException(\Exception::class);
$this->expectExceptionMessage(
'Current customer does not have permission to address with ID "'.$customer1AddressId.'"'
);

$query = <<<QUERY
mutation {
setBillingAddressOnCart(
input: {
cart_id: "$maskedQuoteId"
billing_address: {
customer_address_id: 1
customer_address_id: "$customer1AddressId"
}
}
) {
Expand All @@ -745,7 +779,7 @@ public function testSetBillingAddressIfCustomerIsNotOwnerOfAddress()
}
}
QUERY;
$this->graphQlMutation($query, [], '', $this->getHeaderMap('customer2@search.example.com'));
$this->graphQlMutation($query, [], '', $this->getHeaderMap($customer2->getEmail()));
}

/**
Expand Down
Loading

0 comments on commit fe6755b

Please sign in to comment.