|
| 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\Sales\Block\Adminhtml\Order; |
| 9 | + |
| 10 | +use Magento\Framework\ObjectManagerInterface; |
| 11 | +use Magento\Framework\Phrase; |
| 12 | +use Magento\Framework\View\LayoutInterface; |
| 13 | +use Magento\Sales\Api\Data\OrderInterfaceFactory; |
| 14 | +use Magento\Sales\Block\Adminhtml\Order\Totals\Tax; |
| 15 | +use Magento\Sales\Model\Order; |
| 16 | +use Magento\TestFramework\Helper\Bootstrap; |
| 17 | +use PHPUnit\Framework\TestCase; |
| 18 | + |
| 19 | +/** |
| 20 | + * @magentoAppArea adminhtml |
| 21 | + */ |
| 22 | +class TotalsTest extends TestCase |
| 23 | +{ |
| 24 | + /** |
| 25 | + * @var ObjectManagerInterface |
| 26 | + */ |
| 27 | + private $om; |
| 28 | + |
| 29 | + /** |
| 30 | + * @var LayoutInterface |
| 31 | + */ |
| 32 | + private $layout; |
| 33 | + |
| 34 | + /** |
| 35 | + * @var OrderInterfaceFactory |
| 36 | + */ |
| 37 | + private $orderFactory; |
| 38 | + |
| 39 | + /** |
| 40 | + * @inheritDoc |
| 41 | + */ |
| 42 | + public function setUp() |
| 43 | + { |
| 44 | + $this->om = Bootstrap::getObjectManager(); |
| 45 | + $this->layout = $this->om->get(LayoutInterface::class); |
| 46 | + $this->orderFactory = $this->om->get(OrderInterfaceFactory::class); |
| 47 | + } |
| 48 | + |
| 49 | + /** |
| 50 | + * Test block totals including tax. |
| 51 | + * |
| 52 | + * @magentoConfigFixture default_store tax/sales_display/subtotal 2 |
| 53 | + * @magentoConfigFixture default_store tax/sales_display/shipping 2 |
| 54 | + * |
| 55 | + * @magentoDataFixture Magento/Sales/_files/order.php |
| 56 | + * |
| 57 | + * @return void |
| 58 | + */ |
| 59 | + public function testTotalsInclTax(): void |
| 60 | + { |
| 61 | + $order = $this->prepareOrderInclTax('100000001'); |
| 62 | + |
| 63 | + $blockTotals = $this->getBlockTotals()->setOrder($order); |
| 64 | + $this->assertSubtotal($blockTotals->toHtml(), (float) $order->getSubtotal()); |
| 65 | + $this->assertShipping($blockTotals->toHtml(), (float) $order->getShippingAmount()); |
| 66 | + |
| 67 | + $blockTax = $this->getBlockTax(); |
| 68 | + $blockTotals->setChild('child_tax_block', $blockTax); |
| 69 | + $blockTax->initTotals(); |
| 70 | + |
| 71 | + $this->assertSubtotal($blockTotals->toHtml(), (float) $order->getSubtotalInclTax()); |
| 72 | + $this->assertShipping($blockTotals->toHtml(), (float) $order->getShippingInclTax()); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Check if subtotal amount present in block. |
| 77 | + * |
| 78 | + * @param string $blockTotalsHtml |
| 79 | + * @param float $amount |
| 80 | + * @return void |
| 81 | + */ |
| 82 | + private function assertSubtotal(string $blockTotalsHtml, float $amount): void |
| 83 | + { |
| 84 | + $this->assertTrue( |
| 85 | + $this->isBlockContainsTotalAmount($blockTotalsHtml, __('Subtotal'), $amount), |
| 86 | + 'Subtotal amount is missing or incorrect.' |
| 87 | + ); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * Check if shipping amount present in block. |
| 92 | + * |
| 93 | + * @param string $blockTotalsHtml |
| 94 | + * @param float $amount |
| 95 | + * @return void |
| 96 | + */ |
| 97 | + private function assertShipping(string $blockTotalsHtml, float $amount): void |
| 98 | + { |
| 99 | + $this->assertTrue( |
| 100 | + $this->isBlockContainsTotalAmount($blockTotalsHtml, __('Shipping & Handling'), $amount), |
| 101 | + 'Shipping & Handling amount is missing or incorrect.' |
| 102 | + ); |
| 103 | + } |
| 104 | + |
| 105 | + /** |
| 106 | + * Prepare order for test. |
| 107 | + * |
| 108 | + * @param string $incrementId |
| 109 | + * @return Order |
| 110 | + */ |
| 111 | + private function prepareOrderInclTax(string $incrementId): Order |
| 112 | + { |
| 113 | + /** @var Order $order */ |
| 114 | + $order = $this->orderFactory->create()->loadByIncrementId($incrementId); |
| 115 | + |
| 116 | + $order->setSubtotalInclTax(110); |
| 117 | + $order->setBaseSubtotalInclTax(110); |
| 118 | + |
| 119 | + $order->setShippingAmount(10); |
| 120 | + $order->setBaseShippingAmount(10); |
| 121 | + $order->setShippingInclTax(11); |
| 122 | + $order->setBaseShippingInclTax(11); |
| 123 | + |
| 124 | + return $order; |
| 125 | + } |
| 126 | + |
| 127 | + /** |
| 128 | + * Create block totals. |
| 129 | + * |
| 130 | + * @return Totals |
| 131 | + */ |
| 132 | + private function getBlockTotals(): Totals |
| 133 | + { |
| 134 | + /** @var Totals $block */ |
| 135 | + $block = $this->layout->createBlock(Totals::class, 'block_totals'); |
| 136 | + $block->setTemplate('Magento_Sales::order/totals.phtml'); |
| 137 | + |
| 138 | + return $block; |
| 139 | + } |
| 140 | + |
| 141 | + /** |
| 142 | + * Create block tax. |
| 143 | + * |
| 144 | + * @return Tax |
| 145 | + */ |
| 146 | + private function getBlockTax(): Tax |
| 147 | + { |
| 148 | + /** @var Tax $block */ |
| 149 | + $block = $this->layout->createBlock(Tax::class, 'block_tax'); |
| 150 | + $block->setTemplate('Magento_Sales::order/totals/tax.phtml'); |
| 151 | + |
| 152 | + return $block; |
| 153 | + } |
| 154 | + |
| 155 | + /** |
| 156 | + * Check if amount present in appropriate block node. |
| 157 | + * |
| 158 | + * @param string $blockTotalsHtml |
| 159 | + * @param Phrase $totalLabel |
| 160 | + * @param float $totalAmount |
| 161 | + * @return bool |
| 162 | + */ |
| 163 | + private function isBlockContainsTotalAmount( |
| 164 | + string $blockTotalsHtml, |
| 165 | + Phrase $totalLabel, |
| 166 | + float $totalAmount |
| 167 | + ): bool { |
| 168 | + $dom = new \DOMDocument(); |
| 169 | + $dom->loadHTML($blockTotalsHtml); |
| 170 | + $query = sprintf( |
| 171 | + "//tr[contains(., '%s')]//span[contains(text(), '%01.2f')]", |
| 172 | + $totalLabel, |
| 173 | + $totalAmount |
| 174 | + ); |
| 175 | + |
| 176 | + return (bool) (new \DOMXPath($dom))->query($query)->count(); |
| 177 | + } |
| 178 | +} |
0 commit comments