ProductViewTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. declare(strict_types=1);
  7. namespace Magento\GraphQl\Tax;
  8. use Magento\Catalog\Api\Data\ProductInterface;
  9. use Magento\Catalog\Api\ProductRepositoryInterface;
  10. use Magento\Framework\EntityManager\MetadataPool;
  11. use Magento\TestFramework\ObjectManager;
  12. use Magento\TestFramework\TestCase\GraphQlAbstract;
  13. use Magento\Store\Model\StoreManagerInterface;
  14. use Magento\TestFramework\Helper\Bootstrap;
  15. use Magento\Framework\App\Config\ScopeConfigInterface;
  16. use Magento\Tax\Model\Config;
  17. /**
  18. * @magentoAppIsolation enabled
  19. */
  20. class ProductViewTest extends GraphQlAbstract
  21. {
  22. /**
  23. * @var ObjectManager
  24. */
  25. private $objectManager;
  26. /**
  27. * @var ProductRepositoryInterface
  28. */
  29. private $productRepository;
  30. /** @var \Magento\Tax\Model\Calculation\Rate[] */
  31. private $fixtureTaxRates;
  32. /** @var \Magento\Tax\Model\Calculation\Rule[] */
  33. private $fixtureTaxRules;
  34. /**
  35. * @var StoreManagerInterface
  36. */
  37. private $storeManager;
  38. protected function setUp()
  39. {
  40. $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
  41. $this->productRepository = $this->objectManager->get(ProductRepositoryInterface::class);
  42. $this->storeManager = $this->objectManager->get(StoreManagerInterface::class);
  43. /** @var \Magento\Config\Model\ResourceModel\Config $config */
  44. $config = $this->objectManager->get(\Magento\Config\Model\ResourceModel\Config::class);
  45. //default state tax calculation AL
  46. $config->saveConfig(
  47. Config::CONFIG_XML_PATH_DEFAULT_REGION,
  48. 1,
  49. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  50. 1
  51. );
  52. $config->saveConfig(
  53. Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
  54. 3,
  55. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  56. 1
  57. );
  58. $this->getFixtureTaxRates();
  59. $this->getFixtureTaxRules();
  60. /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */
  61. $config = $this->objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  62. $config->reinit();
  63. }
  64. public function tearDown()
  65. {
  66. /** @var \Magento\Config\Model\ResourceModel\Config $config */
  67. $config = $this->objectManager->get(\Magento\Config\Model\ResourceModel\Config::class);
  68. //default state tax calculation AL
  69. $config->saveConfig(
  70. Config::CONFIG_XML_PATH_DEFAULT_REGION,
  71. null,
  72. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  73. 1
  74. );
  75. $config->saveConfig(
  76. Config::CONFIG_XML_PATH_PRICE_DISPLAY_TYPE,
  77. 1,
  78. ScopeConfigInterface::SCOPE_TYPE_DEFAULT,
  79. 1
  80. );
  81. $taxRules = $this->getFixtureTaxRules();
  82. if (count($taxRules)) {
  83. $taxRates = $this->getFixtureTaxRates();
  84. foreach ($taxRules as $taxRule) {
  85. $taxRule->delete();
  86. }
  87. foreach ($taxRates as $taxRate) {
  88. $taxRate->delete();
  89. }
  90. }
  91. /** @var \Magento\Framework\App\Config\ReinitableConfigInterface $config */
  92. $config = $this->objectManager->get(\Magento\Framework\App\Config\ReinitableConfigInterface::class);
  93. $config->reinit();
  94. }
  95. /**
  96. * @magentoApiDataFixture Magento/Customer/_files/customer.php
  97. * @magentoApiDataFixture Magento/Customer/_files/customer_primary_addresses.php
  98. * @magentoApiDataFixture Magento/Catalog/_files/product_simple_with_all_fields.php
  99. * @magentoApiDataFixture Magento/Tax/_files/tax_rule_region_1_al.php
  100. * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  101. */
  102. public function testQueryAllFieldsSimpleProduct()
  103. {
  104. $productSku = 'simple';
  105. $product = $this->productRepository->get($productSku, null, null, true);
  106. // set product to taxable goods
  107. $product->setData('tax_class_id', 2)->save();
  108. $query = <<<QUERY
  109. {
  110. products(filter: {sku: {eq: "{$productSku}"}})
  111. {
  112. items {
  113. attribute_set_id
  114. created_at
  115. id
  116. name
  117. price {
  118. minimalPrice {
  119. amount {
  120. value
  121. currency
  122. }
  123. adjustments {
  124. amount {
  125. value
  126. currency
  127. }
  128. code
  129. description
  130. }
  131. }
  132. maximalPrice {
  133. amount {
  134. value
  135. currency
  136. }
  137. adjustments {
  138. amount {
  139. value
  140. currency
  141. }
  142. code
  143. description
  144. }
  145. }
  146. regularPrice {
  147. amount {
  148. value
  149. currency
  150. }
  151. adjustments {
  152. amount {
  153. value
  154. currency
  155. }
  156. code
  157. description
  158. }
  159. }
  160. }
  161. sku
  162. type_id
  163. updated_at
  164. ... on PhysicalProductInterface {
  165. weight
  166. }
  167. }
  168. }
  169. }
  170. QUERY;
  171. $response = $this->graphQlQuery($query);
  172. /** @var \Magento\Catalog\Model\Product $product */
  173. $product = $this->productRepository->get($productSku, false, null, true);
  174. /** @var MetadataPool $metadataPool */
  175. $metadataPool = ObjectManager::getInstance()->get(MetadataPool::class);
  176. $product->setId(
  177. $product->getData($metadataPool->getMetadata(ProductInterface::class)->getLinkField())
  178. );
  179. $this->assertArrayHasKey('products', $response);
  180. $this->assertArrayHasKey('items', $response['products']);
  181. $this->assertEquals(1, count($response['products']['items']));
  182. $this->assertArrayHasKey(0, $response['products']['items']);
  183. $this->assertBaseFields($product, $response['products']['items'][0]);
  184. }
  185. /**
  186. * Get tax rates created in Magento\Tax\_files\tax_rule_region_1_al.php
  187. *
  188. * @return \Magento\Tax\Model\Calculation\Rate[]
  189. */
  190. private function getFixtureTaxRates()
  191. {
  192. if ($this->fixtureTaxRates === null) {
  193. $this->fixtureTaxRates = [];
  194. if ($this->getFixtureTaxRules()) {
  195. $taxRateIds = (array)$this->getFixtureTaxRules()[0]->getRates();
  196. foreach ($taxRateIds as $taxRateId) {
  197. /** @var \Magento\Tax\Model\Calculation\Rate $taxRate */
  198. $taxRate = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rate::class);
  199. $this->fixtureTaxRates[] = $taxRate->load($taxRateId);
  200. }
  201. }
  202. }
  203. return $this->fixtureTaxRates;
  204. }
  205. /**
  206. * Get tax rule created in Magento\Tax\_files\tax_rule_region_1_al.php
  207. *
  208. * @return \Magento\Tax\Model\Calculation\Rule[]
  209. */
  210. private function getFixtureTaxRules()
  211. {
  212. if ($this->fixtureTaxRules === null) {
  213. $this->fixtureTaxRules = [];
  214. $taxRuleCodes = ['AL Test Rule'];
  215. foreach ($taxRuleCodes as $taxRuleCode) {
  216. /** @var \Magento\Tax\Model\Calculation\Rule $taxRule */
  217. $taxRule = Bootstrap::getObjectManager()->create(\Magento\Tax\Model\Calculation\Rule::class);
  218. $taxRule->load($taxRuleCode, 'code');
  219. if ($taxRule->getId()) {
  220. $this->fixtureTaxRules[] = $taxRule;
  221. }
  222. }
  223. }
  224. return $this->fixtureTaxRules;
  225. }
  226. /**
  227. * @param ProductInterface $product
  228. * @param array $actualResponse
  229. */
  230. private function assertBaseFields($product, $actualResponse)
  231. {
  232. // ['product_object_field_name', 'expected_value']
  233. $assertionMap = [
  234. ['response_field' => 'attribute_set_id', 'expected_value' => $product->getAttributeSetId()],
  235. ['response_field' => 'created_at', 'expected_value' => $product->getCreatedAt()],
  236. ['response_field' => 'id', 'expected_value' => $product->getId()],
  237. ['response_field' => 'name', 'expected_value' => $product->getName()],
  238. ['response_field' => 'price', 'expected_value' =>
  239. [
  240. 'minimalPrice' => [
  241. 'amount' => [
  242. 'value' => 4.106501,
  243. 'currency' => 'USD'
  244. ],
  245. 'adjustments' => [
  246. 0 =>
  247. [
  248. 'amount' =>
  249. [
  250. 'value' => 0.286501,
  251. 'currency' => 'USD',
  252. ],
  253. 'code' => 'TAX',
  254. 'description' => 'INCLUDED',
  255. ],
  256. ]
  257. ],
  258. 'regularPrice' => [
  259. 'amount' => [
  260. 'value' => 10.750001,
  261. 'currency' => 'USD'
  262. ],
  263. 'adjustments' => [
  264. 0 =>
  265. [
  266. 'amount' =>
  267. [
  268. 'value' => 0.750001,
  269. 'currency' => 'USD',
  270. ],
  271. 'code' => 'TAX',
  272. 'description' => 'INCLUDED',
  273. ],
  274. ]
  275. ],
  276. 'maximalPrice' => [
  277. 'amount' => [
  278. 'value' => 4.106501,
  279. 'currency' => 'USD'
  280. ],
  281. 'adjustments' => [
  282. 0 =>
  283. [
  284. 'amount' =>
  285. [
  286. 'value' => 0.286501,
  287. 'currency' => 'USD',
  288. ],
  289. 'code' => 'TAX',
  290. 'description' => 'INCLUDED',
  291. ],
  292. ]
  293. ],
  294. ]
  295. ],
  296. ['response_field' => 'sku', 'expected_value' => $product->getSku()],
  297. ['response_field' => 'type_id', 'expected_value' => $product->getTypeId()],
  298. ['response_field' => 'updated_at', 'expected_value' => $product->getUpdatedAt()],
  299. ['response_field' => 'weight', 'expected_value' => $product->getWeight()],
  300. ];
  301. $this->assertResponseFields($actualResponse, $assertionMap);
  302. }
  303. }