|
7 | 7 |
|
8 | 8 | namespace Magento\GraphQl\CustomerDownloadableProduct;
|
9 | 9 |
|
| 10 | +use Magento\Integration\Api\CustomerTokenServiceInterface; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
10 | 12 | use Magento\TestFramework\TestCase\GraphQlAbstract;
|
11 |
| -use Magento\TestFramework\ObjectManager; |
12 |
| -use Magento\TestFramework\TestCase\GraphQl\ResponseContainsErrorsException; |
13 |
| -use Magento\Customer\Api\CustomerRepositoryInterface; |
14 |
| -use Magento\Sales\Api\OrderRepositoryInterface; |
15 |
| -use Magento\Framework\Api\SearchCriteriaBuilder; |
16 | 13 |
|
| 14 | +/** |
| 15 | + * Test retrieving of customer downloadable products. |
| 16 | + */ |
17 | 17 | class CustomerDownloadableProductTest extends GraphQlAbstract
|
18 | 18 | {
|
19 |
| - |
20 | 19 | /**
|
21 |
| - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
| 20 | + * @var CustomerTokenServiceInterface |
| 21 | + */ |
| 22 | + private $customerTokenService; |
| 23 | + /** |
| 24 | + * @inheritdoc |
| 25 | + */ |
| 26 | + protected function setUp() |
| 27 | + { |
| 28 | + $objectManager = Bootstrap::getObjectManager(); |
| 29 | + $this->customerTokenService = $objectManager->get(CustomerTokenServiceInterface::class); |
| 30 | + } |
| 31 | + /** |
22 | 32 | * @magentoApiDataFixture Magento/Customer/_files/customer.php
|
23 | 33 | * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php
|
24 |
| - * @magentoApiDataFixture Magento/Downloadable/_files/order_with_downloadable_product.php |
| 34 | + * @magentoApiDataFixture Magento/Downloadable/_files/customer_order_with_downloadable_product.php |
25 | 35 | */
|
26 |
| - public function testGetCustomerDownloadableProducts() |
| 36 | + public function testCustomerDownloadableProducts() |
27 | 37 | {
|
28 |
| - $query = <<<MUTATION |
29 |
| -mutation { |
30 |
| - generateCustomerToken( |
31 |
| - email: "customer@example.com" |
32 |
| - password: "password" |
33 |
| - ) { |
34 |
| - token |
35 |
| - } |
36 |
| -} |
37 |
| -MUTATION; |
38 |
| - $response = $this->graphQlMutation($query); |
39 |
| - $token = $response['generateCustomerToken']['token']; |
40 |
| - $this->headers = ['Authorization' => 'Bearer ' . $token]; |
41 |
| - |
42 |
| - $query = <<<QUERY |
43 |
| - { |
44 |
| - customerDownloadableProducts{ |
45 |
| - items{ |
46 |
| - order_increment_id |
47 |
| - date |
48 |
| - status |
49 |
| - download_url |
50 |
| - remaining_downloads |
51 |
| - } |
| 38 | + $query = $this->getQuery(); |
| 39 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 40 | + |
| 41 | + self::assertArrayHasKey('items', $response['customerDownloadableProducts']); |
| 42 | + self::assertCount(1, $response['customerDownloadableProducts']['items']); |
| 43 | + self::assertArrayHasKey('date', $response['customerDownloadableProducts']['items'][0]); |
| 44 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['date']); |
| 45 | + self::assertArrayHasKey('download_url', $response['customerDownloadableProducts']['items'][0]); |
| 46 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['download_url']); |
| 47 | + self::assertArrayHasKey('order_increment_id', $response['customerDownloadableProducts']['items'][0]); |
| 48 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['order_increment_id']); |
| 49 | + self::assertArrayHasKey('remaining_downloads', $response['customerDownloadableProducts']['items'][0]); |
| 50 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['remaining_downloads']); |
| 51 | + self::assertArrayHasKey('status', $response['customerDownloadableProducts']['items'][0]); |
| 52 | + self::assertNotEmpty($response['customerDownloadableProducts']['items'][0]['status']); |
52 | 53 | }
|
53 |
| -
|
54 |
| -} |
55 |
| -QUERY; |
56 |
| - $objectManager = ObjectManager::getInstance(); |
57 |
| - |
58 |
| - $searchCriteria = $objectManager->get(SearchCriteriaBuilder::class)->create(); |
59 |
| - |
60 |
| - $orderRepository = $objectManager->create(OrderRepositoryInterface::class); |
61 |
| - $orders = $orderRepository->getList($searchCriteria)->getItems(); |
62 |
| - $order = array_pop($orders); |
63 |
| - |
64 |
| - $searchCriteria = $objectManager->get( |
65 |
| - SearchCriteriaBuilder::class |
66 |
| - )->addFilter( |
67 |
| - 'email', |
68 |
| - 'customer@example.com' |
69 |
| - )->create(); |
70 |
| - |
71 |
| - $customerRepository = $objectManager->create(CustomerRepositoryInterface::class); |
72 |
| - $customers = $customerRepository->getList($searchCriteria) |
73 |
| - ->getItems(); |
74 |
| - $customer = array_pop($customers); |
75 |
| - |
76 |
| - $order->setCustomerId($customer->getId())->setCustomerIsGuest(false)->save(); |
77 |
| - $response = $this->graphQlQuery($query, [], '', $this->headers); |
78 |
| - |
79 |
| - $this->assertEquals( |
80 |
| - $order->getIncrementId(), |
81 |
| - $response['customerDownloadableProducts']['items'][0]['order_increment_id'] |
82 |
| - ); |
83 |
| - } |
84 |
| - |
85 | 54 | /**
|
86 |
| - * @SuppressWarnings(PHPMD.ExcessiveMethodLength) |
87 | 55 | * @magentoApiDataFixture Magento/Customer/_files/customer.php
|
| 56 | + * @magentoApiDataFixture Magento/Downloadable/_files/product_downloadable.php |
| 57 | + * @magentoApiDataFixture Magento/Downloadable/_files/customer_order_with_downloadable_product.php |
| 58 | + * |
| 59 | + * @expectedException \Exception |
| 60 | + * @expectedExceptionMessage The current customer isn't authorized. |
88 | 61 | */
|
89 |
| - public function testGetCustomerDownloadableProductsIfProductsDoNotExist() |
| 62 | + public function testGuestCannotAccessDownloadableProducts() |
90 | 63 | {
|
91 |
| - $query = <<<MUTATION |
92 |
| -mutation { |
93 |
| - generateCustomerToken( |
94 |
| - email: "customer@example.com" |
95 |
| - password: "password" |
96 |
| - ) { |
97 |
| - token |
98 |
| - } |
99 |
| -} |
100 |
| -MUTATION; |
101 |
| - $response = $this->graphQlMutation($query); |
102 |
| - $token = $response['generateCustomerToken']['token']; |
103 |
| - $this->headers = ['Authorization' => 'Bearer ' . $token]; |
104 |
| - |
105 |
| - $query = <<<QUERY |
106 |
| - { |
107 |
| - customerDownloadableProducts{ |
108 |
| - items{ |
109 |
| - order_increment_id |
110 |
| - date |
111 |
| - status |
112 |
| - download_url |
113 |
| - remaining_downloads |
114 |
| - } |
| 64 | + $this->graphQlQuery($this->getQuery()); |
115 | 65 | }
|
116 |
| -
|
117 |
| -} |
118 |
| -QUERY; |
119 |
| - |
120 |
| - $response = $this->graphQlQuery($query, [], '', $this->headers); |
121 |
| - $this->assertEmpty($response['customerDownloadableProducts']['items']); |
| 66 | + /** |
| 67 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 68 | + */ |
| 69 | + public function testCustomerHasNoOrders() |
| 70 | + { |
| 71 | + $query = $this->getQuery(); |
| 72 | + $response = $this->graphQlQuery($query, [], '', $this->getHeaderMap()); |
| 73 | + self::assertArrayHasKey('items', $response['customerDownloadableProducts']); |
| 74 | + self::assertCount(0, $response['customerDownloadableProducts']['items']); |
122 | 75 | }
|
123 |
| - |
124 |
| - public function testGuestCannotAccessDownloadableProducts() |
| 76 | + /** |
| 77 | + * @return string |
| 78 | + */ |
| 79 | + private function getQuery(): string |
125 | 80 | {
|
126 |
| - $query = <<<QUERY |
127 |
| - { |
128 |
| - customerDownloadableProducts{ |
129 |
| - items{ |
130 |
| - order_increment_id |
131 |
| - date |
132 |
| - status |
133 |
| - download_url |
134 |
| - remaining_downloads |
135 |
| - } |
| 81 | + return <<<QUERY |
| 82 | +{ |
| 83 | + customerDownloadableProducts { |
| 84 | + items { |
| 85 | + date |
| 86 | + download_url |
| 87 | + order_increment_id |
| 88 | + remaining_downloads |
| 89 | + status |
136 | 90 | }
|
137 |
| -
|
| 91 | + } |
138 | 92 | }
|
139 | 93 | QUERY;
|
140 |
| - |
141 |
| - $this->expectException(ResponseContainsErrorsException::class); |
142 |
| - $this->expectExceptionMessage('GraphQL response contains errors: The current customer isn\'t authorized'); |
143 |
| - $this->graphQlQuery($query); |
| 94 | + } |
| 95 | + /** |
| 96 | + * @param string $username |
| 97 | + * @param string $password |
| 98 | + * @return array |
| 99 | + * @throws \Magento\Framework\Exception\AuthenticationException |
| 100 | + */ |
| 101 | + private function getHeaderMap(string $username = 'customer@example.com', string $password = 'password'): array |
| 102 | + { |
| 103 | + $customerToken = $this->customerTokenService->createCustomerAccessToken($username, $password); |
| 104 | + return ['Authorization' => 'Bearer ' . $customerToken]; |
144 | 105 | }
|
145 | 106 | }
|
0 commit comments