diff --git a/app/code/Magento/Backend/etc/di.xml b/app/code/Magento/Backend/etc/di.xml index 2871ef2508f37..5445af580b20c 100644 --- a/app/code/Magento/Backend/etc/di.xml +++ b/app/code/Magento/Backend/etc/di.xml @@ -12,7 +12,6 @@ - diff --git a/app/code/Magento/Checkout/Controller/Cart/Index.php b/app/code/Magento/Checkout/Controller/Cart/Index.php index cd0bb023dcaab..456cb28e563e3 100644 --- a/app/code/Magento/Checkout/Controller/Cart/Index.php +++ b/app/code/Magento/Checkout/Controller/Cart/Index.php @@ -15,6 +15,7 @@ class Index extends \Magento\Checkout\Controller\Cart */ public function execute() { + $this->_eventManager->dispatch('collect_totals_failed_items'); if ($this->cart->getQuote()->getItemsCount()) { $this->cart->init(); $this->cart->save(); diff --git a/app/code/Magento/Core/etc/di.xml b/app/code/Magento/Core/etc/di.xml index cc73a8a5b388c..3b6eb72b2b36b 100644 --- a/app/code/Magento/Core/etc/di.xml +++ b/app/code/Magento/Core/etc/di.xml @@ -14,14 +14,12 @@ - - diff --git a/app/code/Magento/OfflinePayments/Block/Form/AbstractInstruction.php b/app/code/Magento/OfflinePayments/Block/Form/AbstractInstruction.php new file mode 100644 index 0000000000000..4ad7357cfdad9 --- /dev/null +++ b/app/code/Magento/OfflinePayments/Block/Form/AbstractInstruction.php @@ -0,0 +1,32 @@ +_instructions)) { + $this->_instructions = $this->getMethod()->getInstructions(); + } + return $this->_instructions; + } +} diff --git a/app/code/Magento/OfflinePayments/Block/Form/Banktransfer.php b/app/code/Magento/OfflinePayments/Block/Form/Banktransfer.php index 1693929cdaabd..d71654418a38f 100644 --- a/app/code/Magento/OfflinePayments/Block/Form/Banktransfer.php +++ b/app/code/Magento/OfflinePayments/Block/Form/Banktransfer.php @@ -8,32 +8,12 @@ /** * Block for Bank Transfer payment method form */ -class Banktransfer extends \Magento\Payment\Block\Form +class Banktransfer extends \Magento\OfflinePayments\Block\Form\AbstractInstruction { - /** - * Instructions text - * - * @var string - */ - protected $_instructions; - /** * Bank transfer template * * @var string */ protected $_template = 'form/banktransfer.phtml'; - - /** - * Get instructions text from config - * - * @return string - */ - public function getInstructions() - { - if (is_null($this->_instructions)) { - $this->_instructions = $this->getMethod()->getInstructions(); - } - return $this->_instructions; - } } diff --git a/app/code/Magento/OfflinePayments/Block/Form/Cashondelivery.php b/app/code/Magento/OfflinePayments/Block/Form/Cashondelivery.php index af0850e5860b8..1e14cabb9ba2b 100644 --- a/app/code/Magento/OfflinePayments/Block/Form/Cashondelivery.php +++ b/app/code/Magento/OfflinePayments/Block/Form/Cashondelivery.php @@ -8,32 +8,12 @@ /** * Block for Cash On Delivery payment method form */ -class Cashondelivery extends \Magento\Payment\Block\Form +class Cashondelivery extends \Magento\OfflinePayments\Block\Form\AbstractInstruction { - /** - * Instructions text - * - * @var string - */ - protected $_instructions; - /** * Cash on delivery template * * @var string */ protected $_template = 'form/cashondelivery.phtml'; - - /** - * Get instructions text from config - * - * @return string - */ - public function getInstructions() - { - if (is_null($this->_instructions)) { - $this->_instructions = $this->getMethod()->getInstructions(); - } - return $this->_instructions; - } } diff --git a/app/code/Magento/OfflinePayments/Model/Banktransfer.php b/app/code/Magento/OfflinePayments/Model/Banktransfer.php index 044dd3301ba60..4cc48a5752720 100644 --- a/app/code/Magento/OfflinePayments/Model/Banktransfer.php +++ b/app/code/Magento/OfflinePayments/Model/Banktransfer.php @@ -39,14 +39,4 @@ class Banktransfer extends \Magento\Payment\Model\Method\AbstractMethod * @var bool */ protected $_isOffline = true; - - /** - * Get instructions text from config - * - * @return string - */ - public function getInstructions() - { - return trim($this->getConfigData('instructions')); - } } diff --git a/app/code/Magento/OfflinePayments/Model/Observer.php b/app/code/Magento/OfflinePayments/Model/Observer.php index 61fc00d538940..1cb45ac753f56 100644 --- a/app/code/Magento/OfflinePayments/Model/Observer.php +++ b/app/code/Magento/OfflinePayments/Model/Observer.php @@ -21,9 +21,11 @@ public function beforeOrderPaymentSave(\Magento\Framework\Event\Observer $observ { /** @var \Magento\Sales\Model\Order\Payment $payment */ $payment = $observer->getEvent()->getPayment(); - $banktransfer = \Magento\OfflinePayments\Model\Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE; - if ($payment->getMethod() === $banktransfer) { - $payment->setAdditionalInformation('instructions', $payment->getMethodInstance()->getInstructions()); + if ($payment->getMethod() === Banktransfer::PAYMENT_METHOD_BANKTRANSFER_CODE) { + $payment->setAdditionalInformation( + 'instructions', + $payment->getMethodInstance()->getConfigData('instructions') + ); } } } diff --git a/app/code/Magento/OfflineShipping/Model/Config/Backend/Tablerate.php b/app/code/Magento/OfflineShipping/Model/Config/Backend/Tablerate.php index 6056d7dbbb792..c0b311a1073ad 100644 --- a/app/code/Magento/OfflineShipping/Model/Config/Backend/Tablerate.php +++ b/app/code/Magento/OfflineShipping/Model/Config/Backend/Tablerate.php @@ -46,6 +46,8 @@ public function __construct( */ public function afterSave() { - $this->_tablerateFactory->create()->uploadAndImport($this); + /** @var \Magento\OfflineShipping\Model\Resource\Carrier\Tablerate $tableRate */ + $tableRate = $this->_tablerateFactory->create(); + $tableRate->uploadAndImport($this); } } diff --git a/app/code/Magento/OfflineShipping/Model/Observer/SalesRule/ActionsTab.php b/app/code/Magento/OfflineShipping/Model/Observer/SalesRule/ActionsTab.php index b1e56563d7477..10068f3d7e548 100644 --- a/app/code/Magento/OfflineShipping/Model/Observer/SalesRule/ActionsTab.php +++ b/app/code/Magento/OfflineShipping/Model/Observer/SalesRule/ActionsTab.php @@ -24,22 +24,24 @@ public function prepareForm($observer) $form = $observer->getForm(); foreach ($form->getElements() as $element) { /** @var \Magento\Framework\Data\Form\Element\AbstractElement $element */ - if ($element->getId() == 'action_fieldset') { - $element->addField( - 'simple_free_shipping', - 'select', - [ - 'label' => __('Free Shipping'), - 'title' => __('Free Shipping'), - 'name' => 'simple_free_shipping', - 'options' => [ - 0 => __('No'), - Rule::FREE_SHIPPING_ITEM => __('For matching items only'), - Rule::FREE_SHIPPING_ADDRESS => __('For shipment with matching items'), - ] - ] - ); + if ($element->getId() != 'action_fieldset') { + continue; } + + $element->addField( + 'simple_free_shipping', + 'select', + [ + 'label' => __('Free Shipping'), + 'title' => __('Free Shipping'), + 'name' => 'simple_free_shipping', + 'options' => [ + 0 => __('No'), + Rule::FREE_SHIPPING_ITEM => __('For matching items only'), + Rule::FREE_SHIPPING_ADDRESS => __('For shipment with matching items'), + ] + ] + ); } } } diff --git a/app/code/Magento/Payment/Block/Info/Instructions.php b/app/code/Magento/Payment/Block/Info/Instructions.php index 2caeeb5f2f9e6..55ffee89fa136 100644 --- a/app/code/Magento/Payment/Block/Info/Instructions.php +++ b/app/code/Magento/Payment/Block/Info/Instructions.php @@ -33,7 +33,7 @@ public function getInstructions() if (is_null($this->_instructions)) { $this->_instructions = $this->getInfo()->getAdditionalInformation( 'instructions' - ) ?: $this->getMethod()->getInstructions(); + ) ?: $this->getMethod()->getConfigData('instructions'); } return $this->_instructions; } diff --git a/dev/tests/unit/testsuite/Magento/OfflinePayments/Block/Form/AbstractInstructionTest.php b/dev/tests/unit/testsuite/Magento/OfflinePayments/Block/Form/AbstractInstructionTest.php new file mode 100644 index 0000000000000..a6ef83d4f0e11 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflinePayments/Block/Form/AbstractInstructionTest.php @@ -0,0 +1,40 @@ +getMock('Magento\Framework\View\Element\Template\Context', [], [], '', false); + $this->_model = $this->getMockForAbstractClass( + 'Magento\OfflinePayments\Block\Form\AbstractInstruction', + ['context' => $context] + ); + } + + public function testGetInstructions() + { + $method = $this->getMock( + 'Magento\Payment\Model\MethodInterface', + ['getInstructions', 'getCode', 'getFormBlockType', 'getTitle'], + [], + '', + false + ); + $method->expects($this->once()) + ->method('getInstructions') + ->willReturn('instructions'); + $this->_model->setData('method', $method); + + $this->assertEquals('instructions', $this->_model->getInstructions()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflinePayments/Block/Info/CheckmoTest.php b/dev/tests/unit/testsuite/Magento/OfflinePayments/Block/Info/CheckmoTest.php new file mode 100644 index 0000000000000..04748038a9c2d --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflinePayments/Block/Info/CheckmoTest.php @@ -0,0 +1,85 @@ +getMock('Magento\Framework\View\Element\Template\Context', [], [], '', false); + $this->_model = new \Magento\OfflinePayments\Block\Info\Checkmo($context); + } + + /** + * @dataProvider getPayableToDataProvider + */ + public function testGetPayableTo($details, $expected) + { + $info = $this->getMock('Magento\Payment\Model\Info', ['getAdditionalData'], [], '', false); + $info->expects($this->once()) + ->method('getAdditionalData') + ->willReturn(serialize($details)); + $this->_model->setData('info', $info); + + $this->assertEquals($expected, $this->_model->getPayableTo()); + } + + /** + * @return array + */ + public function getPayableToDataProvider() + { + return [ + [['payable_to' => 'payable'], 'payable'], + ['', ''] + ]; + } + + /** + * @dataProvider getMailingAddressDataProvider + */ + public function testGetMailingAddress($details, $expected) + { + $info = $this->getMock('Magento\Payment\Model\Info', ['getAdditionalData'], [], '', false); + $info->expects($this->once()) + ->method('getAdditionalData') + ->willReturn(serialize($details)); + $this->_model->setData('info', $info); + + $this->assertEquals($expected, $this->_model->getMailingAddress()); + } + + /** + * @return array + */ + public function getMailingAddressDataProvider() + { + return [ + [['mailing_address' => 'blah@blah.com'], 'blah@blah.com'], + ['', ''] + ]; + } + + public function testConvertAdditionalDataIsNeverCalled() + { + $info = $this->getMock('Magento\Payment\Model\Info', ['getAdditionalData'], [], '', false); + $info->expects($this->once()) + ->method('getAdditionalData') + ->willReturn(serialize(['mailing_address' => 'blah@blah.com'])); + $this->_model->setData('info', $info); + + // First we set the property $this->_mailingAddress + $this->_model->getMailingAddress(); + + // And now we get already setted property $this->_mailingAddress + $this->assertEquals('blah@blah.com', $this->_model->getMailingAddress()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/BanktransferTest.php b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/BanktransferTest.php index 7dbc702c60f2c..18fb24e1ae8e9 100644 --- a/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/BanktransferTest.php +++ b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/BanktransferTest.php @@ -12,18 +12,23 @@ class BanktransferTest extends \PHPUnit_Framework_TestCase */ protected $_object; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $_scopeConfig; + protected function setUp() { $objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this); $eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false); $paymentDataMock = $this->getMock('Magento\Payment\Helper\Data', [], [], '', false); - $scopeConfig = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface'); + $this->_scopeConfig = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface', [], [], '', false); $this->_object = $objectManagerHelper->getObject( 'Magento\OfflinePayments\Model\Banktransfer', [ 'eventManager' => $eventManager, 'paymentData' => $paymentDataMock, - 'scopeConfig' => $scopeConfig, + 'scopeConfig' => $this->_scopeConfig, ] ); } diff --git a/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/CashondeliveryTest.php b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/CashondeliveryTest.php index e0f11d0283bfe..cd4d60d27533e 100644 --- a/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/CashondeliveryTest.php +++ b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/CashondeliveryTest.php @@ -12,6 +12,11 @@ class CashondeliveryTest extends \PHPUnit_Framework_TestCase */ protected $_object; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + protected $_scopeConfig; + protected function setUp() { $helper = new \Magento\TestFramework\Helper\ObjectManager($this); @@ -19,13 +24,13 @@ protected function setUp() $eventManager = $this->getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false); $paymentDataMock = $this->getMock('Magento\Payment\Helper\Data', [], [], '', false); - $scopeConfig = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface'); + $this->_scopeConfig = $this->getMock('Magento\Framework\App\Config\ScopeConfigInterface', [], [], '', false); $this->_object = $helper->getObject( 'Magento\OfflinePayments\Model\Cashondelivery', [ 'eventManager' => $eventManager, 'paymentData' => $paymentDataMock, - 'scopeConfig' => $scopeConfig, + 'scopeConfig' => $this->_scopeConfig, ] ); } diff --git a/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/CheckmoTest.php b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/CheckmoTest.php new file mode 100644 index 0000000000000..b6aee65b11e3e --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/CheckmoTest.php @@ -0,0 +1,80 @@ +getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false); + $paymentDataMock = $this->getMock('Magento\Payment\Helper\Data', [], [], '', false); + $this->_scopeConfig = $this->getMock( + 'Magento\Framework\App\Config\ScopeConfigInterface', + ['getValue', 'isSetFlag'], + [], + '', + false + ); + $this->_object = $objectManagerHelper->getObject( + 'Magento\OfflinePayments\Model\Checkmo', + [ + 'eventManager' => $eventManager, + 'paymentData' => $paymentDataMock, + 'scopeConfig' => $this->_scopeConfig, + ] + ); + } + + public function testGetPayableTo() + { + $this->_object->setStore(1); + $this->_scopeConfig->expects($this->once()) + ->method('getValue') + ->with('payment/checkmo/payable_to', 'store', 1) + ->willReturn('payable'); + $this->assertEquals('payable', $this->_object->getPayableTo()); + } + + public function testGetMailingAddress() + { + $this->_object->setStore(1); + $this->_scopeConfig->expects($this->once()) + ->method('getValue') + ->with('payment/checkmo/mailing_address', 'store', 1) + ->willReturn('blah@blah.com'); + $this->assertEquals('blah@blah.com', $this->_object->getMailingAddress()); + } + + public function testAssignData() + { + $details['payable_to'] = 'payable'; + $details['mailing_address'] = 'blah@blah.com'; + $this->_object->setStore(1); + $this->_scopeConfig->expects($this->any()) + ->method('getValue') + ->willReturnMap([ + ['payment/checkmo/payable_to', 'store', 1, 'payable'], + ['payment/checkmo/mailing_address', 'store', 1, 'blah@blah.com'] + ]); + $instance = $this->getMock('Magento\Payment\Model\Info', ['setAdditionalData'], [], '', false); + $instance->expects($this->once()) + ->method('setAdditionalData') + ->with(serialize($details)); + $this->_object->setData('info_instance', $instance); + $this->_object->assignData(''); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/ObserverTest.php b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/ObserverTest.php new file mode 100644 index 0000000000000..708b25093f52b --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/ObserverTest.php @@ -0,0 +1,84 @@ +_model = $objectManagerHelper->getObject('Magento\OfflinePayments\Model\Observer'); + } + + public function testBeforeOrderPaymentSave() + { + $observer = $this->getMock('Magento\Framework\Event\Observer', ['getEvent'], [], '', false); + $event = $this->getMock('Magento\Framework\Event', ['getPayment'], [], '', false); + $payment = $this->getMock( + 'Magento\Sales\Model\Order\Payment', + ['getMethod', 'setAdditionalInformation', 'getMethodInstance'], + [], + '', + false + ); + $payment->expects($this->once()) + ->method('getMethod') + ->willReturn('banktransfer'); + $payment->expects($this->once()) + ->method('setAdditionalInformation') + ->with('instructions', 'payment configuration'); + $method = $this->getMock( + 'Magento\Payment\Model\MethodInterface', + ['getConfigData', 'getFormBlockType', 'getTitle', 'getCode'], + [], + '', + false + ); + $method->expects($this->once()) + ->method('getConfigData') + ->willReturn('payment configuration'); + $payment->expects($this->once()) + ->method('getMethodInstance') + ->willReturn($method); + $event->expects($this->once()) + ->method('getPayment') + ->willReturn($payment); + $observer->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + $this->_model->beforeOrderPaymentSave($observer); + } + + public function testBeforeOrderPaymentSaveNoBanktransfer() + { + $observer = $this->getMock('Magento\Framework\Event\Observer', ['getEvent'], [], '', false); + $event = $this->getMock('Magento\Framework\Event', ['getPayment'], [], '', false); + $payment = $this->getMock( + 'Magento\Sales\Model\Order\Payment', + ['getMethod', 'setAdditionalInformation', 'getMethodInstance'], + [], + '', + false + ); + $payment->expects($this->once()) + ->method('getMethod') + ->willReturn('somepaymentmethod'); + $payment->expects($this->never()) + ->method('setAdditionalInformation'); + $event->expects($this->once()) + ->method('getPayment') + ->willReturn($payment); + $observer->expects($this->once()) + ->method('getEvent') + ->willReturn($event); + $this->_model->beforeOrderPaymentSave($observer); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/PurchaseorderTest.php b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/PurchaseorderTest.php new file mode 100644 index 0000000000000..fb08707e9e4b9 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflinePayments/Model/PurchaseorderTest.php @@ -0,0 +1,52 @@ +getMock('Magento\Framework\Event\ManagerInterface', [], [], '', false); + $paymentDataMock = $this->getMock('Magento\Payment\Helper\Data', [], [], '', false); + $this->_scopeConfig = $this->getMock( + 'Magento\Framework\App\Config\ScopeConfigInterface', + ['getValue', 'isSetFlag'], + [], + '', + false + ); + $this->_object = $objectManagerHelper->getObject( + 'Magento\OfflinePayments\Model\Purchaseorder', + [ + 'eventManager' => $eventManager, + 'paymentData' => $paymentDataMock, + 'scopeConfig' => $this->_scopeConfig, + ] + ); + } + + public function testAssignData() + { + $data = new \Magento\Framework\Object([ + 'po_number' => '12345' + ]); + + $instance = $this->getMock('Magento\Payment\Model\Info', [], [], '', false); + $this->_object->setData('info_instance', $instance); + $this->_object->assignData($data); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflineShipping/Block/Adminhtml/Carrier/Tablerate/GridTest.php b/dev/tests/unit/testsuite/Magento/OfflineShipping/Block/Adminhtml/Carrier/Tablerate/GridTest.php new file mode 100644 index 0000000000000..09434687725e9 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflineShipping/Block/Adminhtml/Carrier/Tablerate/GridTest.php @@ -0,0 +1,127 @@ +storeManagerMock = $this->getMockBuilder('Magento\Store\Model\StoreManagerInterface') + ->disableOriginalConstructor() + ->getMock(); + + $this->context = $objectManager->getObject('Magento\Backend\Block\Template\Context', [ + 'storeManager' => $this->storeManagerMock + ]); + + $this->backendHelperMock = $this->getMockBuilder('\Magento\Backend\Helper\Data') + ->disableOriginalConstructor() + ->getMock(); + + $this->collectionFactoryMock = + $this->getMockBuilder('\Magento\OfflineShipping\Model\Resource\Carrier\Tablerate\CollectionFactory') + ->disableOriginalConstructor() + ->getMock(); + + $this->tablerateMock = $this->getMockBuilder('Magento\OfflineShipping\Model\Carrier\Tablerate') + ->disableOriginalConstructor() + ->getMock(); + + $this->model = new \Magento\OfflineShipping\Block\Adminhtml\Carrier\Tablerate\Grid( + $this->context, + $this->backendHelperMock, + $this->collectionFactoryMock, + $this->tablerateMock + ); + } + + public function testSetWebsiteId() + { + $websiteId = 1; + + $websiteMock = $this->getMockBuilder('Magento\Store\Model\Website') + ->setMethods(['getId']) + ->disableOriginalConstructor() + ->getMock(); + + $this->storeManagerMock->expects($this->once()) + ->method('getWebsite') + ->with($websiteId) + ->willReturn($websiteMock); + + $websiteMock->expects($this->once()) + ->method('getId') + ->willReturn($websiteId); + + $this->assertSame($this->model, $this->model->setWebsiteId($websiteId)); + $this->assertEquals($websiteId, $this->model->getWebsiteId()); + } + + public function testGetWebsiteId() + { + $websiteId = 10; + + $websiteMock = $this->getMockBuilder('Magento\Store\Model\Website') + ->disableOriginalConstructor() + ->setMethods(['getId']) + ->getMock(); + + $websiteMock->expects($this->once()) + ->method('getId') + ->willReturn($websiteId); + + $this->storeManagerMock->expects($this->once()) + ->method('getWebsite') + ->willReturn($websiteMock); + + $this->assertEquals($websiteId, $this->model->getWebsiteId()); + + $this->storeManagerMock->expects($this->never()) + ->method('getWebsite') + ->willReturn($websiteMock); + + $this->assertEquals($websiteId, $this->model->getWebsiteId()); + } + + public function testSetAndGetConditionName() + { + $conditionName = 'someName'; + $this->assertEquals($this->model, $this->model->setConditionName($conditionName)); + $this->assertEquals($conditionName, $this->model->getConditionName()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Backend/TablerateTest.php b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Backend/TablerateTest.php new file mode 100644 index 0000000000000..16ff0b107650f --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Backend/TablerateTest.php @@ -0,0 +1,51 @@ +tableateFactoryMock = + $this->getMockBuilder('Magento\OfflineShipping\Model\Resource\Carrier\TablerateFactory') + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + + $helper = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->model = $helper->getObject('\Magento\OfflineShipping\Model\Config\Backend\Tablerate', [ + 'tablerateFactory' => $this->tableateFactoryMock + ]); + } + + public function testAfterSave() + { + $tablerateMock = $this->getMockBuilder('Magento\OfflineShipping\Model\Resource\Carrier\Tablerate') + ->disableOriginalConstructor() + ->setMethods(['uploadAndImport']) + ->getMock(); + + $this->tableateFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($tablerateMock); + + $tablerateMock->expects($this->once()) + ->method('uploadAndImport') + ->with($this->model); + + $this->model->afterSave(); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Source/FlatrateTest.php b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Source/FlatrateTest.php new file mode 100644 index 0000000000000..c133cd1e07d79 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Source/FlatrateTest.php @@ -0,0 +1,30 @@ +model = new Flatrate(); + } + + public function testToOptionArray() + { + $expected = [ + ['value' => '', 'label' => __('None')], + ['value' => 'O', 'label' => __('Per Order')], + ['value' => 'I', 'label' => __('Per Item')] + ]; + + $this->assertEquals($expected, $this->model->toOptionArray()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Source/TablerateTest.php b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Source/TablerateTest.php new file mode 100644 index 0000000000000..8eea5073c3f3e --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Config/Source/TablerateTest.php @@ -0,0 +1,47 @@ +carrierTablerateMock = $this->getMockBuilder('\Magento\OfflineShipping\Model\Carrier\Tablerate') + ->disableOriginalConstructor() + ->setMethods(['getCode']) + ->getMock(); + + $helper = new \Magento\TestFramework\Helper\ObjectManager($this); + $this->model = $helper->getObject('Magento\OfflineShipping\Model\Config\Source\Tablerate', [ + 'carrierTablerate' => $this->carrierTablerateMock + ]); + } + + public function testToOptionArray() + { + $codes = [1, 2, 3, 4, 5]; + $expected = []; + foreach ($codes as $k => $v) { + $expected[] = ['value' => $k, 'label' => $v]; + } + + $this->carrierTablerateMock->expects($this->once()) + ->method('getCode') + ->willReturn($codes); + + $this->assertEquals($expected, $this->model->toOptionArray()); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Observer/SalesRule/ActionsTabTest.php b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Observer/SalesRule/ActionsTabTest.php new file mode 100644 index 0000000000000..751faf0cf82c8 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Observer/SalesRule/ActionsTabTest.php @@ -0,0 +1,70 @@ +model = new ActionsTab(); + } + + public function testPrepareForm() + { + $observerMock = $this->getMockBuilder('\Magento\Framework\Event\Observer') + ->disableOriginalConstructor() + ->setMethods(['getForm']) + ->getMock(); + + $formMock = $this->getMockBuilder('\Magento\Framework\Data\Form') + ->disableOriginalConstructor() + ->setMethods(['getElements']) + ->getMock(); + + $elementMock = $this->getMockBuilder('\Magento\Framework\Data\Form\Element\AbstractElement') + ->disableOriginalConstructor() + ->setMethods(['getId', 'addField']) + ->getMock(); + + $elementMock->expects($this->once()) + ->method('getId') + ->willReturn('action_fieldset'); + + $elementMock->expects($this->once()) + ->method('addField') + ->with( + 'simple_free_shipping', + 'select', + [ + 'label' => __('Free Shipping'), + 'title' => __('Free Shipping'), + 'name' => 'simple_free_shipping', + 'options' => [ + 0 => __('No'), + Rule::FREE_SHIPPING_ITEM => __('For matching items only'), + Rule::FREE_SHIPPING_ADDRESS => __('For shipment with matching items'), + ] + ] + ); + + $formMock->expects($this->once()) + ->method('getElements') + ->willReturn([$elementMock]); + + $observerMock->expects($this->once()) + ->method('getForm') + ->willReturn($formMock); + + $this->model->prepareForm($observerMock); + } +} diff --git a/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Plugin/Checkout/Block/Cart/ShippingTest.php b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Plugin/Checkout/Block/Cart/ShippingTest.php new file mode 100644 index 0000000000000..0fc0f97fbb760 --- /dev/null +++ b/dev/tests/unit/testsuite/Magento/OfflineShipping/Model/Plugin/Checkout/Block/Cart/ShippingTest.php @@ -0,0 +1,74 @@ +scopeConfigMock = $this->getMockBuilder('\Magento\Framework\App\Config\ScopeConfigInterface') + ->disableOriginalConstructor() + ->setMethods([ + 'getValue', + 'isSetFlag' + ]) + ->getMock(); + + $this->model = $helper->getObject('\Magento\OfflineShipping\Model\Plugin\Checkout\Block\Cart\Shipping', [ + 'scopeConfig' => $this->scopeConfigMock + ]); + } + + /** + * @dataProvider afterGetStateActiveDataProvider + */ + public function testAfterGetStateActive($scopeConfigMockReturnValue, $result, $assertResult) + { + /** @var \Magento\Checkout\Block\Cart\Shipping $subjectMock */ + $subjectMock = $this->getMockBuilder('Magento\Checkout\Block\Cart\Shipping') + ->disableOriginalConstructor() + ->getMock(); + + $this->scopeConfigMock->expects($result ? $this->never() : $this->once()) + ->method('getValue') + ->willReturn($scopeConfigMockReturnValue); + + $this->assertEquals($assertResult, $this->model->afterGetStateActive($subjectMock, $result)); + } + + public function afterGetStateActiveDataProvider() + { + return [ + [ + true, + true, + true + ], + [ + true, + false, + true + ], + [ + false, + false, + false + ] + ]; + } +} diff --git a/dev/tests/unit/testsuite/Magento/Payment/Block/Info/InstructionsTest.php b/dev/tests/unit/testsuite/Magento/Payment/Block/Info/InstructionsTest.php index 43528a3ce3970..7c035de52c8a1 100644 --- a/dev/tests/unit/testsuite/Magento/Payment/Block/Info/InstructionsTest.php +++ b/dev/tests/unit/testsuite/Magento/Payment/Block/Info/InstructionsTest.php @@ -12,12 +12,7 @@ class InstructionsTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Framework\Object - */ - protected $_method; - - /** - * @var \Magento\Payment\Model\Info + * @var \Magento\Payment\Model\Info|\PHPUnit_Framework_MockObject_MockObject */ protected $_info; @@ -28,28 +23,44 @@ class InstructionsTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $objectManagerHelper = new \Magento\TestFramework\Helper\ObjectManager($this); - $this->_method = new \Magento\Framework\Object(); - $this->_info = $objectManagerHelper->getObject('Magento\Payment\Model\Info'); - $this->_instructions = $objectManagerHelper->getObject('Magento\Payment\Block\Info\Instructions'); - - $this->_info->setMethodInstance($this->_method); - $this->_instructions->setInfo($this->_info); + $context = $this->getMock('Magento\Framework\View\Element\Template\Context', [], [], '', false); + $this->_instructions = new \Magento\Payment\Block\Info\Instructions($context); + $this->_info = $this->getMock('Magento\Payment\Model\Info', [], [], '', false); + $this->_instructions->setData('info', $this->_info); } - public function testGetInstructionsSetInstructions() + public function testGetInstructionAdditionalInformation() { - $this->assertNull($this->_instructions->getInstructions()); - $testInstruction = 'first test'; - $this->_method->setInstructions($testInstruction); - $this->assertEquals($testInstruction, $this->_instructions->getInstructions()); + $this->_info->expects($this->once()) + ->method('getAdditionalInformation') + ->with('instructions') + ->willReturn('get the instruction here'); + $this->assertEquals('get the instruction here', $this->_instructions->getInstructions()); + + // And we get the already setted param $this->_instructions + $this->assertEquals('get the instruction here', $this->_instructions->getInstructions()); } - public function testGetInstructionsSetInformation() + public function testGetInstruction() { - $this->assertNull($this->_instructions->getInstructions()); - $testInstruction = 'second test'; - $this->_info->setAdditionalInformation('instructions', $testInstruction); - $this->assertEquals($testInstruction, $this->_instructions->getInstructions()); + $methodInstance = $this->getMock( + 'Magento\Payment\Model\MethodInterface', + ['getConfigData', 'getCode', 'getFormBlockType', 'getTitle'], + [], + '', + false + ); + $methodInstance->expects($this->once()) + ->method('getConfigData') + ->with('instructions') + ->willReturn('get the instruction here'); + $this->_info->expects($this->once()) + ->method('getAdditionalInformation') + ->with('instructions') + ->willReturn(false); + $this->_info->expects($this->once()) + ->method('getMethodInstance') + ->willReturn($methodInstance); + $this->assertEquals('get the instruction here', $this->_instructions->getInstructions()); } } diff --git a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/Config/ReaderTest.php b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/Config/ReaderTest.php index 7ccdb02d2065c..0c2f708332fec 100644 --- a/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/Config/ReaderTest.php +++ b/dev/tests/unit/testsuite/Magento/Tools/Di/Compiler/Config/ReaderTest.php @@ -5,6 +5,9 @@ */ namespace Magento\Tools\Di\Compiler\Config; +use Magento\Framework\App\Area; +use Magento\Tools\Di\Definition\Collection; + class ReaderTest extends \PHPUnit_Framework_TestCase { /** @@ -54,6 +57,9 @@ protected function setUp() false ); $this->argumentsResolver = $this->getMock('Magento\Tools\Di\Compiler\ArgumentsResolver', [], [], '', false); + $this->argumentsResolverFactory->expects($this->any()) + ->method('create') + ->willReturn($this->argumentsResolver); $this->classReaderDecorator = $this->getMock( 'Magento\Tools\Di\Code\Reader\ClassReaderDecorator', [], @@ -72,71 +78,182 @@ protected function setUp() ); } - public function testGenerateCachePerScopeExtends() + public function testGenerateCachePerScopeGlobal() { - $definitionsCollection = $this->getMock('Magento\Tools\Di\Definition\Collection', [], [], '', false); - $this->diContainerConfig->expects($this->once()) - ->method('extend') - ->with([]); - $this->configLoader->expects($this->once()) - ->method('load') - ->with('areaCode') - ->willReturn([]); - - $this->argumentsResolverFactory->expects($this->once()) - ->method('create') - ->with($this->diContainerConfig) - ->willReturn($this->argumentsResolver); - $definitionsCollection->expects($this->exactly(2)) - ->method('getInstancesNamesList') - ->willReturn(['instanceType1'], ['instanceType2']); - $definitionsCollection->expects($this->once()) - ->method('getInstanceArguments') - ->willReturnMap([ - ['instanceType1', null], - ['instanceType2', ['arg1', 'arg2']], - ]); - $this->typeReader->expects($this->exactly(3)) - ->method('isConcrete') - ->willReturnMap([ - ['instanceType1', true], - ['instanceType2', false], - ['originalType1', true], - ['originalType2', false], - ]); - $this->argumentsResolver->expects($this->exactly(2)) - ->method('getResolvedConstructorArguments') - ->willReturnMap([ - ['instanceType1', 'resolvedConstructor1'], - ['instanceVirtualType1', 'resolvedConstructor2'], - ]); - $this->diContainerConfig->expects($this->exactly(2)) + $definitionCollection = $this->getDefinitionsCollection(); + $this->diContainerConfig->expects($this->any()) ->method('getVirtualTypes') - ->willReturn(['instanceVirtualType1' => 1, 'instanceVirtualType2' => 2]); - $this->diContainerConfig->expects($this->exactly(4)) + ->willReturn($this->getVirtualTypes()); + $this->diContainerConfig->expects($this->any()) + ->method('getPreferences') + ->willReturn($this->getPreferences()); + + $getResolvedConstructorArgumentsMap = $this->getResolvedVirtualConstructorArgumentsMap( + $definitionCollection, + $this->getVirtualTypes() + ); + + $this->diContainerConfig->expects($this->any()) ->method('getInstanceType') - ->willReturnMap([ - ['instanceVirtualType1', 'originalType1'], - ['instanceVirtualType2', 'originalType2'], - ]); - $definitionsCollection->expects($this->exactly(2)) - ->method('hasInstance') - ->willReturn(''); - $this->classReaderDecorator->expects($this->once()) - ->method('getConstructor') - ->willReturn('constructor'); - $this->diContainerConfig->expects($this->once()) + ->willReturnMap($this->getInstanceTypeMap($this->getVirtualTypes())); + + $this->diContainerConfig->expects($this->any()) ->method('isShared') - ->willReturnMap([ - ['instanceType1', true], - ['instanceType2', false], - ]); - $this->diContainerConfig->expects($this->once()) + ->willReturnMap($this->getExpectedNonShared()); + + $this->diContainerConfig->expects($this->any()) ->method('getPreference') - ->willReturnMap([ - ['instanceType1', 'instanceType1ss'], - ['instanceType2', 'instanceType2'], - ]); - $this->model->generateCachePerScope($definitionsCollection, 'areaCode'); + ->willReturnMap($this->getPreferencesMap()); + + $isConcreteMap = []; + foreach ($definitionCollection->getInstancesNamesList() as $instanceType) { + $isConcreteMap[] = [$instanceType, strpos($instanceType, 'Interface') === false]; + + $getResolvedConstructorArgumentsMap[] = [ + $instanceType, + $definitionCollection->getInstanceArguments($instanceType), + $this->getResolvedArguments( + $definitionCollection->getInstanceArguments($instanceType) + ) + ]; + } + + $this->typeReader->expects($this->any()) + ->method('isConcrete') + ->willReturnMap($isConcreteMap); + $this->argumentsResolver->expects($this->any()) + ->method('getResolvedConstructorArguments') + ->willReturnMap($getResolvedConstructorArgumentsMap); + + $this->assertEquals( + $this->getExpectedGlobalConfig(), + $this->model->generateCachePerScope($definitionCollection, Area::AREA_GLOBAL) + ); + } + + /** + * @return array + */ + private function getExpectedGlobalConfig() + { + return [ + 'arguments' => [ + 'ConcreteType1' => serialize(['resolved_argument1', 'resolved_argument2']), + 'ConcreteType2' => serialize(['resolved_argument1', 'resolved_argument2']), + 'virtualType1' => serialize(['resolved_argument1', 'resolved_argument2']) + ], + 'nonShared' => [ + 'ConcreteType2' => true, + 'ThirdPartyInterface' => true + ], + 'preferences' => $this->getPreferences(), + 'instanceTypes' => $this->getVirtualTypes(), + ]; + } + + /** + * @return Collection + */ + private function getDefinitionsCollection() + { + $definitionCollection = new Collection(); + $definitionCollection->addDefinition('ConcreteType1', ['argument1', 'argument2']); + $definitionCollection->addDefinition('ConcreteType2', ['argument1', 'argument2']); + $definitionCollection->addDefinition('Interface1', [null]); + + return $definitionCollection; + } + + /** + * @return array + */ + private function getVirtualTypes() + { + return ['virtualType1' => 'ConcreteType1']; + } + + /** + * @return array + */ + private function getExpectedNonShared() + { + return [ + ['ConcreteType1', true], + ['ConcreteType2', false], + ['Interface1', true], + ['ThirdPartyInterface', false] + ]; + } + + /** + * @return array + */ + private function getPreferences() + { + return [ + 'Interface1' => 'ConcreteType1', + 'ThirdPartyInterface' => 'ConcreteType2' + ]; + } + + /** + * @return array + */ + private function getPreferencesMap() + { + return [ + ['ConcreteType1', 'ConcreteType1'], + ['ConcreteType2', 'ConcreteType2'], + ['Interface1', 'ConcreteType1'], + ['ThirdPartyInterface', 'ConcreteType2'] + ]; + } + + /** + * @param array $arguments + * @return array|null + */ + private function getResolvedArguments($arguments) + { + return empty($arguments) ? null : array_map( + function ($argument) { + return 'resolved_' . $argument; + }, + $arguments + ); + } + + /** + * @param array $virtualTypes + * @return array + */ + private function getInstanceTypeMap($virtualTypes) + { + $getInstanceTypeMap = []; + foreach ($virtualTypes as $virtualType => $concreteType) { + $getInstanceTypeMap[] = [$virtualType, $concreteType]; + } + + return $getInstanceTypeMap; + } + + /** + * @param Collection $definitionCollection + * @param array $virtualTypes + * @return array + */ + private function getResolvedVirtualConstructorArgumentsMap(Collection $definitionCollection, array $virtualTypes) + { + $getResolvedConstructorArgumentsMap = []; + foreach ($virtualTypes as $virtualType => $concreteType) { + $getResolvedConstructorArgumentsMap[] = [ + $virtualType, + $definitionCollection->getInstanceArguments($concreteType), + $this->getResolvedArguments( + $definitionCollection->getInstanceArguments($concreteType) + ) + ]; + } + return $getResolvedConstructorArgumentsMap; } } diff --git a/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php b/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php index 42076377419a3..97063a236a2f9 100644 --- a/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php +++ b/dev/tools/Magento/Tools/Di/Code/Reader/ClassesScanner.php @@ -41,7 +41,7 @@ public function getList($path) } $classes = []; $recursiveIterator = new \RecursiveIteratorIterator( - new \RecursiveDirectoryIterator($realPath), + new \RecursiveDirectoryIterator($realPath, \FilesystemIterator::FOLLOW_SYMLINKS), \RecursiveIteratorIterator::SELF_FIRST ); /** @var $fileItem \SplFileInfo */ diff --git a/dev/tools/Magento/Tools/Di/Compiler/Config/Reader.php b/dev/tools/Magento/Tools/Di/Compiler/Config/Reader.php index 8dca09ef4e27d..f885f6fd12cd3 100644 --- a/dev/tools/Magento/Tools/Di/Compiler/Config/Reader.php +++ b/dev/tools/Magento/Tools/Di/Compiler/Config/Reader.php @@ -1,6 +1,5 @@ fillThirdPartyInterfaces($areaConfig, $definitionsCollection); $config['arguments'] = $this->getConfigForScope($definitionsCollection, $areaConfig); foreach ($config['arguments'] as $key => $value) { if ($value !== null) { $config['arguments'][$key] = serialize($value); } } + foreach ($definitionsCollection->getInstancesNamesList() as $instanceName) { if (!$areaConfig->isShared($instanceName)) { $config['nonShared'][$instanceName] = true; @@ -95,6 +97,7 @@ public function generateCachePerScope( $config['preferences'][$instanceName] = $preference; } } + foreach (array_keys($areaConfig->getVirtualTypes()) as $virtualType) { $config['instanceTypes'][$virtualType] = $areaConfig->getInstanceType($virtualType); } @@ -140,4 +143,26 @@ private function getConfigForScope(DefinitionsCollection $definitionsCollection, } return $constructors; } + + /** + * Returns preferences for third party code + * + * @param ConfigInterface $config + * @param DefinitionsCollection $definitionsCollection + * @SuppressWarnings(PHPMD.UnusedLocalVariable) + * + * @return void + */ + private function fillThirdPartyInterfaces(ConfigInterface $config, DefinitionsCollection $definitionsCollection) + { + $definedInstances = $definitionsCollection->getInstancesNamesList(); + + foreach ($config->getPreferences() as $interface => $preference) { + if (in_array($interface, $definedInstances)) { + continue; + } + + $definitionsCollection->addDefinition($interface, []); + } + } } diff --git a/dev/tools/Magento/Tools/Di/Definition/Collection.php b/dev/tools/Magento/Tools/Di/Definition/Collection.php index 82638c7c5578d..580f930d24cdd 100644 --- a/dev/tools/Magento/Tools/Di/Definition/Collection.php +++ b/dev/tools/Magento/Tools/Di/Definition/Collection.php @@ -1,6 +1,5 @@ definitions = $definitions; } @@ -54,7 +53,7 @@ public function addCollection(Collection $collection) * Add new definition for instance * * @param string $instance - * @param array $arguments + * @param array|null $arguments * * @return void */ @@ -67,7 +66,7 @@ public function addDefinition($instance, $arguments = []) * Returns instance arguments * * @param string $instanceName - * @return null + * @return null|array */ public function getInstanceArguments($instanceName) { diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php index 131e4bfcbaad6..a748488206643 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Compiled.php @@ -145,4 +145,14 @@ public function getVirtualTypes() { return $this->virtualTypes; } + + /** + * Returns list on preferences + * + * @return array + */ + public function getPreferences() + { + return $this->preferences; + } } diff --git a/lib/internal/Magento/Framework/ObjectManager/Config/Config.php b/lib/internal/Magento/Framework/ObjectManager/Config/Config.php index 29c0432bdc56d..6f05e61be5351 100644 --- a/lib/internal/Magento/Framework/ObjectManager/Config/Config.php +++ b/lib/internal/Magento/Framework/ObjectManager/Config/Config.php @@ -313,4 +313,14 @@ public function getVirtualTypes() { return $this->_virtualTypes; } + + /** + * Returns list on preferences + * + * @return array + */ + public function getPreferences() + { + return $this->_preferences; + } } diff --git a/lib/internal/Magento/Framework/ObjectManager/ConfigInterface.php b/lib/internal/Magento/Framework/ObjectManager/ConfigInterface.php index 9e023fcc5e3e8..8f8f91297dc2a 100644 --- a/lib/internal/Magento/Framework/ObjectManager/ConfigInterface.php +++ b/lib/internal/Magento/Framework/ObjectManager/ConfigInterface.php @@ -72,4 +72,11 @@ public function getVirtualTypes(); * @return void */ public function extend(array $configuration); + + /** + * Returns list on preferences + * + * @return array + */ + public function getPreferences(); }