ltsecurityinc安防产品在线商城代码

This commit is contained in:
2026-09-14 17:47:10 +08:00
parent 57d8444c15
commit 6a3c9c5bd8
14539 changed files with 1775310 additions and 0 deletions
@@ -0,0 +1,70 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Order;
class CommentsTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Sales\Block\Order\Comments
*/
protected $_block;
protected function setUp(): void
{
$this->_block = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\View\LayoutInterface::class
)->createBlock(
\Magento\Sales\Block\Order\Comments::class
);
}
/**
* @param string $commentedEntity
* @param string $expectedClass
* @dataProvider getCommentsDataProvider
*/
public function testGetComments($commentedEntity, $expectedClass)
{
$commentedEntity = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create($commentedEntity);
$this->_block->setEntity($commentedEntity);
$comments = $this->_block->getComments();
$this->assertInstanceOf($expectedClass, $comments);
}
/**
* @return array
*/
public function getCommentsDataProvider()
{
return [
[
\Magento\Sales\Model\Order\Invoice::class,
\Magento\Sales\Model\ResourceModel\Order\Invoice\Comment\Collection::class,
],
[
\Magento\Sales\Model\Order\Creditmemo::class,
\Magento\Sales\Model\ResourceModel\Order\Creditmemo\Comment\Collection::class
],
[
\Magento\Sales\Model\Order\Shipment::class,
\Magento\Sales\Model\ResourceModel\Order\Shipment\Comment\Collection::class
]
];
}
/**
*/
public function testGetCommentsWrongEntityException()
{
$this->expectException(\Magento\Framework\Exception\LocalizedException::class);
$entity = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->create(
\Magento\Catalog\Model\Product::class
);
$this->_block->setEntity($entity);
$this->_block->getComments();
}
}
@@ -0,0 +1,251 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\Creditmemo;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Text;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for view creditmemo items block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ItemsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var LayoutInterface */
private $layout;
/** @var Items */
private $block;
/** @var CreditmemoInterface */
private $creditmemo;
/** @var Registry */
private $registry;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var PageFactory */
private $pageFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->block = $this->layout->createBlock(Items::class, 'block');
$this->creditmemo = $this->objectManager->get(CreditmemoInterface::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
parent::tearDown();
}
/**
* @magentoAppIsolation enabled
*
* @return void
*/
public function testGetTotalsHtml(): void
{
$childBlock = $this->layout->addBlock(
Text::class,
'creditmemo_totals',
'block'
);
$expectedHtml = '<b>Any html</b>';
$this->assertEmpty($childBlock->getCreditmemo());
$this->assertNotEquals($expectedHtml, $this->block->getTotalsHtml($this->creditmemo));
$childBlock->setText($expectedHtml);
$actualHtml = $this->block->getTotalsHtml($this->creditmemo);
$this->assertSame($this->creditmemo, $childBlock->getCreditmemo());
$this->assertEquals($expectedHtml, $actualHtml);
}
/**
* @magentoAppIsolation enabled
*
* @return void
*/
public function testGetCommentsHtml(): void
{
$childBlock = $this->layout->addBlock(
Text::class,
'creditmemo_comments',
'block'
);
$expectedHtml = '<b>Any html</b>';
$this->assertEmpty($childBlock->getEntity());
$this->assertEmpty($childBlock->getTitle());
$this->assertNotEquals($expectedHtml, $this->block->getCommentsHtml($this->creditmemo));
$childBlock->setText($expectedHtml);
$actualHtml = $this->block->getCommentsHtml($this->creditmemo);
$this->assertSame($this->creditmemo, $childBlock->getEntity());
$this->assertNotEmpty($childBlock->getTitle());
$this->assertEquals($expectedHtml, $actualHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/refunds_for_items.php
*
* @return void
*/
public function testDisplayingCreditmemos(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$blockHtml = $this->renderCreditmemoItemsBlock();
$this->assertCreditmemosBlock($order, $blockHtml);
}
/**
* Assert creditmemos block.
*
* @param OrderInterface $order
* @param string $blockHtml
* @return void
*/
private function assertCreditmemosBlock(OrderInterface $order, string $blockHtml): void
{
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@href, 'sales/order/printCreditmemo/order_id/%s')]/span[contains(text(), '%s')]",
$order->getId(),
__('Print All Refunds')
),
$blockHtml
),
sprintf('%s button was not found.', __('Print All Refunds'))
);
$this->assertNotCount(0, $order->getCreditmemosCollection(), 'Creditmemos collection is empty');
foreach ($order->getCreditmemosCollection() as $creditmemo) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'order-title')]/strong[contains(text(), '%s')]",
__('Refund #') . $creditmemo->getIncrementId()
),
$blockHtml
),
sprintf('Title for %s was not found.', __('Refund #') . $creditmemo->getIncrementId())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@href, 'sales/order/printCreditmemo/creditmemo_id/%s')]"
. "/span[contains(text(), '%s')]",
$creditmemo->getId(),
__('Print Refund')
),
$blockHtml
),
sprintf('%s button for #%s was not found.', __('Print Refund'), $creditmemo->getIncrementId())
);
$this->assertCreditmemoItems($creditmemo, $blockHtml);
}
}
/**
* Assert creditmemo items list.
*
* @param CreditmemoInterface $creditmemo
* @param string $html
* @return void
*/
private function assertCreditmemoItems(CreditmemoInterface $creditmemo, string $html): void
{
$this->assertNotCount(0, $creditmemo->getItemsCollection(), 'Creditmemo items collection is empty');
$fieldsToCheck = [
'name' => "/td[contains(@class, 'name')]/strong[contains(text(), '%s')]",
'sku' => "/td[contains(@class, 'sku') and contains(text(), '%s')]",
'price' => "/td[contains(@class, 'price')]//span[contains(text(), '%01.2f')]",
'qty' => "/td[contains(@class, 'qty') and contains(text(), '%d')]",
'row_total' => "/td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')]",
'discount_amount' => "/td[contains(@class, 'discount')]/span[contains(text(), '%01.2f')]",
];
foreach ($creditmemo->getItemsCollection() as $item) {
$rowXpath = sprintf(
"//table[@id='my-refund-table-%s']//tr[@id='order-item-row-%s']",
$creditmemo->getId(),
$item->getId()
);
foreach ($fieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($rowXpath . $xpath, $item->getData($key)), $html),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
}
}
/**
* Render creditmemo items block.
*
* @return string
*/
private function renderCreditmemoItemsBlock(): string
{
$page = $this->pageFactory->create();
$page->addHandle([
'default',
'sales_order_creditmemo',
]);
$page->getLayout()->generateXml();
$creditmemoItemsBlock = $page->getLayout()->getBlock('creditmemo_items')->unsetChild('creditmemo_totals');
$creditmemoItemsBlock->getRequest()->setRouteName('sales')->setControllerName('order')
->setActionName('creditmemo');
return $creditmemoItemsBlock->toHtml();
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
}
@@ -0,0 +1,97 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\Creditmemo;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for view creditmemo totals block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class TotalsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Totals */
private $block;
/** @var OrderInterfaceFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Totals::class)
->setTemplate('Magento_Sales::order/totals.phtml');
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
}
/**
* @magentoDataFixture Magento/Sales/_files/refunds_for_items.php
*
* @return void
*/
public function testCreditmemoTotalsBlock(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$creditmemo = $order->getCreditmemosCollection()->getFirstItem();
$this->assertNotNull($creditmemo->getId());
$blockHtml = $this->block->setOrder($order)->setCreditmemo($creditmemo)->toHtml();
$message = '"%s" for creditmemo wasn\'t found or not equals to %01.2f';
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//th[contains(text(), '%s')]/following-sibling::td/span[contains(text(), '%01.2f')]",
__('Subtotal'),
$creditmemo->getSubtotal()
),
$blockHtml
),
sprintf($message, __('Subtotal'), $creditmemo->getSubtotal())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//th[contains(text(), '%s')]/following-sibling::td/span[contains(text(), '%01.2f')]",
__('Shipping & Handling'),
$creditmemo->getShippingAmount()
),
$blockHtml
),
sprintf($message, __('Shipping & Handling'), $creditmemo->getShippingAmount())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//tr[contains(@class, 'grand_total') and contains(.//strong, '%s')]"
. "//span[contains(text(), '%01.2f')]",
__('Grand Total'),
$creditmemo->getGrandTotal()
),
$blockHtml
),
sprintf($message, __('Grand Total'), $creditmemo->getGrandTotal())
);
}
}
@@ -0,0 +1,180 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order;
use Magento\Customer\Model\Session;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for customer order grid.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class HistoryTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Session */
private $customerSession;
/** @var History */
private $block;
/** @var OrderInterfaceFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->customerSession = $this->objectManager->get(Session::class);
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(History::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->customerSession->logout();
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
*
* @return void
*/
public function testCustomerOrderGridWithoutOrders(): void
{
$this->customerSession->loginById(1);
$this->assertStringContainsString(
(string)$this->block->getEmptyOrdersMessage(),
strip_tags($this->block->toHtml())
);
}
/**
* @magentoDataFixture Magento/Sales/_files/order_with_customer.php
*
* @return void
*/
public function testCustomerOrderGridWithOrder(): void
{
$this->customerSession->loginById(1);
$this->assertCustomerOrderGrid(['100000001'], $this->block->toHtml());
}
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Sales/_files/orders_with_customer.php
*
* @return void
*/
public function testCustomerOrderGridWithSomeOrders(): void
{
$this->customerSession->loginById(1);
$ordersToCheck = ['100000002', '100000003', '100000004', '100000005', '100000006'];
$this->assertCustomerOrderGrid($ordersToCheck, $this->block->toHtml());
}
/**
* Assert customer order grid.
*
* @param array $ordersToCheck
* @param string $blockHtml
* @return void
*/
private function assertCustomerOrderGrid(array $ordersToCheck, string $blockHtml): void
{
foreach ($ordersToCheck as $orderIncrementId) {
$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
$rowXpath = sprintf("//td[contains(@class,'id') and contains(text(), '%s')]", $order->getRealOrderId());
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$rowXpath . "/following-sibling::td[contains(@class, 'date') and contains(text(), '%s')]",
$this->block->formatDate($order->getCreatedAt())
),
$blockHtml
),
sprintf('Created date for order #%s wasn\'t found in row.', $orderIncrementId)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$rowXpath . "/following-sibling::td[contains(@class, 'total')]/span[contains(text(), '%s')]",
$order->getTotal()
),
$blockHtml
),
sprintf(
'Order Totals for order #%s wasn\'t found or not equal to "%s" in row.',
$orderIncrementId,
$order->getTotal()
)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$rowXpath . "/following-sibling::td[contains(@class, 'status') and contains(text(), '%s')]",
$order->getStatusLabel()
),
$blockHtml
),
sprintf(
'Order status for order #%s wasn\'t found or not equal to "%s" in row.',
$orderIncrementId,
$order->getStatusLabel()
)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$rowXpath . "/following-sibling::td/a[contains(@href, 'sales/order/view/order_id/%s')]"
. "/span[contains(text(), '%s')]",
$order->getId(),
__('View Order')
),
$blockHtml
),
sprintf('View order button for order #%s wasn\'t found.', $orderIncrementId)
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$rowXpath . "/following-sibling::td/a[contains(@data-post,"
. "'sales\/order\/reorder\/order_id\/%s')]/span[contains(text(), '%s')]",
$order->getId(),
__('Reorder')
),
$blockHtml
),
sprintf('Reorder button for order #%s wasn\'t found.', $orderIncrementId)
);
}
}
}
@@ -0,0 +1,115 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\Info;
use Magento\Customer\Model\Session;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Test for order action buttons.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class ButtonsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var Session */
private $customerSession;
/** @var OrderInterface */
private $order;
/** @var Buttons */
private $block;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->customerSession = $this->objectManager->get(Session::class);
$this->order = $this->objectManager->get(OrderInterface::class);
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Buttons::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
$this->customerSession->logout();
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/order_with_customer.php
*
* @return void
*/
public function testDisplayingOrderActionButtons(): void
{
$this->customerSession->loginById(1);
$order = $this->order->loadByIncrementId('100000001');
$this->registerOrder($order);
$blockHtml = $this->block->toHtml();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@data-post, 'sales\/order\/reorder\/order_id\/%s')]/span[contains(text(), '%s')]",
$order->getId(),
__('Reorder')
),
$blockHtml
),
sprintf('%s button wasn\'t found.', __('Reorder'))
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@href, 'sales/order/print/order_id/%s')]/span[contains(text(), '%s')]",
$order->getId(),
__('Print Order')
),
$blockHtml
),
sprintf('%s button wasn\'t found.', __('Print Order'))
);
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
}
@@ -0,0 +1,172 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderAddressInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for order info block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class InfoTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var LayoutInterface */
private $layout;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var CountryFactory */
private $countryFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->countryFactory = $this->objectManager->get(CountryFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/order_with_customer.php
*
* @return void
*/
public function testOrderStatus(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$block = $this->layout->createBlock(Info::class)->setTemplate('Magento_Sales::order/order_status.phtml');
$this->assertStringContainsString((string)__($order->getStatusLabel()), strip_tags($block->toHtml()));
}
/**
* @magentoDataFixture Magento/Sales/_files/order_with_customer.php
*
* @return void
*/
public function testOrderDate(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$block = $this->layout->createBlock(Info::class)->setTemplate('Magento_Sales::order/order_date.phtml');
$this->assertStringContainsString(
(string)__('Order Date: %1', $block->formatDate($order->getCreatedAt(), \IntlDateFormatter::LONG)),
strip_tags($block->toHtml())
);
}
/**
* @magentoDataFixture Magento/Sales/_files/customer_order_with_two_items.php
*
* @return void
*/
public function testOrderInformation(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$blockHtml = $this->layout->createBlock(Info::class)->toHtml();
$this->assertOrderAddress($order->getShippingAddress(), $blockHtml);
$this->assertOrderAddress($order->getBillingAddress(), $blockHtml);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'box-order-shipping-method') and contains(.//span, '%s')]"
. "//div[contains(text(), '%s')]",
__('Shipping Method'),
$order->getShippingDescription()
),
$blockHtml
),
'Shipping method for order wasn\'t found.'
);
}
/**
* Assert order address.
*
* @param OrderAddressInterface $address
* @param string $html
* @return void
*/
private function assertOrderAddress(OrderAddressInterface $address, string $html): void
{
$addressBoxXpath = sprintf("//div[contains(@class, 'box-order-%s-address')]", $address->getAddressType())
. "//address[contains(., '%s')]";
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getName()), $html),
sprintf('Customer name for %s address wasn\'t found.', $address->getAddressType())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$addressBoxXpath,
$this->countryFactory->create()->loadByCode($address->getData('country_id'))->getName()
),
$html
),
sprintf('Country for %s address wasn\'t found.', $address->getAddressType())
);
$attributes = ['company', 'street', 'city', 'region', 'postcode', 'telephone'];
foreach ($attributes as $key) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getData($key)), $html),
sprintf('%s for %s address wasn\'t found.', $key, $address->getAddressType())
);
}
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
}
@@ -0,0 +1,245 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\Invoice;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Text;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\Sales\Api\Data\InvoiceInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for view invoice items block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ItemsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var LayoutInterface */
private $layout;
/** @var Items */
private $block;
/** @var InvoiceInterfaceFactory */
private $invoiceFactory;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var PageFactory */
private $pageFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->block = $this->layout->createBlock(Items::class, 'block');
$this->invoiceFactory = $this->objectManager->get(InvoiceInterfaceFactory::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
parent::tearDown();
}
/**
* @magentoAppIsolation enabled
*
* @return void
*/
public function testGetInvoiceTotalsHtml(): void
{
$childBlock = $this->layout->addBlock(Text::class, 'invoice_totals', 'block');
$expectedHtml = '<b>Any html</b>';
$this->assertEmpty($childBlock->getInvoice());
$invoice = $this->invoiceFactory->create();
$this->assertNotEquals($expectedHtml, $this->block->getInvoiceTotalsHtml($invoice));
$childBlock->setText($expectedHtml);
$actualHtml = $this->block->getInvoiceTotalsHtml($invoice);
$this->assertSame($invoice, $childBlock->getInvoice());
$this->assertEquals($expectedHtml, $actualHtml);
}
/**
* @magentoAppIsolation enabled
*
* @return void
*/
public function testGetInvoiceCommentsHtml(): void
{
$childBlock = $this->layout->addBlock(
Text::class,
'invoice_comments',
'block'
);
$expectedHtml = '<b>Any html</b>';
$this->assertEmpty($childBlock->getEntity());
$this->assertEmpty($childBlock->getTitle());
$invoice = $this->invoiceFactory->create();
$this->assertNotEquals($expectedHtml, $this->block->getInvoiceCommentsHtml($invoice));
$childBlock->setText($expectedHtml);
$actualHtml = $this->block->getInvoiceCommentsHtml($invoice);
$this->assertSame($invoice, $childBlock->getEntity());
$this->assertNotEmpty($childBlock->getTitle());
$this->assertEquals($expectedHtml, $actualHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
*
* @return void
*/
public function testDisplayingInvoices(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$blockHtml = $this->renderInvoiceItemsBlock();
$this->assertInvoicesBlock($order, $blockHtml);
}
/**
* Assert invoices block.
*
* @param OrderInterface $order
* @param string $blockHtml
* @return void
*/
private function assertInvoicesBlock(OrderInterface $order, string $blockHtml): void
{
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@href, 'sales/order/printInvoice/order_id/%s')]/span[contains(text(), '%s')]",
$order->getId(),
__('Print All Invoices')
),
$blockHtml
),
sprintf('%s button was not found.', __('Print All Invoices'))
);
$this->assertNotCount(0, $order->getInvoiceCollection(), 'Invoice collection is empty');
foreach ($order->getInvoiceCollection() as $invoice) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'order-title')]/strong[contains(text(), '%s')]",
__('Invoice #') . $invoice->getIncrementId()
),
$blockHtml
),
sprintf('Title for %s was not found.', __('Invoice #') . $invoice->getIncrementId())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//a[contains(@href, 'sales/order/printInvoice/invoice_id/%s')]/span[contains(text(), '%s')]",
$invoice->getId(),
__('Print Invoice')
),
$blockHtml
),
sprintf('%s button for #%s was not found.', __('Print Invoice'), $invoice->getIncrementId())
);
$this->assertInvoiceItems($invoice, $blockHtml);
}
}
/**
* Assert invoice items list.
*
* @param InvoiceInterface $invoice
* @param string $blockHtml
* @return void
*/
private function assertInvoiceItems(InvoiceInterface $invoice, string $blockHtml): void
{
$this->assertNotCount(0, $invoice->getItemsCollection(), 'Invoice items collection is empty');
$fieldsToCheck = [
'name' => "/td[contains(@class, 'name')]/strong[contains(text(), '%s')]",
'sku' => "/td[contains(@class, 'sku') and contains(text(), '%s')]",
'price' => "/td[contains(@class, 'price')]//span[contains(text(), '%01.2f')]",
'qty' => "/td[contains(@class, 'qty')]/span[contains(text(), '%d')]",
'row_total' => "/td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')]",
];
foreach ($invoice->getItemsCollection() as $item) {
$itemRowXpath = sprintf(
"//table[@id='my-invoice-table-%s']//tr[@id='order-item-row-%s']",
$invoice->getId(),
$item->getId()
);
foreach ($fieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($itemRowXpath . $xpath, $item->getData($key)), $blockHtml),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
}
}
/**
* Render invoice items block.
*
* @return string
*/
private function renderInvoiceItemsBlock(): string
{
$page = $this->pageFactory->create();
$page->addHandle([
'default',
'sales_order_invoice',
]);
$page->getLayout()->generateXml();
$invoiceItemsBlock = $page->getLayout()->getBlock('invoice_items')->unsetChild('invoice_totals');
$invoiceItemsBlock->getRequest()->setRouteName('sales')->setControllerName('order')->setActionName('invoice');
return $invoiceItemsBlock->toHtml();
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
}
@@ -0,0 +1,116 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\Invoice;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for view invoice totals block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class TotalsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Totals */
private $block;
/** @var OrderInterfaceFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Totals::class)
->setTemplate('Magento_Sales::order/totals.phtml');
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
}
/**
* @magentoDataFixture Magento/Sales/_files/order_with_free_shipping_by_coupon_and_invoice.php
*
* @return void
*/
public function testCollectTotals(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$invoice = $order->getInvoiceCollection()->getFirstItem();
$this->block->setOrder($order);
$this->block->setInvoice($invoice);
$this->block->toHtml();
$totals = $this->block->getTotals();
$this->assertArrayHasKey('shipping', $totals);
$this->assertEquals('0.0000', $totals['shipping']['value']);
$this->assertEquals('Shipping & Handling (1234567890)', $totals['shipping']['label']);
}
/**
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
*
* @return void
*/
public function testInvoiceTotalsBlock(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$invoice = $order->getInvoiceCollection()->getFirstItem();
$this->assertNotNull($invoice->getId());
$blockHtml = $this->block->setOrder($order)->setInvoice($invoice)->toHtml();
$message = '"%s" for invoice wasn\'t found or not equals to %01.2f';
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//th[contains(text(), '%s')]/following-sibling::td/span[contains(text(), '%01.2f')]",
__('Subtotal'),
$invoice->getSubtotal()
),
$blockHtml
),
sprintf($message, __('Subtotal'), $invoice->getSubtotal())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//th[contains(text(), '%s')]/following-sibling::td/span[contains(text(), '%01.2f')]",
__('Shipping & Handling'),
$invoice->getShippingAmount()
),
$blockHtml
),
sprintf($message, __('Shipping & Handling'), $invoice->getShippingAmount())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//tr[contains(@class, 'grand_total') and contains(.//strong, '%s')]"
. "//span[contains(text(), '%01.2f')]",
__('Grand Total'),
$invoice->getGrandTotal()
),
$blockHtml
),
sprintf($message, __('Grand Total'), $invoice->getGrandTotal())
);
}
}
@@ -0,0 +1,272 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\Item\Renderer;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\AbstractBlock;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for default renderer order items.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @magentoAppIsolation enabled
*/
class DefaultRendererTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var DefaultRenderer */
private $block;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var PageFactory */
private $pageFactory;
/** @var Registry */
private $registry;
/**
* @var array
*/
private $defaultFieldsToCheck = [
'name' => "//td[contains(@class, 'name')]/strong[contains(text(), '%s')]",
'sku' => "//td[contains(@class, 'sku') and contains(text(), '%s')]",
'qty' => "//td[contains(@class, 'qty') and contains(text(), '%d')]",
];
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(DefaultRenderer::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
$this->registry = $this->objectManager->get(Registry::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/shipment_for_order_with_customer.php
*
* @return void
*/
public function testDisplayingShipmentItem(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$shipment = $order->getShipmentsCollection()->getFirstItem();
$this->assertNotNull($shipment->getId());
$item = $shipment->getAllItems()[0] ?? null;
$this->assertNotNull($item);
$blockHtml = $this->block->setTemplate('Magento_Sales::order/shipment/items/renderer/default.phtml')
->setItem($item)->toHtml();
foreach ($this->defaultFieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($xpath, $item->getData($key)), $blockHtml),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
}
/**
* @magentoDataFixture Magento/Sales/_files/refunds_for_items.php
*
* @return void
*/
public function testCreditmemoItemTotalAmount(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$creditmemo = $order->getCreditmemosCollection()->getFirstItem();
$this->assertNotNull($creditmemo->getId());
$item = $creditmemo->getItemsCollection()->getFirstItem();
$this->assertNotNull($item->getId());
$this->assertEquals(10.00, $this->block->getTotalAmount($item));
}
/**
* @magentoDataFixture Magento/Sales/_files/customer_order_with_two_items.php
*
* @return void
*/
public function testPrintOrderItem(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$item = $order->getItemsCollection()->getFirstItem();
$this->assertNotNull($item->getId());
$block = $this->getBlock('sales_order_print', 'sales.order.print.renderers.default');
$this->assertNotFalse($block);
$blockHtml = $block->setItem($item)->toHtml();
$fieldsToCheck = [
'name' => "//td[contains(@class, 'name')]/strong[contains(text(), '%s')]",
'sku' => "//td[contains(@class, 'sku') and contains(text(), '%s')]",
'price' => "//td[contains(@class, 'price')]//span[contains(text(), '%01.2f')]",
'qty_ordered' => "//td[contains(@class, 'qty')]//span[contains(text(), '" . __('Ordered')
. "')]/following-sibling::span[contains(text(), '%d')]",
'row_total' => "//td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')]",
];
foreach ($fieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($xpath, $item->getData($key)), $blockHtml),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
}
/**
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
*
* @return void
*/
public function testPrintInvoiceItem(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$invoice = $order->getInvoiceCollection()->getFirstItem();
$this->assertNotNull($invoice->getId());
$item = $invoice->getItemsCollection()->getFirstItem();
$this->assertNotNull($item->getId());
$block = $this->getBlock('sales_order_printinvoice', 'sales.order.print.invoice.renderers.default');
$this->assertNotFalse($block);
$blockHtml = $block->setItem($item)->toHtml();
$additionalFields = [
'price' => "//td[contains(@class, 'price')]//span[contains(text(), '%01.2f')]",
'qty' => "//td[contains(@class, 'qty')]/span[contains(text(), '%d')]",
'row_total' => "//td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')]",
];
$this->defaultFieldsToCheck = array_merge($this->defaultFieldsToCheck, $additionalFields);
foreach ($this->defaultFieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($xpath, $item->getData($key)), $blockHtml),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
}
/**
* @magentoDataFixture Magento/Sales/_files/shipment_for_order_with_customer.php
*
* @return void
*/
public function testPrintShipmentItem(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$shipment = $order->getShipmentsCollection()->getFirstItem();
$this->assertNotNull($shipment->getId());
$item = $shipment->getAllItems()[0] ?? null;
$this->assertNotNull($item);
$block = $this->getBlock('sales_order_printshipment', 'sales.order.print.shipment.renderers.default');
$this->assertNotFalse($block);
$blockHtml = $block->setItem($item)->toHtml();
foreach ($this->defaultFieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($xpath, $item->getData($key)), $blockHtml),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
}
/**
* @magentoDataFixture Magento/Sales/_files/refunds_for_items.php
*
* @return void
*/
public function testPrintCreditmemoItem(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$creditmemo = $order->getCreditmemosCollection()->getFirstItem();
$this->assertNotNull($creditmemo->getId());
$item = $creditmemo->getItemsCollection()->getFirstItem();
$this->assertNotNull($item->getId());
$block = $this->getBlock('sales_order_printcreditmemo', 'sales.order.print.creditmemo.renderers.default');
$this->assertNotFalse($block);
$blockHtml = $block->setItem($item)->toHtml();
$additionalFields = [
'price' => "//td[contains(@class, 'price')]//span[contains(text(), '%01.2f')]",
'row_total' => "//td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')]",
'discount_amount' => "//td[contains(@class, 'discount')]/span[contains(text(), '%01.2f')]",
];
$this->defaultFieldsToCheck = array_merge($this->defaultFieldsToCheck, $additionalFields);
foreach ($this->defaultFieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($xpath, $item->getData($key)), $blockHtml),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//td[contains(@class, 'total')]/span[contains(text(), '%01.2f')]",
$this->block->getTotalAmount($item)
),
$blockHtml
),
sprintf('Item total wasn\'t found or not equals to %s.', $this->block->getTotalAmount($item))
);
}
/**
* Get block.
*
* @param string $handle
* @param string $blockName
* @return AbstractBlock
*/
private function getBlock(string $handle, string $blockName): AbstractBlock
{
$page = $this->pageFactory->create();
$page->addHandle(['default', $handle]);
$page->getLayout()->generateXml();
return $page->getLayout()->getBlock($blockName);
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
}
@@ -0,0 +1,242 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\Sales\Model\ResourceModel\Order\Item\Collection;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use Magento\Theme\Block\Html\Pager;
use PHPUnit\Framework\TestCase;
/**
* Tests order items block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ItemsTest extends TestCase
{
/** @var Items */
private $block;
/** @var LayoutInterface */
private $layout;
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var PageFactory */
private $pageFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*
* @return void
*/
public function testGetOrderItems(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$this->block = $this->layout->createBlock(Items::class);
$this->assertCount(1, $this->block->getItems());
}
/**
* @magentoDataFixture Magento/Sales/_files/order_configurable_product.php
*
* @return void
*/
public function testGetPagerCountConfigurable(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$this->prepareBlockWithPager();
/** @var Pager $pagerBlock */
$pagerBlock = $this->block->getChildBlock('sales_order_item_pager');
$this->assertCount(1, $pagerBlock->getCollection()->getItems());
}
/**
* @magentoConfigFixture default/sales/orders/items_per_page 3
* @magentoDataFixture Magento/Sales/_files/order_item_list.php
*
* @return void
*/
public function testPagerIsDisplayed(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$this->prepareBlockWithPager();
$this->assertTrue($this->block->isPagerDisplayed());
}
/**
* @magentoDataFixture Magento/Sales/_files/order_item_list.php
*
* @return void
*/
public function testPagerIsNotDisplayed(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$this->prepareBlockWithPager();
$this->assertFalse($this->block->isPagerDisplayed());
$this->assertEmpty(preg_replace('/\s+/', '', strip_tags($this->block->getPagerHtml())));
}
/**
* @magentoConfigFixture default/sales/orders/items_per_page 3
* @magentoDataFixture Magento/Sales/_files/order_item_list.php
*
* @return void
*/
public function testGetPagerHtml(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$this->prepareBlockWithPager();
$this->assertNotEmpty(preg_replace('/\s+/', '', strip_tags($this->block->getPagerHtml())));
$this->assertTrue($this->block->isPagerDisplayed());
}
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*
* @return void
*/
public function testGetOrder(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$this->block = $this->layout->createBlock(Items::class, 'items_block');
$this->assertEquals($order, $this->block->getOrder());
}
/**
* @magentoDataFixture Magento/Sales/_files/customer_order_with_two_items.php
*
* @return void
*/
public function testDisplayingOrderItems(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$blockHtml = $this->renderOrderItemsBlock();
$this->assertOrderItems($order->getItemsCollection(), $blockHtml);
}
/**
* Render order items block.
*
* @return string
*/
private function renderOrderItemsBlock(): string
{
$page = $this->pageFactory->create();
$page->addHandle([
'default',
'sales_order_view',
]);
$page->getLayout()->generateXml();
$orderItemsBlock = $page->getLayout()->getBlock('order_items')->unsetChild('order_totals');
return $orderItemsBlock->toHtml();
}
/**
* Assert order items list.
*
* @param Collection $orderItems
* @param string $blockHtml
* @return void
*/
private function assertOrderItems(Collection $orderItems, string $blockHtml): void
{
$this->assertNotCount(0, $orderItems, 'Order items collection is empty');
$fieldsToCheck = [
'name' => "/td[contains(@class, 'name')]/strong[contains(text(), '%s')]",
'sku' => "/td[contains(@class, 'sku') and contains(text(), '%s')]",
'price' => "/td[contains(@class, 'price')]//span[contains(text(), '%01.2f')]",
'qty_ordered' => "/td[contains(@class, 'qty')]//span[contains(text(), '" . __('Ordered')
. "')]/following-sibling::span[contains(text(), '%d')]",
'row_total' => "/td[contains(@class, 'subtotal')]//span[contains(text(), '%01.2f')]",
];
foreach ($orderItems as $item) {
$itemRowXpath = sprintf("//tr[@id='order-item-row-%s']", $item->getItemId());
foreach ($fieldsToCheck as $key => $xpath) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($itemRowXpath . $xpath, $item->getData($key)), $blockHtml),
sprintf('Item %s wasn\'t found or not equals to %s.', $key, $item->getData($key))
);
}
}
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
/**
* Create items block with pager
*/
private function prepareBlockWithPager(): void
{
$this->block = $this->layout->createBlock(Items::class, 'items_block');
$this->layout->addBlock(
$this->objectManager->get(Pager::class),
'sales_order_item_pager',
'items_block'
);
$this->block->setLayout($this->layout);
}
}
@@ -0,0 +1,270 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\PrintOrder;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Text;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\Data\CreditmemoInterfaceFactory;
use Magento\Sales\Api\Data\CreditmemoInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\Sales\Api\Data\OrderPaymentInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for print creditmemo block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class CreditmemoTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var LayoutInterface */
private $layout;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var CreditmemoInterfaceFactory */
private $creditmemoFactory;
/** @var PageFactory */
private $pageFactory;
/** @var CountryFactory */
private $countryFactory;
/** @var OrderPaymentInterfaceFactory */
private $orderPaymentFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->creditmemoFactory = $this->objectManager->get(CreditmemoInterfaceFactory::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
$this->countryFactory = $this->objectManager->get(CountryFactory::class);
$this->orderPaymentFactory = $this->objectManager->create(OrderPaymentInterfaceFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
$this->registry->unregister('current_creditmemo');
parent::tearDown();
}
/**
* @magentoAppIsolation enabled
*
* @return void
*/
public function testGetTotalsHtml(): void
{
$order = $this->orderFactory->create();
$this->registerOrder($order);
$payment = $this->orderPaymentFactory->create();
$payment->setMethod('checkmo');
$order->setPayment($payment);
$block = $this->layout->createBlock(Creditmemo::class, 'block');
$childBlock = $this->layout->addBlock(Text::class, 'creditmemo_totals', 'block');
$expectedHtml = '<b>Any html</b>';
$creditmemo = $this->creditmemoFactory->create();
$this->assertEmpty($childBlock->getCreditmemo());
$this->assertNotEquals($expectedHtml, $block->getTotalsHtml($creditmemo));
$childBlock->setText($expectedHtml);
$actualHtml = $block->getTotalsHtml($creditmemo);
$this->assertSame($creditmemo, $childBlock->getCreditmemo());
$this->assertEquals($expectedHtml, $actualHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/refunds_for_items.php
*
* @return void
*/
public function testPrintCreditmemo(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$creditmemo = $order->getCreditmemosCollection()->getFirstItem();
$this->assertNotNull($creditmemo->getId());
$this->registerOrder($order);
$this->registerCreditmemo($creditmemo);
$blockHtml = $this->renderPrintCreditmemoBlock();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'order-title')]/strong[contains(text(), '%s')]",
__('Refund #%1', $creditmemo->getIncrementId())
),
$blockHtml
),
sprintf('Title for %s was not found.', __('Refund #%1', $creditmemo->getIncrementId()))
);
$this->assertOrderInformation($order, $blockHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/refunds_for_items.php
*
* @return void
*/
public function testOrderInformation(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$block = $this->layout->createBlock(Creditmemo::class);
$orderDate = $block->formatDate($order->getCreatedAt(), \IntlDateFormatter::LONG);
$templates = [
'Order status' => [
'template' => 'Magento_Sales::order/order_status.phtml',
'expected_data' => (string)__($order->getStatusLabel()),
],
'Order date' => [
'template' => 'Magento_Sales::order/order_date.phtml',
'expected_data' => (string)__('Order Date: %1', $orderDate),
],
];
foreach ($templates as $key => $data) {
$this->assertStringContainsString(
$data['expected_data'],
strip_tags($block->setTemplate($data['template'])->toHtml()),
sprintf('%s wasn\'t found.', $key)
);
}
}
/**
* Assert order information block.
*
* @param OrderInterface $order
* @param string $html
* @return void
*/
private function assertOrderInformation(OrderInterface $order, string $html): void
{
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
"//div[contains(@class, 'block-order-details-view')]"
. "//strong[contains(text(), '" . __('Order Information') . "')]",
$html
),
__('Order Information') . ' title wasn\'t found.'
);
foreach ([$order->getShippingAddress(), $order->getBillingAddress()] as $address) {
$addressBoxXpath = sprintf("//div[contains(@class, 'box-order-%s-address')]", $address->getAddressType())
. "//address[contains(., '%s')]";
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getName()), $html),
sprintf('Customer name for %s address wasn\'t found.', $address->getAddressType())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$addressBoxXpath,
$this->countryFactory->create()->loadByCode($address->getData('country_id'))->getName()
),
$html
),
sprintf('Country for %s address wasn\'t found.', $address->getAddressType())
);
$attributes = ['company', 'street', 'city', 'region', 'postcode', 'telephone'];
foreach ($attributes as $key) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getData($key)), $html),
sprintf('%s for %s address wasn\'t found.', $key, $address->getAddressType())
);
}
}
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'box-order-shipping-method') and contains(.//strong, '%s')]"
. "//div[contains(text(), '%s')]",
__('Shipping Method'),
$order->getShippingDescription()
),
$html
),
'Shipping method for order wasn\'t found.'
);
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
/**
* Register creditmemo in registry.
*
* @param CreditmemoInterface $creditmemo
* @return void
*/
private function registerCreditmemo(CreditmemoInterface $creditmemo): void
{
$this->registry->unregister('current_creditmemo');
$this->registry->register('current_creditmemo', $creditmemo);
}
/**
* Render print creditmemo block.
*
* @return string
*/
private function renderPrintCreditmemoBlock(): string
{
$page = $this->pageFactory->create();
$page->addHandle([
'default',
'sales_order_printcreditmemo',
]);
$page->getLayout()->generateXml();
$printCreditmemoBlock = $page->getLayout()->getBlock('sales.order.print.creditmemo');
$this->assertNotFalse($printCreditmemoBlock);
return $printCreditmemoBlock->toHtml();
}
}
@@ -0,0 +1,301 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\PrintOrder;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Element\Text;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\Data\InvoiceInterface;
use Magento\Sales\Api\Data\InvoiceInterfaceFactory;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\Sales\Api\Data\OrderPaymentInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for print invoice block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class InvoiceTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var LayoutInterface */
private $layout;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var InvoiceInterfaceFactory */
private $invoiceFactory;
/** @var PageFactory */
private $pageFactory;
/** @var CountryFactory */
private $countryFactory;
/** @var OrderPaymentInterfaceFactory */
private $orderPaymentFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->invoiceFactory = $this->objectManager->get(InvoiceInterfaceFactory::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
$this->countryFactory = $this->objectManager->get(CountryFactory::class);
$this->orderPaymentFactory = $this->objectManager->create(OrderPaymentInterfaceFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
$this->registry->unregister('current_invoice');
parent::tearDown();
}
/**
* @magentoAppIsolation enabled
*
* @return void
*/
public function testGetInvoiceTotalsHtml(): void
{
$order = $this->orderFactory->create();
$this->registerOrder($order);
$payment = $this->orderPaymentFactory->create();
$payment->setMethod('checkmo');
$order->setPayment($payment);
$block = $this->layout->createBlock(Invoice::class, 'block');
$childBlock = $this->layout->addBlock(Text::class, 'invoice_totals', 'block');
$expectedHtml = '<b>Any html</b>';
$invoice = $this->invoiceFactory->create();
$this->assertEmpty($childBlock->getInvoice());
$this->assertNotEquals($expectedHtml, $block->getInvoiceTotalsHtml($invoice));
$childBlock->setText($expectedHtml);
$actualHtml = $block->getInvoiceTotalsHtml($invoice);
$this->assertSame($invoice, $childBlock->getInvoice());
$this->assertEquals($expectedHtml, $actualHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
*
* @return void
*/
public function testPrintInvoice(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$invoice = $order->getInvoiceCollection()->getFirstItem();
$this->assertNotNull($invoice->getId());
$this->assertTrue(is_numeric($invoice->getIncrementId()));
$this->registerOrder($order);
$this->registerInvoice($invoice);
$blockHtml = $this->renderPrintInvoiceBlock();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'order-title')]/strong[contains(text(), '%s')]",
__('Invoice #%1', $invoice->getIncrementId())
),
$blockHtml
),
sprintf('Title for %s was not found.', __('Invoice #%1', $invoice->getIncrementId()))
);
$this->assertOrderInformation($order, $blockHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
*
* @return void
*/
public function testPrintInvoiceWithStringPrefix(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$invoice = $order->getInvoiceCollection()->getFirstItem();
$this->assertNotNull($invoice->getId());
// set text prefix to increment id
$invoice->setIncrementId('prefix-' . $invoice->getIncrementId());
$this->assertFalse(is_numeric($invoice->getIncrementId()));
$this->registerOrder($order);
$this->registerInvoice($invoice);
$blockHtml = $this->renderPrintInvoiceBlock();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'order-title')]/strong[contains(text(), '%s')]",
__('Invoice #%1', $invoice->getIncrementId())
),
$blockHtml
),
sprintf('Title for %s was not found.', __('Invoice #%1', $invoice->getIncrementId()))
);
$this->assertOrderInformation($order, $blockHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/invoices_for_items.php
*
* @return void
*/
public function testOrderInformation(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$block = $this->layout->createBlock(Invoice::class);
$orderDate = $block->formatDate($order->getCreatedAt(), \IntlDateFormatter::LONG);
$templates = [
'Order status' => [
'template' => 'Magento_Sales::order/order_status.phtml',
'expected_data' => (string)__($order->getStatusLabel()),
],
'Order date' => [
'template' => 'Magento_Sales::order/order_date.phtml',
'expected_data' => (string)__('Order Date: %1', $orderDate),
],
];
foreach ($templates as $key => $data) {
$this->assertStringContainsString(
$data['expected_data'],
strip_tags($block->setTemplate($data['template'])->toHtml()),
sprintf('%s wasn\'t found.', $key)
);
}
}
/**
* Assert order information block.
*
* @param OrderInterface $order
* @param string $html
* @return void
*/
private function assertOrderInformation(OrderInterface $order, string $html): void
{
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
"//div[contains(@class, 'block-order-details-view')]"
. "//strong[contains(text(), '" . __('Order Information') . "')]",
$html
),
__('Order Information') . ' title wasn\'t found.'
);
foreach ([$order->getShippingAddress(), $order->getBillingAddress()] as $address) {
$addressBoxXpath = sprintf("//div[contains(@class, 'box-order-%s-address')]", $address->getAddressType())
. "//address[contains(., '%s')]";
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getName()), $html),
sprintf('Customer name for %s address wasn\'t found.', $address->getAddressType())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$addressBoxXpath,
$this->countryFactory->create()->loadByCode($address->getData('country_id'))->getName()
),
$html
),
sprintf('Country for %s address wasn\'t found.', $address->getAddressType())
);
$attributes = ['company', 'street', 'city', 'region', 'postcode', 'telephone'];
foreach ($attributes as $key) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getData($key)), $html),
sprintf('%s for %s address wasn\'t found.', $key, $address->getAddressType())
);
}
}
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'box-order-shipping-method') and contains(.//strong, '%s')]"
. "//div[contains(text(), '%s')]",
__('Shipping Method'),
$order->getShippingDescription()
),
$html
),
'Shipping method for order wasn\'t found.'
);
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
/**
* Register invoice in registry.
*
* @param InvoiceInterface $invoice
* @return void
*/
private function registerInvoice(InvoiceInterface $invoice): void
{
$this->registry->unregister('current_invoice');
$this->registry->register('current_invoice', $invoice);
}
/**
* Render print invoice block.
*
* @return string
*/
private function renderPrintInvoiceBlock(): string
{
$page = $this->pageFactory->create();
$page->addHandle([
'default',
'sales_order_printinvoice',
]);
$page->getLayout()->generateXml();
$printInvoiceBlock = $page->getLayout()->getBlock('sales.order.print.invoice');
$this->assertNotFalse($printInvoiceBlock);
return $printInvoiceBlock->toHtml();
}
}
@@ -0,0 +1,103 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Order\PrintOrder;
use Magento\Framework\App\Filesystem\DirectoryList;
use Magento\Framework\App\State;
use Magento\Framework\Filesystem;
use Magento\Framework\Filesystem\Directory\WriteInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\ViewModel\Header\LogoPathResolver as LogoPathResolverSales;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Theme\Block\Html\Header\Logo;
use Magento\Theme\ViewModel\Block\Html\Header\LogoPathResolver as LogoPathResolverDefault;
use PHPUnit\Framework\TestCase;
class LogoTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var WriteInterface
*/
private $mediaDirectory;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$filesystem = $this->objectManager->get(Filesystem::class);
$this->mediaDirectory = $filesystem->getDirectoryWrite(DirectoryList::MEDIA);
$this->objectManager->get(State::class)
->setAreaCode(\Magento\Framework\App\Area::AREA_FRONTEND);
Bootstrap::getInstance()
->loadArea(\Magento\Framework\App\Area::AREA_FRONTEND);
}
/**
* @magentoConfigFixture default_store design/header/logo_src default/logo.jpg
* @magentoConfigFixture default_store sales/identity/logo_html default/logo_sales.jpg
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function testGetLogoSrc(): void
{
$host = 'http://localhost/media/';
$defaultLogoFile= 'logo.jpg';
$defaultPath = 'logo/default/' . $defaultLogoFile;
$salesLogoFile = 'logo_sales.jpg';
$salesPath = 'sales/store/logo_html/default/' . $salesLogoFile;
$this->mediaDirectory->writeFile($defaultPath, '');
$this->mediaDirectory->writeFile($salesPath, '');
$blockArguments = ['data' =>
['logoPathResolver' => $this->objectManager->get(LogoPathResolverDefault::class)]
];
/** @var Logo $block */
$block = $this->objectManager->create(LayoutInterface::class)
->createBlock(Logo::class, 'logo', $blockArguments);
$this->assertSame($host . $defaultPath, $block->getLogoSrc());
$blockArguments = ['data' =>
['logoPathResolver' => $this->objectManager->get(LogoPathResolverSales::class)]
];
/** @var Logo $block */
$block = $this->objectManager->create(LayoutInterface::class)
->createBlock(Logo::class, 'logo', $blockArguments);
$this->assertSame($host . $salesPath, $block->getLogoSrc());
$this->mediaDirectory->delete($defaultPath);
$this->mediaDirectory->delete($salesPath);
}
/**
* Checks that fallback to header logo works fine
*
* @magentoConfigFixture default_store design/header/logo_src default/logo.jpg
* @throws \Magento\Framework\Exception\FileSystemException
*/
public function testGetLogoSrcWithFallback(): void
{
$host = 'http://localhost/media/';
$defaultLogoFile = 'logo.jpg';
$defaultPath = 'logo/default/' . $defaultLogoFile;
$this->mediaDirectory->writeFile($defaultPath, '');
$blockArguments = ['data' =>
['logoPathResolver' => $this->objectManager->get(LogoPathResolverDefault::class)]
];
/** @var Logo $block */
$block = $this->objectManager->create(LayoutInterface::class)
->createBlock(Logo::class, 'logo', $blockArguments);
$this->assertSame($host . $defaultPath, $block->getLogoSrc());
$blockArguments = ['data' =>
['logoPathResolver' => $this->objectManager->get(LogoPathResolverSales::class)]
];
/** @var Logo $block */
$block = $this->objectManager->create(LayoutInterface::class)
->createBlock(Logo::class, 'logo', $blockArguments);
$this->assertSame($host . $defaultPath, $block->getLogoSrc());
$this->mediaDirectory->delete($defaultPath);
}
}
@@ -0,0 +1,237 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order\PrintOrder;
use Magento\Directory\Model\CountryFactory;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Framework\View\Result\PageFactory;
use Magento\Sales\Api\Data\ShipmentInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for print shipment block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class ShipmentTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var LayoutInterface */
private $layout;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var PageFactory */
private $pageFactory;
/** @var CountryFactory */
private $countryFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->pageFactory = $this->objectManager->get(PageFactory::class);
$this->countryFactory = $this->objectManager->get(CountryFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_order');
$this->registry->unregister('current_shipment');
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/shipment_for_two_items.php
*
* @return void
*/
public function testPrintShipment(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$this->registerOrder($order);
$shipment = $order->getShipmentsCollection()->getFirstItem();
$this->assertNotNull($shipment->getId());
$this->registerOrder($order);
$this->registerShipment($shipment);
$blockHtml = $this->renderPrintShipmentBlock();
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'order-title')]/strong[contains(text(), '%s')]",
__('Shipment #%1', $shipment->getIncrementId())
),
$blockHtml
),
sprintf('Title for %s was not found.', __('Shipment #%1', $shipment->getIncrementId()))
);
$this->assertOrderInformation($order, $blockHtml);
}
/**
* @magentoDataFixture Magento/Sales/_files/shipment_for_order_with_customer.php
*
* @return void
*/
public function testOrderInformation(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$block = $this->layout->createBlock(Shipment::class);
$orderDate = $block->formatDate($order->getCreatedAt(), \IntlDateFormatter::LONG);
$templates = [
'Order status' => [
'template' => 'Magento_Sales::order/order_status.phtml',
'expected_data' => (string)__($order->getStatusLabel()),
],
'Order date' => [
'template' => 'Magento_Sales::order/order_date.phtml',
'expected_data' => (string)__('Order Date: %1', $orderDate),
],
];
foreach ($templates as $key => $data) {
$this->assertStringContainsString(
$data['expected_data'],
strip_tags($block->setTemplate($data['template'])->toHtml()),
sprintf('%s wasn\'t found.', $key)
);
}
}
/**
* Assert order information block.
*
* @param OrderInterface $order
* @param string $html
* @return void
*/
private function assertOrderInformation(OrderInterface $order, string $html): void
{
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
"//div[contains(@class, 'block-order-details-view')]"
. "//strong[contains(text(), '" . __('Order Information') . "')]",
$html
),
__('Order Information') . ' title wasn\'t found.'
);
foreach ([$order->getShippingAddress(), $order->getBillingAddress()] as $address) {
$addressBoxXpath = ($address->getAddressType() == 'shipping')
? "//div[contains(@class, 'box-order-shipping-address')]//address[contains(., '%s')]"
: "//div[contains(@class, 'box-order-billing-address')]//address[contains(., '%s')]";
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getName()), $html),
sprintf('Customer name for %s address wasn\'t found.', $address->getAddressType())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
$addressBoxXpath,
$this->countryFactory->create()->loadByCode($address->getData('country_id'))->getName()
),
$html
),
sprintf('Country for %s address wasn\'t found.', $address->getAddressType())
);
$attributes = ['company', 'street', 'city', 'region', 'postcode', 'telephone'];
foreach ($attributes as $key) {
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(sprintf($addressBoxXpath, $address->getData($key)), $html),
sprintf('%s for %s address wasn\'t found.', $key, $address->getAddressType())
);
}
}
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//div[contains(@class, 'box-order-shipping-method') and contains(.//strong, '%s')]"
. "//div[contains(text(), '%s')]",
__('Shipping Method'),
$order->getShippingDescription()
),
$html
),
'Shipping method for order wasn\'t found.'
);
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
/**
* Register shipment in registry.
*
* @param ShipmentInterface $shipment
* @return void
*/
private function registerShipment(ShipmentInterface $shipment): void
{
$this->registry->unregister('current_shipment');
$this->registry->register('current_shipment', $shipment);
}
/**
* Render print shipment block.
*
* @return string
*/
private function renderPrintShipmentBlock(): string
{
$page = $this->pageFactory->create();
$page->addHandle([
'default',
'sales_order_printshipment',
]);
$page->getLayout()->generateXml();
$printShipmentBlock = $page->getLayout()->getBlock('sales.order.print.shipment');
$this->assertNotFalse($printShipmentBlock);
return $printShipmentBlock->toHtml();
}
}
@@ -0,0 +1,93 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\Layout;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Tests for print shipment block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class PrintShipmentTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Registry */
private $registry;
/** @var Layout */
private $layout;
/** @var OrderInterfaceFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->registry = $this->objectManager->get(Registry::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
}
/**
* @magentoDataFixture Magento/Sales/_files/shipment_for_order_with_customer.php
*
* @return void
*/
public function testOrderInformation(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$this->registerOrder($order);
$block = $this->layout->createBlock(PrintShipment::class);
$orderDate = $block->formatDate($order->getCreatedAt(), \IntlDateFormatter::LONG);
$templates = [
'Order status' => [
'template' => 'Magento_Sales::order/order_status.phtml',
'expected_data' => (string)__($order->getStatusLabel()),
],
'Order date' => [
'template' => 'Magento_Sales::order/order_date.phtml',
'expected_data' => (string)__('Order Date: %1', $orderDate),
],
];
foreach ($templates as $key => $data) {
$this->assertStringContainsString(
$data['expected_data'],
strip_tags($block->setTemplate($data['template'])->toHtml()),
sprintf('%s wasn\'t found.', $key)
);
}
}
/**
* Register order in registry.
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(OrderInterface $order): void
{
$this->registry->unregister('current_order');
$this->registry->register('current_order', $order);
}
}
@@ -0,0 +1,131 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Order;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\Element\Context;
use Magento\Framework\View\Element\Text;
use Magento\Framework\View\Layout;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Tests for order totals block.
*
* @magentoAppArea frontend
* @magentoDbIsolation enabled
*/
class TotalsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Layout */
private $layout;
/** @var Totals */
private $block;
/** @var OrderInterfaceFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->block = $this->layout->createBlock(Totals::class, 'block');
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
}
/**
* @magentoAppIsolation enabled
*
* @return void
*/
public function testToHtmlChildrenInitialized(): void
{
$this->block->setOrder($this->orderFactory->create())->setTemplate('order/totals.phtml');
$context = $this->objectManager->get(Context::class);
$childOne = $this->getMockBuilder(Text::class)
->setMethods(['initTotals'])
->setConstructorArgs([$context])
->getMock();
$childOne->expects($this->once())->method('initTotals');
$this->layout->addBlock($childOne, 'child1', 'block');
$childTwo = $this->getMockBuilder(Text::class)
->setMethods(['initTotals'])
->setConstructorArgs([$context])
->getMock();
$childTwo->expects($this->once())->method('initTotals');
$this->layout->addBlock($childTwo, 'child2', 'block');
$childThree = $this->getMockBuilder(Text::class)
->setMethods(['initTotals'])
->setConstructorArgs([$context])
->getMock();
$childThree->expects($this->once())->method('initTotals');
$this->layout->addBlock($childThree, 'child3', 'block');
$this->block->toHtml();
}
/**
* @magentoDataFixture Magento/Sales/_files/customer_order_with_two_items.php
*
* @return void
*/
public function testOrderTotalsBlock(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000555');
$blockHtml = $this->block->setTemplate('Magento_Sales::order/totals.phtml')->setOrder($order)->toHtml();
$message = '"%s" for order wasn\'t found or not equals to %01.2f';
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//th[contains(text(), '%s')]/following-sibling::td/span[contains(text(), '%01.2f')]",
__('Subtotal'),
$order->getSubtotal()
),
$blockHtml
),
sprintf($message, __('Subtotal'), $order->getSubtotal())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//th[contains(text(), '%s')]/following-sibling::td/span[contains(text(), '%01.2f')]",
__('Shipping & Handling'),
$order->getShippingAmount()
),
$blockHtml
),
sprintf($message, __('Shipping & Handling'), $order->getShippingAmount())
);
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
sprintf(
"//tr[contains(@class, 'grand_total') and contains(.//strong, '%s')]"
. "//span[contains(text(), '%01.2f')]",
__('Grand Total'),
$order->getGrandTotal()
),
$blockHtml
),
sprintf($message, __('Grand Total'), $order->getGrandTotal())
);
}
}