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,39 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Adminhtml\Items;
/**
* @magentoAppArea adminhtml
*/
class AbstractTest extends \PHPUnit\Framework\TestCase
{
public function testGetItemExtraInfoHtml()
{
/** @var $layout \Magento\Framework\View\Layout */
$layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()->get(
\Magento\Framework\View\LayoutInterface::class
);
/** @var $block \Magento\Sales\Block\Adminhtml\Items\AbstractItems */
$block = $layout->createBlock(\Magento\Sales\Block\Adminhtml\Items\AbstractItems::class, 'block');
$item = new \Magento\Framework\DataObject();
$this->assertEmpty($block->getItemExtraInfoHtml($item));
$expectedHtml = '<html><body>some data</body></html>';
/** @var $childBlock \Magento\Framework\View\Element\Text */
$childBlock = $layout->addBlock(
\Magento\Framework\View\Element\Text::class,
'other_block',
'block',
'order_item_extra_info'
);
$childBlock->setText($expectedHtml);
$this->assertEquals($expectedHtml, $block->getItemExtraInfoHtml($item));
$this->assertSame($item, $childBlock->getItem());
}
}
@@ -0,0 +1,50 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Items\Column;
/**
* @magentoAppArea adminhtml
*/
class NameTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Name
*/
private $block;
protected function setUp(): void
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
/** @var $layout \Magento\Framework\View\Layout */
$layout = $objectManager->create(\Magento\Framework\View\LayoutInterface::class);
/** @var $block \Magento\Sales\Block\Adminhtml\Items\AbstractItems */
$this->block = $layout->createBlock(Name::class, 'block');
}
public function testTruncateString() : void
{
$remainder = '';
$this->assertEquals(
'12345',
$this->block->truncateString('1234567890', 5, '', $remainder)
);
}
public function testGetFormattedOptiong() : void
{
$this->assertEquals(
[
'value' => '1234567890123456789012345678901234567890123456789012345',
'remainder' => '67890',
],
$this->block->getFormattedOption(
'123456789012345678901234567890123456789012345678901234567890'
)
);
}
}
@@ -0,0 +1,186 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Address;
use Magento\Directory\Model\ResourceModel\Country\CollectionFactory;
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\App\Config;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Checks order address edit form block
*
* @see \Magento\Sales\Block\Adminhtml\Order\Address\Form
*
* @magentoAppArea adminhtml
*/
class FormTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Form */
private $block;
/** @var Registry */
private $registry;
/** @var OrderInterfaceFactory */
private $orderFactory;
/** @var CollectionFactory */
private $countryCollectionFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Form::class);
$this->orderFactory = $this->objectManager->get(OrderInterfaceFactory::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->objectManager->removeSharedInstance(Config::class);
$this->countryCollectionFactory = $this->objectManager->get(CollectionFactory::class);
}
/**
* @return void
*/
protected function tearDown(): void
{
$this->registry->unregister('order_address');
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*
* @return void
*/
public function testGetFormValues(): void
{
$address = $this->getOrderAddress('100000001');
$this->registerOrderAddress($address);
$formValues = $this->block->getFormValues();
$this->assertEquals($address->getData(), $formValues);
}
/**
* @magentoDbIsolation disabled
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
* @magentoDataFixture Magento/Sales/_files/order_with_customer.php
* @magentoConfigFixture default_store general/country/default US
* @magentoConfigFixture default_store general/country/allow US
* @magentoConfigFixture fixture_second_store_store general/country/default UY
* @magentoConfigFixture fixture_second_store_store general/country/allow UY
* @return void
*/
public function testCountryIdInAllowedList(): void
{
$address = $this->getOrderAddress('100000001');
$this->registerOrderAddress($address);
$this->assertEquals('US', $address->getCountryId());
$this->assertCountryField('US');
}
/**
* @magentoDbIsolation disabled
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
* @magentoDataFixture Magento/Sales/_files/order_with_customer.php
* @magentoConfigFixture default_store general/country/default CA
* @magentoConfigFixture default_store general/country/allow CA
* @magentoConfigFixture fixture_second_store_store general/country/default UY
* @magentoConfigFixture fixture_second_store_store general/country/allow UY
* @return void
*/
public function testCountryIdInNotAllowedList(): void
{
$address = $this->getOrderAddress('100000001');
$this->registerOrderAddress($address);
$this->assertCountryField('CA');
}
/**
* @magentoDbIsolation disabled
*
* @magentoDataFixture Magento/Sales/_files/order_with_customer_on_second_website.php
*
* @magentoConfigFixture default_store general/country/default UA
* @magentoConfigFixture default_store general/country/allow UA
*
* @return void
*/
public function testFormRenderedWithSelectRegionInput(): void
{
$address = $this->getOrderAddress('100000001');
$this->registerOrderAddress($address);
$form = $this->block->getForm();
$countryElement = $form->getElement('country_id');
$this->assertNotNull($countryElement);
$this->assertEquals('US', $countryElement->getEscapedValue());
$html = $form->toHtml();
$regionIdSelectXpath = '//select[@id=\'region_id\']';
$this->assertEquals(1, Xpath::getElementsCountForXpath($regionIdSelectXpath, $html));
$countryOptionsXpath = '//select[@id=\'country_id\']/option';
$allowedCountriesCount = count($this->countryCollectionFactory->create()->loadByStore());
$this->assertEquals($allowedCountriesCount, Xpath::getElementsCountForXpath($countryOptionsXpath, $html));
}
/**
* Prepares address edit from block.
*
* @param OrderAddressInterface $address
* @return void
*/
private function registerOrderAddress(OrderAddressInterface $address): void
{
$this->registry->unregister('order_address');
$this->registry->register('order_address', $address);
}
/**
* Return order billing address.
*
* @param string $orderIncrementId
* @return OrderAddressInterface
*/
private function getOrderAddress(string $orderIncrementId): OrderAddressInterface
{
/** @var OrderInterface $order */
$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
return $order->getBillingAddress();
}
/**
* Asserts country field data.
*
* @param string $countryCode
* @return void
*/
private function assertCountryField(string $countryCode): void
{
$countryIdField = $this->block->getForm()->getElement('country_id');
$this->assertEquals($countryCode, $countryIdField->getValue());
$options = $countryIdField->getValues();
$this->assertCount(1, $options);
$firstOption = reset($options);
$this->assertEquals($countryCode, $firstOption['value']);
}
}
@@ -0,0 +1,111 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order;
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\Model\Order\Address as AddressType;
use Magento\Sales\Model\OrderFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Checks order address edit block
*
* @see \Magento\Sales\Block\Adminhtml\Order\Address
*
* @magentoAppArea adminhtml
*/
class AddressTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Address */
private $block;
/** @var Registry */
private $registry;
/** @var OrderFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Address::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('order_address');
parent::tearDown();
}
/**
* @dataProvider addressTypeProvider
*
* @magentoDataFixture Magento/Sales/_files/order.php
*
* @param string $type
* @return void
*/
public function testGetHeaderText(string $type): void
{
$order = $this->orderFactory->create()->loadByIncrementId(100000001);
$address = $this->getAddressByType($order, $type);
$this->registry->unregister('order_address');
$this->registry->register('order_address', $address);
$text = $this->block->getHeaderText();
$this->assertEquals(
(string)__('Edit Order %1 %2 Address', $order->getIncrementId(), ucfirst($type)),
(string)$text
);
}
/**
* @return array
*/
public function addressTypeProvider(): array
{
return [
'billing_address' => [
AddressType::TYPE_BILLING,
],
'shipping_address' => [
AddressType::TYPE_SHIPPING,
]
];
}
/**
* Get address by address type
*
* @param OrderInterface $order
* @param string $type
* @return OrderAddressInterface|null
*/
private function getAddressByType(OrderInterface $order, string $type): ?OrderAddressInterface
{
return $type === AddressType::TYPE_BILLING ? $order->getBillingAddress() : $order->getShippingAddress();
}
}
@@ -0,0 +1,125 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Create;
use Magento\Config\Model\Config\Backend\Admin\Custom;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Model\Address\AddressModelInterface;
use Magento\Customer\Model\CustomerRegistry;
use Magento\Customer\Model\Metadata\FormFactory;
use Magento\Framework\App\Config\ScopeConfigInterface;
use Magento\Framework\Data\Form;
use Magento\Framework\View\Element\BlockInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Class consist of basic logic to check address form
*/
abstract class AbstractAddressFormTest extends TestCase
{
/** @var LayoutInterface */
protected $layout;
/** @var CustomerRegistry */
protected $customerRegistry;
/** @var CartRepositoryInterface */
private $quoteRepository;
/** @var BlockInterface */
private $form;
/** @var ScopeConfigInterface */
private $config;
/** @var array */
private $formAttributes;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$objectManager = Bootstrap::getObjectManager();
$this->layout = $objectManager->get(LayoutInterface::class);
$this->form = $this->getFormBlock();
$this->customerRegistry = $objectManager->get(CustomerRegistry::class);
$this->quoteRepository = $objectManager->get(CartRepositoryInterface::class);
$this->config = $objectManager->get(ScopeConfigInterface::class);
$this->formAttributes = array_keys($objectManager->get(FormFactory::class)
->create('customer_address', 'adminhtml_customer_address')->getAttributes());
}
/**
* Check that all form values are filled according to address attributes values
*
* @param int $customerId
* @return void
*/
protected function checkFormValuesExist(int $customerId): void
{
$address = $this->getAddress($customerId);
$form = $this->prepareForm($customerId);
foreach ($this->formAttributes as $attribute) {
$this->assertEquals($address->getData($attribute), $form->getElement($attribute)->getValue());
}
}
/**
* Check that form values is empty
*
* @param int $customerId
* @return void
*/
protected function checkFormValuesAreEmpty(int $customerId): void
{
$defaultCountryCode = $this->config->getValue(Custom::XML_PATH_GENERAL_COUNTRY_DEFAULT);
$form = $this->prepareForm($customerId);
foreach ($this->formAttributes as $attribute) {
if ($attribute === AddressInterface::COUNTRY_ID) {
$this->assertEquals($defaultCountryCode, $form->getElement($attribute)->getValue());
continue;
}
$this->assertNull($form->getElement($attribute)->getValue());
}
}
/**
* Prepare form
*
* @param int $customerId
* @return Form
*/
private function prepareForm(int $customerId): Form
{
$quote = $this->quoteRepository->getForCustomer($customerId);
$this->form->getCreateOrderModel()->setQuote($quote);
return $this->form->getForm();
}
/**
* Get form block
*
* @return BlockInterface
*/
abstract protected function getFormBlock(): BlockInterface;
/**
* Get appropriate customer address
*
* @param int $customerId
* @return AddressModelInterface
*/
abstract protected function getAddress(int $customerId): AddressModelInterface;
}
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Create\Billing;
use Magento\Customer\Model\Address\AddressModelInterface;
use Magento\Framework\View\Element\BlockInterface;
use Magento\Sales\Block\Adminhtml\Order\Create\AbstractAddressFormTest;
use Magento\Sales\Block\Adminhtml\Order\Create\Shipping\Address;
/**
* Class checks billing address form behaviour
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*
*/
class FormTest extends AbstractAddressFormTest
{
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*
* @return void
*/
public function testFormValuesExist(): void
{
$this->checkFormValuesExist(1);
}
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
*
* @return void
*/
public function testFormValuesAreEmpty(): void
{
$this->checkFormValuesAreEmpty(1);
}
/**
* @inheritdoc
*/
protected function getFormBlock(): BlockInterface
{
return $this->layout->createBlock(Address::class);
}
/**
* @inheritdoc
*/
protected function getAddress(int $customerId): AddressModelInterface
{
return $this->customerRegistry->retrieve($customerId)->getDefaultBillingAddress();
}
}
@@ -0,0 +1,51 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Adminhtml\Order\Create\Billing\Method;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
/**
* Test for order billing method form.
*
* @magentoAppArea adminhtml
*/
class FormTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var LayoutInterface
*/
private $layout;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->layout = $this->objectManager->get(LayoutInterface::class);
}
/**
* Checks if billing method form generates contentUpdated event
* to parse elements with data-mage-init attributes.
*/
public function testContentUpdated()
{
/** @var Form $block */
$block = $this->layout->createBlock(Form::class, 'order_billing_method');
$block->setTemplate('Magento_Sales::order/create/billing/method/form.phtml');
$html = $block->toHtml();
$this->assertStringContainsString('mage.apply()', $html);
}
}
@@ -0,0 +1,73 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
/**
* Test class for \Magento\Sales\Block\Adminhtml\Order\Create\Form\AbstractForm
*/
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
use Magento\Customer\Api\Data\OptionInterfaceFactory;
use Magento\Customer\Api\Data\ValidationRuleInterfaceFactory;
/**
* Class AbstractTest
*
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AbstractTest extends \PHPUnit\Framework\TestCase
{
/**
* @magentoAppIsolation enabled
*/
public function testAddAttributesToForm()
{
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
\Magento\TestFramework\Helper\Bootstrap::getInstance()
->loadArea(\Magento\Backend\App\Area\FrontNameResolver::AREA_CODE);
$objectManager->get(\Magento\Framework\View\DesignInterface::class)->setDefaultDesignTheme();
$arguments = [
$objectManager->get(\Magento\Backend\Block\Template\Context::class),
$objectManager->get(\Magento\Backend\Model\Session\Quote::class),
$objectManager->get(\Magento\Sales\Model\AdminOrder\Create::class),
$objectManager->get(\Magento\Framework\Pricing\PriceCurrencyInterface::class),
$objectManager->get(\Magento\Framework\Data\FormFactory::class),
$objectManager->get(\Magento\Framework\Reflection\DataObjectProcessor::class)
];
/** @var $block \Magento\Sales\Block\Adminhtml\Order\Create\Form\AbstractForm */
$block = $this->getMockForAbstractClass(
\Magento\Sales\Block\Adminhtml\Order\Create\Form\AbstractForm::class,
$arguments
);
$block->setLayout($objectManager->create(\Magento\Framework\View\Layout::class));
$method = new \ReflectionMethod(
\Magento\Sales\Block\Adminhtml\Order\Create\Form\AbstractForm::class,
'_addAttributesToForm'
);
$method->setAccessible(true);
/** @var $formFactory \Magento\Framework\Data\FormFactory */
$formFactory = $objectManager->get(\Magento\Framework\Data\FormFactory::class);
$form = $formFactory->create();
$fieldset = $form->addFieldset('test_fieldset', []);
/** @var \Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory $attributeMetadataFactory */
$attributeMetadataFactory =
$objectManager->create(\Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory::class);
$dateAttribute = $attributeMetadataFactory->create()->setAttributeCode('date')
->setBackendType('datetime')
->setFrontendInput('date')
->setFrontendLabel('Date');
$attributes = ['date' => $dateAttribute];
$method->invoke($block, $attributes, $fieldset);
$element = $form->getElement('date');
$this->assertNotNull($element);
$this->assertNotEmpty($element->getDateFormat());
}
}
@@ -0,0 +1,396 @@
<?php
/**
* Test class for \Magento\Sales\Block\Adminhtml\Order\Create\Form\Account
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
use Magento\Backend\Block\Template\Context;
use Magento\Backend\Model\Session\Quote as SessionQuote;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterface;
use Magento\Customer\Api\Data\AttributeMetadataInterfaceFactory;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Customer\Model\Data\Option;
use Magento\Customer\Model\Metadata\Form;
use Magento\Customer\Model\Metadata\FormFactory;
use Magento\Framework\App\RequestInterface as Request;
use Magento\Framework\View\LayoutInterface;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
/**
* Class for test Account
*
* @magentoAppArea adminhtml
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class AccountTest extends TestCase
{
/**
* @var Account
*/
private $accountBlock;
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var SessionQuote|MockObject
*/
private $session;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
parent::setUp();
}
/**
* Test for get form with existing customer
*
* @magentoDataFixture Magento/Customer/_files/customer.php
*/
public function testGetFormWithCustomer()
{
$customerGroup = 2;
$quote = $this->objectManager->create(Quote::class);
$this->session = $this->getMockBuilder(SessionQuote::class)
->disableOriginalConstructor()
->setMethods(['getCustomerId','getQuote'])
->getMock();
$this->session->method('getQuote')
->willReturn($quote);
$this->session->method('getCustomerId')
->willReturn(1);
/** @var LayoutInterface $layout */
$layout = $this->objectManager->get(LayoutInterface::class);
$this->accountBlock = $layout->createBlock(
Account::class,
'address_block' . rand(),
['sessionQuote' => $this->session]
);
$fixtureCustomerId = 1;
/** @var CustomerRepositoryInterface $customerRepository */
$customerRepository = $this->objectManager->get(CustomerRepositoryInterface::class);
/** @var CustomerInterface $customer */
$customer = $customerRepository->getById($fixtureCustomerId);
$customer->setGroupId($customerGroup);
$customerRepository->save($customer);
$expectedFields = ['group_id', 'email'];
$form = $this->accountBlock->getForm();
$this->assertEquals(1, $form->getElements()->count(), "Form has invalid number of fieldsets");
$fieldset = $form->getElements()[0];
$content = $form->toHtml();
$this->assertEquals(count($expectedFields), $fieldset->getElements()->count());
foreach ($fieldset->getElements() as $element) {
$this->assertTrue(
in_array($element->getId(), $expectedFields),
sprintf('Unexpected field "%s" in form.', $element->getId())
);
}
self::assertMatchesRegularExpression(
'/<option value="'.$customerGroup.'".*?selected="selected"\>Wholesale\<\/option\>/is',
$content,
'The Customer Group specified for the chosen customer should be selected.'
);
self::assertStringContainsString(
'value="'.$customer->getEmail().'"',
$content,
'The Customer Email specified for the chosen customer should be input '
);
}
/**
* Tests a case when user defined custom attribute has default value.
*
* @magentoDataFixture Magento/Store/_files/core_second_third_fixturestore.php
* @magentoConfigFixture current_store customer/create_account/default_group 2
* @magentoConfigFixture secondstore_store customer/create_account/default_group 3
*/
public function testGetFormWithUserDefinedAttribute()
{
/** @var StoreManagerInterface $storeManager */
$storeManager = Bootstrap::getObjectManager()->get(StoreManagerInterface::class);
$secondStore = $storeManager->getStore('secondstore');
$quoteSession = $this->objectManager->get(SessionQuote::class);
$quoteSession->setStoreId($secondStore->getId());
$formFactory = $this->getFormFactoryMock();
$this->objectManager->addSharedInstance($formFactory, FormFactory::class);
/** @var LayoutInterface $layout */
$layout = $this->objectManager->get(LayoutInterface::class);
$accountBlock = $layout->createBlock(
Account::class,
'address_block' . rand()
);
$form = $accountBlock->getForm();
$form->setUseContainer(true);
$content = $form->toHtml();
self::assertMatchesRegularExpression(
'/\<option value="1".*?selected="selected"\>Yes\<\/option\>/is',
$content,
'Default value for user defined custom attribute should be selected.'
);
self::assertMatchesRegularExpression(
'/<option value="3".*?selected="selected"\>Retailer\<\/option\>/is',
$content,
'The Customer Group specified for the chosen store should be selected.'
);
}
/**
* Test for get form with default customer group
*
*/
public function testGetFormWithDefaultCustomerGroup()
{
$customerGroup = 0;
$quote = $this->objectManager->create(Quote::class);
$quote->setCustomerGroupId($customerGroup);
$this->session = $this->getMockBuilder(SessionQuote::class)
->disableOriginalConstructor()
->setMethods(['getCustomerId', 'getQuote'])
->getMock();
$this->session->method('getQuote')
->willReturn($quote);
$this->session->method('getCustomerId')
->willReturn(1);
$formFactory = $this->getFormFactoryMock();
$this->objectManager->addSharedInstance($formFactory, FormFactory::class);
/** @var LayoutInterface $layout */
$layout = $this->objectManager->get(LayoutInterface::class);
$accountBlock = $layout->createBlock(
Account::class,
'address_block' . rand(),
['sessionQuote' => $this->session]
);
$expectedGroupId = 1;
$form = $accountBlock->getForm();
self::assertEquals(
$expectedGroupId,
$form->getElement('group_id')->getValue(),
'The Customer Group specified for the chosen customer should be selected.'
);
}
/**
* Test for get form with customer group based on vat id validation
*
* @dataProvider getDataForVatValidatedCustomer
* @param int $defaultCustomerGroupId
* @param int $vatValidatedCustomerGroupId
* @param array $customerDetails
* @param array $orderDetails
* @return void
*/
public function testGetFormWithVatValidatedCustomerGroup(
int $defaultCustomerGroupId,
int $vatValidatedCustomerGroupId,
array $customerDetails,
array $orderDetails
): void {
$contextMock = $this->getMockBuilder(Context::class)
->disableOriginalConstructor()
->disableOriginalClone()
->getMock();
$requestMock = $this->getMockBuilder(Request::class)
->getMockForAbstractClass();
$contextMock->expects($this->once())
->method('getRequest')
->willReturn($requestMock);
$requestMock->expects($this->any())
->method('getParam')
->willReturn($orderDetails);
$quote = $this->objectManager->create(Quote::class);
$quote->setCustomerGroupId($defaultCustomerGroupId);
$quote->setData($customerDetails);
$this->session = $this->getMockBuilder(SessionQuote::class)
->disableOriginalConstructor()
->setMethods(['getCustomerId', 'getQuote'])
->getMock();
$this->session->method('getQuote')
->willReturn($quote);
$this->session->method('getCustomerId')
->willReturn($customerDetails['customer_id']);
$formFactory = $this->getFormFactoryMock();
$this->objectManager->addSharedInstance($formFactory, FormFactory::class);
/** @var LayoutInterface $layout */
$layout = $this->objectManager->get(LayoutInterface::class);
$accountBlock = $layout->createBlock(
Account::class,
'address_block' . rand(),
[
'context' => $contextMock,
'sessionQuote' => $this->session
]
);
$form = $accountBlock->getForm();
self::assertEquals(
$vatValidatedCustomerGroupId,
$form->getElement('group_id')->getValue(),
'The Customer Group specified for the chosen customer should be selected.'
);
}
/**
* Data provider for vat validated customer group id
*
* @return array
* @SuppressWarnings(PHPMD.ExcessiveMethodLength)
*/
public function getDataForVatValidatedCustomer(): array
{
return [
'Validated customer group id when its set in quote' => [
'defaultCustomerGroupId' => 0,
'vatValidatedCustomerGroupId' => 3,
'customerDetails' => [
'entity_id' => '35',
'store_id' => 1,
'created_at' => '2020-11-09 01:03:35',
'updated_at' => '2020-11-09 05:44:07',
'customer_id' => 1,
'customer_tax_class_id' => '3',
'customer_group_id' => 3,
'customer_email' => 'test@test.com',
'customer_prefix' => null,
'customer_firstname' => null,
'customer_middlename' => null,
'customer_lastname' => null,
'customer_suffix' => null,
'customer_dob' => null,
],
'orderDetails' => [
'account' => [
'group_id' => 3,
'email' => 'test@test.com'
]
]
],
'Validated customer group id when its set in request' => [
'defaultCustomerGroupId' => 0,
'vatValidatedCustomerGroupId' => 3,
'customerDetails' => [
'entity_id' => '35',
'store_id' => 1,
'created_at' => '2020-11-09 01:03:35',
'updated_at' => '2020-11-09 05:44:07',
'customer_id' => 1,
'customer_tax_class_id' => '3',
'customer_group_id' => null,
'customer_email' => 'test@test.com',
'customer_prefix' => null,
'customer_firstname' => null,
'customer_middlename' => null,
'customer_lastname' => null,
'customer_suffix' => null,
'customer_dob' => null,
],
'orderDetails' => [
'account' => [
'group_id' => 3,
'email' => 'test@test.com'
]
]
]
];
}
/**
* Creates a mock for Form object.
*
* @return MockObject
*/
private function getFormFactoryMock(): MockObject
{
/** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
$attributeMetadataFactory = $this->objectManager->create(AttributeMetadataInterfaceFactory::class);
$booleanAttribute = $attributeMetadataFactory->create()
->setAttributeCode('boolean')
->setBackendType('boolean')
->setFrontendInput('boolean')
->setDefaultValue('1')
->setFrontendLabel('Yes/No');
/** @var Form|MockObject $form */
$form = $this->getMockBuilder(Form::class)
->disableOriginalConstructor()
->getMock();
$form->method('getUserAttributes')->willReturn([$booleanAttribute]);
$form->method('getSystemAttributes')->willReturn([$this->createCustomerGroupAttribute()]);
$formFactory = $this->getMockBuilder(FormFactory::class)
->disableOriginalConstructor()
->getMock();
$formFactory->method('create')->willReturn($form);
return $formFactory;
}
/**
* Creates a customer group attribute object.
*
* @return AttributeMetadataInterface
*/
private function createCustomerGroupAttribute(): AttributeMetadataInterface
{
/** @var Option $option1 */
$option1 = $this->objectManager->create(Option::class);
$option1->setValue(2);
$option1->setLabel('Wholesale');
/** @var Option $option2 */
$option2 = $this->objectManager->create(Option::class);
$option2->setValue(3);
$option2->setLabel('Retailer');
/** @var AttributeMetadataInterfaceFactory $attributeMetadataFactory */
$attributeMetadataFactory = $this->objectManager->create(AttributeMetadataInterfaceFactory::class);
$attribute = $attributeMetadataFactory->create()
->setAttributeCode('group_id')
->setBackendType('static')
->setFrontendInput('select')
->setOptions([$option1, $option2])
->setIsRequired(true);
return $attribute;
}
}
@@ -0,0 +1,273 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Create\Form;
use Magento\Backend\Model\Session\Quote as QuoteSession;
use Magento\Customer\Api\CustomerRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterface;
use Magento\Customer\Api\Data\CustomerInterface;
use Magento\Directory\Model\ResourceModel\Country\Collection;
use Magento\Framework\Data\Form\Element\AbstractElement;
use Magento\Framework\Data\Form\Element\Fieldset;
use Magento\Store\Api\Data\StoreInterface;
use Magento\Store\Api\Data\WebsiteInterface;
use Magento\Store\Api\StoreRepositoryInterface;
use Magento\Store\Api\WebsiteRepositoryInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\MockObject\MockObject as MockObject;
/**
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
* @magentoAppArea adminhtml
*/
class AddressTest extends \PHPUnit\Framework\TestCase
{
/**
* @var ObjectManager
*/
private $objectManager;
/**
* @var Address
*/
private $block;
/**
* @var QuoteSession|MockObject
*/
private $quoteSession;
/**
* @inheritdoc
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->quoteSession = $this->getMockBuilder(QuoteSession::class)
->disableOriginalConstructor()
->setMethods(['getCustomerId', 'getStore', 'getStoreId', 'getQuote'])
->getMock();
$this->block = $this->objectManager->create(
Address::class,
['sessionQuote' => $this->quoteSession]
);
}
/**
* Checks address collection.
*
* @magentoDataFixture Magento/Customer/Fixtures/customer_2_addresses.php
*/
public function testGetAddressCollection()
{
$website = $this->getWebsite('base');
$customer = $this->getCustomer('customer@example.com', (int)$website->getId());
$addresses = $customer->getAddresses();
$this->quoteSession->method('getCustomerId')
->willReturn($customer->getId());
$actual = $this->block->getAddressCollection();
self::assertNotEmpty($actual);
self::assertEquals($addresses, $actual);
}
/**
* Checks address collection output encoded to json.
*
* @magentoDataFixture Magento/Customer/Fixtures/customer_sec_website_2_addresses.php
*/
public function testGetAddressCollectionJson()
{
$website = $this->getWebsite('test');
$customer = $this->getCustomer('customer.web@example.com', (int)$website->getId());
$store = $this->getStore('fixture_second_store');
$this->quoteSession->method('getStore')
->willReturn($store);
$this->quoteSession->method('getCustomerId')
->willReturn($customer->getId());
$addresses = $customer->getAddresses();
$expected = [
0 => [
'firstname' => false,
'lastname' => false,
'company' => false,
'street' => '',
'city' => false,
'country_id' => 'US',
'region' => false,
'region_id' => false,
'postcode' => false,
'telephone' => false,
'vat_id' => false,
],
$addresses[0]->getId() => [
'firstname' => 'John',
'lastname' => 'Smith',
'company' => false,
'street' => 'Green str, 67',
'city' => 'Culver City',
'country_id' => 'US',
'region' => 'California',
'region_id' => 12,
'postcode' => '90230',
'telephone' => '3468676',
'vat_id' => false,
'prefix' => false,
'middlename' => false,
'suffix' => false,
'fax' => false
],
$addresses[1]->getId() => [
'telephone' => '845454465',
'postcode' => '10178',
'country_id' => 'DE',
'city' => 'Berlin',
'street' => 'Tunnel Alexanderpl',
'firstname' => 'John',
'lastname' => 'Smith',
'company' => false,
'region' => false,
'region_id' => 0,
'vat_id' => false,
'prefix' => false,
'middlename' => false,
'suffix' => false,
'fax' => false
]
];
$actual = json_decode($this->block->getAddressCollectionJson(), true);
self::assertEquals($expected, $actual);
}
/**
* Checks one line address formatting
*/
public function testGetAddressAsString()
{
$data = [
'firstname' => 'John',
'lastname' => 'Smith',
'company' => 'Test Company',
'street' => 'Green str, 67',
'city' => 'Culver City',
'country_id' => 'US',
'region' => 'California',
'region_id' => 12,
'postcode' => '90230',
'telephone' => '3468676',
];
$address = $this->objectManager->create(AddressInterface::class, ['data' => $data]);
$expected = 'John Smith, Green str, 67, Culver City, California 90230, United States';
self::assertEquals($expected, $this->block->getAddressAsString($address));
}
public function testGetForm()
{
$expectedFields = [
'prefix',
'firstname',
'middlename',
'lastname',
'suffix',
'company',
'street',
'city',
'country_id',
'region',
'region_id',
'postcode',
'telephone',
'fax',
'vat_id',
];
$form = $this->block->getForm();
self::assertEquals(1, $form->getElements()->count(), 'Form has invalid number of fieldsets');
/** @var Fieldset $fieldset */
$fieldset = $form->getElements()[0];
self::assertEquals(
count($expectedFields),
$fieldset->getElements()->count(),
'Form has invalid number of fields'
);
/** @var AbstractElement $element */
foreach ($fieldset->getElements() as $element) {
self::assertTrue(
in_array($element->getId(), $expectedFields),
sprintf('Unexpected field "%s" in form.', $element->getId())
);
}
/** @var \Magento\Framework\Data\Form\Element\Select $countryIdField */
$countryIdField = $fieldset->getElements()->searchById('country_id');
$actual = Xpath::getElementsCountForXpath('//option', $countryIdField->getElementHtml());
self::assertEquals($this->getNumberOfCountryOptions(), $actual);
}
/**
* Gets customer entity.
*
* @param string $email
* @param int $websiteId
* @return CustomerInterface
* @throws \Magento\Framework\Exception\LocalizedException
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getCustomer(string $email, int $websiteId): CustomerInterface
{
/** @var CustomerRepositoryInterface $repository */
$repository = $this->objectManager->get(CustomerRepositoryInterface::class);
return $repository->get($email, $websiteId);
}
/**
* Gets website by code.
*
* @param string $code
* @return WebsiteInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getWebsite(string $code): WebsiteInterface
{
/** @var WebsiteRepositoryInterface $repository */
$repository = $this->objectManager->get(WebsiteRepositoryInterface::class);
return $repository->get($code);
}
/**
* Gets store by code.
*
* @param string $code
* @return StoreInterface
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getStore(string $code): StoreInterface
{
/** @var StoreRepositoryInterface $repository */
$repository = $this->objectManager->get(StoreRepositoryInterface::class);
return $repository->get($code);
}
/**
* @return int
*/
private function getNumberOfCountryOptions()
{
/** @var Collection $countryCollection */
$countryCollection = $this->objectManager->create(Collection::class);
return count($countryCollection->toOptionArray());
}
}
@@ -0,0 +1,201 @@
<?php
/**
* Test class for Form
*
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Adminhtml\Order\Create;
use Magento\Backend\Model\Session\Quote as QuoteSession;
use Magento\Customer\Api\AddressRepositoryInterface;
use Magento\Customer\Api\Data\AddressInterfaceFactory;
use Magento\Customer\Api\Data\RegionInterfaceFactory;
use Magento\Framework\Api\SearchCriteriaBuilder;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Quote\Api\CartRepositoryInterface;
use Magento\Quote\Model\Quote;
use Magento\Store\Model\Store;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\MockObject\MockObject as MockObject;
/**
* @magentoAppArea adminhtml
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
class FormTest extends \PHPUnit\Framework\TestCase
{
/**
* @var Form
*/
private $block;
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var QuoteSession|MockObject
*/
private $session;
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->session = $this->getMockBuilder(QuoteSession::class)
->disableOriginalConstructor()
->setMethods(['getCustomerId', 'getQuote', 'getStoreId', 'getStore', 'getQuoteId'])
->getMock();
$this->session->method('getCustomerId')
->willReturn(1);
$this->session->method('getStoreId')
->willReturn(1);
$store = $this->getMockBuilder(Store::class)
->disableOriginalConstructor()
->setMethods(['getCurrentCurrencyCode'])
->getMock();
$store->method('getCurrentCurrencyCode')
->willReturn('USD');
$this->session->method('getStore')
->willReturn($store);
/** @var LayoutInterface $layout */
$layout = $this->objectManager->get(LayoutInterface::class);
$this->block = $layout->createBlock(
Form::class,
'order_create_block' . random_int(0, PHP_INT_MAX),
['sessionQuote' => $this->session]
);
parent::setUp();
}
/**
* Checks if all needed order's data is correctly returned to the form.
*
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoDataFixture Magento/Sales/_files/quote.php
*/
public function testOrderDataJson()
{
$customerId = 1;
$quote = $this->getQuote('test01');
$this->session->method('getQuote')
->willReturn($quote);
$this->session->method('getQuoteId')
->willReturn($quote->getId());
$addressData = $this->getAddressData();
$addressIds = $this->setUpMockAddress($customerId, $addressData);
$expected = [
'customer_id' => $customerId,
'addresses' => [
$addressIds[0] => $addressData[0],
$addressIds[1] => $addressData[1]
],
'store_id' => 1,
'currency_symbol' => '$',
'shipping_method_reseted' => true,
'payment_method' => 'checkmo',
'quote_id' => $quote->getId()
];
self::assertEquals($expected, json_decode($this->block->getOrderDataJson(), true));
}
/**
* Saves customer's addresses.
*
* @param int $customerId
* @param array $addressData
* @return array
* @throws \Magento\Framework\Exception\LocalizedException
*/
private function setUpMockAddress(int $customerId, array $addressData)
{
/** @var RegionInterfaceFactory $regionFactory */
$regionFactory = $this->objectManager->create(RegionInterfaceFactory::class);
/** @var AddressInterfaceFactory $addressFactory */
$addressFactory = $this->objectManager->create(AddressInterfaceFactory::class);
/** @var AddressRepositoryInterface $addressRepository */
$addressRepository = $this->objectManager->create(AddressRepositoryInterface::class);
$region = $regionFactory->create()
->setRegionCode('AL')
->setRegion('Alabama')
->setRegionId(1);
$ids = [];
foreach ($addressData as $data) {
$address = $addressFactory->create(['data' => $data]);
$address->setRegion($region)
->setCustomerId($customerId)
->setIsDefaultBilling(true)
->setIsDefaultShipping(true);
$address = $addressRepository->save($address);
$ids[] = $address->getId();
}
return $ids;
}
/**
* Gets test address data.
*
* @return array
*/
private function getAddressData(): array
{
return [
[
'firstname' => 'John',
'lastname' => 'Smith',
'company' => false,
'street' => 'Green str, 67',
'city' => 'CityM',
'country_id' => 'US',
'region' => 'Alabama',
'region_id' => 1,
'postcode' => '75477',
'telephone' => '3468676',
'vat_id' => false
],
[
'firstname' => 'John',
'lastname' => 'Smith',
'company' => false,
'street' => 'Black str, 48',
'city' => 'CityX',
'country_id' => 'US',
'region' => 'Alabama',
'region_id' => 1,
'postcode' => '47676',
'telephone' => '3234676',
'vat_id' => false,
]
];
}
/**
* Gets quote by ID.
*
* @param string $reservedOrderId
* @return Quote
* @throws \Magento\Framework\Exception\NoSuchEntityException
*/
private function getQuote(string $reservedOrderId): Quote
{
/** @var SearchCriteriaBuilder $searchCriteriaBuilder */
$searchCriteriaBuilder = $this->objectManager->get(SearchCriteriaBuilder::class);
$searchCriteria = $searchCriteriaBuilder->addFilter('reserved_order_id', $reservedOrderId)
->create();
/** @var CartRepositoryInterface $repository */
$repository = $this->objectManager->get(CartRepositoryInterface::class);
$items = $repository->getList($searchCriteria)
->getItems();
return array_pop($items);
}
}
@@ -0,0 +1,33 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Adminhtml\Order\Create\Giftmessage;
/**
* @magentoAppArea adminhtml
*/
class FormTest extends \PHPUnit\Framework\TestCase
{
/**
* @magentoDataFixture Magento/Customer/_files/customer.php
* @magentoAppIsolation enabled
*/
public function testGetDefaultSenderWithCurrentCustomer()
{
/** Preconditions */
$objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager();
$fixtureCustomerId = 1;
/** @var \Magento\Backend\Model\Session\Quote $backendQuoteSession */
$backendQuoteSession = $objectManager->get(\Magento\Backend\Model\Session\Quote::class);
$backendQuoteSession->setCustomerId($fixtureCustomerId);
/** @var \Magento\Sales\Block\Adminhtml\Order\Create\Giftmessage\Form $block */
$block = $objectManager->create(\Magento\Sales\Block\Adminhtml\Order\Create\Giftmessage\Form::class);
$block->setEntity(new \Magento\Framework\DataObject());
/** SUT execution and assertions */
$this->assertEquals('John Smith', $block->getDefaultSender(), 'Sender name is invalid.');
}
}
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Adminhtml\Order\Create;
use Magento\TestFramework\Helper\Bootstrap;
/**
* @magentoAppArea adminhtml
*/
class HeaderTest extends \PHPUnit\Framework\TestCase
{
/** @var \Magento\Sales\Block\Adminhtml\Order\Create\Header */
protected $_block;
protected function setUp(): void
{
$this->_block = Bootstrap::getObjectManager()->create(
\Magento\Sales\Block\Adminhtml\Order\Create\Header::class
);
parent::setUp();
}
/**
* @param int|null $customerId
* @param int|null $storeId
* @param string $expectedResult
* @magentoDataFixture Magento/Customer/_files/customer.php
* @dataProvider toHtmlDataProvider
*/
public function testToHtml($customerId, $storeId, $expectedResult)
{
/** @var \Magento\Backend\Model\Session\Quote $session */
$session = Bootstrap::getObjectManager()->get(\Magento\Backend\Model\Session\Quote::class);
$session->setCustomerId($customerId);
$session->setStoreId($storeId);
$this->assertEquals($expectedResult, $this->_block->toHtml());
}
public function toHtmlDataProvider()
{
$customerIdFromFixture = 1;
$defaultStoreView = 1;
return [
'Customer and store' => [
$customerIdFromFixture,
$defaultStoreView,
'Create New Order for John Smith in Default Store View',
],
'No store' => [$customerIdFromFixture, null, 'Create New Order for John Smith'],
'No customer' => [null, $defaultStoreView, 'Create New Order in Default Store View'],
'No customer, no store' => [null, null, 'Create New Order for New Customer']
];
}
}
@@ -0,0 +1,69 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Create\Items;
use Magento\Backend\Model\Session\Quote;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Block\Adminhtml\Order\Create\Items;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Quote\Model\GetQuoteByReservedOrderId;
use PHPUnit\Framework\TestCase;
/**
* Checks order items grid
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class GridTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var LayoutInterface */
private $layout;
/** @var Grid */
private $block;
/** @var Quote */
private $session;
/** @var GetQuoteByReservedOrderId */
private $getQuoteByReservedOrderId;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->getQuoteByReservedOrderId = $this->objectManager->get(GetQuoteByReservedOrderId::class);
$this->session = $this->objectManager->get(Quote::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->block = $this->layout->createBlock(Grid::class);
$this->layout->createBlock(Items::class)->setChild('items_grid', $this->block);
}
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
*
* @return void
*/
public function testGetItems(): void
{
$quote = $this->getQuoteByReservedOrderId->execute('test_order_with_customer_without_address');
$this->session->setQuoteId($quote->getId());
$items = $this->block->getItems();
$this->assertCount(1, $items);
$this->assertEquals('simple2', reset($items)->getSku());
}
}
@@ -0,0 +1,57 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Create\Shipping;
use Magento\Customer\Model\Address\AddressModelInterface;
use Magento\Framework\View\Element\BlockInterface;
use Magento\Sales\Block\Adminhtml\Order\Create\AbstractAddressFormTest;
/**
* Class checks shipping address form behaviour
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class FormTest extends AbstractAddressFormTest
{
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_address_saved.php
*
* @return void
*/
public function testFormValuesExist(): void
{
$this->checkFormValuesExist(1);
}
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
*
* @return void
*/
public function testFormValuesAreEmpty(): void
{
$this->checkFormValuesAreEmpty(1);
}
/**
* @inheritdoc
*/
protected function getFormBlock(): BlockInterface
{
return $this->layout->createBlock(Address::class);
}
/**
* @inheritdoc
*/
protected function getAddress(int $customerId): AddressModelInterface
{
return $this->customerRegistry->retrieve($customerId)->getDefaultShippingAddress();
}
}
@@ -0,0 +1,104 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Create\Sidebar;
use Magento\Backend\Model\Session\Quote;
use Magento\Catalog\Model\ProductRepository;
use Magento\Framework\View\LayoutInterface;
use Magento\Quote\Model\QuoteRepository;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\ObjectManager;
use PHPUnit\Framework\TestCase;
/**
* Check sidebar shopping cart section block
*
* @see \Magento\Sales\Block\Adminhtml\Order\Create\Sidebar\Cart
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class CartTest extends TestCase
{
/** @var ObjectManager */
private $objectManager;
/** @var Cart */
private $block;
/** @var Quote */
private $session;
/** @var ProductRepository */
private $productRepository;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Cart::class);
$this->session = $this->objectManager->get(Quote::class);
$this->productRepository = $this->objectManager->get(ProductRepository::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->session->clearStorage();
$this->objectManager->removeSharedInstance(\Magento\Sales\Model\AdminOrder\Create::class, true);
$this->objectManager->removeSharedInstance(QuoteRepository::class);
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Checkout/_files/quote_with_customer_without_address.php
*
* @return void
*/
public function testGetItemCollection(): void
{
$this->session->setCustomerId(1);
$items = $this->block->getItemCollection();
$this->assertCount(1, $items);
$this->assertEquals('simple2', reset($items)->getSku());
}
/**
* @return void
*/
public function testClearShoppingCartButton(): void
{
$confirmation = __('Are you sure you want to delete all items from shopping cart?');
$button = $this->block->getChildBlock('empty_customer_cart_button');
$this->assertEquals(sprintf("order.clearShoppingCart('%s')", $confirmation), $button->getOnclick());
$this->assertEquals(__('Clear Shopping Cart'), $button->getLabel());
}
/**
* @magentoDataFixture Magento/Directory/_files/usd_cny_rate.php
* @magentoDataFixture Magento/Sales/_files/quote_with_two_products_and_customer_and_custom_price.php
*
* @magentoConfigFixture default/currency/options/base USD
* @magentoConfigFixture current_store currency/options/default CNY
* @magentoConfigFixture current_store currency/options/allow CNY,USD
*/
public function testGetItemPriceConvert()
{
$this->session->setCustomerId(1);
$customPrice = $this->block->getItemPrice($this->productRepository->get('simple'));
$this->assertStringContainsString('84.00', $customPrice);
$price = $this->block->getItemPrice($this->productRepository->get('custom-design-simple-product'));
$this->assertStringContainsString('70.00', $price);
}
}
@@ -0,0 +1,86 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Invoice\Create;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* Checks invoiced items grid appearance
*
* @see \Magento\Sales\Block\Adminhtml\Order\Invoice\Create\Items
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class ItemsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Items */
private $block;
/** @var OrderFactory */
private $orderFactory;
/** @var Registry */
private $registry;
/** @var CollectionFactory */
private $invoiceCollectionFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Items::class);
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->invoiceCollectionFactory = $this->objectManager->get(CollectionFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_invoice');
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/invoice.php
*
* @return void
*/
public function testGetUpdateButtonHtml(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$invoice = $this->invoiceCollectionFactory->create()->setOrderFilter($order)->setPageSize(1)->getFirstItem();
$this->registry->unregister('current_invoice');
$this->registry->register('current_invoice', $invoice);
$this->block->toHtml();
$button = $this->block->getChildBlock('update_button');
$this->assertEquals((string)__('Update Qty\'s'), (string)$button->getLabel());
$this->assertStringContainsString(
sprintf('sales/index/updateQty/order_id/%u/', (int)$order->getEntityId()),
$button->getOnClick()
);
}
}
@@ -0,0 +1,81 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\Invoice;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\Sales\Model\ResourceModel\Order\Invoice\CollectionFactory;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class TotalsTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var Totals */
private $block;
/** @var OrderFactory */
private $orderFactory;
/** @var Registry */
private $registry;
/** @var CollectionFactory */
private $invoiceCollectionFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->block = $this->objectManager->get(LayoutInterface::class)->createBlock(Totals::class);
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
$this->registry = $this->objectManager->get(Registry::class);
$this->invoiceCollectionFactory = $this->objectManager->get(CollectionFactory::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('current_invoice');
parent::tearDown();
}
/**
* @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 = $this->invoiceCollectionFactory->create()->setOrderFilter($order)->setPageSize(1)->getFirstItem();
$this->registry->unregister('current_invoice');
$this->registry->register('current_invoice', $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']);
}
}
@@ -0,0 +1,231 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Phrase;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterfaceFactory;
use Magento\Sales\Block\Adminhtml\Order\Totals\Tax;
use Magento\Sales\Model\Order;
use Magento\Tax\Model\ResourceModel\Sales\Order\Tax\Collection as TaxCollection;
use Magento\Tax\Model\Sales\Order\Tax as SalesTax;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;
/**
* @magentoAppArea adminhtml
*/
class TotalsTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $om;
/**
* @var LayoutInterface
*/
private $layout;
/**
* @var OrderInterfaceFactory
*/
private $orderFactory;
/**
* @inheritDoc
*/
protected function setUp(): void
{
$this->om = Bootstrap::getObjectManager();
$this->layout = $this->om->get(LayoutInterface::class);
$this->orderFactory = $this->om->get(OrderInterfaceFactory::class);
}
/**
* Test block totals including tax.
*
* @magentoConfigFixture default_store tax/sales_display/subtotal 2
* @magentoConfigFixture default_store tax/sales_display/shipping 2
* @magentoConfigFixture default_store tax/sales_display/full_summary 1
*
* @magentoDataFixture Magento/Tax/_files/order_with_tax.php
*
* @return void
*/
public function testTotalsInclTax(): void
{
$order = $this->prepareOrderInclTax('100000001');
$blockTotals = $this->getBlockTotals()->setOrder($order);
$this->assertSubtotal($blockTotals->toHtml(), (float) $order->getSubtotal());
$this->assertShipping($blockTotals->toHtml(), (float) $order->getShippingAmount());
$blockTax = $this->getBlockTax();
$blockTotals->setChild('child_tax_block', $blockTax);
$blockTax->initTotals();
/** @var TaxCollection $taxCollection */
$taxCollection = $this->om->get(TaxCollection::class);
$tax = $taxCollection->loadByOrder($order)->getItems();
$tax = array_pop($tax);
$this->assertTaxRate($blockTax->toHtml(), $tax);
$this->assertSubtotal($blockTotals->toHtml(), (float) $order->getSubtotalInclTax());
$this->assertShipping($blockTotals->toHtml(), (float) $order->getShippingInclTax());
}
/**
* Test block totals including canceled amount.
*
* @magentoDataFixture Magento/Sales/_files/order.php
*
* @return void
*/
public function testTotalCanceled(): void
{
$order = $this->orderFactory->create()->loadByIncrementId('100000001');
$order->cancel();
$blockTotals = $this->getBlockTotals()->setOrder($order);
$this->assertCanceled($blockTotals->toHtml(), $order->getTotalCanceled());
}
/**
* Check if canceled amount present in block.
*
* @param string $blockTotalsHtml
* @param float $amount
* @return void
*/
private function assertCanceled(string $blockTotalsHtml, float $amount): void
{
$this->assertTrue(
$this->isBlockContainsTotalAmount($blockTotalsHtml, __('Total Canceled'), $amount),
'Canceled amount is missing or incorrect.'
);
}
/**
* Check if subtotal amount present in block.
*
* @param string $blockTotalsHtml
* @param float $amount
* @return void
*/
private function assertSubtotal(string $blockTotalsHtml, float $amount): void
{
$this->assertTrue(
$this->isBlockContainsTotalAmount($blockTotalsHtml, __('Subtotal'), $amount),
'Subtotal amount is missing or incorrect.'
);
}
/**
* Check if shipping amount present in block.
*
* @param string $blockTotalsHtml
* @param float $amount
* @return void
*/
private function assertShipping(string $blockTotalsHtml, float $amount): void
{
$this->assertTrue(
$this->isBlockContainsTotalAmount($blockTotalsHtml, __('Shipping & Handling'), $amount),
'Shipping & Handling amount is missing or incorrect.'
);
}
/**
* Check if tax rate present in block.
*
* @param string $blockTaxHtml
* @param SalesTax $tax
* @return void
*/
private function assertTaxRate(string $blockTaxHtml, SalesTax $tax)
{
$this->assertStringContainsString(
$tax->getTitle() . ' (' . (int)$tax->getAmount() . '%)',
$blockTaxHtml
);
}
/**
* Prepare order for test.
*
* @param string $incrementId
* @return Order
*/
private function prepareOrderInclTax(string $incrementId): Order
{
/** @var Order $order */
$order = $this->orderFactory->create()->loadByIncrementId($incrementId);
$order->setSubtotalInclTax(110);
$order->setBaseSubtotalInclTax(110);
$order->setShippingAmount(10);
$order->setBaseShippingAmount(10);
$order->setShippingInclTax(11);
$order->setBaseShippingInclTax(11);
return $order;
}
/**
* Create block totals.
*
* @return Totals
*/
private function getBlockTotals(): Totals
{
/** @var Totals $block */
$block = $this->layout->createBlock(Totals::class, 'block_totals');
$block->setTemplate('Magento_Sales::order/totals.phtml');
return $block;
}
/**
* Create block tax.
*
* @return Tax
*/
private function getBlockTax(): Tax
{
/** @var Tax $block */
$block = $this->layout->createBlock(Tax::class, 'block_tax');
$block->setTemplate('Magento_Sales::order/totals/tax.phtml');
return $block;
}
/**
* Check if amount present in appropriate block node.
*
* @param string $blockTotalsHtml
* @param Phrase $totalLabel
* @param float $totalAmount
* @return bool
*/
private function isBlockContainsTotalAmount(
string $blockTotalsHtml,
Phrase $totalLabel,
float $totalAmount
): bool {
$dom = new \DOMDocument();
$dom->loadHTML($blockTotalsHtml);
$query = sprintf(
"//tr[contains(., '%s')]//span[contains(text(), '%01.2f')]",
$totalLabel,
$totalAmount
);
return (bool) (new \DOMXPath($dom))->query($query)->count();
}
}
@@ -0,0 +1,66 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml\Order\View;
use Magento\Framework\App\Config\ReinitableConfigInterface;
use Magento\Framework\ObjectManagerInterface;
use Magento\Sales\Model\Order;
use Magento\Store\Model\StoreManagerInterface;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\Sales\Model\Order\Address\Renderer as OrderAddressRenderer;
use PHPUnit\Framework\TestCase;
/**
* Test class for \Magento\Sales\Block\Adminhtml\Order\View\AddressInfo
*/
class AddressInfoTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;
/**
* @var OrderAddressRenderer
*/
private $orderAddressRenderer;
/**
* Set up
*/
protected function setUp(): void
{
$this->objectManager = Bootstrap::getObjectManager();
$this->orderAddressRenderer = $this->objectManager->get(OrderAddressRenderer::class);
}
/**
* Verify customer address attributes (e.g. Company) are visible on second website order.
*
* @magentoDataFixture Magento/Store/_files/second_website_with_store_group_and_store.php
* @magentoDataFixture Magento/Sales/_files/order_on_second_website.php
* @magentoAppArea adminhtml
* @magentoDbIsolation disabled
* @magentoAppIsolation enabled
*/
public function testCompanyAddressAttributeVisibleForOrderOnSecondWebsite()
{
$storeManager = $this->objectManager->get(StoreManagerInterface::class);
$website = $storeManager->getWebsites(false, true)['base'];
$config = $this->objectManager->get(ReinitableConfigInterface::class);
$config->setValue(
'customer/address/company_show',
'websites',
'',
$website->getId()
);
$orderFixtureStore = $this->objectManager->create(Order::class)->loadByIncrementId('100000001');
$address = $orderFixtureStore->getBillingAddress();
self::assertStringContainsString('Test Company', $this->orderAddressRenderer->format($address, 'html'));
}
}
@@ -0,0 +1,98 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Adminhtml\Order\View;
/**
* Test class for \Magento\Sales\Block\Adminhtml\Order\View\Info
*/
class InfoTest extends \Magento\TestFramework\TestCase\AbstractBackendController
{
/**
* Value for the user defined custom attribute, which is created by attribute_user_defined_customer.php fixture.
*/
const ORDER_USER_DEFINED_ATTRIBUTE_VALUE = 'custom_attr_value';
public function testCustomerGridAction()
{
$layout = $this->_objectManager->get(\Magento\Framework\View\LayoutInterface::class);
/** @var \Magento\Sales\Block\Adminhtml\Order\View\Info $infoBlock */
$infoBlock = $layout->createBlock(
\Magento\Sales\Block\Adminhtml\Order\View\Info::class,
'info_block' . random_int(0, PHP_INT_MAX),
[]
);
$result = $infoBlock->getCustomerAccountData();
$this->assertEquals([], $result, 'Customer has additional account data.');
}
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*/
public function testGetCustomerGroupName()
{
$layout = $this->_objectManager->get(\Magento\Framework\View\LayoutInterface::class);
/** @var \Magento\Sales\Block\Adminhtml\Order\View\Info $customerGroupBlock */
$customerGroupBlock = $layout->createBlock(
\Magento\Sales\Block\Adminhtml\Order\View\Info::class,
'info_block' . random_int(0, PHP_INT_MAX),
['registry' => $this->_putOrderIntoRegistry()]
);
$result = $customerGroupBlock->getCustomerGroupName();
$this->assertEquals('NOT LOGGED IN', $result);
}
/**
* @magentoDataFixture Magento/Sales/_files/order.php
* @magentoDataFixture Magento/Customer/_files/attribute_user_defined_customer.php
*/
public function testGetCustomerAccountData()
{
$layout = $this->_objectManager->get(\Magento\Framework\View\LayoutInterface::class);
$orderData = [
'customer_' . FIXTURE_ATTRIBUTE_USER_DEFINED_CUSTOMER_NAME => self::ORDER_USER_DEFINED_ATTRIBUTE_VALUE,
];
/** @var \Magento\Sales\Block\Adminhtml\Order\View\Info $customerGroupBlock */
$customerGroupBlock = $layout->createBlock(
\Magento\Sales\Block\Adminhtml\Order\View\Info::class,
'info_block' . random_int(0, PHP_INT_MAX),
['registry' => $this->_putOrderIntoRegistry($orderData)]
);
$this->assertEquals(
[
200 => [
'label' => FIXTURE_ATTRIBUTE_USER_DEFINED_CUSTOMER_FRONTEND_LABEL,
'value' => self::ORDER_USER_DEFINED_ATTRIBUTE_VALUE,
],
],
$customerGroupBlock->getCustomerAccountData()
);
}
/**
* @param array $additionalOrderData
* @return \Magento\Framework\Registry|\PHPUnit\Framework\MockObject\MockObject
*/
protected function _putOrderIntoRegistry(array $additionalOrderData = [])
{
$registry = $this->getMockBuilder(\Magento\Framework\Registry::class)->disableOriginalConstructor()->getMock();
$order = $this->_objectManager->get(
\Magento\Sales\Model\Order::class
)->load(
'100000001'
)->setData(
array_merge(['customer_group_id' => 0], $additionalOrderData)
);
$registry->expects($this->any())->method('registry')->with('current_order')->willReturn($order);
return $registry;
}
}
@@ -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\Adminhtml\Order;
use Magento\Backend\Model\Search\AuthorizationMock;
use Magento\Framework\Authorization;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\Registry;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Api\Data\OrderInterface;
use Magento\Sales\Model\OrderFactory;
use Magento\TestFramework\Helper\Bootstrap;
use Magento\TestFramework\Helper\Xpath;
use PHPUnit\Framework\TestCase;
/**
* Checks order create block
*
* @see \Magento\Sales\Block\Adminhtml\Order\View
*
* @magentoAppArea adminhtml
* @magentoDbIsolation enabled
*/
class ViewTest extends TestCase
{
/** @var ObjectManagerInterface */
private $objectManager;
/** @var LayoutInterface */
private $layout;
/** @var Registry */
private $registry;
/** @var OrderFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->objectManager = Bootstrap::getObjectManager();
$this->objectManager->addSharedInstance(
$this->objectManager->get(AuthorizationMock::class),
Authorization::class
);
$this->registry = $this->objectManager->get(Registry::class);
$this->orderFactory = $this->objectManager->get(OrderFactory::class);
$this->layout = $this->objectManager->get(LayoutInterface::class);
}
/**
* @inheritdoc
*/
protected function tearDown(): void
{
$this->registry->unregister('sales_order');
parent::tearDown();
}
/**
* @magentoDataFixture Magento/Sales/_files/order.php
*
* @return void
*/
public function testInvoiceButton(): void
{
$this->registerOrder('100000001');
$this->assertEquals(
1,
Xpath::getElementsCountForXpath(
'//button[@id=\'order_invoice\']',
$this->layout->createBlock(View::class)->getButtonsHtml()
)
);
}
/**
* @magentoDataFixture Magento/Sales/_files/order_with_bundle_and_invoiced.php
*
* @return void
*/
public function testInvoiceButtonIsNotVisible(): void
{
$this->registerOrder('100000001');
$this->assertEmpty(
Xpath::getElementsCountForXpath(
'//button[@id=\'order_invoice\']',
$this->layout->createBlock(View::class)->getButtonsHtml()
)
);
}
/**
* Register order
*
* @param OrderInterface $order
* @return void
*/
private function registerOrder(string $orderIncrementId): void
{
$order = $this->orderFactory->create()->loadByIncrementId($orderIncrementId);
$this->registry->unregister('sales_order');
$this->registry->register('sales_order', $order);
}
}
@@ -0,0 +1,47 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Sales\Block\Adminhtml\Report\Filter\Form;
/**
* @magentoAppArea adminhtml
*/
class CouponTest extends \PHPUnit\Framework\TestCase
{
/**
* Layout
*
* @var \Magento\Framework\View\LayoutInterface
*/
protected $_layout;
protected function setUp(): void
{
parent::setUp();
$this->_layout = \Magento\TestFramework\Helper\Bootstrap::getObjectManager()
->get(\Magento\Framework\View\LayoutInterface::class);
}
/**
* @covers \Magento\Sales\Block\Adminhtml\Report\Filter\Form\Coupon::_afterToHtml
*/
public function testAfterToHtml()
{
/** @var $block \Magento\Sales\Block\Adminhtml\Report\Filter\Form\Coupon */
$block = $this->_layout->createBlock(\Magento\Sales\Block\Adminhtml\Report\Filter\Form\Coupon::class);
$block->setFilterData(new \Magento\Framework\DataObject());
$html = $block->toHtml();
$expectedStrings = [
'FormElementDependenceController',
'sales_report_rules_list',
'sales_report_price_rule_type',
];
foreach ($expectedStrings as $expectedString) {
$this->assertStringContainsString($expectedString, $html);
}
}
}
@@ -0,0 +1,59 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);
namespace Magento\Sales\Block\Adminhtml;
use Magento\Framework\View\LayoutInterface;
use Magento\Sales\Model\Order;
use Magento\Sales\Model\OrderFactory;
/**
* Test class for \Magento\Sales\Block\Adminhtml\Totals
*/
class TotalsTest extends \Magento\TestFramework\TestCase\AbstractBackendController
{
/** @var LayoutInterface */
private $layout;
/** @var Totals */
private $block;
/** @var OrderFactory */
private $orderFactory;
/**
* @inheritdoc
*/
protected function setUp(): void
{
parent::setUp();
$this->layout = $this->_objectManager->get(LayoutInterface::class);
$this->block = $this->layout->createBlock(Totals::class, 'totals_block');
$this->orderFactory = $this->_objectManager->get(OrderFactory::class);
}
/**
* @magentoDataFixture Magento/Sales/_files/order_with_free_shipping_by_coupon.php
*/
public function testShowShippingCoupon()
{
/** @var Order $order */
$order = $this->orderFactory->create();
$order->loadByIncrementId('100000001');
$this->block->setOrder($order);
$this->block->toHtml();
$shippingTotal = $this->block->getTotal('shipping');
$this->assertNotFalse($shippingTotal, 'Shipping method is absent on the total\'s block.');
$this->assertStringContainsString(
'1234567890',
$shippingTotal->getLabel(),
'Coupon code is absent in the shipping method label name.'
);
}
}