|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\GraphQl\Catalog; |
| 9 | + |
| 10 | +use Magento\Catalog\Api\ProductRepositoryInterface; |
| 11 | +use Magento\TestFramework\ObjectManager; |
| 12 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test that product is not present in GQL after it was deleted |
| 16 | + */ |
| 17 | +class ProductDeleteTest extends GraphQlAbstract |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var \Magento\TestFramework\ObjectManager |
| 21 | + */ |
| 22 | + private $objectManager; |
| 23 | + |
| 24 | + protected function setUp(): void |
| 25 | + { |
| 26 | + $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); |
| 27 | + } |
| 28 | + |
| 29 | + /** |
| 30 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 31 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_all_fields.php |
| 32 | + */ |
| 33 | + public function testQuerySimpleProductAfterDelete() |
| 34 | + { |
| 35 | + $productSku = 'simple'; |
| 36 | + |
| 37 | + $query = <<<QUERY |
| 38 | +{ |
| 39 | + products(filter: {sku: {eq: "{$productSku}"}}) |
| 40 | + { |
| 41 | + items { |
| 42 | + attribute_set_id |
| 43 | + } |
| 44 | + } |
| 45 | +} |
| 46 | +QUERY; |
| 47 | + // get customer ID token |
| 48 | + /** @var \Magento\Integration\Api\CustomerTokenServiceInterface $customerTokenService */ |
| 49 | + $customerTokenService = $this->objectManager->create( |
| 50 | + \Magento\Integration\Api\CustomerTokenServiceInterface::class |
| 51 | + ); |
| 52 | + $customerToken = $customerTokenService-> createCustomerAccessToken( '[email protected]', 'password'); |
| 53 | + |
| 54 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 55 | + $response = $this->graphQlQuery($query, [], '', $headerMap); |
| 56 | + $this->assertArrayHasKey('products', $response); |
| 57 | + $this->assertArrayHasKey('items', $response['products']); |
| 58 | + $this->assertCount(1, $response['products']['items']); |
| 59 | + |
| 60 | + // Delete the product and verify it is actually not accessible via the storefront anymore |
| 61 | + /** @var ProductRepositoryInterface $productRepository */ |
| 62 | + $productRepository = ObjectManager::getInstance()->get(ProductRepositoryInterface::class); |
| 63 | + |
| 64 | + $registry = ObjectManager::getInstance()->get(\Magento\Framework\Registry::class); |
| 65 | + $registry->unregister('isSecureArea'); |
| 66 | + $registry->register('isSecureArea', true); |
| 67 | + $productRepository->deleteById($productSku); |
| 68 | + $registry->unregister('isSecureArea'); |
| 69 | + $registry->register('isSecureArea', false); |
| 70 | + |
| 71 | + $response = $this->graphQlQuery($query, [], '', $headerMap); |
| 72 | + $this->assertArrayHasKey('products', $response); |
| 73 | + $this->assertArrayHasKey('items', $response['products']); |
| 74 | + $this->assertCount(0, $response['products']['items']); |
| 75 | + } |
| 76 | +} |
0 commit comments