|
| 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\Downloadable; |
| 9 | + |
| 10 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 11 | +use Magento\Quote\Api\CartManagementInterface; |
| 12 | +use Magento\Quote\Model\ResourceModel\Quote as QuoteResource; |
| 13 | +use Magento\TestFramework\Helper\Bootstrap; |
| 14 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 15 | +use Magento\Quote\Model\QuoteFactory; |
| 16 | + |
| 17 | +/** |
| 18 | + * Test retrieving of customer download products |
| 19 | + */ |
| 20 | +class CustomerDownloadableProductsTest extends GraphQlAbstract |
| 21 | +{ |
| 22 | + /** |
| 23 | + * @var QuoteFactory |
| 24 | + */ |
| 25 | + protected $quoteFactory; |
| 26 | + |
| 27 | + /** |
| 28 | + * @var QuoteResource |
| 29 | + */ |
| 30 | + protected $quoteResource; |
| 31 | + |
| 32 | + /** |
| 33 | + * @var CustomerTokenServiceInterface |
| 34 | + */ |
| 35 | + private $customerTokenService; |
| 36 | + |
| 37 | + /** |
| 38 | + * @var CartManagementInterface |
| 39 | + */ |
| 40 | + private $cartManagement; |
| 41 | + |
| 42 | + /** |
| 43 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 44 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/downloadable_product.php |
| 45 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 46 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_downloadable_product.php |
| 47 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php |
| 48 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 49 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php |
| 50 | + */ |
| 51 | + public function testCustomerDownloadableProducts() |
| 52 | + { |
| 53 | + $quote = $this->quoteFactory->create(); |
| 54 | + $this->quoteResource->load($quote, 'test_quote', 'reserved_order_id'); |
| 55 | + $this->cartManagement->placeOrder($quote->getId()); |
| 56 | + |
| 57 | + $query = $this->getQuery(); |
| 58 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 59 | + |
| 60 | + self::assertArrayHasKey('items', $response['customerDownloadableProducts']); |
| 61 | + self::assertCount(1, $response['customerDownloadableProducts']['items']); |
| 62 | + |
| 63 | + self::assertArrayHasKey('date', $response['customerDownloadableProducts']['items'][0]); |
| 64 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['date']); |
| 65 | + |
| 66 | + self::assertArrayHasKey('download_url', $response['customerDownloadableProducts']['items'][0]); |
| 67 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['download_url']); |
| 68 | + |
| 69 | + self::assertArrayHasKey('order_increment_id', $response['customerDownloadableProducts']['items'][0]); |
| 70 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['order_increment_id']); |
| 71 | + |
| 72 | + self::assertArrayHasKey('remaining_downloads', $response['customerDownloadableProducts']['items'][0]); |
| 73 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['remaining_downloads']); |
| 74 | + |
| 75 | + self::assertArrayHasKey('status', $response['customerDownloadableProducts']['items'][0]); |
| 76 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['status']); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 81 | + */ |
| 82 | + public function testCustomerHasNoOrders() |
| 83 | + { |
| 84 | + $query = $this->getQuery(); |
| 85 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 86 | + |
| 87 | + self::assertArrayHasKey('items', $response['customerDownloadableProducts']); |
| 88 | + self::assertCount(0, $response['customerDownloadableProducts']['items']); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 93 | + * @magentoApiDataFixture Magento/GraphQl/Catalog/_files/simple_product.php |
| 94 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/customer/create_empty_cart.php |
| 95 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/add_simple_product.php |
| 96 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/enable_offline_payment_methods.php |
| 97 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_new_billing_address.php |
| 98 | + * @magentoApiDataFixture Magento/GraphQl/Quote/_files/set_checkmo_payment_method.php |
| 99 | + */ |
| 100 | + public function testCustomerHasNoDownloadableProducts() |
| 101 | + { |
| 102 | + $query = $this->getQuery(); |
| 103 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 104 | + |
| 105 | + self::assertArrayHasKey('items', $response['customerDownloadableProducts']); |
| 106 | + self::assertCount(0, $response['customerDownloadableProducts']['items']); |
| 107 | + } |
| 108 | + |
| 109 | + /** |
| 110 | + * @inheritdoc |
| 111 | + */ |
| 112 | + protected function setUp() |
| 113 | + { |
| 114 | + $objectManager = Bootstrap::getObjectManager(); |
| 115 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 116 | + $this->quoteFactory = $objectManager->get(QuoteFactory::class); |
| 117 | + $this->quoteResource = $objectManager->get(QuoteResource::class); |
| 118 | + $this->cartManagement = $objectManager->get(CartManagementInterface::class); |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * @return string |
| 123 | + */ |
| 124 | + private function getQuery(): string |
| 125 | + { |
| 126 | + return <<<QUERY |
| 127 | +{ |
| 128 | + customerDownloadableProducts { |
| 129 | + items { |
| 130 | + date |
| 131 | + download_url |
| 132 | + order_increment_id |
| 133 | + remaining_downloads |
| 134 | + status |
| 135 | + } |
| 136 | + } |
| 137 | +} |
| 138 | +QUERY; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * @param string $username |
| 143 | + * @param string $password |
| 144 | + * @return array |
| 145 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 146 | + */ |
| 147 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 148 | + { |
| 149 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 150 | + $headerMap = ['Authorization' => 'Bearer ' . $customerToken]; |
| 151 | + return $headerMap; |
| 152 | + } |
| 153 | +} |
0 commit comments