|
| 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\TestFramework\Helper\Bootstrap; |
| 12 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 13 | + |
| 14 | +/** |
| 15 | + * Test retrieving of customer downloadable products. |
| 16 | + */ |
| 17 | +class CustomerDownloadableProductsTest extends GraphQlAbstract |
| 18 | +{ |
| 19 | + /** |
| 20 | + * @var CustomerTokenServiceInterface |
| 21 | + */ |
| 22 | + private $customerTokenService; |
| 23 | + |
| 24 | + /** |
| 25 | + * @inheritdoc |
| 26 | + */ |
| 27 | + protected function setUp() |
| 28 | + { |
| 29 | + $objectManager = Bootstrap::getObjectManager(); |
| 30 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 31 | + } |
| 32 | + |
| 33 | + /** |
| 34 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 35 | + * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php |
| 36 | + * @magentoApiDataFixture Magento/Downloadable/_files/customer_order_with_downloadable_product.php |
| 37 | + */ |
| 38 | + public function testCustomerDownloadableProducts() |
| 39 | + { |
| 40 | + $query = $this->getQuery(); |
| 41 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 42 | + |
| 43 | + self::assertArrayHasKey('items', $response['customerDownloadableProducts']); |
| 44 | + self::assertCount(1, $response['customerDownloadableProducts']['items']); |
| 45 | + |
| 46 | + self::assertArrayHasKey('date', $response['customerDownloadableProducts']['items'][0]); |
| 47 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['date']); |
| 48 | + |
| 49 | + self::assertArrayHasKey('download_url', $response['customerDownloadableProducts']['items'][0]); |
| 50 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['download_url']); |
| 51 | + |
| 52 | + self::assertArrayHasKey('order_increment_id', $response['customerDownloadableProducts']['items'][0]); |
| 53 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['order_increment_id']); |
| 54 | + |
| 55 | + self::assertArrayHasKey('remaining_downloads', $response['customerDownloadableProducts']['items'][0]); |
| 56 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['remaining_downloads']); |
| 57 | + |
| 58 | + self::assertArrayHasKey('status', $response['customerDownloadableProducts']['items'][0]); |
| 59 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['status']); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 64 | + * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php |
| 65 | + * @magentoApiDataFixture Magento/Downloadable/_files/customer_order_with_downloadable_product.php |
| 66 | + * |
| 67 | + * @expectedException \Exception |
| 68 | + * @expectedExceptionMessage The current customer isn't authorized. |
| 69 | + */ |
| 70 | + public function testGuestCannotAccessDownloadableProducts() |
| 71 | + { |
| 72 | + $this->graphQlQuery($this->getQuery()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 77 | + */ |
| 78 | + public function testCustomerHasNoOrders() |
| 79 | + { |
| 80 | + $query = $this->getQuery(); |
| 81 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 82 | + |
| 83 | + self::assertArrayHasKey('items', $response['customerDownloadableProducts']); |
| 84 | + self::assertCount(0, $response['customerDownloadableProducts']['items']); |
| 85 | + } |
| 86 | + |
| 87 | + /** |
| 88 | + * @return string |
| 89 | + */ |
| 90 | + private function getQuery(): string |
| 91 | + { |
| 92 | + return <<<QUERY |
| 93 | +{ |
| 94 | + customerDownloadableProducts { |
| 95 | + items { |
| 96 | + date |
| 97 | + download_url |
| 98 | + order_increment_id |
| 99 | + remaining_downloads |
| 100 | + status |
| 101 | + } |
| 102 | + } |
| 103 | +} |
| 104 | +QUERY; |
| 105 | + } |
| 106 | + |
| 107 | + /** |
| 108 | + * @param string $username |
| 109 | + * @param string $password |
| 110 | + * @return array |
| 111 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 112 | + */ |
| 113 | + private function getHeaderMap( string $username = '[email protected]', string $password = 'password'): array |
| 114 | + { |
| 115 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 116 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
| 117 | + } |
| 118 | +} |
0 commit comments