|
| 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\CatalogGraphQl; |
| 9 | + |
| 10 | +use Magento\GraphQl\GetCustomerAuthenticationHeader; |
| 11 | +use Magento\TestFramework\Helper\Bootstrap; |
| 12 | +use Magento\TestFramework\ObjectManager; |
| 13 | +use Magento\TestFramework\TestCase\GraphQlAbstract; |
| 14 | + |
| 15 | +/** |
| 16 | + * Test class to verify catalog price rule is applied for |
| 17 | + * tier prices for different customer groups. |
| 18 | + */ |
| 19 | +class PriceRangeTest extends GraphQlAbstract |
| 20 | +{ |
| 21 | + /** |
| 22 | + * @var ObjectManager|null |
| 23 | + */ |
| 24 | + private $objectManager; |
| 25 | + |
| 26 | + /** |
| 27 | + * @var GetCustomerAuthenticationHeader |
| 28 | + */ |
| 29 | + private $getCustomerAuthenticationHeader; |
| 30 | + |
| 31 | + protected function setUp(): void |
| 32 | + { |
| 33 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 34 | + $this->getCustomerAuthenticationHeader = $this->objectManager->get(GetCustomerAuthenticationHeader::class); |
| 35 | + } |
| 36 | + |
| 37 | + /** |
| 38 | + * Test for checking if catalog rule price has been applied for all customer group |
| 39 | + * |
| 40 | + * @magentoApiDataFixture Magento/Catalog/_files/product_simple.php |
| 41 | + * @magentoApiDataFixture Magento/CatalogRule/_files/catalog_rule_25_customer_group_all.php |
| 42 | + */ |
| 43 | + public function testCheckIfCatalogRuleIsAppliedForTierPriceForAllGroups(): void |
| 44 | + { |
| 45 | + $productSku = 'simple'; |
| 46 | + $query = $this->getProductSearchQuery($productSku); |
| 47 | + |
| 48 | + $response = $this->graphQlQuery($query); |
| 49 | + |
| 50 | + $this->assertNotEmpty($response['products']); |
| 51 | + $priceRange = $response['products']['items'][0]['price_range']; |
| 52 | + $this->assertEquals(10, $priceRange['minimum_price']['regular_price']['value']); |
| 53 | + $this->assertEquals(7.5, $priceRange['minimum_price']['final_price']['value']); |
| 54 | + $this->assertEquals(2.5, $priceRange['minimum_price']['discount']['amount_off']); |
| 55 | + $this->assertEquals(25, $priceRange['minimum_price']['discount']['percent_off']); |
| 56 | + $this->assertEquals(10, $priceRange['maximum_price']['regular_price']['value']); |
| 57 | + $this->assertEquals(7.5, $priceRange['maximum_price']['final_price']['value']); |
| 58 | + $this->assertEquals(2.5, $priceRange['maximum_price']['discount']['amount_off']); |
| 59 | + $this->assertEquals(25, $priceRange['maximum_price']['discount']['percent_off']); |
| 60 | + } |
| 61 | + |
| 62 | + /** |
| 63 | + * Test for checking if catalog rule price has been applied for registered customer |
| 64 | + * |
| 65 | + * @magentoApiDataFixture Magento/Customer/_files/customer.php |
| 66 | + * @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_logged_in_group.php |
| 67 | + * @magentoApiDataFixture Magento/CatalogRule/_files/catalog_rule_50_registered_customer_group.php |
| 68 | + */ |
| 69 | + public function testCheckIfCatalogRuleIsAppliedForTierPriceForRegisteredCustomer(): void |
| 70 | + { |
| 71 | + $productSku = 'simple'; |
| 72 | + $query = $this->getProductSearchQuery($productSku); |
| 73 | + $response = $this->graphQlQuery( |
| 74 | + $query, |
| 75 | + [], |
| 76 | + '', |
| 77 | + $this->getCustomerAuthenticationHeader->execute('customer@example.com', 'password') |
| 78 | + ); |
| 79 | + |
| 80 | + $this->assertNotEmpty($response['products']); |
| 81 | + $priceRange = $response['products']['items'][0]['price_range']; |
| 82 | + $this->assertEquals(10, $priceRange['minimum_price']['regular_price']['value']); |
| 83 | + $this->assertEquals(5, $priceRange['minimum_price']['final_price']['value']); |
| 84 | + $this->assertEquals(5, $priceRange['minimum_price']['discount']['amount_off']); |
| 85 | + $this->assertEquals(50, $priceRange['minimum_price']['discount']['percent_off']); |
| 86 | + $this->assertEquals(10, $priceRange['maximum_price']['regular_price']['value']); |
| 87 | + $this->assertEquals(5, $priceRange['maximum_price']['final_price']['value']); |
| 88 | + $this->assertEquals(5, $priceRange['maximum_price']['discount']['amount_off']); |
| 89 | + $this->assertEquals(50, $priceRange['maximum_price']['discount']['percent_off']); |
| 90 | + } |
| 91 | + |
| 92 | + /** |
| 93 | + * Test for checking if catalog rule price has been applied for guest |
| 94 | + * |
| 95 | + * @magentoApiDataFixture Magento/Catalog/_files/simple_product_with_tier_prices_for_not_logged_in_group.php |
| 96 | + * @magentoApiDataFixture Magento/CatalogRule/_files/catalog_rule_10_off_not_logged.php |
| 97 | + */ |
| 98 | + public function testCheckIfCatalogRuleIsAppliedForTierPriceForGuest(): void |
| 99 | + { |
| 100 | + $productSku = 'simple'; |
| 101 | + $query = $this->getProductSearchQuery($productSku); |
| 102 | + $response = $this->graphQlQuery($query); |
| 103 | + |
| 104 | + $this->assertNotEmpty($response['products']); |
| 105 | + $priceRange = $response['products']['items'][0]['price_range']; |
| 106 | + $this->assertEquals(10, $priceRange['minimum_price']['regular_price']['value']); |
| 107 | + $this->assertEquals(9, $priceRange['minimum_price']['final_price']['value']); |
| 108 | + $this->assertEquals(1, $priceRange['minimum_price']['discount']['amount_off']); |
| 109 | + $this->assertEquals(10, $priceRange['minimum_price']['discount']['percent_off']); |
| 110 | + $this->assertEquals(10, $priceRange['maximum_price']['regular_price']['value']); |
| 111 | + $this->assertEquals(9, $priceRange['maximum_price']['final_price']['value']); |
| 112 | + $this->assertEquals(1, $priceRange['maximum_price']['discount']['amount_off']); |
| 113 | + $this->assertEquals(10, $priceRange['maximum_price']['discount']['percent_off']); |
| 114 | + } |
| 115 | + |
| 116 | + /** |
| 117 | + * Get a query which user filter for product sku and returns price_tiers |
| 118 | + * |
| 119 | + * @param string $productSku |
| 120 | + * @return string |
| 121 | + */ |
| 122 | + private function getProductSearchQuery(string $productSku): string |
| 123 | + { |
| 124 | + return <<<QUERY |
| 125 | +{ |
| 126 | + products(filter: {sku: {eq: "{$productSku}"}}) { |
| 127 | + items { |
| 128 | + name |
| 129 | + sku |
| 130 | + price_range { |
| 131 | + minimum_price { |
| 132 | + regular_price { |
| 133 | + value |
| 134 | + currency |
| 135 | + } |
| 136 | + final_price { |
| 137 | + value |
| 138 | + currency |
| 139 | + } |
| 140 | + discount { |
| 141 | + amount_off |
| 142 | + percent_off |
| 143 | + } |
| 144 | + } |
| 145 | + maximum_price { |
| 146 | + regular_price { |
| 147 | + value |
| 148 | + currency |
| 149 | + } |
| 150 | + final_price { |
| 151 | + value |
| 152 | + currency |
| 153 | + } |
| 154 | + discount { |
| 155 | + amount_off |
| 156 | + percent_off |
| 157 | + } |
| 158 | + } |
| 159 | + } |
| 160 | + price_tiers{ |
| 161 | + discount{ |
| 162 | + amount_off |
| 163 | + percent_off |
| 164 | + } |
| 165 | + final_price{ |
| 166 | + value |
| 167 | + } |
| 168 | + quantity |
| 169 | + } |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | +QUERY; |
| 174 | + } |
| 175 | +} |
0 commit comments