From edf4c105c46f688cc9d86fa994fd3f73f00ab9f6 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 9 Aug 2016 11:01:04 +0300 Subject: [PATCH 001/115] MAGETWO-56577: Rename interface orderInvoice to invoiceOrder --- ...Interface.php => InvoiceOrderInterface.php} | 4 ++-- .../{OrderInvoice.php => InvoiceOrder.php} | 4 ++-- ...derInvoiceTest.php => InvoiceOrderTest.php} | 18 +++++++++--------- app/code/Magento/Sales/etc/di.xml | 2 +- app/code/Magento/Sales/etc/webapi.xml | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) rename app/code/Magento/Sales/Api/{OrderInvoiceInterface.php => InvoiceOrderInterface.php} (93%) rename app/code/Magento/Sales/Model/{OrderInvoice.php => InvoiceOrder.php} (98%) rename app/code/Magento/Sales/Test/Unit/Model/{OrderInvoiceTest.php => InvoiceOrderTest.php} (96%) diff --git a/app/code/Magento/Sales/Api/OrderInvoiceInterface.php b/app/code/Magento/Sales/Api/InvoiceOrderInterface.php similarity index 93% rename from app/code/Magento/Sales/Api/OrderInvoiceInterface.php rename to app/code/Magento/Sales/Api/InvoiceOrderInterface.php index 80fa6159afc3e..0f0ce45969916 100644 --- a/app/code/Magento/Sales/Api/OrderInvoiceInterface.php +++ b/app/code/Magento/Sales/Api/InvoiceOrderInterface.php @@ -7,11 +7,11 @@ namespace Magento\Sales\Api; /** - * Class OrderInvoiceInterface + * Class InvoiceOrderInterface * * @api */ -interface OrderInvoiceInterface +interface InvoiceOrderInterface { /** * @param int $orderId diff --git a/app/code/Magento/Sales/Model/OrderInvoice.php b/app/code/Magento/Sales/Model/InvoiceOrder.php similarity index 98% rename from app/code/Magento/Sales/Model/OrderInvoice.php rename to app/code/Magento/Sales/Model/InvoiceOrder.php index b499b7028bce1..d3f73a18b0157 100644 --- a/app/code/Magento/Sales/Model/OrderInvoice.php +++ b/app/code/Magento/Sales/Model/InvoiceOrder.php @@ -9,7 +9,7 @@ use Magento\Framework\App\ResourceConnection; use Magento\Sales\Api\Data\InvoiceCommentCreationInterface; use Magento\Sales\Api\Data\InvoiceCreationArgumentsInterface; -use Magento\Sales\Api\OrderInvoiceInterface; +use Magento\Sales\Api\InvoiceOrderInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\Order\Config as OrderConfig; use Magento\Sales\Model\Order\Invoice\NotifierInterface; @@ -24,7 +24,7 @@ * Class InvoiceService * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class OrderInvoice implements OrderInvoiceInterface +class InvoiceOrder implements InvoiceOrderInterface { /** * @var ResourceConnection diff --git a/app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php similarity index 96% rename from app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php rename to app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php index bc36da112aa29..412a05ff77967 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php @@ -20,15 +20,15 @@ use Magento\Sales\Model\Order\InvoiceValidatorInterface; use Magento\Sales\Model\Order\OrderStateResolverInterface; use Magento\Sales\Model\Order\PaymentAdapterInterface; -use Magento\Sales\Model\OrderInvoice; +use Magento\Sales\Model\InvoiceOrder; use Psr\Log\LoggerInterface; /** - * Class OrderInvoiceTest + * Class InvoiceOrderTest * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ -class OrderInvoiceTest extends \PHPUnit_Framework_TestCase +class InvoiceOrderTest extends \PHPUnit_Framework_TestCase { /** * @var ResourceConnection|\PHPUnit_Framework_MockObject_MockObject @@ -76,9 +76,9 @@ class OrderInvoiceTest extends \PHPUnit_Framework_TestCase private $notifierInterfaceMock; /** - * @var OrderInvoice|\PHPUnit_Framework_MockObject_MockObject + * @var InvoiceOrder|\PHPUnit_Framework_MockObject_MockObject */ - private $orderInvoice; + private $invoiceOrder; /** * @var InvoiceCreationArgumentsInterface|\PHPUnit_Framework_MockObject_MockObject @@ -172,7 +172,7 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); - $this->orderInvoice = new OrderInvoice( + $this->invoiceOrder = new InvoiceOrder( $this->resourceConnectionMock, $this->orderRepositoryMock, $this->invoiceDocumentFactoryMock, @@ -271,7 +271,7 @@ public function testOrderInvoice($orderId, $capture, $items, $notify, $appendCom $this->assertEquals( 2, - $this->orderInvoice->execute( + $this->invoiceOrder->execute( $orderId, $capture, $items, @@ -314,7 +314,7 @@ public function testDocumentValidationException() ->with($this->invoiceMock, $this->orderMock) ->willReturn($errorMessages); - $this->orderInvoice->execute( + $this->invoiceOrder->execute( $orderId, $capture, $items, @@ -372,7 +372,7 @@ public function testCouldNotInvoiceException() $this->adapterInterface->expects($this->once()) ->method('rollBack'); - $this->orderInvoice->execute( + $this->invoiceOrder->execute( $orderId, $capture, $items, diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 383650c8688fc..4e4050123717c 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -88,7 +88,7 @@ - + diff --git a/app/code/Magento/Sales/etc/webapi.xml b/app/code/Magento/Sales/etc/webapi.xml index 8d1b1fda5bc31..ed273c96f299b 100644 --- a/app/code/Magento/Sales/etc/webapi.xml +++ b/app/code/Magento/Sales/etc/webapi.xml @@ -254,7 +254,7 @@ - + From a1f9114fa2a3997135f7c847bc64dc5227533ab1 Mon Sep 17 00:00:00 2001 From: Nadiya Syvokonenko Date: Tue, 9 Aug 2016 11:50:40 +0300 Subject: [PATCH 002/115] MAGETWO-56491: Introduce and implement ShipmentCreationArgumentsInterface --- .../ShipmentCreationArgumentsInterface.php | 32 ++++++++++++++++ .../Order/Shipment/CreationArguments.php | 37 +++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 1 + 3 files changed, 70 insertions(+) create mode 100644 app/code/Magento/Sales/Api/Data/ShipmentCreationArgumentsInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/CreationArguments.php diff --git a/app/code/Magento/Sales/Api/Data/ShipmentCreationArgumentsInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentCreationArgumentsInterface.php new file mode 100644 index 0000000000000..f03d05be3c742 --- /dev/null +++ b/app/code/Magento/Sales/Api/Data/ShipmentCreationArgumentsInterface.php @@ -0,0 +1,32 @@ +extensionAttributes; + } + + /** + * {@inheritdoc} + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\ShipmentCreationArgumentsExtensionInterface $extensionAttributes + ) { + $this->extensionAttributes = $extensionAttributes; + return $this; + } +} diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 383650c8688fc..49fd2891fa9c6 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -30,6 +30,7 @@ + From 30f7a8a23fe4d0ff917b5d2b4144c16f29e3121b Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Tue, 9 Aug 2016 16:34:12 +0300 Subject: [PATCH 003/115] MAGETWO-56692: Introduce extension attributes for InvoiceCreation Interfaces --- .../Data/InvoiceCommentCreationInterface.php | 16 +++ .../Api/Data/InvoiceCommentInterface.php | 2 +- .../Api/Data/InvoiceItemCreationInterface.php | 16 +++ .../Sales/Api/Data/InvoiceItemInterface.php | 15 --- .../Model/Order/Invoice/CommentCreation.php | 97 +++++++++++++++++++ .../Model/Order/Invoice/ItemCreation.php | 28 ++++++ app/code/Magento/Sales/etc/di.xml | 6 +- 7 files changed, 161 insertions(+), 19 deletions(-) create mode 100644 app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php diff --git a/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php index 85c114da3821e..340393107781c 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php @@ -13,4 +13,20 @@ */ interface InvoiceCommentCreationInterface extends \Magento\Sales\Api\Data\CommentInterface { + /** + * Retrieve existing extension attributes object or create a new one. + * + * @return \Magento\Sales\Api\Data\InvoiceCommentCreationExtensionInterface|null + */ + public function getExtensionAttributes(); + + /** + * Set an extension attributes object. + * + * @param \Magento\Sales\Api\Data\InvoiceCommentCreationExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\InvoiceCommentCreationExtensionInterface $extensionAttributes + ); } diff --git a/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php index 1447fab476ad9..985d0dc8f14db 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php @@ -13,7 +13,7 @@ * @api */ interface InvoiceCommentInterface extends \Magento\Framework\Api\ExtensibleDataInterface, -\Magento\Sales\Api\Data\InvoiceCommentCreationInterface + \Magento\Sales\Api\Data\CommentInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case. diff --git a/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php index 4b5c6fa2c6136..d4b250ed911ec 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php @@ -15,4 +15,20 @@ */ interface InvoiceItemCreationInterface extends LineItemInterface { + /** + * Retrieve existing extension attributes object or create a new one. + * + * @return \Magento\Sales\Api\Data\InvoiceItemCreationExtensionInterface|null + */ + public function getExtensionAttributes(); + + /** + * Set an extension attributes object. + * + * @param \Magento\Sales\Api\Data\InvoiceItemCreationExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\InvoiceItemCreationExtensionInterface $extensionAttributes + ); } diff --git a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php index ecdcd07bc0ca5..011c613d14522 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php @@ -448,19 +448,4 @@ public function setDiscountTaxCompensationAmount($amount); * @return $this */ public function setBaseDiscountTaxCompensationAmount($amount); - - /** - * Retrieve existing extension attributes object or create a new one. - * - * @return \Magento\Sales\Api\Data\InvoiceItemExtensionInterface|null - */ - public function getExtensionAttributes(); - - /** - * Set an extension attributes object. - * - * @param \Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes - * @return $this - */ - public function setExtensionAttributes(\Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes); } diff --git a/app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php b/app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php new file mode 100644 index 0000000000000..58ad64a0e5816 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php @@ -0,0 +1,97 @@ +comment; + } + + /** + * Sets the comment for the invoice. + * + * @param string $comment + * @return $this + */ + public function setComment($comment) + { + $this->comment = $comment; + return $this; + } + + /** + * Gets the is-visible-on-storefront flag value for the invoice. + * + * @return int Is-visible-on-storefront flag value. + */ + public function getIsVisibleOnFront() + { + return $this->isVisibleOnFront; + } + + /** + * Sets the is-visible-on-storefront flag value for the invoice. + * + * @param int $isVisibleOnFront + * @return $this + */ + public function setIsVisibleOnFront($isVisibleOnFront) + { + $this->isVisibleOnFront = $isVisibleOnFront; + return $this; + } + + /** + * Retrieve existing extension attributes object or create a new one. + * + * @return \Magento\Sales\Api\Data\InvoiceCommentCreationExtensionInterface|null + */ + public function getExtensionAttributes() + { + return $this->extensionAttributes; + } + + /** + * Set an extension attributes object. + * + * @param \Magento\Sales\Api\Data\InvoiceCommentCreationExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\InvoiceCommentCreationExtensionInterface $extensionAttributes + ) { + $this->extensionAttributes = $extensionAttributes; + } +} diff --git a/app/code/Magento/Sales/Model/Order/Invoice/ItemCreation.php b/app/code/Magento/Sales/Model/Order/Invoice/ItemCreation.php index abc19c3aaa73d..26d8d7ae6ca99 100644 --- a/app/code/Magento/Sales/Model/Order/Invoice/ItemCreation.php +++ b/app/code/Magento/Sales/Model/Order/Invoice/ItemCreation.php @@ -23,6 +23,11 @@ class ItemCreation implements InvoiceItemCreationInterface */ private $qty; + /** + * @var \Magento\Sales\Api\Data\InvoiceItemCreationExtensionInterface + */ + private $extensionAttributes; + /** * {@inheritdoc} */ @@ -54,4 +59,27 @@ public function setQty($qty) { $this->qty = $qty; } + + /** + * Retrieve existing extension attributes object or create a new one. + * + * @return \Magento\Sales\Api\Data\InvoiceItemCreationExtensionInterface|null + */ + public function getExtensionAttributes() + { + return $this->extensionAttributes; + } + + /** + * Set an extension attributes object. + * + * @param \Magento\Sales\Api\Data\InvoiceItemCreationExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\InvoiceItemCreationExtensionInterface $extensionAttributes + ) { + $this->extensionAttributes = $extensionAttributes; + return $this; + } } diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 383650c8688fc..d16fdc11c2952 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -49,7 +49,9 @@ - + + + @@ -86,8 +88,6 @@ - - From a9be1d142839e03dc159285444ee070126a95f7c Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Tue, 9 Aug 2016 17:18:58 +0300 Subject: [PATCH 004/115] MAGETWO-56692: Introduce extension attributes for InvoiceCreation Interfaces --- .../Sales/Api/Data/CommentInterface.php | 10 ++++ .../Sales/Api/Data/EntityInterface.php | 52 +++++++++++++++++++ .../Api/Data/InvoiceCommentInterface.php | 48 +---------------- .../Sales/Api/Data/InvoiceItemInterface.php | 4 +- 4 files changed, 65 insertions(+), 49 deletions(-) create mode 100644 app/code/Magento/Sales/Api/Data/EntityInterface.php diff --git a/app/code/Magento/Sales/Api/Data/CommentInterface.php b/app/code/Magento/Sales/Api/Data/CommentInterface.php index d7021dc9f9546..fcab786319340 100644 --- a/app/code/Magento/Sales/Api/Data/CommentInterface.php +++ b/app/code/Magento/Sales/Api/Data/CommentInterface.php @@ -12,6 +12,16 @@ */ interface CommentInterface { + /* + * Is-visible-on-storefront flag. + */ + const IS_VISIBLE_ON_FRONT = 'is_visible_on_front'; + + /* + * Comment. + */ + const COMMENT = 'comment'; + /** * Gets the comment for the invoice. * diff --git a/app/code/Magento/Sales/Api/Data/EntityInterface.php b/app/code/Magento/Sales/Api/Data/EntityInterface.php new file mode 100644 index 0000000000000..30eb054bc2cf6 --- /dev/null +++ b/app/code/Magento/Sales/Api/Data/EntityInterface.php @@ -0,0 +1,52 @@ + Date: Wed, 10 Aug 2016 11:38:22 +0300 Subject: [PATCH 005/115] MAGETWO-56573: Introduce and implement ShipmentItemCreationInterface --- .../Data/ShipmentItemCreationInterface.php | 35 ++++++++ .../Sales/Api/Data/ShipmentItemInterface.php | 33 +------ .../Model/Order/Shipment/ItemCreation.php | 86 +++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 1 + 4 files changed, 124 insertions(+), 31 deletions(-) create mode 100644 app/code/Magento/Sales/Api/Data/ShipmentItemCreationInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php diff --git a/app/code/Magento/Sales/Api/Data/ShipmentItemCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentItemCreationInterface.php new file mode 100644 index 0000000000000..57c908fb88c5e --- /dev/null +++ b/app/code/Magento/Sales/Api/Data/ShipmentItemCreationInterface.php @@ -0,0 +1,35 @@ +orderItemId; + } + + /** + * {@inheritdoc} + */ + public function setOrderItemId($orderItemId) + { + $this->orderItemId = $orderItemId; + } + + /** + * {@inheritdoc} + */ + public function getQty() + { + return $this->qty; + } + + /** + * {@inheritdoc} + */ + public function setQty($qty) + { + $this->qty = $qty; + } + + /** + * {@inheritdoc} + * + * @return \Magento\Sales\Api\Data\ShipmentItemCreationExtensionInterface|null + */ + public function getExtensionAttributes() + { + return $this->extensionAttributes; + } + + /** + * {@inheritdoc} + * + * @param \Magento\Sales\Api\Data\ShipmentItemCreationExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\ShipmentItemCreationExtensionInterface $extensionAttributes + ) { + $this->extensionAttributes = $extensionAttributes; + return $this; + } + //@codeCoverageIgnoreEnd +} diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 49fd2891fa9c6..a6c84816dd387 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -32,6 +32,7 @@ + From 0a973e55ab479ff5b1945caa2247ecffe5a8e52f Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Wed, 10 Aug 2016 12:19:47 +0300 Subject: [PATCH 006/115] MAGETWO-56572: Introduce and implement ShipmentCommentCreationInterface --- .../Data/ShipmentCommentCreationInterface.php | 30 ++++++ .../Api/Data/ShipmentCommentInterface.php | 79 +-------------- .../Model/Order/Shipment/CommentCreation.php | 97 +++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 1 + 4 files changed, 130 insertions(+), 77 deletions(-) create mode 100644 app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php diff --git a/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php new file mode 100644 index 0000000000000..62495a654aeec --- /dev/null +++ b/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php @@ -0,0 +1,30 @@ +extensionAttributes; + } + + /** + * Set an extension attributes object. + * + * @param \Magento\Sales\Api\Data\ShipmentCommentCreationExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\ShipmentCommentCreationExtensionInterface $extensionAttributes + ) + { + $this->extensionAttributes = $extensionAttributes; + return $this; + } + + /** + * Gets the comment for the invoice. + * + * @return string Comment. + */ + public function getComment() + { + return $this->comment; + } + + /** + * Sets the comment for the invoice. + * + * @param string $comment + * @return $this + */ + public function setComment($comment) + { + $this->comment = $comment; + return $this; + } + + /** + * Gets the is-visible-on-storefront flag value for the invoice. + * + * @return int Is-visible-on-storefront flag value. + */ + public function getIsVisibleOnFront() + { + return $this->isVisibleOnFront; + } + + /** + * Sets the is-visible-on-storefront flag value for the invoice. + * + * @param int $isVisibleOnFront + * @return $this + */ + public function setIsVisibleOnFront($isVisibleOnFront) + { + $this->isVisibleOnFront = $isVisibleOnFront; + return $isVisibleOnFront; + } +} diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index d16fdc11c2952..cc10bb9a917ae 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -52,6 +52,7 @@ + From 33bfbda7613e948a14985e24d0c1ec2b8b24288e Mon Sep 17 00:00:00 2001 From: Nadiya Syvokonenko Date: Wed, 10 Aug 2016 14:58:17 +0300 Subject: [PATCH 007/115] MAGETWO-56493: Introduce and implement OrderRegisterInterface --- .../Model/Order/Shipment/OrderRegistrar.php | 28 +++++++ .../Shipment/OrderRegistrarInterface.php | 26 +++++++ .../Order/Shipment/OrderRegistrarTest.php | 73 +++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 3 +- 4 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrar.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php create mode 100644 app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/OrderRegistrarTest.php diff --git a/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrar.php b/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrar.php new file mode 100644 index 0000000000000..040ab12949be1 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrar.php @@ -0,0 +1,28 @@ +getItems() as $item) { + if ($item->getQty() > 0) { + $item->register(); + } + } + return $order; + } +} diff --git a/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php b/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php new file mode 100644 index 0000000000000..6131b3585bc99 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php @@ -0,0 +1,26 @@ +orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + $this->shipmentMock = $this->getMockBuilder(\Magento\Sales\Api\Data\ShipmentInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->model = new \Magento\Sales\Model\Order\Shipment\OrderRegistrar(); + } + + public function testRegister() + { + $item1 = $this->getShipmentItemMock(); + $item1->expects($this->once()) + ->method('getQty') + ->willReturn(0); + $item1->expects($this->never()) + ->method('register'); + + $item2 = $this->getShipmentItemMock(); + $item2->expects($this->once()) + ->method('getQty') + ->willReturn(0.5); + $item2->expects($this->once()) + ->method('register'); + + $items = [$item1, $item2]; + $this->shipmentMock->expects($this->once()) + ->method('getItems') + ->willReturn($items); + $this->assertEquals( + $this->orderMock, + $this->model->register($this->orderMock, $this->shipmentMock) + ); + } + + /** + * @return \PHPUnit_Framework_MockObject_MockObject + */ + private function getShipmentItemMock() + { + return $this->getMockBuilder(\Magento\Sales\Api\Data\ShipmentItemInterface::class) + ->disableOriginalConstructor() + ->setMethods(['register']) + ->getMockForAbstractClass(); + } +} diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index a6c84816dd387..89594620b097a 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -67,10 +67,11 @@ - + + From 8d27ecac1a9a6aa4b55e390a4ddbd89d5d4612e5 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Wed, 10 Aug 2016 18:24:30 +0300 Subject: [PATCH 008/115] MAGETWO-56572: Introduce and implement ShipmentCommentCreationInterface --- .../Magento/Sales/Api/Data/EntityInterface.php | 3 ++- .../Sales/Api/Data/InvoiceCommentInterface.php | 6 ++++-- .../Sales/Api/Data/InvoiceItemInterface.php | 18 +++++++++++++++++- .../Data/ShipmentCommentCreationInterface.php | 4 +++- .../Api/Data/ShipmentCommentInterface.php | 6 ++++-- .../Model/Order/Shipment/CommentCreation.php | 3 +-- 6 files changed, 31 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Sales/Api/Data/EntityInterface.php b/app/code/Magento/Sales/Api/Data/EntityInterface.php index 30eb054bc2cf6..d09b25920f899 100644 --- a/app/code/Magento/Sales/Api/Data/EntityInterface.php +++ b/app/code/Magento/Sales/Api/Data/EntityInterface.php @@ -7,6 +7,7 @@ /** * Interface EntityInterface + * @api */ interface EntityInterface { @@ -49,4 +50,4 @@ public function getEntityId(); * @return $this */ public function setEntityId($entityId); -} \ No newline at end of file +} diff --git a/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php index b81a7b3b07bb4..dd9b1a7718033 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php @@ -12,8 +12,10 @@ * invoice history. * @api */ -interface InvoiceCommentInterface extends \Magento\Framework\Api\ExtensibleDataInterface, - \Magento\Sales\Api\Data\CommentInterface, \Magento\Sales\Api\Data\EntityInterface +interface InvoiceCommentInterface + extends \Magento\Framework\Api\ExtensibleDataInterface, + \Magento\Sales\Api\Data\CommentInterface, + \Magento\Sales\Api\Data\EntityInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case. diff --git a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php index 7ea3646c0bb89..0f07bfcad23ea 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php @@ -11,7 +11,8 @@ * An invoice is a record of the receipt of payment for an order. An invoice item is a purchased item in an invoice. * @api */ -interface InvoiceItemInterface extends \Magento\Framework\Api\ExtensibleDataInterface, +interface InvoiceItemInterface + extends \Magento\Framework\Api\ExtensibleDataInterface, \Magento\Sales\Api\Data\LineItemInterface { /**#@+ @@ -448,4 +449,19 @@ public function setDiscountTaxCompensationAmount($amount); * @return $this */ public function setBaseDiscountTaxCompensationAmount($amount); + + /** + * Retrieve existing extension attributes object or create a new one. + * + * @return \Magento\Sales\Api\Data\InvoiceItemExtensionInterface|null + */ + public function getExtensionAttributes(); + + /** + * Set an extension attributes object. + * + * @param \Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes(\Magento\Sales\Api\Data\InvoiceItemExtensionInterface $extensionAttributes); } diff --git a/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php index 62495a654aeec..0c17af1aae2c3 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php @@ -7,8 +7,10 @@ /** * Interface ShipmentCommentCreationInterface + * @api */ -interface ShipmentCommentCreationInterface extends \Magento\Framework\Api\ExtensibleDataInterface, +interface ShipmentCommentCreationInterface + extends \Magento\Framework\Api\ExtensibleDataInterface, \Magento\Sales\Api\Data\CommentInterface { /** diff --git a/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php index 8c01cecd98df6..3b17b1b955906 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php @@ -12,8 +12,10 @@ * document lists the products and their quantities in the delivery package. A shipment document can contain comments. * @api */ -interface ShipmentCommentInterface extends \Magento\Framework\Api\ExtensibleDataInterface, - \Magento\Sales\Api\Data\CommentInterface, \Magento\Sales\Api\Data\EntityInterface +interface ShipmentCommentInterface + extends \Magento\Framework\Api\ExtensibleDataInterface, + \Magento\Sales\Api\Data\CommentInterface, + \Magento\Sales\Api\Data\EntityInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case. diff --git a/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php index b32aa7bb29788..89c92ad80f7d5 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php @@ -45,8 +45,7 @@ public function getExtensionAttributes() */ public function setExtensionAttributes( \Magento\Sales\Api\Data\ShipmentCommentCreationExtensionInterface $extensionAttributes - ) - { + ) { $this->extensionAttributes = $extensionAttributes; return $this; } From 82015792aa0a7f02b67accde37059ebefb29b826 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Thu, 11 Aug 2016 11:44:26 +0300 Subject: [PATCH 009/115] MAGETWO-56488: Introduce and implement ShipmentTrackCreationInterface --- .../Data/ShipmentTrackCreationInterface.php | 137 +++++++++++++ .../Sales/Api/Data/ShipmentTrackInterface.php | 107 +--------- .../Magento/Sales/Api/Data/TrackInterface.php | 120 +++++++++++ .../Model/Order/Shipment/TrackCreation.php | 189 ++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 1 + 5 files changed, 448 insertions(+), 106 deletions(-) create mode 100644 app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php create mode 100644 app/code/Magento/Sales/Api/Data/TrackInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php diff --git a/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php new file mode 100644 index 0000000000000..dcca11da20d6f --- /dev/null +++ b/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php @@ -0,0 +1,137 @@ +weight; + } + + /** + * {@inheritdoc} + */ + public function setWeight($weight) + { + $this->weight = $weight; + } + + /** + * {@inheritdoc} + */ + public function getQty() + { + return $this->qty; + } + + /** + * {@inheritdoc} + */ + public function setQty($qty) + { + $this->qty = $qty; + } + + /** + * {@inheritdoc} + */ + public function getOrderId() + { + return $this->orderId; + } + + /** + * {@inheritdoc} + */ + public function setOrderId($orderId) + { + $this->orderId = $orderId; + } + + /** + * {@inheritdoc} + */ + public function getTrackNumber() + { + return $this->trackNumber; + } + + /** + * {@inheritdoc} + */ + public function setTrackNumber($trackNumber) + { + $this->trackNumber = $trackNumber; + } + + /** + * {@inheritdoc} + */ + public function getDescription() + { + return $this->description; + } + + /** + * {@inheritdoc} + */ + public function setDescription($description) + { + $this->description = $description; + } + + /** + * {@inheritdoc} + */ + public function getTitle() + { + return $this->title; + } + + /** + * {@inheritdoc} + */ + public function setTitle($title) + { + $this->title = $title; + } + + /** + * {@inheritdoc} + */ + public function getCarrierCode() + { + return $this->carrierCode; + } + + /** + * {@inheritdoc} + */ + public function setCarrierCode($carrierCode) + { + $this->carrierCode = $carrierCode; + } + + /** + * {@inheritdoc} + * + * @return \Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface|null + */ + public function getExtensionAttributes() + { + return $this->extensionAttributes; + } + + /** + * {@inheritdoc} + * + * @param \Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes(\Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface $extensionAttributes) + { + $this->extensionAttributes = $extensionAttributes; + return $this; + } +} diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 89594620b097a..22e982a6b2c63 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -93,6 +93,7 @@ + From be8e36a71c88a8dc21e656ea974761b77607d174 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 11 Aug 2016 12:48:21 +0300 Subject: [PATCH 010/115] MAGETWO-56572: Introduce and implement ShipmentCommentCreationInterface --- .../Sales/Api/Data/InvoiceCommentCreationInterface.php | 4 +++- .../Magento/Sales/Api/Data/InvoiceCommentInterface.php | 7 +++---- .../Sales/Api/Data/InvoiceItemCreationInterface.php | 4 +++- app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php | 5 ++--- .../Sales/Api/Data/ShipmentCommentCreationInterface.php | 6 +++--- .../Magento/Sales/Api/Data/ShipmentCommentInterface.php | 7 +++---- 6 files changed, 17 insertions(+), 16 deletions(-) diff --git a/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php index 340393107781c..5e4d85e93cb98 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceCommentCreationInterface.php @@ -6,12 +6,14 @@ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; + /** * Interface InvoiceCommentCreationInterface * * @api */ -interface InvoiceCommentCreationInterface extends \Magento\Sales\Api\Data\CommentInterface +interface InvoiceCommentCreationInterface extends ExtensibleDataInterface, CommentInterface { /** * Retrieve existing extension attributes object or create a new one. diff --git a/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php index dd9b1a7718033..f661a1ab6a4ca 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceCommentInterface.php @@ -5,6 +5,8 @@ */ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; + /** * Invoice comment interface. * @@ -12,10 +14,7 @@ * invoice history. * @api */ -interface InvoiceCommentInterface - extends \Magento\Framework\Api\ExtensibleDataInterface, - \Magento\Sales\Api\Data\CommentInterface, - \Magento\Sales\Api\Data\EntityInterface +interface InvoiceCommentInterface extends ExtensibleDataInterface, CommentInterface, EntityInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case. diff --git a/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php index d4b250ed911ec..2f2ddfbf758a0 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceItemCreationInterface.php @@ -6,6 +6,8 @@ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; + /** * Input argument for invoice creation * @@ -13,7 +15,7 @@ * * @api */ -interface InvoiceItemCreationInterface extends LineItemInterface +interface InvoiceItemCreationInterface extends LineItemInterface, ExtensibleDataInterface { /** * Retrieve existing extension attributes object or create a new one. diff --git a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php index 0f07bfcad23ea..811b0ad0653b1 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php @@ -5,15 +5,14 @@ */ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; /** * Invoice item interface. * * An invoice is a record of the receipt of payment for an order. An invoice item is a purchased item in an invoice. * @api */ -interface InvoiceItemInterface - extends \Magento\Framework\Api\ExtensibleDataInterface, - \Magento\Sales\Api\Data\LineItemInterface +interface InvoiceItemInterface extends ExtensibleDataInterface, LineItemInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case. diff --git a/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php index 0c17af1aae2c3..40beaa32d02f2 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentCommentCreationInterface.php @@ -5,13 +5,13 @@ */ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; + /** * Interface ShipmentCommentCreationInterface * @api */ -interface ShipmentCommentCreationInterface - extends \Magento\Framework\Api\ExtensibleDataInterface, - \Magento\Sales\Api\Data\CommentInterface +interface ShipmentCommentCreationInterface extends ExtensibleDataInterface, CommentInterface { /** * Retrieve existing extension attributes object or create a new one. diff --git a/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php index 3b17b1b955906..0c00bf80d78f1 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentCommentInterface.php @@ -5,6 +5,8 @@ */ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; + /** * Shipment comment interface. * @@ -12,10 +14,7 @@ * document lists the products and their quantities in the delivery package. A shipment document can contain comments. * @api */ -interface ShipmentCommentInterface - extends \Magento\Framework\Api\ExtensibleDataInterface, - \Magento\Sales\Api\Data\CommentInterface, - \Magento\Sales\Api\Data\EntityInterface +interface ShipmentCommentInterface extends ExtensibleDataInterface, CommentInterface, EntityInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case. From ddcddce21b8b24157567a29bea5f30b046b9d68a Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 11 Aug 2016 13:55:08 +0300 Subject: [PATCH 011/115] MAGETWO-56572: Introduce and implement ShipmentCommentCreationInterface --- app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php index 811b0ad0653b1..37a933f97a29a 100644 --- a/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php +++ b/app/code/Magento/Sales/Api/Data/InvoiceItemInterface.php @@ -6,6 +6,7 @@ namespace Magento\Sales\Api\Data; use Magento\Framework\Api\ExtensibleDataInterface; + /** * Invoice item interface. * From 8a80791406f9968e549360942962fa1f5e4fa697 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Thu, 11 Aug 2016 14:56:32 +0300 Subject: [PATCH 012/115] MAGETWO-56490: Implement ShipmentPackageInterface --- .../Sales/Model/Order/Shipment/Package.php | 36 +++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 1 + 2 files changed, 37 insertions(+) create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/Package.php diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Package.php b/app/code/Magento/Sales/Model/Order/Shipment/Package.php new file mode 100644 index 0000000000000..c9bc3371822f6 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/Package.php @@ -0,0 +1,36 @@ +extensionAttributes; + } + + /** + * {@inheritdoc} + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\ShipmentPackageExtensionInterface $extensionAttributes + ) { + $this->extensionAttributes = $extensionAttributes; + return $this; + } +} diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 4aba7ac450330..9786018d923af 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -95,6 +95,7 @@ + From 444d906377d57e0b107aeebf16e3aef40f6c431c Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 11 Aug 2016 16:14:10 +0300 Subject: [PATCH 013/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation --- .../Model/Order/Invoice/InvoiceValidator.php | 38 +++++++++++++ ...dator.php => InvoiceQuantityValidator.php} | 34 +++++------ .../Model/Order/InvoiceValidatorInterface.php | 25 --------- .../Sales/Model/Order/OrderValidator.php | 46 +++++++-------- .../Model/Order/OrderValidatorInterface.php | 2 +- .../Model/Order/Validation/CanInvoice.php | 56 +++++++++++++++++++ app/code/Magento/Sales/Model/OrderInvoice.php | 30 ++++++++-- app/code/Magento/Sales/Model/Validator.php | 51 +++++++++++++++++ .../Sales/Model/ValidatorInterface.php | 21 +++++++ .../Validator/InvoiceOrderTest.php} | 12 ++-- .../Unit/Model/Order/InvoiceValidatorTest.php | 2 +- app/code/Magento/Sales/etc/di.xml | 5 ++ 12 files changed, 241 insertions(+), 81 deletions(-) create mode 100644 app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php rename app/code/Magento/Sales/Model/Order/{InvoiceValidator.php => InvoiceQuantityValidator.php} (72%) delete mode 100644 app/code/Magento/Sales/Model/Order/InvoiceValidatorInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php create mode 100644 app/code/Magento/Sales/Model/Validator.php create mode 100644 app/code/Magento/Sales/Model/ValidatorInterface.php rename app/code/Magento/Sales/Test/Unit/Model/Order/{OrderValidatorTest.php => Invoice/Validator/InvoiceOrderTest.php} (90%) diff --git a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php new file mode 100644 index 0000000000000..089443c1f0833 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php @@ -0,0 +1,38 @@ +validator = $validator; + } + + /** + * @param InvoiceInterface $entity + * @param array $validators + * @return string[] + */ + public function validate(InvoiceInterface $entity, array $validators) + { + return $this->validator->validate($entity, $validators); + } +} \ No newline at end of file diff --git a/app/code/Magento/Sales/Model/Order/InvoiceValidator.php b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php similarity index 72% rename from app/code/Magento/Sales/Model/Order/InvoiceValidator.php rename to app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php index 35222599fc69e..51e98f8277b3b 100644 --- a/app/code/Magento/Sales/Model/Order/InvoiceValidator.php +++ b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php @@ -9,42 +9,42 @@ use Magento\Sales\Api\Data\InvoiceInterface; use Magento\Sales\Api\Data\InvoiceItemInterface; use Magento\Sales\Api\Data\OrderInterface; +use Magento\Sales\Api\OrderRepositoryInterface; +use Magento\Sales\Model\Order\Invoice\Validator\InvoiceValidatorRunner; +use Magento\Sales\Model\ValidatorInterface; /** * Interface InvoiceValidatorInterface */ -class InvoiceValidator implements InvoiceValidatorInterface +class InvoiceQuantityValidator implements ValidatorInterface { /** - * @var OrderValidatorInterface + * @var InvoiceValidatorRunner */ - private $orderValidator; + private $invoiceValidatorRunner; + /** + * @var OrderRepositoryInterface + */ + private $orderRepository; /** * InvoiceValidator constructor. - * @param OrderValidatorInterface $orderValidator + * @param InvoiceValidatorRunner $invoiceValidatorRunner */ - public function __construct(OrderValidatorInterface $orderValidator) + public function __construct(InvoiceValidatorRunner $invoiceValidatorRunner, OrderRepositoryInterface $orderRepository) { - $this->orderValidator = $orderValidator; + $this->invoiceValidatorRunner = $invoiceValidatorRunner; + $this->orderRepository = $orderRepository; } /** * @param InvoiceInterface $invoice - * @param OrderInterface $order * @return array */ - public function validate(InvoiceInterface $invoice, OrderInterface $order) + public function validate($invoice) { - $messages = $this->checkQtyAvailability($invoice, $order); - - if (!$this->orderValidator->canInvoice($order)) { - $messages[] = __( - 'An invoice cannot be created when an order has a status of %1.', - $order->getStatus() - ); - } - return $messages; + $order = $this->orderRepository->get($invoice->getOrderId()); + return $this->checkQtyAvailability($invoice, $order); } /** diff --git a/app/code/Magento/Sales/Model/Order/InvoiceValidatorInterface.php b/app/code/Magento/Sales/Model/Order/InvoiceValidatorInterface.php deleted file mode 100644 index 64b2f98dfe37e..0000000000000 --- a/app/code/Magento/Sales/Model/Order/InvoiceValidatorInterface.php +++ /dev/null @@ -1,25 +0,0 @@ -validator = $validator; + } + + /** + * @param OrderInterface $entity + * @param array $validators + * @return string[] + * @throws DocumentValidationException */ - public function canInvoice(OrderInterface $order) + public function validate(OrderInterface $entity, array $validators) { - if ($order->getState() === Order::STATE_PAYMENT_REVIEW || - $order->getState() === Order::STATE_HOLDED || - $order->getState() === Order::STATE_CANCELED || - $order->getState() === Order::STATE_COMPLETE || - $order->getState() === Order::STATE_CLOSED - ) { - return false; - }; - /** @var \Magento\Sales\Model\Order\Item $item */ - foreach ($order->getItems() as $item) { - if ($item->getQtyToInvoice() > 0 && !$item->getLockedDoInvoice()) { - return true; - } - } - return false; + return $this->validator->validate($entity, $validators); } -} +} \ No newline at end of file diff --git a/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php b/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php index d0dcc38af642a..6ed3bacc05797 100644 --- a/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php +++ b/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php @@ -19,5 +19,5 @@ interface OrderValidatorInterface * @param OrderInterface $order * @return bool */ - public function canInvoice(OrderInterface $order); + public function validate(OrderInterface $order); } diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php new file mode 100644 index 0000000000000..785ec5acdb74d --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php @@ -0,0 +1,56 @@ +canInvoice($entity)) { + $messages[] = __( + 'An invoice cannot be created when an order has a status of %1.', + $entity->getStatus() + ); + } + + return $messages; + } + + /** + * @param OrderInterface $order + * @return bool + */ + private function canInvoice(OrderInterface $order) { + if ($order->getState() === Order::STATE_PAYMENT_REVIEW || + $order->getState() === Order::STATE_HOLDED || + $order->getState() === Order::STATE_CANCELED || + $order->getState() === Order::STATE_COMPLETE || + $order->getState() === Order::STATE_CLOSED + ) { + return false; + }; + /** @var \Magento\Sales\Model\Order\Item $item */ + foreach ($order->getItems() as $item) { + if ($item->getQtyToInvoice() > 0 && !$item->getLockedDoInvoice()) { + return true; + } + } + return false; + } +} diff --git a/app/code/Magento/Sales/Model/OrderInvoice.php b/app/code/Magento/Sales/Model/OrderInvoice.php index b499b7028bce1..e8cb28fca11b7 100644 --- a/app/code/Magento/Sales/Model/OrderInvoice.php +++ b/app/code/Magento/Sales/Model/OrderInvoice.php @@ -12,12 +12,15 @@ use Magento\Sales\Api\OrderInvoiceInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\Order\Config as OrderConfig; +use Magento\Sales\Model\Order\Invoice\InvoiceValidator; use Magento\Sales\Model\Order\Invoice\NotifierInterface; use Magento\Sales\Model\Order\InvoiceDocumentFactory; +use Magento\Sales\Model\Order\InvoiceQuantityValidator; use Magento\Sales\Model\Order\InvoiceRepository; -use Magento\Sales\Model\Order\InvoiceValidatorInterface; use Magento\Sales\Model\Order\OrderStateResolverInterface; use Magento\Sales\Model\Order\PaymentAdapterInterface; +use Magento\Sales\Model\Order\Validation\CanInvoice; +use Magento\Sales\Model\Order\OrderValidator; use Psr\Log\LoggerInterface; /** @@ -42,7 +45,7 @@ class OrderInvoice implements OrderInvoiceInterface private $invoiceDocumentFactory; /** - * @var InvoiceValidatorInterface + * @var InvoiceValidator */ private $invoiceValidator; @@ -76,12 +79,18 @@ class OrderInvoice implements OrderInvoiceInterface */ private $logger; + /** + * @var OrderValidator + */ + private $orderValidator; + /** * OrderInvoice constructor. * @param ResourceConnection $resourceConnection * @param OrderRepositoryInterface $orderRepository * @param InvoiceDocumentFactory $invoiceDocumentFactory - * @param InvoiceValidatorInterface $invoiceValidator + * @param InvoiceValidator $invoiceValidator + * @param OrderValidator $orderValidator * @param PaymentAdapterInterface $paymentAdapter * @param OrderStateResolverInterface $orderStateResolver * @param OrderConfig $config @@ -94,7 +103,8 @@ public function __construct( ResourceConnection $resourceConnection, OrderRepositoryInterface $orderRepository, InvoiceDocumentFactory $invoiceDocumentFactory, - InvoiceValidatorInterface $invoiceValidator, + InvoiceValidator $invoiceValidator, + OrderValidator $orderValidator, PaymentAdapterInterface $paymentAdapter, OrderStateResolverInterface $orderStateResolver, OrderConfig $config, @@ -106,6 +116,7 @@ public function __construct( $this->orderRepository = $orderRepository; $this->invoiceDocumentFactory = $invoiceDocumentFactory; $this->invoiceValidator = $invoiceValidator; + $this->orderValidator = $orderValidator; $this->paymentAdapter = $paymentAdapter; $this->orderStateResolver = $orderStateResolver; $this->config = $config; @@ -147,7 +158,16 @@ public function execute( ($appendComment && $notify), $arguments ); - $errorMessages = $this->invoiceValidator->validate($invoice, $order); + $errorMessages = array_merge( + $this->invoiceValidator->validate( + $invoice, + [InvoiceQuantityValidator::class] + ), + $this->orderValidator->validate( + $order, + [CanInvoice::class] + ) + ); if (!empty($errorMessages)) { throw new \Magento\Sales\Exception\DocumentValidationException( __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages)) diff --git a/app/code/Magento/Sales/Model/Validator.php b/app/code/Magento/Sales/Model/Validator.php new file mode 100644 index 0000000000000..5c18fd24b5829 --- /dev/null +++ b/app/code/Magento/Sales/Model/Validator.php @@ -0,0 +1,51 @@ +objectManager = $objectManager; + } + + /** + * @inheritdoc + */ + public function validate($entity, array $validators) + { + $messages = []; + foreach ($validators as $validatorName) { + $validator = $this->objectManager->get($validatorName); + if (!$validator instanceof ValidatorInterface) { + throw new ConfigurationMismatchException( + __( + sprintf('Validator %s is not instance of general validator interface', $validatorName) + ) + ); + } + $messages = array_merge($messages, $validator->validate($entity)); + } + + return $messages; + } +} diff --git a/app/code/Magento/Sales/Model/ValidatorInterface.php b/app/code/Magento/Sales/Model/ValidatorInterface.php new file mode 100644 index 0000000000000..a42f938ea879d --- /dev/null +++ b/app/code/Magento/Sales/Model/ValidatorInterface.php @@ -0,0 +1,21 @@ +setMethods(['getQtyToInvoice', 'getLockedDoInvoice']) ->getMockForAbstractClass(); - $this->model = new \Magento\Sales\Model\Order\OrderValidator(); + $this->model = new \Magento\Sales\Model\Order\Invoice\Validator\InvoiceOrder(); } /** @@ -64,7 +64,7 @@ public function testCanInvoiceWrongState($state) ->method('getItems'); $this->assertEquals( false, - $this->model->canInvoice($this->orderMock) + $this->model->validate($this->orderMock) ); } @@ -95,7 +95,7 @@ public function testCanInvoiceNoItems() $this->assertEquals( false, - $this->model->canInvoice($this->orderMock) + $this->model->validate($this->orderMock) ); } @@ -125,7 +125,7 @@ public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedRes $this->assertEquals( $expectedResult, - $this->model->canInvoice($this->orderMock) + $this->model->validate($this->orderMock) ); } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceValidatorTest.php index 6fdfdb61b3635..c8113721e4fdd 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceValidatorTest.php @@ -58,7 +58,7 @@ protected function setUp() ->getMockForAbstractClass(); $this->model = $this->objectManager->getObject( - \Magento\Sales\Model\Order\InvoiceValidator::class, + \Magento\Sales\Model\Order\InvoiceQuantityValidator::class, ['orderValidator' => $this->orderValidatorMock] ); } diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 383650c8688fc..ade1dc7631d6d 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -909,4 +909,9 @@ + + + Magento\Sales\Model\Invoice\Validator\InvoiceOrder + + From 04474e59983db20ab048d86cf9432c149f6e6794 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 11 Aug 2016 17:01:37 +0300 Subject: [PATCH 014/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation --- .../Model/Order/InvoiceQuantityValidator.php | 10 +-- .../Model/Order/OrderValidatorInterface.php | 23 ------ .../Model/Order/Validation/CanInvoice.php | 11 ++- ...t.php => InvoiceQuantityValidatorTest.php} | 76 ++++--------------- .../CanInvoiceTest.php} | 15 ++-- 5 files changed, 29 insertions(+), 106 deletions(-) delete mode 100644 app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php rename app/code/Magento/Sales/Test/Unit/Model/Order/{InvoiceValidatorTest.php => InvoiceQuantityValidatorTest.php} (66%) rename app/code/Magento/Sales/Test/Unit/Model/Order/{Invoice/Validator/InvoiceOrderTest.php => Validation/CanInvoiceTest.php} (89%) diff --git a/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php index 51e98f8277b3b..a7c1008968926 100644 --- a/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php @@ -10,7 +10,6 @@ use Magento\Sales\Api\Data\InvoiceItemInterface; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\OrderRepositoryInterface; -use Magento\Sales\Model\Order\Invoice\Validator\InvoiceValidatorRunner; use Magento\Sales\Model\ValidatorInterface; /** @@ -18,10 +17,6 @@ */ class InvoiceQuantityValidator implements ValidatorInterface { - /** - * @var InvoiceValidatorRunner - */ - private $invoiceValidatorRunner; /** * @var OrderRepositoryInterface */ @@ -29,11 +24,10 @@ class InvoiceQuantityValidator implements ValidatorInterface /** * InvoiceValidator constructor. - * @param InvoiceValidatorRunner $invoiceValidatorRunner + * @param OrderRepositoryInterface $orderRepository */ - public function __construct(InvoiceValidatorRunner $invoiceValidatorRunner, OrderRepositoryInterface $orderRepository) + public function __construct(OrderRepositoryInterface $orderRepository) { - $this->invoiceValidatorRunner = $invoiceValidatorRunner; $this->orderRepository = $orderRepository; } diff --git a/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php b/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php deleted file mode 100644 index 6ed3bacc05797..0000000000000 --- a/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php +++ /dev/null @@ -1,23 +0,0 @@ -objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); - $this->orderValidatorMock = $this->getMockBuilder(\Magento\Sales\Model\Order\OrderValidatorInterface::class) - ->disableOriginalConstructor() - ->setMethods(['canInvoice']) - ->getMockForAbstractClass(); - $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class) ->disableOriginalConstructor() ->setMethods(['getStatus']) @@ -56,10 +51,13 @@ protected function setUp() ->disableOriginalConstructor() ->setMethods(['getTotalQty', 'getItems']) ->getMockForAbstractClass(); - + $this->orderRepositoryMock = $this->getMockBuilder( + OrderRepositoryInterface::class + )->disableOriginalConstructor()->getMockForAbstractClass(); + $this->orderRepositoryMock->expects($this->any())->method('get')->willReturn($this->orderMock); $this->model = $this->objectManager->getObject( \Magento\Sales\Model\Order\InvoiceQuantityValidator::class, - ['orderValidator' => $this->orderValidatorMock] + ['orderRepository' => $this->orderRepositoryMock] ); } @@ -75,39 +73,9 @@ public function testValidate() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([$orderItemMock]); - $this->orderValidatorMock->expects($this->once()) - ->method('canInvoice') - ->with($this->orderMock) - ->willReturn(true); - $this->assertEquals( - $expectedResult, - $this->model->validate($this->invoiceMock, $this->orderMock) - ); - } - - public function testValidateCanNotInvoiceOrder() - { - $orderStatus = 'Test Status'; - $expectedResult = [__('An invoice cannot be created when an order has a status of %1.', $orderStatus)]; - $invoiceItemMock = $this->getInvoiceItemMock(1, 1); - $this->invoiceMock->expects($this->once()) - ->method('getItems') - ->willReturn([$invoiceItemMock]); - - $orderItemMock = $this->getOrderItemMock(1, 1, true); - $this->orderMock->expects($this->once()) - ->method('getItems') - ->willReturn([$orderItemMock]); - $this->orderMock->expects($this->once()) - ->method('getStatus') - ->willReturn($orderStatus); - $this->orderValidatorMock->expects($this->once()) - ->method('canInvoice') - ->with($this->orderMock) - ->willReturn(false); $this->assertEquals( $expectedResult, - $this->model->validate($this->invoiceMock, $this->orderMock) + $this->model->validate($this->invoiceMock) ); } @@ -125,13 +93,9 @@ public function testValidateInvoiceQtyBiggerThanOrder() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([$orderItemMock]); - $this->orderValidatorMock->expects($this->once()) - ->method('canInvoice') - ->with($this->orderMock) - ->willReturn(true); $this->assertEquals( $expectedResult, - $this->model->validate($this->invoiceMock, $this->orderMock) + $this->model->validate($this->invoiceMock) ); } @@ -146,13 +110,9 @@ public function testValidateNoOrderItems() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([]); - $this->orderValidatorMock->expects($this->once()) - ->method('canInvoice') - ->with($this->orderMock) - ->willReturn(true); $this->assertEquals( $expectedResult, - $this->model->validate($this->invoiceMock, $this->orderMock) + $this->model->validate($this->invoiceMock) ); } @@ -169,13 +129,9 @@ public function testValidateNoInvoiceItems() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([$orderItemMock]); - $this->orderValidatorMock->expects($this->once()) - ->method('canInvoice') - ->with($this->orderMock) - ->willReturn(true); $this->assertEquals( $expectedResult, - $this->model->validate($this->invoiceMock, $this->orderMock) + $this->model->validate($this->invoiceMock) ); } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/Validator/InvoiceOrderTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php similarity index 89% rename from app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/Validator/InvoiceOrderTest.php rename to app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php index 6942567ec143f..7d33050ad3aaa 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Invoice/Validator/InvoiceOrderTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php @@ -11,10 +11,10 @@ /** * Test for \Magento\Sales\Model\Order\OrderValidator class */ -class InvoiceOrderTest extends \PHPUnit_Framework_TestCase +class CanInvoiceTest extends \PHPUnit_Framework_TestCase { /** - * @var \Magento\Sales\Model\Order\OrderValidatorInterface|\PHPUnit_Framework_MockObject_MockObject + * @var \Magento\Sales\Model\Order\Validation\CanInvoice|\PHPUnit_Framework_MockObject_MockObject */ private $model; @@ -47,7 +47,7 @@ protected function setUp() ->setMethods(['getQtyToInvoice', 'getLockedDoInvoice']) ->getMockForAbstractClass(); - $this->model = new \Magento\Sales\Model\Order\Invoice\Validator\InvoiceOrder(); + $this->model = new \Magento\Sales\Model\Order\Validation\CanInvoice(); } /** @@ -62,8 +62,7 @@ public function testCanInvoiceWrongState($state) ->willReturn($state); $this->orderMock->expects($this->never()) ->method('getItems'); - $this->assertEquals( - false, + $this->assertNotEmpty( $this->model->validate($this->orderMock) ); } @@ -93,8 +92,7 @@ public function testCanInvoiceNoItems() ->method('getItems') ->willReturn([]); - $this->assertEquals( - false, + $this->assertNotEmpty( $this->model->validate($this->orderMock) ); } @@ -124,8 +122,7 @@ public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedRes ->willReturn($itemLockedDoInvoice); $this->assertEquals( - $expectedResult, - $this->model->validate($this->orderMock) + $expectedResult, empty($this->model->validate($this->orderMock)) ); } From f9f1534fac7730b41cfb3a13103b4d741c2bb8da Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 11 Aug 2016 17:58:24 +0300 Subject: [PATCH 015/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation --- .../Model/Order/Validation/CanInvoiceTest.php | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php index 7d33050ad3aaa..31d4e6cde9e12 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php @@ -125,6 +125,30 @@ public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedRes $expectedResult, empty($this->model->validate($this->orderMock)) ); } + + public function testValidateCanNotInvoiceOrder() + { + $orderStatus = 'Test Status'; + $expectedResult = [__('An invoice cannot be created when an order has a status of %1.', $orderStatus)]; + $orderItemMock = $orderItemMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderItemInterface::class) + ->disableOriginalConstructor() + ->setMethods(['getId', 'getQtyToInvoice', 'isDummy', 'getSku']) + ->getMockForAbstractClass(); + $orderItemMock->expects($this->any())->method('getId')->willReturn(1); + $orderItemMock->expects($this->any())->method('getQtyToInvoice')->willReturn(0); + $orderItemMock->expects($this->any())->method('isDummy')->willReturn(true); + $orderItemMock->expects($this->any())->method('getSku')->willReturn(1); + $this->orderMock->expects($this->once()) + ->method('getItems') + ->willReturn([$orderItemMock]); + $this->orderMock->expects($this->once()) + ->method('getStatus') + ->willReturn($orderStatus); + $this->assertEquals( + $expectedResult, + $this->model->validate($this->orderMock) + ); + } /** * Data provider for testCanInvoice From 0b4d732c8b07343e200251bbbed3730836588116 Mon Sep 17 00:00:00 2001 From: Dmytro Voskoboinikov Date: Thu, 11 Aug 2016 18:06:45 +0300 Subject: [PATCH 016/115] MAGETWO-56495: Introduce and implement ShipmentNotifier --- .../Sales/Model/Order/Shipment/Notifier.php | 41 ++ .../Order/Shipment/NotifierInterface.php | 31 ++ .../Order/Shipment/Sender/EmailSender.php | 149 ++++++++ .../Model/Order/Shipment/SenderInterface.php | 29 ++ .../Order/Shipment/Sender/EmailSenderTest.php | 361 ++++++++++++++++++ 5 files changed, 611 insertions(+) create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/Notifier.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/NotifierInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/Sender/EmailSender.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/SenderInterface.php create mode 100644 app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Sender/EmailSenderTest.php diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Notifier.php b/app/code/Magento/Sales/Model/Order/Shipment/Notifier.php new file mode 100644 index 0000000000000..21dd5ad4a58f6 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/Notifier.php @@ -0,0 +1,41 @@ +senders = $senders; + } + + /** + * {@inheritdoc} + */ + public function notify( + \Magento\Sales\Api\Data\OrderInterface $order, + \Magento\Sales\Api\Data\ShipmentInterface $shipment, + \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment = null, + $forceSyncMode = false + ) { + foreach ($this->senders as $sender) { + $sender->send($order, $shipment, $comment, $forceSyncMode); + } + } +} diff --git a/app/code/Magento/Sales/Model/Order/Shipment/NotifierInterface.php b/app/code/Magento/Sales/Model/Order/Shipment/NotifierInterface.php new file mode 100644 index 0000000000000..f34eb6178d094 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/NotifierInterface.php @@ -0,0 +1,31 @@ +paymentHelper = $paymentHelper; + $this->shipmentResource = $shipmentResource; + $this->globalConfig = $globalConfig; + $this->eventManager = $eventManager; + } + + /** + * Sends order shipment email to the customer. + * + * Email will be sent immediately in two cases: + * + * - if asynchronous email sending is disabled in global settings + * - if $forceSyncMode parameter is set to TRUE + * + * Otherwise, email will be sent later during running of + * corresponding cron job. + * + * @param \Magento\Sales\Api\Data\OrderInterface $order + * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment + * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment + * @param bool $forceSyncMode + * + * @return bool + */ + public function send( + \Magento\Sales\Api\Data\OrderInterface $order, + \Magento\Sales\Api\Data\ShipmentInterface $shipment, + \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment = null, + $forceSyncMode = false + ) { + $shipment->setSendEmail(true); + + if (!$this->globalConfig->getValue('sales_email/general/async_sending') || $forceSyncMode) { + $transport = [ + 'order' => $order, + 'shipment' => $shipment, + 'comment' => $comment ? $comment->getComment() : '', + 'billing' => $order->getBillingAddress(), + 'payment_html' => $this->getPaymentHtml($order), + 'store' => $order->getStore(), + 'formattedShippingAddress' => $this->getFormattedShippingAddress($order), + 'formattedBillingAddress' => $this->getFormattedBillingAddress($order) + ]; + + $this->eventManager->dispatch( + 'email_shipment_set_template_vars_before', + ['sender' => $this, 'transport' => $transport] + ); + + $this->templateContainer->setTemplateVars($transport); + + if ($this->checkAndSend($order)) { + $shipment->setEmailSent(true); + + $this->shipmentResource->saveAttribute($shipment, ['send_email', 'email_sent']); + + return true; + } + } else { + $shipment->setEmailSent(null); + + $this->shipmentResource->saveAttribute($shipment, 'email_sent'); + } + + $this->shipmentResource->saveAttribute($shipment, 'send_email'); + + return false; + } + + /** + * Returns payment info block as HTML. + * + * @param \Magento\Sales\Api\Data\OrderInterface $order + * + * @return string + */ + private function getPaymentHtml(\Magento\Sales\Api\Data\OrderInterface $order) + { + return $this->paymentHelper->getInfoBlockHtml( + $order->getPayment(), + $this->identityContainer->getStore()->getStoreId() + ); + } +} diff --git a/app/code/Magento/Sales/Model/Order/Shipment/SenderInterface.php b/app/code/Magento/Sales/Model/Order/Shipment/SenderInterface.php new file mode 100644 index 0000000000000..a030038b7b139 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/SenderInterface.php @@ -0,0 +1,29 @@ +orderMock = $this->getMockBuilder(\Magento\Sales\Model\Order::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->storeMock = $this->getMockBuilder(\Magento\Store\Model\Store::class) + ->setMethods(['getStoreId']) + ->disableOriginalConstructor() + ->getMock(); + + $this->storeMock->expects($this->any()) + ->method('getStoreId') + ->willReturn(1); + $this->orderMock->expects($this->any()) + ->method('getStore') + ->willReturn($this->storeMock); + + $this->senderMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Email\Sender::class) + ->disableOriginalConstructor() + ->setMethods(['send', 'sendCopyTo']) + ->getMock(); + + $this->loggerMock = $this->getMockBuilder(\Psr\Log\LoggerInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->shipmentMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Shipment::class) + ->disableOriginalConstructor() + ->setMethods(['setSendEmail', 'setEmailSent']) + ->getMock(); + + $this->commentMock = $this->getMockBuilder(\Magento\Sales\Api\Data\ShipmentCommentCreationInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->commentMock->expects($this->any()) + ->method('getComment') + ->willReturn('Comment text'); + + $this->addressMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Address::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->orderMock->expects($this->any()) + ->method('getBillingAddress') + ->willReturn($this->addressMock); + $this->orderMock->expects($this->any()) + ->method('getShippingAddress') + ->willReturn($this->addressMock); + + $this->globalConfigMock = $this->getMockBuilder(\Magento\Framework\App\Config\ScopeConfigInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->eventManagerMock = $this->getMockBuilder(\Magento\Framework\Event\ManagerInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->paymentInfoMock = $this->getMockBuilder(\Magento\Payment\Model\Info::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->orderMock->expects($this->any()) + ->method('getPayment') + ->willReturn($this->paymentInfoMock); + + $this->paymentHelperMock = $this->getMockBuilder(\Magento\Payment\Helper\Data::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->paymentHelperMock->expects($this->any()) + ->method('getInfoBlockHtml') + ->with($this->paymentInfoMock, 1) + ->willReturn('Payment Info Block'); + + $this->shipmentResourceMock = $this->getMockBuilder(\Magento\Sales\Model\ResourceModel\Order\Shipment::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->addressRendererMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Address\Renderer::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->addressRendererMock->expects($this->any()) + ->method('format') + ->with($this->addressMock, 'html') + ->willReturn('Formatted address'); + + $this->templateContainerMock = $this->getMockBuilder(\Magento\Sales\Model\Order\Email\Container\Template::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->identityContainerMock = $this->getMockBuilder( + \Magento\Sales\Model\Order\Email\Container\ShipmentIdentity::class + ) + ->disableOriginalConstructor() + ->getMock(); + + $this->identityContainerMock->expects($this->any()) + ->method('getStore') + ->willReturn($this->storeMock); + + $this->senderBuilderFactoryMock = $this->getMockBuilder( + \Magento\Sales\Model\Order\Email\SenderBuilderFactory::class + ) + ->disableOriginalConstructor() + ->setMethods(['create']) + ->getMock(); + + $this->subject = new \Magento\Sales\Model\Order\Shipment\Sender\EmailSender( + $this->templateContainerMock, + $this->identityContainerMock, + $this->senderBuilderFactoryMock, + $this->loggerMock, + $this->addressRendererMock, + $this->paymentHelperMock, + $this->shipmentResourceMock, + $this->globalConfigMock, + $this->eventManagerMock + ); + } + + /** + * @param int $configValue + * @param bool $forceSyncMode + * @param bool $isComment + * @param bool $emailSendingResult + * + * @dataProvider sendDataProvider + * + * @return void + * + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) + */ + public function testSend($configValue, $forceSyncMode, $isComment, $emailSendingResult) + { + $this->globalConfigMock->expects($this->once()) + ->method('getValue') + ->with('sales_email/general/async_sending') + ->willReturn($configValue); + + if (!$isComment) { + $this->commentMock = null; + } + + $this->shipmentMock->expects($this->once()) + ->method('setSendEmail') + ->with(true); + + if (!$configValue || $forceSyncMode) { + $transport = [ + 'order' => $this->orderMock, + 'shipment' => $this->shipmentMock, + 'comment' => $isComment ? 'Comment text' : '', + 'billing' => $this->addressMock, + 'payment_html' => 'Payment Info Block', + 'store' => $this->storeMock, + 'formattedShippingAddress' => 'Formatted address', + 'formattedBillingAddress' => 'Formatted address', + ]; + + $this->eventManagerMock->expects($this->once()) + ->method('dispatch') + ->with( + 'email_shipment_set_template_vars_before', + [ + 'sender' => $this->subject, + 'transport' => $transport, + ] + ); + + $this->templateContainerMock->expects($this->once()) + ->method('setTemplateVars') + ->with($transport); + + $this->identityContainerMock->expects($this->once()) + ->method('isEnabled') + ->willReturn($emailSendingResult); + + if ($emailSendingResult) { + $this->senderBuilderFactoryMock->expects($this->once()) + ->method('create') + ->willReturn($this->senderMock); + + $this->senderMock->expects($this->once()) + ->method('send'); + + $this->senderMock->expects($this->once()) + ->method('sendCopyTo'); + + $this->shipmentMock->expects($this->once()) + ->method('setEmailSent') + ->with(true); + + $this->shipmentResourceMock->expects($this->once()) + ->method('saveAttribute') + ->with($this->shipmentMock, ['send_email', 'email_sent']); + + $this->assertTrue( + $this->subject->send( + $this->orderMock, + $this->shipmentMock, + $this->commentMock, + $forceSyncMode + ) + ); + } else { + $this->shipmentResourceMock->expects($this->once()) + ->method('saveAttribute') + ->with($this->shipmentMock, 'send_email'); + + $this->assertFalse( + $this->subject->send( + $this->orderMock, + $this->shipmentMock, + $this->commentMock, + $forceSyncMode + ) + ); + } + } else { + $this->shipmentMock->expects($this->once()) + ->method('setEmailSent') + ->with(null); + + $this->shipmentResourceMock->expects($this->at(0)) + ->method('saveAttribute') + ->with($this->shipmentMock, 'email_sent'); + $this->shipmentResourceMock->expects($this->at(1)) + ->method('saveAttribute') + ->with($this->shipmentMock, 'send_email'); + + $this->assertFalse( + $this->subject->send( + $this->orderMock, + $this->shipmentMock, + $this->commentMock, + $forceSyncMode + ) + ); + } + } + + /** + * @return array + */ + public function sendDataProvider() + { + return [ + 'Successful sync sending with comment' => [0, false, true, true], + 'Successful sync sending without comment' => [0, false, false, true], + 'Failed sync sending with comment' => [0, false, true, false], + 'Successful forced sync sending with comment' => [1, true, true, true], + 'Async sending' => [1, false, false, false], + ]; + } +} From a43af2592bef09499cbf02c149fb3f4bc770e3a6 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 11 Aug 2016 18:08:14 +0300 Subject: [PATCH 017/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation --- .../Model/Order/Invoice/InvoiceValidator.php | 4 +--- .../Invoice/InvoiceValidatorInterface.php | 21 +++++++++++++++++ .../Sales/Model/Order/OrderValidator.php | 7 ++---- .../Model/Order/OrderValidatorInterface.php | 23 +++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 7 ++---- 5 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php diff --git a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php index 089443c1f0833..21428cd6d4ff2 100644 --- a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php +++ b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php @@ -27,9 +27,7 @@ public function __construct(\Magento\Sales\Model\Validator $validator) } /** - * @param InvoiceInterface $entity - * @param array $validators - * @return string[] + * @inheritdoc */ public function validate(InvoiceInterface $entity, array $validators) { diff --git a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php new file mode 100644 index 0000000000000..e1c830d0d58e6 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php @@ -0,0 +1,21 @@ + + + @@ -915,9 +917,4 @@ - - - Magento\Sales\Model\Invoice\Validator\InvoiceOrder - - From 395d9b330d4619fc8179151bdb20386ed6291048 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Thu, 11 Aug 2016 18:23:01 +0300 Subject: [PATCH 018/115] MAGETWO-56490: Implement ShipmentPackageInterface --- app/code/Magento/Sales/Model/Order/Shipment/Package.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Package.php b/app/code/Magento/Sales/Model/Order/Shipment/Package.php index c9bc3371822f6..6f8f54336a2c5 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Package.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Package.php @@ -1,5 +1,4 @@ Date: Thu, 11 Aug 2016 18:33:39 +0300 Subject: [PATCH 019/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation --- .../Sales/Model/Order/Invoice/InvoiceValidatorInterface.php | 5 ++++- .../Magento/Sales/Model/Order/OrderValidatorInterface.php | 3 ++- app/code/Magento/Sales/Model/Validator.php | 5 ++++- app/code/Magento/Sales/etc/di.xml | 1 + 4 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php index e1c830d0d58e6..568019a40fce5 100644 --- a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php +++ b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidatorInterface.php @@ -6,6 +6,8 @@ namespace Magento\Sales\Model\Order\Invoice; use Magento\Sales\Api\Data\InvoiceInterface; +use Magento\Sales\Exception\DocumentValidationException; +use Magento\Sales\Model\ValidatorInterface; /** * Interface InvoiceValidatorInterface @@ -14,8 +16,9 @@ interface InvoiceValidatorInterface { /** * @param InvoiceInterface $entity - * @param array $validators + * @param ValidatorInterface[] $validators * @return string[] + * @throws DocumentValidationException */ public function validate(InvoiceInterface $entity, array $validators); } diff --git a/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php b/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php index df8a33cd5967c..c5a9a6c1d3296 100644 --- a/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php +++ b/app/code/Magento/Sales/Model/Order/OrderValidatorInterface.php @@ -7,6 +7,7 @@ use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Exception\DocumentValidationException; +use Magento\Sales\Model\ValidatorInterface; /** * Interface OrderValidatorInterface @@ -15,7 +16,7 @@ interface OrderValidatorInterface { /** * @param OrderInterface $entity - * @param array $validators + * @param ValidatorInterface[] $validators * @return string[] * @throws DocumentValidationException */ diff --git a/app/code/Magento/Sales/Model/Validator.php b/app/code/Magento/Sales/Model/Validator.php index 5c18fd24b5829..0b7dbc5eb83d3 100644 --- a/app/code/Magento/Sales/Model/Validator.php +++ b/app/code/Magento/Sales/Model/Validator.php @@ -29,7 +29,10 @@ public function __construct(ObjectManager $objectManager) } /** - * @inheritdoc + * @param object $entity + * @param ValidatorInterface[] $validators + * @return string[] + * @throws ConfigurationMismatchException */ public function validate($entity, array $validators) { diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 6ca50ce41193c..00dbb0b4226d5 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -98,6 +98,7 @@ + From 097714c01664e5eab6833fcf7cbe1589faa58f8a Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 11 Aug 2016 18:34:09 +0300 Subject: [PATCH 020/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface --- .../Order/Shipment/ShipmentValidator.php | 36 ++++++ .../Shipment/ShipmentValidatorInterface.php | 24 ++++ .../Model/Order/ShipmentQuantityValidator.php | 103 ++++++++++++++++++ 3 files changed, 163 insertions(+) create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidatorInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php diff --git a/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php new file mode 100644 index 0000000000000..75239253d4cf4 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php @@ -0,0 +1,36 @@ +validator = $validator; + } + + /** + * @inheritdoc + */ + public function validate(ShipmentInterface $entity, array $validators) + { + return $this->validator->validate($entity, $validators); + } +} diff --git a/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidatorInterface.php b/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidatorInterface.php new file mode 100644 index 0000000000000..198a4019bf6b8 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidatorInterface.php @@ -0,0 +1,24 @@ +orderRepository = $orderRepository; + } + + /** + * @param ShipmentInterface $entity + * @return string[] + * @throws DocumentValidationException + */ + public function validate($entity) + { + $messages = []; + + $order = $this->orderRepository->get($entity->getOrderId()); + foreach ($entity->getItems() as $shipmentItem) { + $messages = array_merge($messages, $this->validateShipmentItem($shipmentItem, $order)); + } + + return $messages; + } + + /** + * @param ShipmentItemInterface $item + * @param OrderInterface $order + * @return string[] + */ + public function validateShipmentItem(ShipmentItemInterface $item, OrderInterface $order) + { + $messages = []; + $orderItem = $this->getOrderItemById($order, $item->getOrderItemId()); + if ($orderItem === null) { + return [__('We can not found item "%1".', $item->getOrderItemId())]; + } + if ($orderItem->getIsQtyDecimal()) { + $qty = (double)$item->getQty(); + } else { + $qty = (int)$item->getQty(); + } + $qty = $qty > 0 ? $qty : 0; + + if (!$this->isQtyAvailable($orderItem, $qty)) { + $messages[] =__('We found an invalid quantity to ship for item "%1".', $item->getName()); + } + + return $messages; + } + + /** + * @param OrderInterface $order + * @param $id + * @return \Magento\Sales\Api\Data\OrderItemInterface|null + */ + private function getOrderItemById(OrderInterface $order, $id) + { + foreach ($order->getItems() as $item) { + if ($item->getItemId() === $id) { + return $item; + } + } + + return null; + } + + /** + * @param Item $orderItem + * @param int $qty + * @return bool + */ + private function isQtyAvailable(Item $orderItem, $qty) + { + return $qty <= $orderItem->getQtyToShip() || $orderItem->isDummy(true); + } +} From 0dd6d38f1156e5efb7092b3b88fa1325bf9877c7 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 11 Aug 2016 19:05:26 +0300 Subject: [PATCH 021/115] MAGETWO-49556: The REST API intermittently rejects shipments submitted via a POST with Invalid Quantity to Ship Error --- .../Sales/Model/Order/Shipment/Item.php | 47 ++++++++++++------- .../Sales/Model/Order/ShipmentFactory.php | 33 ++++++++++++- .../Unit/Model/Order/ShipmentFactoryTest.php | 17 +++++++ 3 files changed, 80 insertions(+), 17 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Item.php b/app/code/Magento/Sales/Model/Order/Shipment/Item.php index c7fdca853b17e..09b9d442ba761 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Item.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Item.php @@ -9,8 +9,10 @@ namespace Magento\Sales\Model\Order\Shipment; use Magento\Framework\Api\AttributeValueFactory; +use Magento\Framework\App\ObjectManager; use Magento\Sales\Api\Data\ShipmentItemInterface; use Magento\Sales\Model\AbstractModel; +use Magento\Sales\Model\Order\ShipmentQuantityValidator; /** * @method \Magento\Sales\Model\ResourceModel\Order\Shipment\Item _getResource() @@ -44,6 +46,11 @@ class Item extends AbstractModel implements ShipmentItemInterface */ protected $_orderItemFactory; + /** + * @var ShipmentValidatorInterface + */ + private $shipmentValidator; + /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry @@ -151,22 +158,7 @@ public function getOrderItem() */ public function setQty($qty) { - if ($this->getOrderItem()->getIsQtyDecimal()) { - $qty = (double)$qty; - } else { - $qty = (int)$qty; - } - $qty = $qty > 0 ? $qty : 0; - /** - * Check qty availability - */ - if ($qty <= $this->getOrderItem()->getQtyToShip() || $this->getOrderItem()->isDummy(true)) { - $this->setData('qty', $qty); - } else { - throw new \Magento\Framework\Exception\LocalizedException( - __('We found an invalid quantity to ship for item "%1".', $this->getName()) - ); - } + $this->setData('qty', $qty); return $this; } @@ -174,13 +166,36 @@ public function setQty($qty) * Applying qty to order item * * @return $this + * @throws \Magento\Framework\Exception\LocalizedException */ public function register() { + $errorMessages = $this->getShipmentValidator()->validate( + $this->getShipment(), + [ShipmentQuantityValidator::class] + ); + if (!empty($errors)) { + throw new \Magento\Framework\Exception\LocalizedException( + __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages)) + ); + } $this->getOrderItem()->setQtyShipped($this->getOrderItem()->getQtyShipped() + $this->getQty()); return $this; } + /** + * @return ShipmentValidatorInterface + * @deprecated + */ + private function getShipmentValidator() + { + if ($this->shipmentValidator === null) { + $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); + } + + return $this->shipmentValidator; + } + //@codeCoverageIgnoreStart /** * Returns additional_data diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index 2ac012760ee47..74dc5e1f7f663 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -5,6 +5,10 @@ */ namespace Magento\Sales\Model\Order; +use Magento\Framework\App\ObjectManager; +use Magento\Framework\Exception\LocalizedException; +use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; + /** * Factory class for @see \Magento\Sales\Api\Data\ShipmentInterface */ @@ -31,6 +35,11 @@ class ShipmentFactory */ protected $instanceName; + /** + * @var ShipmentValidatorInterface + */ + private $shipmentValidator; + /** * Factory constructor. * @@ -72,6 +81,7 @@ public function create(\Magento\Sales\Model\Order $order, array $items = [], $tr * @param \Magento\Sales\Model\Order $order * @param array $items * @return \Magento\Sales\Api\Data\ShipmentInterface + * @throws LocalizedException */ protected function prepareItems( \Magento\Sales\Api\Data\ShipmentInterface $shipment, @@ -129,7 +139,15 @@ protected function prepareItems( $item->setQty($qty); $shipment->addItem($item); } - + $errorMessages = $this->getShipmentValidator()->validate( + $shipment, + [ShipmentQuantityValidator::class] + ); + if (!empty($errors)) { + throw new \Magento\Framework\Exception\LocalizedException( + __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages)) + ); + } return $shipment->setTotalQty($totalQty); } @@ -211,4 +229,17 @@ protected function canShipItem($item, array $items = []) return $item->getQtyToShip() > 0; } } + + /** + * @return ShipmentValidatorInterface + * @deprecated + */ + private function getShipmentValidator() + { + if ($this->shipmentValidator === null) { + $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); + } + + return $this->shipmentValidator; + } } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php index 3760934457a85..df35aae943e74 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php @@ -6,6 +6,7 @@ namespace Magento\Sales\Test\Unit\Model\Order; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; +use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; /** * Unit test for shipment factory class. @@ -33,6 +34,11 @@ class ShipmentFactoryTest extends \PHPUnit_Framework_TestCase */ protected $trackFactory; + /** + * @var \PHPUnit_Framework_MockObject_MockObject + */ + private $shipmentValidatorMock; + /** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) @@ -67,6 +73,9 @@ protected function setUp() '', false ); + $this->shipmentValidatorMock = $this->getMockBuilder( + ShipmentValidatorInterface::class + )->getMockForAbstractClass(); $this->subject = $objectManager->getObject( \Magento\Sales\Model\Order\ShipmentFactory::class, @@ -75,6 +84,12 @@ protected function setUp() 'trackFactory' => $this->trackFactory ] ); + + $objectManager->setBackwardCompatibleProperty( + $this->subject, + 'shipmentValidator', + $this->shipmentValidatorMock + ); } /** @@ -181,6 +196,8 @@ public function testCreate($tracks) } } + $this->shipmentValidatorMock->expects($this->once())->method('validate')->willReturn([]); + $this->assertEquals($shipment, $this->subject->create($order, ['1' => 5], $tracks)); } From 13a6d7a8d2c1a89066fa6ddd56ca804a4e13c21b Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 10:54:06 +0300 Subject: [PATCH 022/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation - change test --- app/code/Magento/Sales/Model/Validator.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Sales/Model/Validator.php b/app/code/Magento/Sales/Model/Validator.php index 0b7dbc5eb83d3..7dd55e38f85ea 100644 --- a/app/code/Magento/Sales/Model/Validator.php +++ b/app/code/Magento/Sales/Model/Validator.php @@ -5,25 +5,25 @@ */ namespace Magento\Sales\Model; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\ConfigurationMismatchException; +use Magento\Framework\ObjectManagerInterface; /** - * Class ValidatorRunner + * Class Validator */ class Validator { /** - * @var ObjectManager + * @var ObjectManagerInterface */ private $objectManager; /** * ValidatorRunner constructor. * - * @param ObjectManager $objectManager + * @param ObjectManagerInterface $objectManager */ - public function __construct(ObjectManager $objectManager) + public function __construct(ObjectManagerInterface $objectManager) { $this->objectManager = $objectManager; } From 8f498d0a22883a66cab6a15150be98f11a7f46be Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 14:01:01 +0300 Subject: [PATCH 023/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation - change test --- .../Model/Order/Invoice/InvoiceValidator.php | 2 +- .../Model/Order/InvoiceQuantityValidator.php | 6 ++-- .../Sales/Model/Order/OrderValidator.php | 8 +++-- .../Model/Order/Validation/CanInvoice.php | 5 +-- app/code/Magento/Sales/Model/Validator.php | 4 ++- .../Model/Order/Validation/CanInvoiceTest.php | 34 +++---------------- 6 files changed, 20 insertions(+), 39 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php index 21428cd6d4ff2..4fed2a56cc302 100644 --- a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php +++ b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php @@ -33,4 +33,4 @@ public function validate(InvoiceInterface $entity, array $validators) { return $this->validator->validate($entity, $validators); } -} \ No newline at end of file +} diff --git a/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php index a7c1008968926..f0f468a39238e 100644 --- a/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php @@ -32,11 +32,13 @@ public function __construct(OrderRepositoryInterface $orderRepository) } /** - * @param InvoiceInterface $invoice - * @return array + * @inheritdoc */ public function validate($invoice) { + if ($invoice->getOrderId() === null) { + return [__('OrderId in invoice document is required')]; + } $order = $this->orderRepository->get($invoice->getOrderId()); return $this->checkQtyAvailability($invoice, $order); } diff --git a/app/code/Magento/Sales/Model/Order/OrderValidator.php b/app/code/Magento/Sales/Model/Order/OrderValidator.php index 9f1bb5f21e630..8208af96c93d3 100644 --- a/app/code/Magento/Sales/Model/Order/OrderValidator.php +++ b/app/code/Magento/Sales/Model/Order/OrderValidator.php @@ -9,7 +9,7 @@ use Magento\Sales\Exception\DocumentValidationException; /** - * Class OrderValidatorRunner + * Class OrderValidator */ class OrderValidator implements OrderValidatorInterface { @@ -18,6 +18,10 @@ class OrderValidator implements OrderValidatorInterface */ private $validator; + /** + * OrderValidator constructor. + * @param \Magento\Sales\Model\Validator $validator + */ public function __construct(\Magento\Sales\Model\Validator $validator) { $this->validator = $validator; @@ -30,4 +34,4 @@ public function validate(OrderInterface $entity, array $validators) { return $this->validator->validate($entity, $validators); } -} \ No newline at end of file +} diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php index 0dcb99b18b109..dffff83b97fd2 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php @@ -22,10 +22,7 @@ public function validate($entity) { $messages = []; if (!$this->canInvoice($entity)) { - $messages[] = __( - 'An invoice cannot be created when an order has a status of %1.', - $entity->getStatus() - ); + $messages[] = __('The order does not allow an invoice to be created.'); } return $messages; diff --git a/app/code/Magento/Sales/Model/Validator.php b/app/code/Magento/Sales/Model/Validator.php index 7dd55e38f85ea..b8d57ded29702 100644 --- a/app/code/Magento/Sales/Model/Validator.php +++ b/app/code/Magento/Sales/Model/Validator.php @@ -10,6 +10,8 @@ /** * Class Validator + * + * @internal */ class Validator { @@ -19,7 +21,7 @@ class Validator private $objectManager; /** - * ValidatorRunner constructor. + * Validator constructor. * * @param ObjectManagerInterface $objectManager */ diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php index 31d4e6cde9e12..dae95c4a758eb 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php @@ -122,31 +122,7 @@ public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedRes ->willReturn($itemLockedDoInvoice); $this->assertEquals( - $expectedResult, empty($this->model->validate($this->orderMock)) - ); - } - - public function testValidateCanNotInvoiceOrder() - { - $orderStatus = 'Test Status'; - $expectedResult = [__('An invoice cannot be created when an order has a status of %1.', $orderStatus)]; - $orderItemMock = $orderItemMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderItemInterface::class) - ->disableOriginalConstructor() - ->setMethods(['getId', 'getQtyToInvoice', 'isDummy', 'getSku']) - ->getMockForAbstractClass(); - $orderItemMock->expects($this->any())->method('getId')->willReturn(1); - $orderItemMock->expects($this->any())->method('getQtyToInvoice')->willReturn(0); - $orderItemMock->expects($this->any())->method('isDummy')->willReturn(true); - $orderItemMock->expects($this->any())->method('getSku')->willReturn(1); - $this->orderMock->expects($this->once()) - ->method('getItems') - ->willReturn([$orderItemMock]); - $this->orderMock->expects($this->once()) - ->method('getStatus') - ->willReturn($orderStatus); - $this->assertEquals( - $expectedResult, - $this->model->validate($this->orderMock) + $expectedResult, $this->model->validate($this->orderMock) ); } @@ -158,10 +134,10 @@ public function testValidateCanNotInvoiceOrder() public function canInvoiceDataProvider() { return [ - [0, null, false], - [-1, null, false], - [1, true, false], - [0.5, false, true], + [0, null, [__('The order does not allow an invoice to be created.')]], + [-1, null, [__('The order does not allow an invoice to be created.')]], + [1, true, [__('The order does not allow an invoice to be created.')]], + [0.5, false, []], ]; } } From cb578e98e28f2d0ce406627094f9d15d6b70b0c0 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 14:16:00 +0300 Subject: [PATCH 024/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation - change test --- app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php index f0f468a39238e..9ae81dacb0a17 100644 --- a/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/InvoiceQuantityValidator.php @@ -37,7 +37,7 @@ public function __construct(OrderRepositoryInterface $orderRepository) public function validate($invoice) { if ($invoice->getOrderId() === null) { - return [__('OrderId in invoice document is required')]; + return [__('Order Id is required for invoice document')]; } $order = $this->orderRepository->get($invoice->getOrderId()); return $this->checkQtyAvailability($invoice, $order); From 7c971560ba14c6e6bcb7698ae9c93a6e632b627d Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 14:23:11 +0300 Subject: [PATCH 025/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation - change test --- app/code/Magento/Sales/Model/ValidatorInterface.php | 1 + .../Unit/Model/Order/InvoiceQuantityValidatorTest.php | 9 +++++++++ 2 files changed, 10 insertions(+) diff --git a/app/code/Magento/Sales/Model/ValidatorInterface.php b/app/code/Magento/Sales/Model/ValidatorInterface.php index a42f938ea879d..26f37f9873bbe 100644 --- a/app/code/Magento/Sales/Model/ValidatorInterface.php +++ b/app/code/Magento/Sales/Model/ValidatorInterface.php @@ -16,6 +16,7 @@ interface ValidatorInterface * @param object $entity * @return string[] * @throws DocumentValidationException + * @throws \Magento\Framework\Exception\NoSuchEntityException */ public function validate($entity); } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php index 19af7c28ddcbf..309d4c4b5ce9b 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php @@ -116,6 +116,15 @@ public function testValidateNoOrderItems() ); } + public function testValidateNoOrder() + { + $expectedResult = [__('Order Id is required for invoice document')]; + $this->assertEquals( + $expectedResult, + $this->model->validate($this->invoiceMock) + ); + } + public function testValidateNoInvoiceItems() { $expectedResult = [__('You can\'t create an invoice without products.')]; From 0adc3c282bc2e44355e35f4848222770aaadeeea Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 14:23:52 +0300 Subject: [PATCH 026/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation - change test --- app/code/Magento/Sales/Model/ValidatorInterface.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/ValidatorInterface.php b/app/code/Magento/Sales/Model/ValidatorInterface.php index 26f37f9873bbe..6c50af2c685d6 100644 --- a/app/code/Magento/Sales/Model/ValidatorInterface.php +++ b/app/code/Magento/Sales/Model/ValidatorInterface.php @@ -5,6 +5,7 @@ */ namespace Magento\Sales\Model; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Exception\DocumentValidationException; /** @@ -16,7 +17,7 @@ interface ValidatorInterface * @param object $entity * @return string[] * @throws DocumentValidationException - * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws NoSuchEntityException */ public function validate($entity); } From 2aac2b2d7fea23e4c7d2d4baf6270779ff26857f Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 14:32:59 +0300 Subject: [PATCH 027/115] MAGETWO-56711: Refactoring Order Invoice Business logic validation - change test --- .../Model/Order/InvoiceQuantityValidatorTest.php | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php index 309d4c4b5ce9b..8c6a3df2efda0 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php @@ -73,6 +73,9 @@ public function testValidate() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([$orderItemMock]); + $this->invoiceMock->expects($this->exactly(2)) + ->method('getOrderId') + ->willReturn(1); $this->assertEquals( $expectedResult, $this->model->validate($this->invoiceMock) @@ -93,6 +96,9 @@ public function testValidateInvoiceQtyBiggerThanOrder() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([$orderItemMock]); + $this->invoiceMock->expects($this->exactly(2)) + ->method('getOrderId') + ->willReturn(1); $this->assertEquals( $expectedResult, $this->model->validate($this->invoiceMock) @@ -110,6 +116,9 @@ public function testValidateNoOrderItems() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([]); + $this->invoiceMock->expects($this->exactly(2)) + ->method('getOrderId') + ->willReturn(1); $this->assertEquals( $expectedResult, $this->model->validate($this->invoiceMock) @@ -138,6 +147,9 @@ public function testValidateNoInvoiceItems() $this->orderMock->expects($this->once()) ->method('getItems') ->willReturn([$orderItemMock]); + $this->invoiceMock->expects($this->exactly(2)) + ->method('getOrderId') + ->willReturn(1); $this->assertEquals( $expectedResult, $this->model->validate($this->invoiceMock) From 7e9c8c9864b2b6638f109ad3141a91de1c27e2eb Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 14:56:29 +0300 Subject: [PATCH 028/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface --- .../Model/Order/ShipmentQuantityValidator.php | 5 ++ .../Sales/Model/Order/Validation/CanShip.php | 58 +++++++++++++++++++ 2 files changed, 63 insertions(+) create mode 100644 app/code/Magento/Sales/Model/Order/Validation/CanShip.php diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index 7da9dfdc0cf9d..b84813d1e564c 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -5,6 +5,7 @@ */ namespace Magento\Sales\Model\Order; +use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\Data\ShipmentItemInterface; @@ -36,9 +37,13 @@ public function __construct( * @param ShipmentInterface $entity * @return string[] * @throws DocumentValidationException + * @throws NoSuchEntityException */ public function validate($entity) { + if ($entity->getOrderId() === null) { + return [__('Order Id is required for shipment document')]; + } $messages = []; $order = $this->orderRepository->get($entity->getOrderId()); diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php new file mode 100644 index 0000000000000..adb71e3aa1b63 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php @@ -0,0 +1,58 @@ +canShip($entity)) { + $messages[] = __('The order does not allow an shipment to be created.'); + } + + return $messages; + } + + /** + * @param OrderInterface $order + * @return bool + */ + private function canShip(OrderInterface $order) + { + if ($order->getState() === Order::STATE_PAYMENT_REVIEW || + $order->getState() === Order::STATE_HOLDED || + $order->getIsVirtual() || + $order->getState() === Order::STATE_CANCELED + ) { + return false; + } + + /** @var \Magento\Sales\Model\Order\Item $item */ + foreach ($order->getItems() as $item) { + if ($item->getQtyToShip() > 0 && !$item->getIsVirtual() && !$item->getLockedDoShip()) { + return true; + } + } + + return false; + } +} From d01340e13015af257c27feae7adcb84c1cfe2986 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 15:01:53 +0300 Subject: [PATCH 029/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface --- .../Model/Order/Validation/CanShipTest.php | 141 ++++++++++++++++++ 1 file changed, 141 insertions(+) create mode 100644 app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php new file mode 100644 index 0000000000000..1e07a7194fcdf --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php @@ -0,0 +1,141 @@ +objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class) + ->disableOriginalConstructor() + ->setMethods(['getStatus', 'getItems']) + ->getMockForAbstractClass(); + + $this->orderItemMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderItemInterface::class) + ->disableOriginalConstructor() + ->setMethods(['getQtyToShip', 'getLockedDoShip']) + ->getMockForAbstractClass(); + + $this->model = new \Magento\Sales\Model\Order\Validation\CanShip(); + } + + /** + * @param string $state + * + * @dataProvider canShipWrongStateDataProvider + */ + public function testCanShipWrongState($state) + { + $this->orderMock->expects($this->any()) + ->method('getState') + ->willReturn($state); + $this->orderMock->expects($this->never()) + ->method('getItems'); + $this->assertNotEmpty( + $this->model->validate($this->orderMock) + ); + } + + /** + * Data provider for testCanShipWrongState + * @return array + */ + public function canShipWrongStateDataProvider() + { + return [ + [Order::STATE_PAYMENT_REVIEW], + [Order::STATE_HOLDED], + [Order::STATE_CANCELED], + ]; + } + + public function testCanShipNoItems() + { + $this->orderMock->expects($this->any()) + ->method('getState') + ->willReturn(Order::STATE_PROCESSING); + + $this->orderMock->expects($this->once()) + ->method('getItems') + ->willReturn([]); + + $this->assertNotEmpty( + $this->model->validate($this->orderMock) + ); + } + + /** + * @param float $qtyToShipment + * @param bool|null $itemLockedDoShipment + * @param bool $expectedResult + * + * @dataProvider canShipDataProvider + */ + public function testCanShip($qtyToShipment, $itemLockedDoShipment, $expectedResult) + { + $this->orderMock->expects($this->any()) + ->method('getState') + ->willReturn(Order::STATE_PROCESSING); + + $items = [$this->orderItemMock]; + $this->orderMock->expects($this->once()) + ->method('getItems') + ->willReturn($items); + $this->orderItemMock->expects($this->any()) + ->method('getQtyToShip') + ->willReturn($qtyToShipment); + $this->orderItemMock->expects($this->any()) + ->method('getLockedDoShip') + ->willReturn($itemLockedDoShipment); + + $this->assertEquals( + $expectedResult, $this->model->validate($this->orderMock) + ); + } + + /** + * Data provider for testCanShip + * + * @return array + */ + public function canShipDataProvider() + { + return [ + [0, null, [__('The order does not allow an shipment to be created.')]], + [-1, null, [__('The order does not allow an shipment to be created.')]], + [1, true, [__('The order does not allow an shipment to be created.')]], + [0.5, false, []], + ]; + } +} From b99bac10e9d6e833b6b7a40730d9934024717cd6 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 15:03:45 +0300 Subject: [PATCH 030/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface - grammar fix --- app/code/Magento/Sales/Model/Order/Validation/CanShip.php | 2 +- .../Sales/Test/Unit/Model/Order/Validation/CanShipTest.php | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php index adb71e3aa1b63..da4c05ab737c3 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php @@ -26,7 +26,7 @@ public function validate($entity) { $messages = []; if (!$this->canShip($entity)) { - $messages[] = __('The order does not allow an shipment to be created.'); + $messages[] = __('The order does not allow a shipment to be created.'); } return $messages; diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php index 1e07a7194fcdf..c31fd28b7b48c 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php @@ -132,9 +132,9 @@ public function testCanShip($qtyToShipment, $itemLockedDoShipment, $expectedResu public function canShipDataProvider() { return [ - [0, null, [__('The order does not allow an shipment to be created.')]], - [-1, null, [__('The order does not allow an shipment to be created.')]], - [1, true, [__('The order does not allow an shipment to be created.')]], + [0, null, [__('The order does not allow a shipment to be created.')]], + [-1, null, [__('The order does not allow a shipment to be created.')]], + [1, true, [__('The order does not allow a shipment to be created.')]], [0.5, false, []], ]; } From de793dbc61c129dc2ca81a63d8ed713165e3b5d7 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 15:08:00 +0300 Subject: [PATCH 031/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface - grammar fix --- app/code/Magento/Sales/Model/Order/Validation/CanShip.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php index da4c05ab737c3..30575cc734f94 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php @@ -20,7 +20,6 @@ class CanShip implements ValidatorInterface * @param OrderInterface $entity * @return string[] * @throws DocumentValidationException - * @throws NoSuchEntityException */ public function validate($entity) { From 891bc759bff684b70e7e173974ca8643f89a8c18 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 15:18:45 +0300 Subject: [PATCH 032/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface - grammar fix --- app/code/Magento/Sales/Model/Order/Validation/CanShip.php | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php index 30575cc734f94..cebdc4b0afe28 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php @@ -19,7 +19,6 @@ class CanShip implements ValidatorInterface /** * @param OrderInterface $entity * @return string[] - * @throws DocumentValidationException */ public function validate($entity) { From 11d0f10288c868c70d15b1af225eaac07c97c828 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Fri, 12 Aug 2016 15:20:21 +0300 Subject: [PATCH 033/115] MAGETWO-56488: Introduce and implement ShipmentTrackCreationInterface --- .../Data/ShipmentTrackCreationInterface.php | 105 ------------------ .../Sales/Api/Data/ShipmentTrackInterface.php | 15 +++ .../Magento/Sales/Api/Data/TrackInterface.php | 15 --- .../Model/Order/Shipment/TrackCreation.php | 12 +- 4 files changed, 22 insertions(+), 125 deletions(-) diff --git a/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php index dcca11da20d6f..f3c3347534aca 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php @@ -13,111 +13,6 @@ */ interface ShipmentTrackCreationInterface extends TrackInterface { - /** - * Sets the weight for the shipment package. - * - * @param float $weight - * @return $this - */ - public function setWeight($weight); - - /** - * Gets the weight for the shipment package. - * - * @return float Weight. - */ - public function getWeight(); - - /** - * Sets the quantity for the shipment package. - * - * @param float $qty - * @return $this - */ - public function setQty($qty); - - /** - * Gets the quantity for the shipment package. - * - * @return float Quantity. - */ - public function getQty(); - - /** - * Sets the order_id for the shipment package. - * - * @param int $id - * @return $this - */ - public function setOrderId($id); - - /** - * Gets the order_id for the shipment package. - * - * @return int - */ - public function getOrderId(); - - /** - * Sets the track number for the shipment package. - * - * @param string $trackNumber - * @return $this - */ - public function setTrackNumber($trackNumber); - - /** - * Gets the track number for the shipment package. - * - * @return string Track number. - */ - public function getTrackNumber(); - - /** - * Sets the description for the shipment package. - * - * @param string $description - * @return $this - */ - public function setDescription($description); - - /** - * Gets the description for the shipment package. - * - * @return string Description. - */ - public function getDescription(); - - /** - * Sets the title for the shipment package. - * - * @param string $title - * @return $this - */ - public function setTitle($title); - - /** - * Gets the title for the shipment package. - * - * @return string Title. - */ - public function getTitle(); - - /** - * Sets the carrier code for the shipment package. - * - * @param string $code - * @return $this - */ - public function setCarrierCode($code); - - /** - * Gets the carrier code for the shipment package. - * - * @return string Carrier code. - */ - public function getCarrierCode(); - /** * Retrieve existing extension attributes object or create a new one. * diff --git a/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php index e641d285bc58e..c6c610433cb80 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php @@ -63,6 +63,21 @@ interface ShipmentTrackInterface extends TrackInterface */ const UPDATED_AT = 'updated_at'; + /** + * Sets the order_id for the shipment package. + * + * @param int $id + * @return $this + */ + public function setOrderId($id); + + /** + * Gets the order_id for the shipment package. + * + * @return int + */ + public function getOrderId(); + /** * Gets the created-at timestamp for the shipment package. * diff --git a/app/code/Magento/Sales/Api/Data/TrackInterface.php b/app/code/Magento/Sales/Api/Data/TrackInterface.php index 5b48aee85b3a3..d082f6ca5e066 100644 --- a/app/code/Magento/Sales/Api/Data/TrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/TrackInterface.php @@ -43,21 +43,6 @@ public function setQty($qty); */ public function getQty(); - /** - * Sets the order_id for the shipment package. - * - * @param int $id - * @return $this - */ - public function setOrderId($id); - - /** - * Gets the order_id for the shipment package. - * - * @return int - */ - public function getOrderId(); - /** * Sets the track number for the shipment package. * diff --git a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php index 4774ada6a5a85..4c9f6d99afd31 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php @@ -67,6 +67,7 @@ public function getWeight() public function setWeight($weight) { $this->weight = $weight; + return $this; } /** @@ -83,6 +84,7 @@ public function getQty() public function setQty($qty) { $this->qty = $qty; + return $this; } /** @@ -99,6 +101,7 @@ public function getOrderId() public function setOrderId($orderId) { $this->orderId = $orderId; + return $this; } /** @@ -115,6 +118,7 @@ public function getTrackNumber() public function setTrackNumber($trackNumber) { $this->trackNumber = $trackNumber; + return $this; } /** @@ -131,6 +135,7 @@ public function getDescription() public function setDescription($description) { $this->description = $description; + return $this; } /** @@ -147,6 +152,7 @@ public function getTitle() public function setTitle($title) { $this->title = $title; + return $this; } /** @@ -163,12 +169,11 @@ public function getCarrierCode() public function setCarrierCode($carrierCode) { $this->carrierCode = $carrierCode; + return $this; } /** * {@inheritdoc} - * - * @return \Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface|null */ public function getExtensionAttributes() { @@ -177,9 +182,6 @@ public function getExtensionAttributes() /** * {@inheritdoc} - * - * @param \Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface $extensionAttributes - * @return $this */ public function setExtensionAttributes(\Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface $extensionAttributes) { From 02e33e1998ccfde10afb52bf9fab0bf84bad7b4c Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 12 Aug 2016 15:33:18 +0300 Subject: [PATCH 034/115] MAGETWO-49556: The REST API intermittently rejects shipments submitted via a POST with Invalid Quantity to Ship Error - fix typo --- app/code/Magento/Sales/Model/Order/Shipment/Item.php | 2 +- app/code/Magento/Sales/Model/Order/ShipmentFactory.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Item.php b/app/code/Magento/Sales/Model/Order/Shipment/Item.php index 09b9d442ba761..ec1b2940fbf59 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Item.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Item.php @@ -174,7 +174,7 @@ public function register() $this->getShipment(), [ShipmentQuantityValidator::class] ); - if (!empty($errors)) { + if (!empty($errorMessages)) { throw new \Magento\Framework\Exception\LocalizedException( __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages)) ); diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index 74dc5e1f7f663..518f3909cd73b 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -143,7 +143,7 @@ protected function prepareItems( $shipment, [ShipmentQuantityValidator::class] ); - if (!empty($errors)) { + if (!empty($errorMessages)) { throw new \Magento\Framework\Exception\LocalizedException( __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages)) ); From dd2084187d63ef0783735f28ab6cb8e3cb0cb179 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Fri, 12 Aug 2016 16:21:13 +0300 Subject: [PATCH 035/115] MAGETWO-56488: Introduce and implement ShipmentTrackCreationInterface --- .../Model/Order/Shipment/TrackCreation.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php index 4c9f6d99afd31..80129fae628a4 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php @@ -87,23 +87,6 @@ public function setQty($qty) return $this; } - /** - * {@inheritdoc} - */ - public function getOrderId() - { - return $this->orderId; - } - - /** - * {@inheritdoc} - */ - public function setOrderId($orderId) - { - $this->orderId = $orderId; - return $this; - } - /** * {@inheritdoc} */ From 0189202bd0ed2857d5031777f4989abb3ff5a5eb Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Mon, 15 Aug 2016 11:18:07 +0300 Subject: [PATCH 036/115] MAGETWO-56488: Introduce and implement ShipmentTrackCreationInterface --- .../Magento/Sales/Model/Order/Shipment/TrackCreation.php | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php index 80129fae628a4..0618ae0f6ab26 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php @@ -23,11 +23,6 @@ class TrackCreation implements ShipmentTrackCreationInterface */ private $qty; - /** - * @var int - */ - private $orderId; - /** * @var string */ From 5742369bf515f2a76361d8305e5e0c0a6b864b42 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Mon, 15 Aug 2016 18:12:22 +0300 Subject: [PATCH 037/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface - test fix --- app/code/Magento/Sales/Model/OrderInvoice.php | 16 +++++----- .../Test/Unit/Model/OrderInvoiceTest.php | 31 ++++++++++++++++--- app/code/Magento/Sales/etc/di.xml | 2 -- 3 files changed, 35 insertions(+), 14 deletions(-) diff --git a/app/code/Magento/Sales/Model/OrderInvoice.php b/app/code/Magento/Sales/Model/OrderInvoice.php index e8cb28fca11b7..12d1e74d15004 100644 --- a/app/code/Magento/Sales/Model/OrderInvoice.php +++ b/app/code/Magento/Sales/Model/OrderInvoice.php @@ -12,15 +12,15 @@ use Magento\Sales\Api\OrderInvoiceInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\Order\Config as OrderConfig; -use Magento\Sales\Model\Order\Invoice\InvoiceValidator; +use Magento\Sales\Model\Order\Invoice\InvoiceValidatorInterface; use Magento\Sales\Model\Order\Invoice\NotifierInterface; use Magento\Sales\Model\Order\InvoiceDocumentFactory; use Magento\Sales\Model\Order\InvoiceQuantityValidator; use Magento\Sales\Model\Order\InvoiceRepository; use Magento\Sales\Model\Order\OrderStateResolverInterface; +use Magento\Sales\Model\Order\OrderValidatorInterface; use Magento\Sales\Model\Order\PaymentAdapterInterface; use Magento\Sales\Model\Order\Validation\CanInvoice; -use Magento\Sales\Model\Order\OrderValidator; use Psr\Log\LoggerInterface; /** @@ -45,7 +45,7 @@ class OrderInvoice implements OrderInvoiceInterface private $invoiceDocumentFactory; /** - * @var InvoiceValidator + * @var InvoiceValidatorInterface */ private $invoiceValidator; @@ -80,7 +80,7 @@ class OrderInvoice implements OrderInvoiceInterface private $logger; /** - * @var OrderValidator + * @var OrderValidatorInterface */ private $orderValidator; @@ -89,8 +89,8 @@ class OrderInvoice implements OrderInvoiceInterface * @param ResourceConnection $resourceConnection * @param OrderRepositoryInterface $orderRepository * @param InvoiceDocumentFactory $invoiceDocumentFactory - * @param InvoiceValidator $invoiceValidator - * @param OrderValidator $orderValidator + * @param InvoiceValidatorInterface $invoiceValidator + * @param OrderValidatorInterface $orderValidator * @param PaymentAdapterInterface $paymentAdapter * @param OrderStateResolverInterface $orderStateResolver * @param OrderConfig $config @@ -103,8 +103,8 @@ public function __construct( ResourceConnection $resourceConnection, OrderRepositoryInterface $orderRepository, InvoiceDocumentFactory $invoiceDocumentFactory, - InvoiceValidator $invoiceValidator, - OrderValidator $orderValidator, + InvoiceValidatorInterface $invoiceValidator, + OrderValidatorInterface $orderValidator, PaymentAdapterInterface $paymentAdapter, OrderStateResolverInterface $orderStateResolver, OrderConfig $config, diff --git a/app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php index bc36da112aa29..e035650593203 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/OrderInvoiceTest.php @@ -14,11 +14,12 @@ use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Model\Order; use Magento\Sales\Model\Order\Config as OrderConfig; +use Magento\Sales\Model\Order\Invoice\InvoiceValidatorInterface; use Magento\Sales\Model\Order\Invoice\NotifierInterface; use Magento\Sales\Model\Order\InvoiceDocumentFactory; use Magento\Sales\Model\Order\InvoiceRepository; -use Magento\Sales\Model\Order\InvoiceValidatorInterface; use Magento\Sales\Model\Order\OrderStateResolverInterface; +use Magento\Sales\Model\Order\OrderValidatorInterface; use Magento\Sales\Model\Order\PaymentAdapterInterface; use Magento\Sales\Model\OrderInvoice; use Psr\Log\LoggerInterface; @@ -50,6 +51,11 @@ class OrderInvoiceTest extends \PHPUnit_Framework_TestCase */ private $invoiceValidatorMock; + /** + * @var OrderValidatorInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $orderValidatorMock; + /** * @var PaymentAdapterInterface|\PHPUnit_Framework_MockObject_MockObject */ @@ -128,6 +134,10 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); + $this->orderValidatorMock = $this->getMockBuilder(OrderValidatorInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->paymentAdapterMock = $this->getMockBuilder(PaymentAdapterInterface::class) ->disableOriginalConstructor() ->getMock(); @@ -177,6 +187,7 @@ protected function setUp() $this->orderRepositoryMock, $this->invoiceDocumentFactoryMock, $this->invoiceValidatorMock, + $this->orderValidatorMock, $this->paymentAdapterMock, $this->orderStateResolverMock, $this->configMock, @@ -212,7 +223,11 @@ public function testOrderInvoice($orderId, $capture, $items, $notify, $appendCom $this->invoiceValidatorMock->expects($this->once()) ->method('validate') - ->with($this->invoiceMock, $this->orderMock) + ->with($this->invoiceMock) + ->willReturn([]); + $this->orderValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->orderMock) ->willReturn([]); $this->paymentAdapterMock->expects($this->once()) @@ -311,8 +326,12 @@ public function testDocumentValidationException() $this->invoiceValidatorMock->expects($this->once()) ->method('validate') - ->with($this->invoiceMock, $this->orderMock) + ->with($this->invoiceMock) ->willReturn($errorMessages); + $this->orderValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->orderMock) + ->willReturn([]); $this->orderInvoice->execute( $orderId, @@ -356,7 +375,11 @@ public function testCouldNotInvoiceException() $this->invoiceValidatorMock->expects($this->once()) ->method('validate') - ->with($this->invoiceMock, $this->orderMock) + ->with($this->invoiceMock) + ->willReturn([]); + $this->orderValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->orderMock) ->willReturn([]); $e = new \Exception(); diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 00dbb0b4226d5..5ee1fb73e1b26 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -69,8 +69,6 @@ - - From 7051bfb1d3710c7f7122a1f8b35ddf86c626e656 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Tue, 16 Aug 2016 10:59:30 +0300 Subject: [PATCH 038/115] MAGETWO-56492: Implement ShipmentDocumentFactory --- .../Magento/Sales/Model/Order/Shipment.php | 2 +- .../Model/Order/ShipmentDocumentFactory.php | 83 +++++++++++++++++++ .../Order/ShipmentDocumentFactoryTest.php | 79 ++++++++++++++++++ 3 files changed, 163 insertions(+), 1 deletion(-) create mode 100644 app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php create mode 100644 app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php diff --git a/app/code/Magento/Sales/Model/Order/Shipment.php b/app/code/Magento/Sales/Model/Order/Shipment.php index 6647bae750fff..2277f92d6e0e8 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment.php +++ b/app/code/Magento/Sales/Model/Order/Shipment.php @@ -405,7 +405,7 @@ public function addTrack(\Magento\Sales\Model\Order\Shipment\Track $track) * Adds comment to shipment with additional possibility to send it to customer via email * and show it in customer account * - * @param \Magento\Sales\Model\Order\Shipment\Comment $comment + * @param \Magento\Sales\Model\Order\Shipment\Comment|string $comment * @param bool $notify * @param bool $visibleOnFront * @return $this diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php new file mode 100644 index 0000000000000..6509aacd932c3 --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -0,0 +1,83 @@ +shipmentFactory = $shipmentFactory; + } + + /** + * @param OrderInterface $order + * @param array $items + * @param array $tracks + * @param ShipmentCommentCreationInterface|null $comment + * @param bool $appendComment + * @param array $packages + * @return Shipment + */ + public function create( + OrderInterface $order, + $items = [], + $tracks = [], + ShipmentCommentCreationInterface $comment = null, + $appendComment = false, + $packages = [] + ) { + + $shipmentItems = $this->itemsToArray($items); + /** @var Shipment $shipment */ + $shipment = $this->shipmentFactory->create( + $order, + $shipmentItems, + $tracks + ); + + if ($comment) { + $shipment->addComment( + $comment->getComment(), + $appendComment, + $comment->getIsVisibleOnFront() + ); + } + + return $shipment; + } + + /** + * Convert Items To Array + * + * @param InvoiceItemCreationInterface[] $items + * @return array + */ + private function itemsToArray($items = []) + { + $invoiceItems = []; + foreach ($items as $item) { + $invoiceItems[$item->getOrderItemId()] = $item->getQty(); + } + return $invoiceItems; + } +} diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php new file mode 100644 index 0000000000000..a3ee72ab318dc --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php @@ -0,0 +1,79 @@ +shipmentFactoryMock = $this->getMockBuilder(ShipmentFactory::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->orderMock = $this->getMockBuilder(Order::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->itemMock = $this->getMockBuilder(ShipmentItemCreationInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->commentMock = $this->getMockBuilder(ShipmentCommentCreationInterface::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->invoiceDocumentFactory = new ShipmentDocumentFactory($this->shipmentFactoryMock); + } + + public function testCreate() + { + $tracks = ["1234567890"]; + $appendComment = true; + $packages = []; + $this->invoiceDocumentFactory->create( + $this->orderMock, + $this->itemMock, + $this->commentMock, + $appendComment, + $packages + ); + } +} From fc52177d869f4c4574b608590dbae2b727035638 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 11:00:18 +0300 Subject: [PATCH 039/115] MAGETWO-56494: Introduce and implement ShipmentValidatorInterface - test fix --- app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php index 4fed2a56cc302..cbb68edaa8a55 100644 --- a/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php +++ b/app/code/Magento/Sales/Model/Order/Invoice/InvoiceValidator.php @@ -10,7 +10,7 @@ /** * Class InvoiceValidatorRunner */ -class InvoiceValidator +class InvoiceValidator implements InvoiceValidatorInterface { /** * @var \Magento\Sales\Model\Validator From 74aaf3e479b0ca61345e13da9941bb5d9f35e1c6 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Tue, 16 Aug 2016 11:59:31 +0300 Subject: [PATCH 040/115] MAGETWO-56492: Implement ShipmentDocumentFactory --- .../Order/ShipmentDocumentFactoryTest.php | 64 +++++++++++++++++-- 1 file changed, 58 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php index a3ee72ab318dc..30ae71012095c 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php @@ -11,6 +11,7 @@ use Magento\Sales\Model\Order\ShipmentFactory; use Magento\Sales\Model\Order\ShipmentDocumentFactory; use Magento\Sales\Model\Order; +use Magento\Sales\Api\Data\ShipmentInterface; /** * Class InvoiceDocumentFactoryTest @@ -37,6 +38,11 @@ class ShipmentDocumentFactoryTest extends \PHPUnit_Framework_TestCase */ private $commentMock; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|ShipmentInterface + */ + private $shipmentMock; + /** * @var ShipmentDocumentFactory */ @@ -60,6 +66,12 @@ protected function setUp() ->disableOriginalConstructor() ->getMock(); + $this->shipmentMock = $this->getMockBuilder(ShipmentInterface::class) + ->disableOriginalConstructor() + ->setMethods(['addComment']) + ->getMockForAbstractClass(); + + $this->invoiceDocumentFactory = new ShipmentDocumentFactory($this->shipmentFactoryMock); } @@ -68,12 +80,52 @@ public function testCreate() $tracks = ["1234567890"]; $appendComment = true; $packages = []; - $this->invoiceDocumentFactory->create( - $this->orderMock, - $this->itemMock, - $this->commentMock, - $appendComment, - $packages + $items = [1 => 10]; + + $this->itemMock->expects($this->once()) + ->method('getOrderItemId') + ->willReturn(1); + + $this->itemMock->expects($this->once()) + ->method('getQty') + ->willReturn(10); + + $this->shipmentFactoryMock->expects($this->once()) + ->method('create') + ->with( + $this->orderMock, + $items, + $tracks + ) + ->willReturn($this->shipmentMock); + + if ($appendComment) { + $comment = "New comment!"; + $visibleOnFront = true; + $this->commentMock->expects($this->once()) + ->method('getComment') + ->willReturn($comment); + + $this->commentMock->expects($this->once()) + ->method('getIsVisibleOnFront') + ->willReturn($visibleOnFront); + + $this->shipmentMock->expects($this->once()) + ->method('addComment') + ->with($comment, $appendComment, $visibleOnFront) + ->willReturnSelf(); + } + + $this->assertEquals( + $this->invoiceDocumentFactory->create( + $this->orderMock, + [$this->itemMock], + $tracks, + $this->commentMock, + $appendComment, + $packages + ), + $this->shipmentMock ); } } From 9616b80af2bf5a997601ad1c9392a65ac9ad158a Mon Sep 17 00:00:00 2001 From: Nadiya Syvokonenko Date: Tue, 16 Aug 2016 13:38:23 +0300 Subject: [PATCH 041/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Magento/Sales/Api/ShipOrderInterface.php | 38 ++++ app/code/Magento/Sales/Model/ShipOrder.php | 191 ++++++++++++++++++ app/code/Magento/Sales/etc/di.xml | 1 + app/code/Magento/Sales/etc/webapi.xml | 6 + 4 files changed, 236 insertions(+) create mode 100644 app/code/Magento/Sales/Api/ShipOrderInterface.php create mode 100644 app/code/Magento/Sales/Model/ShipOrder.php diff --git a/app/code/Magento/Sales/Api/ShipOrderInterface.php b/app/code/Magento/Sales/Api/ShipOrderInterface.php new file mode 100644 index 0000000000000..58f17ff7cc8c4 --- /dev/null +++ b/app/code/Magento/Sales/Api/ShipOrderInterface.php @@ -0,0 +1,38 @@ +resourceConnection = $resourceConnection; + $this->orderRepository = $orderRepository; + $this->shipmentDocumentFactory = $shipmentDocumentFactory; + $this->shipmentValidator = $shipmentValidator; + $this->orderValidator = $orderValidator; + $this->orderStateResolver = $orderStateResolver; + $this->config = $config; + $this->shipmentRepository = $shipmentRepository; + $this->notifierInterface = $notifierInterface; + $this->logger = $logger; + } + + /** + * @param int $orderId + * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items + * @param bool $notify + * @param bool $appendComment + * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment + * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks + * @param \Magento\Sales\Api\Data\ShipmentPackageInterface[] $packages + * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface|null $arguments + * @return int + * @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface + * @throws \Magento\Sales\Api\Exception\CouldNotShipExceptionInterface + * @throws \Magento\Framework\Exception\InputException + * @throws \Magento\Framework\Exception\NoSuchEntityException + * @throws \DomainException + */ + public function execute( + $orderId, + array $items = [], + $notify = false, + $appendComment = false, + \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment = null, + array $tracks = [], + array $packages = [], + \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface $arguments = null + ) { + $connection = $this->resourceConnection->getConnection('sales'); + $order = $this->orderRepository->get($orderId); + $shipment = $this->shipmentDocumentFactory->create( + $order, + $items, + $tracks, + $comment, + ($appendComment && $notify), + $packages, + $arguments + ); + $errorMessages = array_merge( + $this->shipmentValidator->validate( + $shipment, + [ShipmentQuantityValidator::class] + ), + $this->orderValidator->validate( + $order, + [CanShip::class] + ) + ); + if (!empty($errorMessages)) { + throw new \Magento\Sales\Exception\DocumentValidationException( + __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) + ); + } + $connection->beginTransaction(); + try { + $order->setState( + $this->orderStateResolver->getStateForOrder($order, [OrderStateResolverInterface::IN_PROGRESS]) + ); + $order->setStatus($this->config->getStateDefaultStatus($order->getState())); + $this->shipmentRepository->save($shipment); + $this->orderRepository->save($order); + $connection->commit(); + } catch (\Exception $e) { + $this->logger->critical($e); + $connection->rollBack(); + throw new \Magento\Sales\Exception\CouldNotShipException( + __('Could not save a shipment, see error log for details') + ); + } + if ($notify) { + if (!$appendComment) { + $comment = null; + } + $this->notifierInterface->notify($order, $shipment, $comment); + } + return $shipment->getEntityId(); + } +} diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 5ee1fb73e1b26..21a7ec666c41a 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -67,6 +67,7 @@ + diff --git a/app/code/Magento/Sales/etc/webapi.xml b/app/code/Magento/Sales/etc/webapi.xml index 8d1b1fda5bc31..1dd26e55d342b 100644 --- a/app/code/Magento/Sales/etc/webapi.xml +++ b/app/code/Magento/Sales/etc/webapi.xml @@ -235,6 +235,12 @@ + + + + + + From 62a0272bfe4e0655f2ed184c82ee5118c975ff43 Mon Sep 17 00:00:00 2001 From: Nadiya Syvokonenko Date: Tue, 16 Aug 2016 13:41:18 +0300 Subject: [PATCH 042/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Exception/CouldNotShipExceptionInterface.php | 13 +++++++++++++ 1 file changed, 13 insertions(+) create mode 100644 app/code/Magento/Sales/Api/Exception/CouldNotShipExceptionInterface.php diff --git a/app/code/Magento/Sales/Api/Exception/CouldNotShipExceptionInterface.php b/app/code/Magento/Sales/Api/Exception/CouldNotShipExceptionInterface.php new file mode 100644 index 0000000000000..b52e627791249 --- /dev/null +++ b/app/code/Magento/Sales/Api/Exception/CouldNotShipExceptionInterface.php @@ -0,0 +1,13 @@ + Date: Tue, 16 Aug 2016 13:51:36 +0300 Subject: [PATCH 043/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface - implement CouldNotShipException exception --- .../Sales/Exception/CouldNotShipException.php | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) create mode 100644 app/code/Magento/Sales/Exception/CouldNotShipException.php diff --git a/app/code/Magento/Sales/Exception/CouldNotShipException.php b/app/code/Magento/Sales/Exception/CouldNotShipException.php new file mode 100644 index 0000000000000..a847b7a6a8b37 --- /dev/null +++ b/app/code/Magento/Sales/Exception/CouldNotShipException.php @@ -0,0 +1,16 @@ + Date: Tue, 16 Aug 2016 14:49:18 +0300 Subject: [PATCH 044/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- app/code/Magento/Sales/Exception/CouldNotShipException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Exception/CouldNotShipException.php b/app/code/Magento/Sales/Exception/CouldNotShipException.php index a847b7a6a8b37..4881bbd79a3a3 100644 --- a/app/code/Magento/Sales/Exception/CouldNotShipException.php +++ b/app/code/Magento/Sales/Exception/CouldNotShipException.php @@ -11,6 +11,6 @@ /** * Class CouldNotShipException */ -class CouldNotInvoiceException extends LocalizedException implements CouldNotShipExceptionInterface +class CouldNotShipException extends LocalizedException implements CouldNotShipExceptionInterface { } From 2ee0861470a28221229f0756dd347a172ddc0741 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 14:57:24 +0300 Subject: [PATCH 045/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php | 1 + .../Magento/Sales/Model/Order/Shipment/TrackCreation.php | 5 +++-- .../Magento/Sales/Model/Order/ShipmentQuantityValidator.php | 2 +- .../Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php | 1 + .../Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php | 2 -- 5 files changed, 6 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php index ee0e251453507..129e55e29c3cc 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php @@ -28,6 +28,7 @@ class ItemCreation implements ShipmentItemCreationInterface private $extensionAttributes; //@codeCoverageIgnoreStart + /** * {@inheritdoc} */ diff --git a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php index 0618ae0f6ab26..6c54e0a72c1d7 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php @@ -161,8 +161,9 @@ public function getExtensionAttributes() /** * {@inheritdoc} */ - public function setExtensionAttributes(\Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface $extensionAttributes) - { + public function setExtensionAttributes( + \Magento\Sales\Api\Data\ShipmentTrackCreationExtensionInterface $extensionAttributes + ) { $this->extensionAttributes = $extensionAttributes; return $this; } diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index b84813d1e564c..ca17c1d695c22 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -82,7 +82,7 @@ public function validateShipmentItem(ShipmentItemInterface $item, OrderInterface /** * @param OrderInterface $order - * @param $id + * @param int $id * @return \Magento\Sales\Api\Data\OrderItemInterface|null */ private function getOrderItemById(OrderInterface $order, $id) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php index 8c6a3df2efda0..b01a0af5e50ce 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php @@ -28,6 +28,7 @@ class InvoiceQuantityValidatorTest extends \PHPUnit_Framework_TestCase * @var \Magento\Sales\Api\Data\OrderInterface|\PHPUnit_Framework_MockObject_MockObject */ private $orderMock; + /** * @var \Magento\Sales\Api\OrderRepositoryInterface|\PHPUnit_Framework_MockObject_MockObject */ diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php index 30ae71012095c..4d3bad7a377e8 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php @@ -1,5 +1,4 @@ setMethods(['addComment']) ->getMockForAbstractClass(); - $this->invoiceDocumentFactory = new ShipmentDocumentFactory($this->shipmentFactoryMock); } From 8455f454208d39804586a1451b275c7c3de3ea96 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 15:01:09 +0300 Subject: [PATCH 046/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php | 2 +- app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php | 3 ++- .../Sales/Test/Unit/Model/Order/Validation/CanShipTest.php | 3 ++- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php index 129e55e29c3cc..e3cb2f23d7cf3 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/ItemCreation.php @@ -28,7 +28,7 @@ class ItemCreation implements ShipmentItemCreationInterface private $extensionAttributes; //@codeCoverageIgnoreStart - + /** * {@inheritdoc} */ diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php index dffff83b97fd2..106882418ab9f 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php @@ -32,7 +32,8 @@ public function validate($entity) * @param OrderInterface $order * @return bool */ - private function canInvoice(OrderInterface $order) { + private function canInvoice(OrderInterface $order) + { if ($order->getState() === Order::STATE_PAYMENT_REVIEW || $order->getState() === Order::STATE_HOLDED || $order->getState() === Order::STATE_CANCELED || diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php index c31fd28b7b48c..439e322b9c3d6 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php @@ -120,7 +120,8 @@ public function testCanShip($qtyToShipment, $itemLockedDoShipment, $expectedResu ->willReturn($itemLockedDoShipment); $this->assertEquals( - $expectedResult, $this->model->validate($this->orderMock) + $expectedResult, + $this->model->validate($this->orderMock) ); } From ef9c96c06a3b108254150d530f3fb7db1f64e278 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 16:31:24 +0300 Subject: [PATCH 047/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Magento/Sales/Api/Data/TrackInterface.php | 17 +++++++++++++++++ .../Model/Order/Validation/CanInvoiceTest.php | 2 +- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Api/Data/TrackInterface.php b/app/code/Magento/Sales/Api/Data/TrackInterface.php index d082f6ca5e066..2a43b92fe6839 100644 --- a/app/code/Magento/Sales/Api/Data/TrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/TrackInterface.php @@ -102,4 +102,21 @@ public function setCarrierCode($code); * @return string Carrier code. */ public function getCarrierCode(); + + /** + * Retrieve existing extension attributes object or create a new one. + * + * @return \Magento\Sales\Api\Data\TrackExtensionInterface|null + */ + public function getExtensionAttributes(); + + /** + * Set an extension attributes object. + * + * @param \Magento\Sales\Api\Data\TrackExtensionInterface $extensionAttributes + * @return $this + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\TrackExtensionInterface $extensionAttributes + ); } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php index dae95c4a758eb..b0704fcb01de4 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php @@ -4,7 +4,7 @@ * See COPYING.txt for license details. */ -namespace Magento\Sales\Test\Unit\Model\Order\Invoice\Validator; +namespace Magento\Sales\Test\Unit\Model\Order\Validation; use Magento\Sales\Model\Order; From dfdfd6468f768f2bf9e807095d9f0fac99bda980 Mon Sep 17 00:00:00 2001 From: Dmytro Voskoboinikov Date: Tue, 16 Aug 2016 16:36:46 +0300 Subject: [PATCH 048/115] MAGETWO-56495: Introduce and implement ShipmentNotifier --- app/code/Magento/Sales/etc/di.xml | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 21a7ec666c41a..daf2a69a6193f 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -70,6 +70,7 @@ + @@ -917,4 +918,11 @@ + + + + Magento\Sales\Model\Order\Shipment\Sender\EmailSender + + + From 3577e43e5812b1ef13ea4ff8c728152b1ef0a293 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 16:37:51 +0300 Subject: [PATCH 049/115] MAGETWO-54677: Order status change after Shipment creation through API - redundant validation remove --- .../Sales/Model/Order/Shipment/Item.php | 24 ------------------- 1 file changed, 24 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Item.php b/app/code/Magento/Sales/Model/Order/Shipment/Item.php index ec1b2940fbf59..c8bdac365cc51 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Item.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Item.php @@ -9,10 +9,8 @@ namespace Magento\Sales\Model\Order\Shipment; use Magento\Framework\Api\AttributeValueFactory; -use Magento\Framework\App\ObjectManager; use Magento\Sales\Api\Data\ShipmentItemInterface; use Magento\Sales\Model\AbstractModel; -use Magento\Sales\Model\Order\ShipmentQuantityValidator; /** * @method \Magento\Sales\Model\ResourceModel\Order\Shipment\Item _getResource() @@ -170,32 +168,10 @@ public function setQty($qty) */ public function register() { - $errorMessages = $this->getShipmentValidator()->validate( - $this->getShipment(), - [ShipmentQuantityValidator::class] - ); - if (!empty($errorMessages)) { - throw new \Magento\Framework\Exception\LocalizedException( - __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages)) - ); - } $this->getOrderItem()->setQtyShipped($this->getOrderItem()->getQtyShipped() + $this->getQty()); return $this; } - /** - * @return ShipmentValidatorInterface - * @deprecated - */ - private function getShipmentValidator() - { - if ($this->shipmentValidator === null) { - $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); - } - - return $this->shipmentValidator; - } - //@codeCoverageIgnoreStart /** * Returns additional_data From 98537ad8c3abd720cbe3f7646ee6769bc316a2da Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 16:43:29 +0300 Subject: [PATCH 050/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 8 ++++++++ .../Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php | 7 ++----- .../Test/Unit/Model/Order/Validation/CanInvoiceTest.php | 3 ++- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 6509aacd932c3..5a263ea1795e8 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -21,8 +21,15 @@ */ class ShipmentDocumentFactory { + /** + * @var ShipmentFactory + */ private $shipmentFactory; + /** + * ShipmentDocumentFactory constructor. + * @param ShipmentFactory $shipmentFactory + */ public function __construct( ShipmentFactory $shipmentFactory ) { @@ -37,6 +44,7 @@ public function __construct( * @param bool $appendComment * @param array $packages * @return Shipment + * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function create( OrderInterface $order, diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php index df35aae943e74..4461e16b672c2 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php @@ -5,9 +5,6 @@ */ namespace Magento\Sales\Test\Unit\Model\Order; -use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; -use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; - /** * Unit test for shipment factory class. */ @@ -45,7 +42,7 @@ class ShipmentFactoryTest extends \PHPUnit_Framework_TestCase */ protected function setUp() { - $objectManager = new ObjectManager($this); + $objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); $this->converter = $this->getMock( \Magento\Sales\Model\Convert\Order::class, @@ -74,7 +71,7 @@ protected function setUp() false ); $this->shipmentValidatorMock = $this->getMockBuilder( - ShipmentValidatorInterface::class + \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class )->getMockForAbstractClass(); $this->subject = $objectManager->getObject( diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php index b0704fcb01de4..f3d93d734741b 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php @@ -122,7 +122,8 @@ public function testCanInvoice($qtyToInvoice, $itemLockedDoInvoice, $expectedRes ->willReturn($itemLockedDoInvoice); $this->assertEquals( - $expectedResult, $this->model->validate($this->orderMock) + $expectedResult, + $this->model->validate($this->orderMock) ); } From a3584889ac047672ff2da9d9747854204b12640a Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 16:48:53 +0300 Subject: [PATCH 051/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Magento/Sales/Api/Data/TrackInterface.php | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/app/code/Magento/Sales/Api/Data/TrackInterface.php b/app/code/Magento/Sales/Api/Data/TrackInterface.php index 2a43b92fe6839..d082f6ca5e066 100644 --- a/app/code/Magento/Sales/Api/Data/TrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/TrackInterface.php @@ -102,21 +102,4 @@ public function setCarrierCode($code); * @return string Carrier code. */ public function getCarrierCode(); - - /** - * Retrieve existing extension attributes object or create a new one. - * - * @return \Magento\Sales\Api\Data\TrackExtensionInterface|null - */ - public function getExtensionAttributes(); - - /** - * Set an extension attributes object. - * - * @param \Magento\Sales\Api\Data\TrackExtensionInterface $extensionAttributes - * @return $this - */ - public function setExtensionAttributes( - \Magento\Sales\Api\Data\TrackExtensionInterface $extensionAttributes - ); } From c5208159c836b2f03a3ab14909bd26fa5041db98 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 16:51:42 +0300 Subject: [PATCH 052/115] MAGETWO-54677: Order status change after Shipment creation through API - redundant validation remove --- .../Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php | 4 +++- app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php | 4 +++- app/code/Magento/Sales/Api/Data/TrackInterface.php | 2 +- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php index f3c3347534aca..6a45242fcd156 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentTrackCreationInterface.php @@ -6,12 +6,14 @@ */ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; + /** * Shipment Track Creation interface. * * @api */ -interface ShipmentTrackCreationInterface extends TrackInterface +interface ShipmentTrackCreationInterface extends TrackInterface, ExtensibleDataInterface { /** * Retrieve existing extension attributes object or create a new one. diff --git a/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php index c6c610433cb80..c5ba339bf02cf 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php @@ -5,6 +5,8 @@ */ namespace Magento\Sales\Api\Data; +use Magento\Framework\Api\ExtensibleDataInterface; + /** * Shipment track interface. * @@ -13,7 +15,7 @@ * shipments. * @api */ -interface ShipmentTrackInterface extends TrackInterface +interface ShipmentTrackInterface extends TrackInterface, ExtensibleDataInterface { /**#@+ * Constants for keys of data array. Identical to the name of the getter in snake case. diff --git a/app/code/Magento/Sales/Api/Data/TrackInterface.php b/app/code/Magento/Sales/Api/Data/TrackInterface.php index d082f6ca5e066..075d6b4518272 100644 --- a/app/code/Magento/Sales/Api/Data/TrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/TrackInterface.php @@ -11,7 +11,7 @@ * * @api */ -interface TrackInterface extends \Magento\Framework\Api\ExtensibleDataInterface +interface TrackInterface { /** * Sets the weight for the shipment package. From 61dcb9922e0db8e8b744eeb305562a8bf5e4b881 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 18:08:56 +0300 Subject: [PATCH 053/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- app/code/Magento/Sales/Model/Order/Shipment/Item.php | 5 ----- app/code/Magento/Sales/Model/Order/ShipmentFactory.php | 1 + .../Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php | 1 + .../Sales/Test/Unit/Model/Order/Validation/CanShipTest.php | 2 +- 4 files changed, 3 insertions(+), 6 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Item.php b/app/code/Magento/Sales/Model/Order/Shipment/Item.php index c8bdac365cc51..8627f76031b06 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Item.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Item.php @@ -44,11 +44,6 @@ class Item extends AbstractModel implements ShipmentItemInterface */ protected $_orderItemFactory; - /** - * @var ShipmentValidatorInterface - */ - private $shipmentValidator; - /** * @param \Magento\Framework\Model\Context $context * @param \Magento\Framework\Registry $registry diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index 518f3909cd73b..dc03ebc191e1f 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -82,6 +82,7 @@ public function create(\Magento\Sales\Model\Order $order, array $items = [], $tr * @param array $items * @return \Magento\Sales\Api\Data\ShipmentInterface * @throws LocalizedException + * @SuppressWarnings(PHPMD.CyclomaticComplexity) */ protected function prepareItems( \Magento\Sales\Api\Data\ShipmentInterface $shipment, diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php index 4461e16b672c2..3c1ed06f9b930 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php @@ -7,6 +7,7 @@ /** * Unit test for shipment factory class. + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class ShipmentFactoryTest extends \PHPUnit_Framework_TestCase { diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php index 439e322b9c3d6..443453726000c 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php @@ -4,7 +4,7 @@ * See COPYING.txt for license details. */ -namespace Magento\Sales\Test\Unit\Model\Order\Shipment\Validator; +namespace Magento\Sales\Test\Unit\Model\Order\Validation; use Magento\Sales\Model\Order; From e0b372ad7c2e41faa58159cd7441b6cec57724f2 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Tue, 16 Aug 2016 18:23:37 +0300 Subject: [PATCH 054/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Model/Order/Shipment/OrderRegistrarInterface.php | 4 ++-- app/code/Magento/Sales/Model/ShipOrder.php | 9 +++++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php b/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php index 6131b3585bc99..7d54acece3599 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/OrderRegistrarInterface.php @@ -19,8 +19,8 @@ interface OrderRegistrarInterface { /** * @param OrderInterface $order - * @param ShipmentInterface $invoice + * @param ShipmentInterface $shipment * @return OrderInterface */ - public function register(OrderInterface $order, ShipmentInterface $invoice); + public function register(OrderInterface $order, ShipmentInterface $shipment); } diff --git a/app/code/Magento/Sales/Model/ShipOrder.php b/app/code/Magento/Sales/Model/ShipOrder.php index a554b83f6b2a9..a989ee5e86510 100644 --- a/app/code/Magento/Sales/Model/ShipOrder.php +++ b/app/code/Magento/Sales/Model/ShipOrder.php @@ -17,6 +17,7 @@ use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; use Magento\Sales\Model\Order\ShipmentQuantityValidator; use Magento\Sales\Model\Order\Validation\CanShip; +use Magento\Sales\Model\Order\Shipment\OrderRegistrarInterface; use Psr\Log\LoggerInterface; /** @@ -75,6 +76,11 @@ class ShipOrder implements ShipOrderInterface */ private $orderValidator; + /** + * @var OrderRegistrarInterface + */ + private $orderRegistrar; + /** * @param ResourceConnection $resourceConnection * @param OrderRepositoryInterface $orderRepository @@ -98,6 +104,7 @@ public function __construct( OrderConfig $config, ShipmentRepositoryInterface $shipmentRepository, NotifierInterface $notifierInterface, + OrderRegistrarInterface $orderRegistrar, LoggerInterface $logger ) { $this->resourceConnection = $resourceConnection; @@ -110,6 +117,7 @@ public function __construct( $this->shipmentRepository = $shipmentRepository; $this->notifierInterface = $notifierInterface; $this->logger = $logger; + $this->orderRegistrar = $orderRegistrar; } /** @@ -166,6 +174,7 @@ public function execute( } $connection->beginTransaction(); try { + $this->orderRegistrar->register($order, $shipment); $order->setState( $this->orderStateResolver->getStateForOrder($order, [OrderStateResolverInterface::IN_PROGRESS]) ); From b3e9f4772eff32a364558078df9c35482a6ba0e1 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Tue, 16 Aug 2016 18:57:09 +0300 Subject: [PATCH 055/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Magento/Sales/Model/Order/ShipmentQuantityValidator.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index ca17c1d695c22..fb3fc9818c101 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -44,6 +44,10 @@ public function validate($entity) if ($entity->getOrderId() === null) { return [__('Order Id is required for shipment document')]; } + + if ($entity->getItems() === null) { + return [__('You can\'t create a shipment without products.')]; + } $messages = []; $order = $this->orderRepository->get($entity->getOrderId()); @@ -64,7 +68,7 @@ public function validateShipmentItem(ShipmentItemInterface $item, OrderInterface $messages = []; $orderItem = $this->getOrderItemById($order, $item->getOrderItemId()); if ($orderItem === null) { - return [__('We can not found item "%1".', $item->getOrderItemId())]; + return [__('We can not found item "%1" in order.', $item->getOrderItemId())]; } if ($orderItem->getIsQtyDecimal()) { $qty = (double)$item->getQty(); From 87a1b6aaac85df9d12f2fb973bcce4a36c5e4845 Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Tue, 16 Aug 2016 19:38:32 +0300 Subject: [PATCH 056/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- app/code/Magento/Sales/Model/Order/ShipmentFactory.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index dc03ebc191e1f..efa4a43da4588 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -156,21 +156,21 @@ protected function prepareItems( * Adds tracks to the shipment. * * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment - * @param array $tracks + * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks * @throws \Magento\Framework\Exception\LocalizedException * @return \Magento\Sales\Api\Data\ShipmentInterface */ protected function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipment, array $tracks) { - foreach ($tracks as $data) { - if (empty($data['number'])) { + foreach ($tracks as $track) { + if (!$track->getTrackNumber()) { throw new \Magento\Framework\Exception\LocalizedException( __('Please enter a tracking number.') ); } $shipment->addTrack( - $this->trackFactory->create()->addData($data) + $this->trackFactory->create()->addData($track->getData()) ); } From 234dc9d84f39d123c2e0afc268a8a8eafce829d0 Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Tue, 16 Aug 2016 19:44:29 +0300 Subject: [PATCH 057/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Sales/Model/Order/ShipmentFactory.php | 22 +++++++++++++------ 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index efa4a43da4588..e363f6b47cf02 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -6,7 +6,9 @@ namespace Magento\Sales\Model\Order; use Magento\Framework\App\ObjectManager; +use Magento\Framework\EntityManager\HydratorPool; use Magento\Framework\Exception\LocalizedException; +use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; /** @@ -41,18 +43,26 @@ class ShipmentFactory private $shipmentValidator; /** - * Factory constructor. + * @var HydratorPool + */ + private $hydratorPool; + + /** + * ShipmentFactory constructor. * * @param \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory - * @param \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory + * @param Shipment\TrackFactory $trackFactory + * @param HydratorPool $hydratorPool */ public function __construct( \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory, - \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory + \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory, + HydratorPool $hydratorPool ) { $this->converter = $convertOrderFactory->create(); $this->trackFactory = $trackFactory; $this->instanceName = \Magento\Sales\Api\Data\ShipmentInterface::class; + $this->hydratorPool = $hydratorPool; } /** @@ -168,10 +178,8 @@ protected function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $ship __('Please enter a tracking number.') ); } - - $shipment->addTrack( - $this->trackFactory->create()->addData($track->getData()) - ); + $hydrator = $this->hydratorPool->getHydrator(ShipmentTrackCreationInterface::class); + $shipment->addTrack($hydrator->extract($track)); } return $shipment; From ab5a67380461be5270df8c20a040da0d56bec64f Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Tue, 16 Aug 2016 19:51:32 +0300 Subject: [PATCH 058/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- app/code/Magento/Sales/etc/di.xml | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index daf2a69a6193f..10e404631e969 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -925,4 +925,11 @@ + + + + Magento\Framework\EntityManager\HydratorInterface + + + From dc38d66864506e7702185a646b4ec5137e6d394c Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Tue, 16 Aug 2016 20:02:43 +0300 Subject: [PATCH 059/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- lib/internal/Magento/Framework/EntityManager/TypeResolver.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/EntityManager/TypeResolver.php b/lib/internal/Magento/Framework/EntityManager/TypeResolver.php index 28e2bdaa70942..e40eb50595cb4 100644 --- a/lib/internal/Magento/Framework/EntityManager/TypeResolver.php +++ b/lib/internal/Magento/Framework/EntityManager/TypeResolver.php @@ -64,7 +64,9 @@ public function resolve($type) $this->typeMapping[$className] = $dataInterface; } } - + if (empty($this->typeMapping[$className])) { + $this->typeMapping[$className] = reset($dataInterfaces); + } return $this->typeMapping[$className]; } } From 79e6f0c6ba0da6f4bf8f4e06d6256de6716bc51c Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Tue, 16 Aug 2016 20:10:11 +0300 Subject: [PATCH 060/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Magento/Framework/EntityManager/CustomAttributesMapper.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php b/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php index 9147d47f3d9dd..0d350cc122e3c 100644 --- a/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php +++ b/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php @@ -55,6 +55,9 @@ public function __construct( */ public function entityToDatabase($entityType, $data) { + if (!$this->metadataPool->hasConfiguration($entityType)) { + return $data; + } $metadata = $this->metadataPool->getMetadata($entityType); if (!$metadata->getEavEntityType()) { return $data; From da2224d692f323f54b37c917dd20446bda7bd2a3 Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Tue, 16 Aug 2016 20:25:59 +0300 Subject: [PATCH 061/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Model/Order/ShipmentDocumentFactory.php | 50 +++++++++++++++++-- .../Sales/Model/Order/ShipmentFactory.php | 28 ++++------- 2 files changed, 56 insertions(+), 22 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 5a263ea1795e8..347d9d311de29 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -6,6 +6,7 @@ namespace Magento\Sales\Model\Order; +use Magento\Framework\EntityManager\HydratorPool; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; use Magento\Sales\Api\Data\ShipmentCommentInterface; @@ -13,6 +14,7 @@ use Magento\Sales\Api\Data\ShipmentItemCreationInterface; use Magento\Sales\Api\Data\ShipmentPackageInterface; use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; +use Magento\Sales\Model\Order\Shipment\TrackFactory; /** * Class InvoiceDocumentFactory @@ -26,14 +28,31 @@ class ShipmentDocumentFactory */ private $shipmentFactory; + /** + * @var TrackFactory + */ + private $trackFactory; + + /** + * @var HydratorPool + */ + private $hydratorPool; + /** * ShipmentDocumentFactory constructor. + * * @param ShipmentFactory $shipmentFactory + * @param HydratorPool $hydratorPool + * @param TrackFactory $trackFactory */ public function __construct( - ShipmentFactory $shipmentFactory + ShipmentFactory $shipmentFactory, + HydratorPool $hydratorPool, + TrackFactory $trackFactory ) { $this->shipmentFactory = $shipmentFactory; + $this->trackFactory = $trackFactory; + $this->hydratorPool = $hydratorPool; } /** @@ -59,10 +78,9 @@ public function create( /** @var Shipment $shipment */ $shipment = $this->shipmentFactory->create( $order, - $shipmentItems, - $tracks + $shipmentItems ); - + $this->prepareTracks($shipment, $tracks); if ($comment) { $shipment->addComment( $comment->getComment(), @@ -74,6 +92,30 @@ public function create( return $shipment; } + /** + * Adds tracks to the shipment. + * + * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment + * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks + * @throws \Magento\Framework\Exception\LocalizedException + * @return \Magento\Sales\Api\Data\ShipmentInterface + */ + private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipment, array $tracks) + { + foreach ($tracks as $track) { + if (!$track->getTrackNumber()) { + throw new \Magento\Framework\Exception\LocalizedException( + __('Please enter a tracking number.') + ); + } + $hydrator = $this->hydratorPool->getHydrator(ShipmentTrackCreationInterface::class); + $shipment->addTrack($this->trackFactory->create($hydrator->extract($track))); + } + + return $shipment; + } + + /** * Convert Items To Array * diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index e363f6b47cf02..dc03ebc191e1f 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -6,9 +6,7 @@ namespace Magento\Sales\Model\Order; use Magento\Framework\App\ObjectManager; -use Magento\Framework\EntityManager\HydratorPool; use Magento\Framework\Exception\LocalizedException; -use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; /** @@ -43,26 +41,18 @@ class ShipmentFactory private $shipmentValidator; /** - * @var HydratorPool - */ - private $hydratorPool; - - /** - * ShipmentFactory constructor. + * Factory constructor. * * @param \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory - * @param Shipment\TrackFactory $trackFactory - * @param HydratorPool $hydratorPool + * @param \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory */ public function __construct( \Magento\Sales\Model\Convert\OrderFactory $convertOrderFactory, - \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory, - HydratorPool $hydratorPool + \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory ) { $this->converter = $convertOrderFactory->create(); $this->trackFactory = $trackFactory; $this->instanceName = \Magento\Sales\Api\Data\ShipmentInterface::class; - $this->hydratorPool = $hydratorPool; } /** @@ -166,20 +156,22 @@ protected function prepareItems( * Adds tracks to the shipment. * * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment - * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks + * @param array $tracks * @throws \Magento\Framework\Exception\LocalizedException * @return \Magento\Sales\Api\Data\ShipmentInterface */ protected function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipment, array $tracks) { - foreach ($tracks as $track) { - if (!$track->getTrackNumber()) { + foreach ($tracks as $data) { + if (empty($data['number'])) { throw new \Magento\Framework\Exception\LocalizedException( __('Please enter a tracking number.') ); } - $hydrator = $this->hydratorPool->getHydrator(ShipmentTrackCreationInterface::class); - $shipment->addTrack($hydrator->extract($track)); + + $shipment->addTrack( + $this->trackFactory->create()->addData($data) + ); } return $shipment; From 78b53638084215952780a1a0270cf36acba58aed Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Tue, 16 Aug 2016 20:35:17 +0300 Subject: [PATCH 062/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 347d9d311de29..18ad880c76fc5 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -109,9 +109,8 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme ); } $hydrator = $this->hydratorPool->getHydrator(ShipmentTrackCreationInterface::class); - $shipment->addTrack($this->trackFactory->create($hydrator->extract($track))); + $shipment->addTrack($this->trackFactory->create(['data' => $hydrator->extract($track)])); } - return $shipment; } From 238de275e9a7dd796b01bf69ccbb8d4972e0e8fe Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Wed, 17 Aug 2016 10:54:24 +0300 Subject: [PATCH 063/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Sales/Api/Data/ShipmentTrackInterface.php | 45 ++++++++++++ .../Magento/Sales/Api/Data/TrackInterface.php | 46 ------------- .../Model/Order/Shipment/TrackCreation.php | 68 ------------------- 3 files changed, 45 insertions(+), 114 deletions(-) diff --git a/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php index c5ba339bf02cf..beafa1370f107 100644 --- a/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/ShipmentTrackInterface.php @@ -140,6 +140,51 @@ public function setUpdatedAt($timestamp); */ public function setParentId($id); + /** + * Sets the weight for the shipment package. + * + * @param float $weight + * @return $this + */ + public function setWeight($weight); + + /** + * Gets the weight for the shipment package. + * + * @return float Weight. + */ + public function getWeight(); + + /** + * Sets the quantity for the shipment package. + * + * @param float $qty + * @return $this + */ + public function setQty($qty); + + /** + * Gets the quantity for the shipment package. + * + * @return float Quantity. + */ + public function getQty(); + + /** + * Sets the description for the shipment package. + * + * @param string $description + * @return $this + */ + public function setDescription($description); + + /** + * Gets the description for the shipment package. + * + * @return string Description. + */ + public function getDescription(); + /** * Retrieve existing extension attributes object or create a new one. * diff --git a/app/code/Magento/Sales/Api/Data/TrackInterface.php b/app/code/Magento/Sales/Api/Data/TrackInterface.php index 075d6b4518272..17c08ff9b9830 100644 --- a/app/code/Magento/Sales/Api/Data/TrackInterface.php +++ b/app/code/Magento/Sales/Api/Data/TrackInterface.php @@ -3,7 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Sales\Api\Data; /** @@ -13,36 +12,6 @@ */ interface TrackInterface { - /** - * Sets the weight for the shipment package. - * - * @param float $weight - * @return $this - */ - public function setWeight($weight); - - /** - * Gets the weight for the shipment package. - * - * @return float Weight. - */ - public function getWeight(); - - /** - * Sets the quantity for the shipment package. - * - * @param float $qty - * @return $this - */ - public function setQty($qty); - - /** - * Gets the quantity for the shipment package. - * - * @return float Quantity. - */ - public function getQty(); - /** * Sets the track number for the shipment package. * @@ -58,21 +27,6 @@ public function setTrackNumber($trackNumber); */ public function getTrackNumber(); - /** - * Sets the description for the shipment package. - * - * @param string $description - * @return $this - */ - public function setDescription($description); - - /** - * Gets the description for the shipment package. - * - * @return string Description. - */ - public function getDescription(); - /** * Sets the title for the shipment package. * diff --git a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php index 6c54e0a72c1d7..d2d7e366e9a8a 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php @@ -3,7 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Sales\Model\Order\Shipment; use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; @@ -13,26 +12,11 @@ */ class TrackCreation implements ShipmentTrackCreationInterface { - /** - * @var float - */ - private $weight; - - /** - * @var float - */ - private $qty; - /** * @var string */ private $trackNumber; - /** - * @var string - */ - private $description; - /** * @var string */ @@ -48,40 +32,6 @@ class TrackCreation implements ShipmentTrackCreationInterface */ private $extensionAttributes; - /** - * {@inheritdoc} - */ - public function getWeight() - { - return $this->weight; - } - - /** - * {@inheritdoc} - */ - public function setWeight($weight) - { - $this->weight = $weight; - return $this; - } - - /** - * {@inheritdoc} - */ - public function getQty() - { - return $this->qty; - } - - /** - * {@inheritdoc} - */ - public function setQty($qty) - { - $this->qty = $qty; - return $this; - } - /** * {@inheritdoc} */ @@ -98,24 +48,6 @@ public function setTrackNumber($trackNumber) $this->trackNumber = $trackNumber; return $this; } - - /** - * {@inheritdoc} - */ - public function getDescription() - { - return $this->description; - } - - /** - * {@inheritdoc} - */ - public function setDescription($description) - { - $this->description = $description; - return $this; - } - /** * {@inheritdoc} */ From ade4c84c25b7e2d28f490daf8ffb13e99b4fdd98 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 11:11:48 +0300 Subject: [PATCH 064/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/ShipmentFactory.php | 23 --------------- app/code/Magento/Sales/Model/ShipOrder.php | 8 +++--- .../Adminhtml/Order/Shipment/Save.php | 28 ++++++++++++++++++- 3 files changed, 31 insertions(+), 28 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index dc03ebc191e1f..9127bba57a168 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -5,7 +5,6 @@ */ namespace Magento\Sales\Model\Order; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; @@ -140,15 +139,6 @@ protected function prepareItems( $item->setQty($qty); $shipment->addItem($item); } - $errorMessages = $this->getShipmentValidator()->validate( - $shipment, - [ShipmentQuantityValidator::class] - ); - if (!empty($errorMessages)) { - throw new \Magento\Framework\Exception\LocalizedException( - __("Invoice Document Validation Error(s):\n" . implode("\n", $errorMessages)) - ); - } return $shipment->setTotalQty($totalQty); } @@ -230,17 +220,4 @@ protected function canShipItem($item, array $items = []) return $item->getQtyToShip() > 0; } } - - /** - * @return ShipmentValidatorInterface - * @deprecated - */ - private function getShipmentValidator() - { - if ($this->shipmentValidator === null) { - $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); - } - - return $this->shipmentValidator; - } } diff --git a/app/code/Magento/Sales/Model/ShipOrder.php b/app/code/Magento/Sales/Model/ShipOrder.php index a989ee5e86510..4221676d927bc 100644 --- a/app/code/Magento/Sales/Model/ShipOrder.php +++ b/app/code/Magento/Sales/Model/ShipOrder.php @@ -158,13 +158,13 @@ public function execute( $arguments ); $errorMessages = array_merge( - $this->shipmentValidator->validate( - $shipment, - [ShipmentQuantityValidator::class] - ), $this->orderValidator->validate( $order, [CanShip::class] + ), + $this->shipmentValidator->validate( + $shipment, + [ShipmentQuantityValidator::class] ) ); if (!empty($errorMessages)) { diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index adbd96624649d..2626ae0988f5e 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -7,7 +7,10 @@ namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment; use Magento\Backend\App\Action; +use Magento\Framework\App\ObjectManager; use Magento\Sales\Model\Order\Email\Sender\ShipmentSender; +use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; +use Magento\Sales\Model\Order\ShipmentQuantityValidator; class Save extends \Magento\Backend\App\Action { @@ -33,6 +36,11 @@ class Save extends \Magento\Backend\App\Action */ protected $shipmentSender; + /** + * @var ShipmentValidatorInterface + */ + private $shipmentValidator; + /** * @param Action\Context $context * @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader @@ -119,7 +127,12 @@ public function execute() $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } - + $errorMessages = $this->getShipmentValidator()->validate($shipment, [ShipmentQuantityValidator::class]); + if (!empty($errorMessages)) { + throw new \Magento\Sales\Exception\DocumentValidationException( + __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) + ); + } $shipment->register(); $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); @@ -168,4 +181,17 @@ public function execute() $this->_redirect('sales/order/view', ['order_id' => $shipment->getOrderId()]); } } + + /** + * @return ShipmentValidatorInterface + * @deprecated + */ + private function getShipmentValidator() + { + if ($this->shipmentValidator === null) { + $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); + } + + return $this->shipmentValidator; + } } From 1c8ced92740a6be9a8e13f78d3f315a1cf157361 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 11:29:12 +0300 Subject: [PATCH 065/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Magento/Sales/Model/Order/Shipment/Item.php | 17 ++++++++++++++++- 1 file changed, 16 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Item.php b/app/code/Magento/Sales/Model/Order/Shipment/Item.php index 8627f76031b06..119639c352267 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Item.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Item.php @@ -151,7 +151,22 @@ public function getOrderItem() */ public function setQty($qty) { - $this->setData('qty', $qty); + if ($this->getOrderItem()->getIsQtyDecimal()) { + $qty = (double)$qty; + } else { + $qty = (int)$qty; + } + $qty = $qty > 0 ? $qty : 0; + /** + * Check qty availability + */ + if ($qty <= $this->getOrderItem()->getQtyToShip() || $this->getOrderItem()->isDummy(true)) { + $this->setData('qty', $qty); + } else { + throw new \Magento\Framework\Exception\LocalizedException( + __('We found an invalid quantity to ship for item "%1".', $this->getName()) + ); + } return $this; } From 32d9799e6444227d33e534a3d9b356f4f9ee3e0e Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 11:38:54 +0300 Subject: [PATCH 066/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/Shipment/Item.php | 17 +------------- .../Sales/Model/Order/ShipmentFactory.php | 23 +++++++++++++++++++ .../Adminhtml/Order/Shipment/Save.php | 6 ----- 3 files changed, 24 insertions(+), 22 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Item.php b/app/code/Magento/Sales/Model/Order/Shipment/Item.php index 119639c352267..8627f76031b06 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Item.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Item.php @@ -151,22 +151,7 @@ public function getOrderItem() */ public function setQty($qty) { - if ($this->getOrderItem()->getIsQtyDecimal()) { - $qty = (double)$qty; - } else { - $qty = (int)$qty; - } - $qty = $qty > 0 ? $qty : 0; - /** - * Check qty availability - */ - if ($qty <= $this->getOrderItem()->getQtyToShip() || $this->getOrderItem()->isDummy(true)) { - $this->setData('qty', $qty); - } else { - throw new \Magento\Framework\Exception\LocalizedException( - __('We found an invalid quantity to ship for item "%1".', $this->getName()) - ); - } + $this->setData('qty', $qty); return $this; } diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index 9127bba57a168..09e03fcaf5557 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -5,6 +5,7 @@ */ namespace Magento\Sales\Model\Order; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; @@ -139,6 +140,15 @@ protected function prepareItems( $item->setQty($qty); $shipment->addItem($item); } + $errorMessages = $this->getShipmentValidator()->validate( + $shipment, + [ShipmentQuantityValidator::class] + ); + if (!empty($errorMessages)) { + throw new \Magento\Framework\Exception\LocalizedException( + __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) + ); + } return $shipment->setTotalQty($totalQty); } @@ -220,4 +230,17 @@ protected function canShipItem($item, array $items = []) return $item->getQtyToShip() > 0; } } + + /** + * @return ShipmentValidatorInterface + * @deprecated + */ + private function getShipmentValidator() + { + if ($this->shipmentValidator === null) { + $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); + } + + return $this->shipmentValidator; + } } diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index 2626ae0988f5e..6898ade5a7774 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -127,12 +127,6 @@ public function execute() $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } - $errorMessages = $this->getShipmentValidator()->validate($shipment, [ShipmentQuantityValidator::class]); - if (!empty($errorMessages)) { - throw new \Magento\Sales\Exception\DocumentValidationException( - __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) - ); - } $shipment->register(); $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); From 0ed35d3dac7703393c275b8f6a9009fcdaddf8b3 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 11:47:35 +0300 Subject: [PATCH 067/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/ShipmentFactory.php | 23 -------------- .../Adminhtml/Order/ShipmentLoader.php | 30 +++++++++++++++++++ 2 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index 09e03fcaf5557..9127bba57a168 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -5,7 +5,6 @@ */ namespace Magento\Sales\Model\Order; -use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; @@ -140,15 +139,6 @@ protected function prepareItems( $item->setQty($qty); $shipment->addItem($item); } - $errorMessages = $this->getShipmentValidator()->validate( - $shipment, - [ShipmentQuantityValidator::class] - ); - if (!empty($errorMessages)) { - throw new \Magento\Framework\Exception\LocalizedException( - __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) - ); - } return $shipment->setTotalQty($totalQty); } @@ -230,17 +220,4 @@ protected function canShipItem($item, array $items = []) return $item->getQtyToShip() > 0; } } - - /** - * @return ShipmentValidatorInterface - * @deprecated - */ - private function getShipmentValidator() - { - if ($this->shipmentValidator === null) { - $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); - } - - return $this->shipmentValidator; - } } diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php index b452c88887c9e..2c49937edf74e 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php @@ -6,7 +6,10 @@ */ namespace Magento\Shipping\Controller\Adminhtml\Order; +use Magento\Framework\App\ObjectManager; use Magento\Framework\DataObject; +use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; +use Magento\Sales\Model\Order\ShipmentQuantityValidator; /** * Class ShipmentLoader @@ -53,6 +56,11 @@ class ShipmentLoader extends DataObject */ protected $orderRepository; + /** + * @var ShipmentValidatorInterface + */ + private $shipmentValidator; + /** * @param \Magento\Framework\Message\ManagerInterface $messageManager * @param \Magento\Framework\Registry $registry @@ -135,9 +143,31 @@ public function load() $this->getItemQtys(), $this->getTracking() ); + $errorMessages = $this->getShipmentValidator()->validate( + $shipment, + [ShipmentQuantityValidator::class] + ); + if (!empty($errorMessages)) { + throw new \Magento\Framework\Exception\LocalizedException( + __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) + ); + } } $this->registry->register('current_shipment', $shipment); return $shipment; } + + /** + * @return ShipmentValidatorInterface + * @deprecated + */ + private function getShipmentValidator() + { + if ($this->shipmentValidator === null) { + $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); + } + + return $this->shipmentValidator; + } } From a18c342cc9096b36b1403e55904bf303b23b1822 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Wed, 17 Aug 2016 12:01:39 +0300 Subject: [PATCH 068/115] MAGETWO-54677: Unit tests fixed --- .../Order/ShipmentDocumentFactoryTest.php | 81 +++++++++++++++++-- .../Unit/Model/Order/ShipmentFactoryTest.php | 2 - 2 files changed, 76 insertions(+), 7 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php index 4d3bad7a377e8..93f6b7408d619 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php @@ -5,12 +5,17 @@ */ namespace Magento\Sales\Test\Unit\Model\Order; +use Magento\Framework\EntityManager\HydratorPool; use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; use Magento\Sales\Api\Data\ShipmentItemCreationInterface; +use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; use Magento\Sales\Model\Order\ShipmentFactory; use Magento\Sales\Model\Order\ShipmentDocumentFactory; use Magento\Sales\Model\Order; use Magento\Sales\Api\Data\ShipmentInterface; +use Magento\Sales\Model\Order\Shipment\TrackFactory; +use Magento\Sales\Model\Order\Shipment\Track; +use Magento\Framework\EntityManager\HydratorInterface; /** * Class InvoiceDocumentFactoryTest @@ -47,6 +52,26 @@ class ShipmentDocumentFactoryTest extends \PHPUnit_Framework_TestCase */ private $invoiceDocumentFactory; + /** + * @var \PHPUnit_Framework_MockObject_MockObject|HydratorPool + */ + private $hydratorPoolMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|TrackFactory + */ + private $trackFactoryMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|TrackFactory + */ + private $hydratorMock; + + /** + * @var \PHPUnit_Framework_MockObject_MockObject|HydratorInterface + */ + private $trackMock; + protected function setUp() { $this->shipmentFactoryMock = $this->getMockBuilder(ShipmentFactory::class) @@ -67,15 +92,39 @@ protected function setUp() $this->shipmentMock = $this->getMockBuilder(ShipmentInterface::class) ->disableOriginalConstructor() - ->setMethods(['addComment']) + ->setMethods(['addComment', 'addTrack']) ->getMockForAbstractClass(); - $this->invoiceDocumentFactory = new ShipmentDocumentFactory($this->shipmentFactoryMock); + $this->hydratorPoolMock = $this->getMockBuilder(HydratorPool::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->trackFactoryMock = $this->getMockBuilder(TrackFactory::class) + ->setMethods(['create']) + ->disableOriginalConstructor() + ->getMock(); + + $this->trackMock = $this->getMockBuilder(Track::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->hydratorMock = $this->getMockBuilder(HydratorInterface::class) + ->disableOriginalConstructor() + ->setMethods(['extract']) + ->getMockForAbstractClass(); + + $this->invoiceDocumentFactory = new ShipmentDocumentFactory( + $this->shipmentFactoryMock, + $this->hydratorPoolMock, + $this->trackFactoryMock + ); } public function testCreate() { - $tracks = ["1234567890"]; + $trackNum = "123456789"; + $trackData = [$trackNum]; + $tracks = [$this->trackMock]; $appendComment = true; $packages = []; $items = [1 => 10]; @@ -92,11 +141,33 @@ public function testCreate() ->method('create') ->with( $this->orderMock, - $items, - $tracks + $items ) ->willReturn($this->shipmentMock); + $this->trackMock->expects($this->once()) + ->method('getTrackNumber') + ->willReturn($trackNum); + + $this->shipmentMock->expects($this->once()) + ->method('addTrack') + ->willReturnSelf(); + + $this->hydratorPoolMock->expects($this->once()) + ->method('getHydrator') + ->with(ShipmentTrackCreationInterface::class) + ->willReturn($this->hydratorMock); + + $this->hydratorMock->expects($this->once()) + ->method('extract') + ->with($this->trackMock) + ->willReturn($trackData); + + $this->trackFactoryMock->expects($this->once()) + ->method('create') + ->with(['data' => $trackData]) + ->willReturn($this->trackMock); + if ($appendComment) { $comment = "New comment!"; $visibleOnFront = true; diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php index 3c1ed06f9b930..cbf637f124889 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php @@ -194,8 +194,6 @@ public function testCreate($tracks) } } - $this->shipmentValidatorMock->expects($this->once())->method('validate')->willReturn([]); - $this->assertEquals($shipment, $this->subject->create($order, ['1' => 5], $tracks)); } From 4276e5be35ac7758420c6fa14a0956de9098cf29 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 14:03:12 +0300 Subject: [PATCH 069/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/ShipmentFactory.php | 2 +- .../Model/Order/ShipmentQuantityValidator.php | 2 +- .../Adminhtml/Order/Shipment/Save.php | 8 ++++++- .../Adminhtml/Order/ShipmentLoader.php | 22 ------------------- 4 files changed, 9 insertions(+), 25 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index 9127bba57a168..3f9dc8143990f 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -5,6 +5,7 @@ */ namespace Magento\Sales\Model\Order; +use Magento\Framework\App\ObjectManager; use Magento\Framework\Exception\LocalizedException; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; @@ -89,7 +90,6 @@ protected function prepareItems( array $items = [] ) { $totalQty = 0; - foreach ($order->getAllItems() as $orderItem) { if (!$this->canShipItem($orderItem, $items)) { continue; diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index fb3fc9818c101..e5b7d5d52b69e 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -107,6 +107,6 @@ private function getOrderItemById(OrderInterface $order, $id) */ private function isQtyAvailable(Item $orderItem, $qty) { - return $qty <= $orderItem->getQtyToShip() || $orderItem->isDummy(true); + return $qty !== 0 && ($qty <= $orderItem->getQtyToShip() || $orderItem->isDummy(true)); } } diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index 6898ade5a7774..30a22f6157f73 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -127,11 +127,17 @@ public function execute() $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } + $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; + $errorMessages = $this->getShipmentValidator()->validate($shipment, [ShipmentQuantityValidator::class]); + if (!empty($errorMessages)) { + throw new \Magento\Sales\Exception\DocumentValidationException( + __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) + ); + } $shipment->register(); $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $responseAjax = new \Magento\Framework\DataObject(); - $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; if ($isNeedCreateLabel) { $this->labelGenerator->create($shipment, $this->_request); diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php index 2c49937edf74e..0b537d39005d7 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php @@ -143,31 +143,9 @@ public function load() $this->getItemQtys(), $this->getTracking() ); - $errorMessages = $this->getShipmentValidator()->validate( - $shipment, - [ShipmentQuantityValidator::class] - ); - if (!empty($errorMessages)) { - throw new \Magento\Framework\Exception\LocalizedException( - __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) - ); - } } $this->registry->register('current_shipment', $shipment); return $shipment; } - - /** - * @return ShipmentValidatorInterface - * @deprecated - */ - private function getShipmentValidator() - { - if ($this->shipmentValidator === null) { - $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); - } - - return $this->shipmentValidator; - } } From ba54a989e562e9c99185468de07f62a4c37dd622 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Wed, 17 Aug 2016 14:23:06 +0300 Subject: [PATCH 070/115] MAGETWO-54677: Added Arguments interface --- .../Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 18ad880c76fc5..ae1140815acbb 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -10,6 +10,7 @@ use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; use Magento\Sales\Api\Data\ShipmentCommentInterface; +use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface; use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\Data\ShipmentItemCreationInterface; use Magento\Sales\Api\Data\ShipmentPackageInterface; @@ -62,6 +63,7 @@ public function __construct( * @param ShipmentCommentCreationInterface|null $comment * @param bool $appendComment * @param array $packages + * @param ShipmentCreationArgumentsInterface $arguments * @return Shipment * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ @@ -71,7 +73,8 @@ public function create( $tracks = [], ShipmentCommentCreationInterface $comment = null, $appendComment = false, - $packages = [] + $packages = [], + ShipmentCreationArgumentsInterface $arguments = [] ) { $shipmentItems = $this->itemsToArray($items); From f89a52bb64bee6544a12cde487c3ae483469fd39 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 14:51:09 +0300 Subject: [PATCH 071/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php | 1 + app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 1 - 2 files changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php index d2d7e366e9a8a..f36889353afad 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php @@ -48,6 +48,7 @@ public function setTrackNumber($trackNumber) $this->trackNumber = $trackNumber; return $this; } + /** * {@inheritdoc} */ diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 18ad880c76fc5..15486cfde2ec9 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -114,7 +114,6 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme return $shipment; } - /** * Convert Items To Array * From 9de64f2548bce6d60b9bd91e8896e53bf29f9eae Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 14:52:41 +0300 Subject: [PATCH 072/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 4ce3257bb467f..adedf2fed3c47 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -74,7 +74,7 @@ public function create( ShipmentCommentCreationInterface $comment = null, $appendComment = false, $packages = [], - ShipmentCreationArgumentsInterface $arguments = [] + ShipmentCreationArgumentsInterface $arguments = null ) { $shipmentItems = $this->itemsToArray($items); From 64113951c2d4ce452a3710a847908c86b975e787 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 14:57:03 +0300 Subject: [PATCH 073/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/ShipmentFactory.php | 5 ----- .../Adminhtml/Order/Shipment/Save.php | 20 +++++++++++-------- 2 files changed, 12 insertions(+), 13 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index 3f9dc8143990f..db0d8a024190b 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -35,11 +35,6 @@ class ShipmentFactory */ protected $instanceName; - /** - * @var ShipmentValidatorInterface - */ - private $shipmentValidator; - /** * Factory constructor. * diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index 30a22f6157f73..b7e0834c11bde 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -8,10 +8,12 @@ use Magento\Backend\App\Action; use Magento\Framework\App\ObjectManager; -use Magento\Sales\Model\Order\Email\Sender\ShipmentSender; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; use Magento\Sales\Model\Order\ShipmentQuantityValidator; +/** + * Class Save + */ class Save extends \Magento\Backend\App\Action { /** @@ -32,7 +34,7 @@ class Save extends \Magento\Backend\App\Action protected $labelGenerator; /** - * @var ShipmentSender + * @var \Magento\Sales\Model\Order\Email\Sender\ShipmentSender */ protected $shipmentSender; @@ -42,16 +44,16 @@ class Save extends \Magento\Backend\App\Action private $shipmentValidator; /** - * @param Action\Context $context + * @param \Magento\Backend\App\Action\Context $context * @param \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader * @param \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator - * @param ShipmentSender $shipmentSender + * @param \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender */ public function __construct( - Action\Context $context, + \Magento\Backend\App\Action\Context $context, \Magento\Shipping\Controller\Adminhtml\Order\ShipmentLoader $shipmentLoader, \Magento\Shipping\Model\Shipping\LabelGenerator $labelGenerator, - ShipmentSender $shipmentSender + \Magento\Sales\Model\Order\Email\Sender\ShipmentSender $shipmentSender ) { $this->shipmentLoader = $shipmentLoader; $this->labelGenerator = $labelGenerator; @@ -183,13 +185,15 @@ public function execute() } /** - * @return ShipmentValidatorInterface + * @return \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface * @deprecated */ private function getShipmentValidator() { if ($this->shipmentValidator === null) { - $this->shipmentValidator = ObjectManager::getInstance()->get(ShipmentValidatorInterface::class); + $this->shipmentValidator = ObjectManager::getInstance()->get( + \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class + ); } return $this->shipmentValidator; From b1ee7023532d41ca0f13d7ebaac9e22226839b53 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 15:12:12 +0300 Subject: [PATCH 074/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Adminhtml/Order/Shipment/Save.php | 22 +++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index b7e0834c11bde..666ff569849b6 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -129,17 +129,19 @@ public function execute() $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } - $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; $errorMessages = $this->getShipmentValidator()->validate($shipment, [ShipmentQuantityValidator::class]); if (!empty($errorMessages)) { - throw new \Magento\Sales\Exception\DocumentValidationException( + $this->messageManager->addError( __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) ); + $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); + return; } $shipment->register(); $shipment->getOrder()->setCustomerNoteNotify(!empty($data['send_email'])); $responseAjax = new \Magento\Framework\DataObject(); + $isNeedCreateLabel = isset($data['create_shipping_label']) && $data['create_shipping_label']; if ($isNeedCreateLabel) { $this->labelGenerator->create($shipment, $this->_request); @@ -198,4 +200,20 @@ private function getShipmentValidator() return $this->shipmentValidator; } + + /** + * @param $isNeedCreateLabel + * @param $responseAjax + * @param $message + */ + private function handleError($isNeedCreateLabel, $responseAjax, $message) + { + if ($isNeedCreateLabel) { + $responseAjax->setError(true); + $responseAjax->setMessage($message); + } else { + $this->messageManager->addError($message); + $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); + } + } } From a8d463b6ff870053073703dbe07b8bc3023eec6d Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 15:16:51 +0300 Subject: [PATCH 075/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 5 +---- app/code/Magento/Sales/Model/ShipOrder.php | 1 + .../Shipping/Controller/Adminhtml/Order/ShipmentLoader.php | 5 ----- 3 files changed, 2 insertions(+), 9 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index adedf2fed3c47..d9b7b51a229be 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -9,11 +9,8 @@ use Magento\Framework\EntityManager\HydratorPool; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; -use Magento\Sales\Api\Data\ShipmentCommentInterface; use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface; -use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\Data\ShipmentItemCreationInterface; -use Magento\Sales\Api\Data\ShipmentPackageInterface; use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; use Magento\Sales\Model\Order\Shipment\TrackFactory; @@ -120,7 +117,7 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme /** * Convert Items To Array * - * @param InvoiceItemCreationInterface[] $items + * @param ShipmentItemCreationInterface[] $items * @return array */ private function itemsToArray($items = []) diff --git a/app/code/Magento/Sales/Model/ShipOrder.php b/app/code/Magento/Sales/Model/ShipOrder.php index 4221676d927bc..b5498b1085b52 100644 --- a/app/code/Magento/Sales/Model/ShipOrder.php +++ b/app/code/Magento/Sales/Model/ShipOrder.php @@ -91,6 +91,7 @@ class ShipOrder implements ShipOrderInterface * @param OrderConfig $config * @param ShipmentRepositoryInterface $shipmentRepository * @param NotifierInterface $notifierInterface + * @param OrderRegistrarInterface $orderRegistrar * @param LoggerInterface $logger * @SuppressWarnings(PHPMD.ExcessiveParameterList) */ diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php index 0b537d39005d7..c1ad965393777 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php @@ -56,11 +56,6 @@ class ShipmentLoader extends DataObject */ protected $orderRepository; - /** - * @var ShipmentValidatorInterface - */ - private $shipmentValidator; - /** * @param \Magento\Framework\Message\ManagerInterface $messageManager * @param \Magento\Framework\Registry $registry From 73fe2cfcc7b7ef0e847f9c6837956470e0e4fd9a Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Wed, 17 Aug 2016 15:26:30 +0300 Subject: [PATCH 076/115] MAGETWO-54677: Fixed Unit tests --- .../Unit/Model/Order/ShipmentFactoryTest.php | 14 ------------- .../Adminhtml/Order/Shipment/SaveTest.php | 20 ++++++++++++++++++- 2 files changed, 19 insertions(+), 15 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php index cbf637f124889..46d6ac62fc256 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentFactoryTest.php @@ -32,11 +32,6 @@ class ShipmentFactoryTest extends \PHPUnit_Framework_TestCase */ protected $trackFactory; - /** - * @var \PHPUnit_Framework_MockObject_MockObject - */ - private $shipmentValidatorMock; - /** * @return void * @SuppressWarnings(PHPMD.ExcessiveMethodLength) @@ -71,9 +66,6 @@ protected function setUp() '', false ); - $this->shipmentValidatorMock = $this->getMockBuilder( - \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class - )->getMockForAbstractClass(); $this->subject = $objectManager->getObject( \Magento\Sales\Model\Order\ShipmentFactory::class, @@ -82,12 +74,6 @@ protected function setUp() 'trackFactory' => $this->trackFactory ] ); - - $objectManager->setBackwardCompatibleProperty( - $this->subject, - 'shipmentValidator', - $this->shipmentValidatorMock - ); } /** diff --git a/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php b/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php index af89f1a9146be..bdc4dd17d1088 100644 --- a/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php +++ b/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php @@ -11,6 +11,9 @@ use Magento\Backend\App\Action; use Magento\Sales\Model\Order\Email\Sender\ShipmentSender; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; +use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; +use Magento\Sales\Model\Order\ShipmentQuantityValidator; + /** * Class SaveTest * @@ -88,6 +91,11 @@ class SaveTest extends \PHPUnit_Framework_TestCase */ protected $saveAction; + /** + * @var ShipmentValidatorInterface|\PHPUnit_Framework_MockObject_MockObject + */ + private $shipmentValidatorMock; + /** * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ @@ -210,6 +218,10 @@ protected function setUp() ->method('getFormKeyValidator') ->will($this->returnValue($this->formKeyValidator)); + $this->shipmentValidatorMock = $this->getMockBuilder(ShipmentValidatorInterface::class) + ->disableOriginalConstructor() + ->getMock(); + $this->saveAction = $objectManagerHelper->getObject( \Magento\Shipping\Controller\Adminhtml\Order\Shipment\Save::class, [ @@ -218,7 +230,8 @@ protected function setUp() 'context' => $this->context, 'shipmentLoader' => $this->shipmentLoader, 'request' => $this->request, - 'response' => $this->response + 'response' => $this->response, + 'shipmentValidator' => $this->shipmentValidatorMock ] ); } @@ -346,6 +359,11 @@ public function testExecute($formKeyIsValid, $isPost) ->will($this->returnValue($orderId)); $this->prepareRedirect($path, $arguments); + $this->shipmentValidatorMock->expects($this->once()) + ->method('validate') + ->with($shipment, [ShipmentQuantityValidator::class]) + ->willReturn([]); + $this->saveAction->execute(); $this->assertEquals($this->response, $this->saveAction->getResponse()); } From bafc685f4cb536077a836a41d4db312705f0a7b6 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 15:58:53 +0300 Subject: [PATCH 077/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Controller/Adminhtml/Order/Shipment/Save.php | 16 ---------------- 1 file changed, 16 deletions(-) diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index 666ff569849b6..f20b98bca545c 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -200,20 +200,4 @@ private function getShipmentValidator() return $this->shipmentValidator; } - - /** - * @param $isNeedCreateLabel - * @param $responseAjax - * @param $message - */ - private function handleError($isNeedCreateLabel, $responseAjax, $message) - { - if ($isNeedCreateLabel) { - $responseAjax->setError(true); - $responseAjax->setMessage($message); - } else { - $this->messageManager->addError($message); - $this->_redirect('*/*/new', ['order_id' => $this->getRequest()->getParam('order_id')]); - } - } } From 737fc07740324d6f83cbce4186bad0c5a9da53e8 Mon Sep 17 00:00:00 2001 From: Oleksandr Radchenko Date: Wed, 17 Aug 2016 16:07:31 +0300 Subject: [PATCH 078/115] MAGETWO-54677: Fixed Unit tests --- .../Test/Unit/CustomAttributesMapperTest.php | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php b/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php index a39ad4afaa6ee..a766c28ba1193 100644 --- a/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php +++ b/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php @@ -48,12 +48,15 @@ public function testEntityToDatabase() $metadataPool = $this->getMockBuilder(\Magento\Framework\EntityManager\MetadataPool::class) ->disableOriginalConstructor() - ->setMethods(['getMetadata']) + ->setMethods(['getMetadata', 'hasConfiguration']) ->getMock(); $metadataPool->expects($this->any()) ->method('getMetadata') ->with($this->equalTo(\Magento\Customer\Api\Data\AddressInterface::class)) ->will($this->returnValue($metadata)); + $metadataPool->expects($this->once()) + ->method('hasConfiguration') + ->willReturn(true); $searchCriteriaBuilder = $this->getMockBuilder(\Magento\Framework\Api\SearchCriteriaBuilder::class) ->disableOriginalConstructor() @@ -76,6 +79,9 @@ public function testEntityToDatabase() 'metadataPool' => $metadataPool, 'searchCriteriaBuilder' => $searchCriteriaBuilder ]); + + + $actual = $customAttributesMapper->entityToDatabase( \Magento\Customer\Api\Data\AddressInterface::class, [ From ef6dcd571302fb6a6522e01bd36cb9a266d2503b Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 16:08:18 +0300 Subject: [PATCH 079/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Model/Order/ShipmentDocumentFactory.php | 44 ++++++++----------- .../Adminhtml/Order/Shipment/Save.php | 3 +- 2 files changed, 20 insertions(+), 27 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index d9b7b51a229be..89821696962a9 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -6,14 +6,6 @@ namespace Magento\Sales\Model\Order; -use Magento\Framework\EntityManager\HydratorPool; -use Magento\Sales\Api\Data\OrderInterface; -use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; -use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface; -use Magento\Sales\Api\Data\ShipmentItemCreationInterface; -use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; -use Magento\Sales\Model\Order\Shipment\TrackFactory; - /** * Class InvoiceDocumentFactory * @@ -22,31 +14,31 @@ class ShipmentDocumentFactory { /** - * @var ShipmentFactory + * @var \Magento\Framework\EntityManager\HydratorPool */ private $shipmentFactory; /** - * @var TrackFactory + * @var \Magento\Sales\Model\Order\Shipment\TrackFactory */ private $trackFactory; /** - * @var HydratorPool + * @var \Magento\Framework\EntityManager\HydratorPool */ private $hydratorPool; /** * ShipmentDocumentFactory constructor. * - * @param ShipmentFactory $shipmentFactory - * @param HydratorPool $hydratorPool - * @param TrackFactory $trackFactory + * @param \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory + * @param \Magento\Framework\EntityManager\HydratorPool $hydratorPool + * @param \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory */ public function __construct( - ShipmentFactory $shipmentFactory, - HydratorPool $hydratorPool, - TrackFactory $trackFactory + \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory, + \Magento\Framework\EntityManager\HydratorPool $hydratorPool, + \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory ) { $this->shipmentFactory = $shipmentFactory; $this->trackFactory = $trackFactory; @@ -54,24 +46,24 @@ public function __construct( } /** - * @param OrderInterface $order + * @param \Magento\Sales\Api\Data\OrderInterface $order * @param array $items * @param array $tracks - * @param ShipmentCommentCreationInterface|null $comment + * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment * @param bool $appendComment * @param array $packages - * @param ShipmentCreationArgumentsInterface $arguments + * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface $arguments * @return Shipment * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function create( - OrderInterface $order, + \Magento\Sales\Api\Data\OrderInterface $order, $items = [], $tracks = [], - ShipmentCommentCreationInterface $comment = null, + \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment = null, $appendComment = false, $packages = [], - ShipmentCreationArgumentsInterface $arguments = null + \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface $arguments = null ) { $shipmentItems = $this->itemsToArray($items); @@ -108,7 +100,9 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme __('Please enter a tracking number.') ); } - $hydrator = $this->hydratorPool->getHydrator(ShipmentTrackCreationInterface::class); + $hydrator = $this->hydratorPool->getHydrator( + \Magento\Sales\Api\Data\ShipmentTrackCreationInterface::class + ); $shipment->addTrack($this->trackFactory->create(['data' => $hydrator->extract($track)])); } return $shipment; @@ -117,7 +111,7 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme /** * Convert Items To Array * - * @param ShipmentItemCreationInterface[] $items + * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items * @return array */ private function itemsToArray($items = []) diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index f20b98bca545c..7ca7577754485 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -8,7 +8,6 @@ use Magento\Backend\App\Action; use Magento\Framework\App\ObjectManager; -use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; use Magento\Sales\Model\Order\ShipmentQuantityValidator; /** @@ -39,7 +38,7 @@ class Save extends \Magento\Backend\App\Action protected $shipmentSender; /** - * @var ShipmentValidatorInterface + * @var \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface */ private $shipmentValidator; From b8544a43bc20e120ba5d48f5663483b211388984 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 16:21:20 +0300 Subject: [PATCH 080/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Shipping/Controller/Adminhtml/Order/Shipment/Save.php | 1 + .../EntityManager/Test/Unit/CustomAttributesMapperTest.php | 2 -- 2 files changed, 1 insertion(+), 2 deletions(-) diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index 7ca7577754485..9be027576c28b 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -12,6 +12,7 @@ /** * Class Save + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class Save extends \Magento\Backend\App\Action { diff --git a/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php b/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php index a766c28ba1193..4c351efd9365e 100644 --- a/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php +++ b/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php @@ -80,8 +80,6 @@ public function testEntityToDatabase() 'searchCriteriaBuilder' => $searchCriteriaBuilder ]); - - $actual = $customAttributesMapper->entityToDatabase( \Magento\Customer\Api\Data\AddressInterface::class, [ From f3188efe084e3badf225f453a692f0aae9a640d9 Mon Sep 17 00:00:00 2001 From: Dmytro Voskoboinikov Date: Wed, 17 Aug 2016 17:02:25 +0300 Subject: [PATCH 081/115] MAGETWO-56497: Create API test --- .../Sales/Service/V1/ShipOrderTest.php | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/ShipOrderTest.php diff --git a/dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/ShipOrderTest.php b/dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/ShipOrderTest.php new file mode 100644 index 0000000000000..8de7c4dc7f65b --- /dev/null +++ b/dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/ShipOrderTest.php @@ -0,0 +1,100 @@ +objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + + $this->shipmentRepository = $this->objectManager->get( + \Magento\Sales\Api\ShipmentRepositoryInterface::class + ); + } + + /** + * @magentoApiDataFixture Magento/Sales/_files/order_new.php + */ + public function testShipOrder() + { + /** @var \Magento\Sales\Model\Order $existingOrder */ + $existingOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class) + ->loadByIncrementId('100000001'); + + $serviceInfo = [ + 'rest' => [ + 'resourcePath' => '/V1/order/' . $existingOrder->getId() . '/ship', + 'httpMethod' => \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST, + ], + 'soap' => [ + 'service' => self::SERVICE_READ_NAME, + 'serviceVersion' => self::SERVICE_VERSION, + 'operation' => self::SERVICE_READ_NAME . 'execute', + ], + ]; + + $requestData = [ + 'orderId' => $existingOrder->getId(), + 'items' => [], + 'comment' => [ + 'comment' => 'Test Comment', + 'is_visible_on_front' => 1, + ], + 'tracks' => [ + [ + 'track_number' => 'TEST_TRACK_0001', + 'title' => 'Simple shipment track', + 'carrier_code' => 'UPS' + ] + ] + ]; + + /** @var \Magento\Sales\Api\Data\OrderItemInterface $item */ + foreach ($existingOrder->getAllItems() as $item) { + $requestData['items'][] = [ + 'order_item_id' => $item->getItemId(), + 'qty' => $item->getQtyOrdered(), + ]; + } + + $result = $this->_webApiCall($serviceInfo, $requestData); + + $this->assertNotEmpty($result); + + try { + $this->shipmentRepository->get($result); + } catch (\Magento\Framework\Exception\NoSuchEntityException $e) { + $this->fail('Failed asserting that Shipment was created'); + } + + /** @var \Magento\Sales\Model\Order $updatedOrder */ + $updatedOrder = $this->objectManager->create(\Magento\Sales\Model\Order::class) + ->loadByIncrementId('100000001'); + + $this->assertNotEquals( + $existingOrder->getStatus(), + $updatedOrder->getStatus(), + 'Failed asserting that Order status was changed' + ); + } +} From 8e062f344c0d75a9a43d9a6bf6b1c54be213e7cd Mon Sep 17 00:00:00 2001 From: Nadiya Syvokonenko Date: Wed, 17 Aug 2016 17:05:13 +0300 Subject: [PATCH 082/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface - add unit test --- .../Model/Order/ShipmentDocumentFactory.php | 14 +- .../Sales/Test/Unit/Model/ShipOrderTest.php | 429 ++++++++++++++++++ 2 files changed, 436 insertions(+), 7 deletions(-) create mode 100644 app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 89821696962a9..baf6acbd27924 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -47,22 +47,22 @@ public function __construct( /** * @param \Magento\Sales\Api\Data\OrderInterface $order - * @param array $items - * @param array $tracks + * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items + * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment * @param bool $appendComment - * @param array $packages - * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface $arguments + * @param \Magento\Sales\Api\Data\ShipmentPackageInterface[] $packages + * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface|null $arguments * @return Shipment * @SuppressWarnings(PHPMD.UnusedFormalParameter) */ public function create( \Magento\Sales\Api\Data\OrderInterface $order, - $items = [], - $tracks = [], + array $items = [], + array $tracks = [], \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment = null, $appendComment = false, - $packages = [], + array $packages = [], \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface $arguments = null ) { diff --git a/app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php b/app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php new file mode 100644 index 0000000000000..43f8e16a66cf2 --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php @@ -0,0 +1,429 @@ +resourceConnectionMock = $this->getMockBuilder(ResourceConnection::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->orderRepositoryMock = $this->getMockBuilder(OrderRepositoryInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->shipmentDocumentFactoryMock = $this->getMockBuilder(ShipmentDocumentFactory::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->shipmentValidatorMock = $this->getMockBuilder(ShipmentValidatorInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->orderValidatorMock = $this->getMockBuilder(OrderValidatorInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->orderRegistrarMock = $this->getMockBuilder(OrderRegistrarInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->orderStateResolverMock = $this->getMockBuilder(OrderStateResolverInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->configMock = $this->getMockBuilder(OrderConfig::class) + ->disableOriginalConstructor() + ->getMock(); + + $this->shipmentRepositoryMock = $this->getMockBuilder(ShipmentRepositoryInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->notifierInterfaceMock = $this->getMockBuilder(NotifierInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->loggerMock = $this->getMockBuilder(LoggerInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->shipmentCommentCreationMock = $this->getMockBuilder(ShipmentCommentCreationInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->shipmentCreationArgumentsMock = $this->getMockBuilder(ShipmentCreationArgumentsInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->orderMock = $this->getMockBuilder(OrderInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->shipmentMock = $this->getMockBuilder(ShipmentInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->packageMock = $this->getMockBuilder(ShipmentPackageInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->trackMock = $this->getMockBuilder(ShipmentTrackCreationInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $this->adapterMock = $this->getMockBuilder(AdapterInterface::class) + ->disableOriginalConstructor() + ->getMockForAbstractClass(); + + $helper = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this); + + $this->model = $helper->getObject( + ShipOrder::class, + [ + 'resourceConnection' => $this->resourceConnectionMock, + 'orderRepository' => $this->orderRepositoryMock, + 'shipmentRepository' => $this->shipmentRepositoryMock, + 'shipmentDocumentFactory' => $this->shipmentDocumentFactoryMock, + 'shipmentValidator' => $this->shipmentValidatorMock, + 'orderValidator' => $this->orderValidatorMock, + 'orderStateResolver' => $this->orderStateResolverMock, + 'orderRegistrar' => $this->orderRegistrarMock, + 'notifierInterface' => $this->notifierInterfaceMock, + 'config' => $this->configMock, + 'logger' => $this->loggerMock + ] + ); + } + + /** + * @dataProvider dataProvider + */ + public function testExecute($orderId, $items, $notify, $appendComment) + { + $this->resourceConnectionMock->expects($this->once()) + ->method('getConnection') + ->with('sales') + ->willReturn($this->adapterMock); + + $this->orderRepositoryMock->expects($this->once()) + ->method('get') + ->willReturn($this->orderMock); + + $this->shipmentDocumentFactoryMock->expects($this->once()) + ->method('create') + ->with( + $this->orderMock, + $items, + [$this->trackMock], + $this->shipmentCommentCreationMock, + ($appendComment && $notify), + [$this->packageMock], + $this->shipmentCreationArgumentsMock + )->willReturn($this->shipmentMock); + + $this->shipmentValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->shipmentMock) + ->willReturn([]); + $this->orderValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->orderMock) + ->willReturn([]); + + $this->orderRegistrarMock->expects($this->once()) + ->method('register') + ->with($this->orderMock, $this->shipmentMock) + ->willReturn($this->orderMock); + + $this->orderStateResolverMock->expects($this->once()) + ->method('getStateForOrder') + ->with($this->orderMock, [OrderStateResolverInterface::IN_PROGRESS]) + ->willReturn(Order::STATE_PROCESSING); + + $this->orderMock->expects($this->once()) + ->method('setState') + ->with(Order::STATE_PROCESSING) + ->willReturnSelf(); + + $this->orderMock->expects($this->once()) + ->method('getState') + ->willReturn(Order::STATE_PROCESSING); + + $this->configMock->expects($this->once()) + ->method('getStateDefaultStatus') + ->with(Order::STATE_PROCESSING) + ->willReturn('Processing'); + + $this->orderMock->expects($this->once()) + ->method('setStatus') + ->with('Processing') + ->willReturnSelf(); + + $this->shipmentRepositoryMock->expects($this->once()) + ->method('save') + ->with($this->shipmentMock) + ->willReturn($this->shipmentMock); + + $this->orderRepositoryMock->expects($this->once()) + ->method('save') + ->with($this->orderMock) + ->willReturn($this->orderMock); + + if ($notify) { + $this->notifierInterfaceMock->expects($this->once()) + ->method('notify') + ->with($this->orderMock, $this->shipmentMock, $this->shipmentCommentCreationMock); + } + + $this->shipmentMock->expects($this->once()) + ->method('getEntityId') + ->willReturn(2); + + $this->assertEquals( + 2, + $this->model->execute( + $orderId, + $items, + $notify, + $appendComment, + $this->shipmentCommentCreationMock, + [$this->trackMock], + [$this->packageMock], + $this->shipmentCreationArgumentsMock + ) + ); + } + + /** + * @expectedException \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface + */ + public function testDocumentValidationException() + { + $orderId = 1; + $items = [1 => 2]; + $notify = true; + $appendComment = true; + $errorMessages = ['error1', 'error2']; + + $this->orderRepositoryMock->expects($this->once()) + ->method('get') + ->willReturn($this->orderMock); + + $this->shipmentDocumentFactoryMock->expects($this->once()) + ->method('create') + ->with( + $this->orderMock, + $items, + [$this->trackMock], + $this->shipmentCommentCreationMock, + ($appendComment && $notify), + [$this->packageMock], + $this->shipmentCreationArgumentsMock + )->willReturn($this->shipmentMock); + + $this->shipmentValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->shipmentMock) + ->willReturn($errorMessages); + $this->orderValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->orderMock) + ->willReturn([]); + + $this->model->execute( + $orderId, + $items, + $notify, + $appendComment, + $this->shipmentCommentCreationMock, + [$this->trackMock], + [$this->packageMock], + $this->shipmentCreationArgumentsMock + ); + } + + /** + * @expectedException \Magento\Sales\Api\Exception\CouldNotShipExceptionInterface + */ + public function testCouldNotInvoiceException() + { + $orderId = 1; + $this->resourceConnectionMock->expects($this->once()) + ->method('getConnection') + ->with('sales') + ->willReturn($this->adapterMock); + + $this->orderRepositoryMock->expects($this->once()) + ->method('get') + ->willReturn($this->orderMock); + + $this->shipmentDocumentFactoryMock->expects($this->once()) + ->method('create') + ->with( + $this->orderMock + )->willReturn($this->shipmentMock); + + $this->shipmentValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->shipmentMock) + ->willReturn([]); + $this->orderValidatorMock->expects($this->once()) + ->method('validate') + ->with($this->orderMock) + ->willReturn([]); + $e = new \Exception(); + + $this->orderRegistrarMock->expects($this->once()) + ->method('register') + ->with($this->orderMock, $this->shipmentMock) + ->willThrowException($e); + + $this->loggerMock->expects($this->once()) + ->method('critical') + ->with($e); + + $this->adapterMock->expects($this->once()) + ->method('rollBack'); + + $this->model->execute( + $orderId + ); + } + + /** + * @return array + */ + public function dataProvider() + { + return [ + 'TestWithNotifyTrue' => [1, [1 => 2], true, true], + 'TestWithNotifyFalse' => [1, [1 => 2], false, true], + ]; + } +} From 61fa5f048464ceac285ee43b17ecf1a6a3d2ea76 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 19:07:13 +0300 Subject: [PATCH 083/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/ShipmentFactory.php | 20 +++++++++++++++++-- .../Model/Order/ShipmentQuantityValidator.php | 10 ++-------- 2 files changed, 20 insertions(+), 10 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php index db0d8a024190b..a8839c7537587 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentFactory.php @@ -108,7 +108,7 @@ protected function prepareItems( $qty = $bundleSelectionAttributes['qty'] * $items[$orderItem->getParentItemId()]; $qty = min($qty, $orderItem->getSimpleQtyToShip()); - $item->setQty($qty); + $item->setQty($this->castQty($orderItem, $qty)); $shipment->addItem($item); continue; @@ -131,7 +131,7 @@ protected function prepareItems( $totalQty += $qty; - $item->setQty($qty); + $item->setQty($this->castQty($orderItem, $qty)); $shipment->addItem($item); } return $shipment->setTotalQty($totalQty); @@ -215,4 +215,20 @@ protected function canShipItem($item, array $items = []) return $item->getQtyToShip() > 0; } } + + /** + * @param Item $item + * @param string|int|float $qty + * @return float|int + */ + private function castQty(\Magento\Sales\Model\Order\Item $item, $qty) + { + if ($item->getIsQtyDecimal()) { + $qty = (double)$qty; + } else { + $qty = (int)$qty; + } + + return $qty > 0 ? $qty : 0; + } } diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index e5b7d5d52b69e..58870793a878d 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -70,14 +70,8 @@ public function validateShipmentItem(ShipmentItemInterface $item, OrderInterface if ($orderItem === null) { return [__('We can not found item "%1" in order.', $item->getOrderItemId())]; } - if ($orderItem->getIsQtyDecimal()) { - $qty = (double)$item->getQty(); - } else { - $qty = (int)$item->getQty(); - } - $qty = $qty > 0 ? $qty : 0; - if (!$this->isQtyAvailable($orderItem, $qty)) { + if (!$this->isQtyAvailable($orderItem, $item->getQty())) { $messages[] =__('We found an invalid quantity to ship for item "%1".', $item->getName()); } @@ -107,6 +101,6 @@ private function getOrderItemById(OrderInterface $order, $id) */ private function isQtyAvailable(Item $orderItem, $qty) { - return $qty !== 0 && ($qty <= $orderItem->getQtyToShip() || $orderItem->isDummy(true)); + return $qty <= $orderItem->getQtyToShip() || $orderItem->isDummy(true); } } From 9700f147f32bb123cdb639d4f7230e2b80f83849 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 19:24:26 +0300 Subject: [PATCH 084/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Model/Order/ShipmentQuantityValidator.php | 19 ++++++++++++++++--- 1 file changed, 16 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index 58870793a878d..efaf861ae03d4 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -51,10 +51,23 @@ public function validate($entity) $messages = []; $order = $this->orderRepository->get($entity->getOrderId()); - foreach ($entity->getItems() as $shipmentItem) { - $messages = array_merge($messages, $this->validateShipmentItem($shipmentItem, $order)); - } + $totalQuantity = 0; + foreach ($entity->getItems() as $item) { + $orderItem = $this->getOrderItemById($order, $item->getOrderItemId()); + if ($orderItem === null) { + $messages[] = __('We can not found item "%1" in order.', $item->getOrderItemId()); + continue; + } + if (!$this->isQtyAvailable($orderItem, $item->getQty())) { + $messages[] =__('We found an invalid quantity to ship for item "%1".', $item->getName()); + } else { + $totalQuantity += $item->getQty(); + } + } + if ($totalQuantity <= 0) { + $messages[] = __('You can\'t create a shipment without products.'); + } return $messages; } From 1ec50f1f03f89f87e670c6189e7261ef3d79647f Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Wed, 17 Aug 2016 19:25:02 +0300 Subject: [PATCH 085/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Model/Order/ShipmentQuantityValidator.php | 20 ------------------- 1 file changed, 20 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index efaf861ae03d4..bce62402ba35b 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -71,26 +71,6 @@ public function validate($entity) return $messages; } - /** - * @param ShipmentItemInterface $item - * @param OrderInterface $order - * @return string[] - */ - public function validateShipmentItem(ShipmentItemInterface $item, OrderInterface $order) - { - $messages = []; - $orderItem = $this->getOrderItemById($order, $item->getOrderItemId()); - if ($orderItem === null) { - return [__('We can not found item "%1" in order.', $item->getOrderItemId())]; - } - - if (!$this->isQtyAvailable($orderItem, $item->getQty())) { - $messages[] =__('We found an invalid quantity to ship for item "%1".', $item->getName()); - } - - return $messages; - } - /** * @param OrderInterface $order * @param int $id From e3dc161b37a26391efcf14317676e6f2a1669b99 Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Thu, 18 Aug 2016 11:16:54 +0300 Subject: [PATCH 086/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php b/app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php index 43f8e16a66cf2..b719babf209f0 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/ShipOrderTest.php @@ -29,6 +29,7 @@ /** * Class ShipOrderTest * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + * @SuppressWarnings(PHPMD.TooManyFields) */ class ShipOrderTest extends \PHPUnit_Framework_TestCase { From 2c3f4b26108c53a5f54ddc0f84bdf117592eee20 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 18 Aug 2016 14:54:19 +0300 Subject: [PATCH 087/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Data/ShipmentPackageCreationInterface.php | 33 ++++++++++ .../Magento/Sales/Api/ShipOrderInterface.php | 2 +- .../Model/Order/Shipment/PackageCreation.php | 36 +++++++++++ .../Validation/QuantityValidator.php} | 7 +- .../Shipment/Validation/TrackValidator.php | 30 +++++++++ .../Model/Order/ShipmentDocumentFactory.php | 64 ++++++++++--------- app/code/Magento/Sales/Model/ShipOrder.php | 31 +++++---- app/code/Magento/Sales/etc/di.xml | 1 + 8 files changed, 156 insertions(+), 48 deletions(-) create mode 100644 app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/PackageCreation.php rename app/code/Magento/Sales/Model/Order/{ShipmentQuantityValidator.php => Shipment/Validation/QuantityValidator.php} (93%) create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php diff --git a/app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php new file mode 100644 index 0000000000000..c83c54b3019be --- /dev/null +++ b/app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php @@ -0,0 +1,33 @@ +extensionAttributes; + } + + /** + * {@inheritdoc} + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\ShipmentPackageCreationExtensionInterface $extensionAttributes + ) { + $this->extensionAttributes = $extensionAttributes; + return $this; + } +} diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php similarity index 93% rename from app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php rename to app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php index bce62402ba35b..e0db63cd7c420 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php @@ -3,20 +3,19 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Sales\Model\Order; +namespace Magento\Sales\Model\Order\Shipment\Validation; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\ShipmentInterface; -use Magento\Sales\Api\Data\ShipmentItemInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Exception\DocumentValidationException; use Magento\Sales\Model\ValidatorInterface; /** - * Class ShipmentQuantityValidator + * Class QuantityValidator */ -class ShipmentQuantityValidator implements ValidatorInterface +class QuantityValidator implements ValidatorInterface { /** * @var OrderRepositoryInterface diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php new file mode 100644 index 0000000000000..01efae2f1e69d --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php @@ -0,0 +1,30 @@ +getTracks() as $track) { + if (!$track->getTrackNumber()) { + $messages[] = __('Please enter a tracking number.'); + } + } + return $messages; + } +} diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index baf6acbd27924..0ca549b9e5080 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -6,6 +6,15 @@ namespace Magento\Sales\Model\Order; +use Magento\Sales\Api\Data\ShipmentInterface; +use Magento\Sales\Api\Data\ShipmentItemCreationInterface; +use Magento\Sales\Api\Data\ShipmentPackageCreationInterface; +use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; +use Magento\Framework\EntityManager\HydratorPool; +use Magento\Sales\Model\Order\Shipment\TrackFactory; +use Magento\Sales\Api\Data\OrderInterface; +use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; +use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface; /** * Class InvoiceDocumentFactory * @@ -14,31 +23,31 @@ class ShipmentDocumentFactory { /** - * @var \Magento\Framework\EntityManager\HydratorPool + * @var ShipmentFactory */ private $shipmentFactory; /** - * @var \Magento\Sales\Model\Order\Shipment\TrackFactory + * @var TrackFactory */ private $trackFactory; /** - * @var \Magento\Framework\EntityManager\HydratorPool + * @var HydratorPool */ private $hydratorPool; /** * ShipmentDocumentFactory constructor. * - * @param \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory - * @param \Magento\Framework\EntityManager\HydratorPool $hydratorPool - * @param \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory + * @param ShipmentFactory $shipmentFactory + * @param HydratorPool $hydratorPool + * @param TrackFactory $trackFactory */ public function __construct( - \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory, - \Magento\Framework\EntityManager\HydratorPool $hydratorPool, - \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory + ShipmentFactory $shipmentFactory, + HydratorPool $hydratorPool, + TrackFactory $trackFactory ) { $this->shipmentFactory = $shipmentFactory; $this->trackFactory = $trackFactory; @@ -46,24 +55,25 @@ public function __construct( } /** - * @param \Magento\Sales\Api\Data\OrderInterface $order - * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items - * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks - * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment - * @param bool $appendComment - * @param \Magento\Sales\Api\Data\ShipmentPackageInterface[] $packages - * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface|null $arguments - * @return Shipment * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * + * @param OrderInterface $order + * @param ShipmentItemCreationInterface[] $items + * @param ShipmentTrackCreationInterface[] $tracks + * @param ShipmentCommentCreationInterface|null $comment + * @param bool $appendComment + * @param ShipmentPackageCreationInterface[] $packages + * @param ShipmentCreationArgumentsInterface|null $arguments + * @return ShipmentInterface */ public function create( - \Magento\Sales\Api\Data\OrderInterface $order, + OrderInterface $order, array $items = [], array $tracks = [], - \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment = null, + ShipmentCommentCreationInterface $comment = null, $appendComment = false, array $packages = [], - \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface $arguments = null + ShipmentCreationArgumentsInterface $arguments = null ) { $shipmentItems = $this->itemsToArray($items); @@ -87,19 +97,13 @@ public function create( /** * Adds tracks to the shipment. * - * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment - * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks - * @throws \Magento\Framework\Exception\LocalizedException - * @return \Magento\Sales\Api\Data\ShipmentInterface + * @param ShipmentInterface $shipment + * @param ShipmentTrackCreationInterface[] $tracks + * @return ShipmentInterface */ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipment, array $tracks) { foreach ($tracks as $track) { - if (!$track->getTrackNumber()) { - throw new \Magento\Framework\Exception\LocalizedException( - __('Please enter a tracking number.') - ); - } $hydrator = $this->hydratorPool->getHydrator( \Magento\Sales\Api\Data\ShipmentTrackCreationInterface::class ); @@ -111,7 +115,7 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme /** * Convert Items To Array * - * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items + * @param ShipmentItemCreationInterface[] $items * @return array */ private function itemsToArray($items = []) diff --git a/app/code/Magento/Sales/Model/ShipOrder.php b/app/code/Magento/Sales/Model/ShipOrder.php index b5498b1085b52..e7fd7707a125d 100644 --- a/app/code/Magento/Sales/Model/ShipOrder.php +++ b/app/code/Magento/Sales/Model/ShipOrder.php @@ -15,7 +15,8 @@ use Magento\Sales\Model\Order\ShipmentDocumentFactory; use Magento\Sales\Model\Order\Shipment\NotifierInterface; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; -use Magento\Sales\Model\Order\ShipmentQuantityValidator; +use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator; +use Magento\Sales\Model\Order\Shipment\Validation\TrackValidator; use Magento\Sales\Model\Order\Validation\CanShip; use Magento\Sales\Model\Order\Shipment\OrderRegistrarInterface; use Psr\Log\LoggerInterface; @@ -128,7 +129,7 @@ public function __construct( * @param bool $appendComment * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks - * @param \Magento\Sales\Api\Data\ShipmentPackageInterface[] $packages + * @param \Magento\Sales\Api\Data\ShipmentPackageCreationInterface[] $packages * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface|null $arguments * @return int * @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface @@ -158,19 +159,23 @@ public function execute( $packages, $arguments ); - $errorMessages = array_merge( - $this->orderValidator->validate( - $order, - [CanShip::class] - ), - $this->shipmentValidator->validate( - $shipment, - [ShipmentQuantityValidator::class] - ) + $orderValidationResult = $this->orderValidator->validate( + $order, + [ + CanShip::class + ] + ); + $invoiceValidationResult = $this->shipmentValidator->validate( + $shipment, + [ + QuantityValidator::class, + TrackValidator::class + ] ); - if (!empty($errorMessages)) { + $validationMessages = array_merge($orderValidationResult, $invoiceValidationResult); + if (!empty($validationMessages)) { throw new \Magento\Sales\Exception\DocumentValidationException( - __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) + __("Shipment Document Validation Error(s):\n" . implode("\n", $validationMessages)) ); } $connection->beginTransaction(); diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 10e404631e969..802445bb70b96 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -96,6 +96,7 @@ + From 05b64184766004a13dae7c3d1d19fdd4545c50ba Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 18 Aug 2016 14:54:19 +0300 Subject: [PATCH 088/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Data/ShipmentPackageCreationInterface.php | 33 ++++++++++ .../Magento/Sales/Api/ShipOrderInterface.php | 2 +- .../Model/Order/Shipment/PackageCreation.php | 36 +++++++++++ .../Validation/QuantityValidator.php} | 7 +- .../Shipment/Validation/TrackValidator.php | 30 +++++++++ .../Model/Order/ShipmentDocumentFactory.php | 64 ++++++++++--------- app/code/Magento/Sales/Model/ShipOrder.php | 31 +++++---- app/code/Magento/Sales/etc/di.xml | 1 + 8 files changed, 156 insertions(+), 48 deletions(-) create mode 100644 app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/PackageCreation.php rename app/code/Magento/Sales/Model/Order/{ShipmentQuantityValidator.php => Shipment/Validation/QuantityValidator.php} (93%) create mode 100644 app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php diff --git a/app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php b/app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php new file mode 100644 index 0000000000000..c83c54b3019be --- /dev/null +++ b/app/code/Magento/Sales/Api/Data/ShipmentPackageCreationInterface.php @@ -0,0 +1,33 @@ +extensionAttributes; + } + + /** + * {@inheritdoc} + */ + public function setExtensionAttributes( + \Magento\Sales\Api\Data\ShipmentPackageCreationExtensionInterface $extensionAttributes + ) { + $this->extensionAttributes = $extensionAttributes; + return $this; + } +} diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php similarity index 93% rename from app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php rename to app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php index bce62402ba35b..e0db63cd7c420 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php @@ -3,20 +3,19 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ -namespace Magento\Sales\Model\Order; +namespace Magento\Sales\Model\Order\Shipment\Validation; use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\ShipmentInterface; -use Magento\Sales\Api\Data\ShipmentItemInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Exception\DocumentValidationException; use Magento\Sales\Model\ValidatorInterface; /** - * Class ShipmentQuantityValidator + * Class QuantityValidator */ -class ShipmentQuantityValidator implements ValidatorInterface +class QuantityValidator implements ValidatorInterface { /** * @var OrderRepositoryInterface diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php new file mode 100644 index 0000000000000..01efae2f1e69d --- /dev/null +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php @@ -0,0 +1,30 @@ +getTracks() as $track) { + if (!$track->getTrackNumber()) { + $messages[] = __('Please enter a tracking number.'); + } + } + return $messages; + } +} diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index baf6acbd27924..0ca549b9e5080 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -6,6 +6,15 @@ namespace Magento\Sales\Model\Order; +use Magento\Sales\Api\Data\ShipmentInterface; +use Magento\Sales\Api\Data\ShipmentItemCreationInterface; +use Magento\Sales\Api\Data\ShipmentPackageCreationInterface; +use Magento\Sales\Api\Data\ShipmentTrackCreationInterface; +use Magento\Framework\EntityManager\HydratorPool; +use Magento\Sales\Model\Order\Shipment\TrackFactory; +use Magento\Sales\Api\Data\OrderInterface; +use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; +use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface; /** * Class InvoiceDocumentFactory * @@ -14,31 +23,31 @@ class ShipmentDocumentFactory { /** - * @var \Magento\Framework\EntityManager\HydratorPool + * @var ShipmentFactory */ private $shipmentFactory; /** - * @var \Magento\Sales\Model\Order\Shipment\TrackFactory + * @var TrackFactory */ private $trackFactory; /** - * @var \Magento\Framework\EntityManager\HydratorPool + * @var HydratorPool */ private $hydratorPool; /** * ShipmentDocumentFactory constructor. * - * @param \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory - * @param \Magento\Framework\EntityManager\HydratorPool $hydratorPool - * @param \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory + * @param ShipmentFactory $shipmentFactory + * @param HydratorPool $hydratorPool + * @param TrackFactory $trackFactory */ public function __construct( - \Magento\Sales\Model\Order\ShipmentFactory $shipmentFactory, - \Magento\Framework\EntityManager\HydratorPool $hydratorPool, - \Magento\Sales\Model\Order\Shipment\TrackFactory $trackFactory + ShipmentFactory $shipmentFactory, + HydratorPool $hydratorPool, + TrackFactory $trackFactory ) { $this->shipmentFactory = $shipmentFactory; $this->trackFactory = $trackFactory; @@ -46,24 +55,25 @@ public function __construct( } /** - * @param \Magento\Sales\Api\Data\OrderInterface $order - * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items - * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks - * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment - * @param bool $appendComment - * @param \Magento\Sales\Api\Data\ShipmentPackageInterface[] $packages - * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface|null $arguments - * @return Shipment * @SuppressWarnings(PHPMD.UnusedFormalParameter) + * + * @param OrderInterface $order + * @param ShipmentItemCreationInterface[] $items + * @param ShipmentTrackCreationInterface[] $tracks + * @param ShipmentCommentCreationInterface|null $comment + * @param bool $appendComment + * @param ShipmentPackageCreationInterface[] $packages + * @param ShipmentCreationArgumentsInterface|null $arguments + * @return ShipmentInterface */ public function create( - \Magento\Sales\Api\Data\OrderInterface $order, + OrderInterface $order, array $items = [], array $tracks = [], - \Magento\Sales\Api\Data\ShipmentCommentCreationInterface $comment = null, + ShipmentCommentCreationInterface $comment = null, $appendComment = false, array $packages = [], - \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface $arguments = null + ShipmentCreationArgumentsInterface $arguments = null ) { $shipmentItems = $this->itemsToArray($items); @@ -87,19 +97,13 @@ public function create( /** * Adds tracks to the shipment. * - * @param \Magento\Sales\Api\Data\ShipmentInterface $shipment - * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks - * @throws \Magento\Framework\Exception\LocalizedException - * @return \Magento\Sales\Api\Data\ShipmentInterface + * @param ShipmentInterface $shipment + * @param ShipmentTrackCreationInterface[] $tracks + * @return ShipmentInterface */ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipment, array $tracks) { foreach ($tracks as $track) { - if (!$track->getTrackNumber()) { - throw new \Magento\Framework\Exception\LocalizedException( - __('Please enter a tracking number.') - ); - } $hydrator = $this->hydratorPool->getHydrator( \Magento\Sales\Api\Data\ShipmentTrackCreationInterface::class ); @@ -111,7 +115,7 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme /** * Convert Items To Array * - * @param \Magento\Sales\Api\Data\ShipmentItemCreationInterface[] $items + * @param ShipmentItemCreationInterface[] $items * @return array */ private function itemsToArray($items = []) diff --git a/app/code/Magento/Sales/Model/ShipOrder.php b/app/code/Magento/Sales/Model/ShipOrder.php index b5498b1085b52..e7fd7707a125d 100644 --- a/app/code/Magento/Sales/Model/ShipOrder.php +++ b/app/code/Magento/Sales/Model/ShipOrder.php @@ -15,7 +15,8 @@ use Magento\Sales\Model\Order\ShipmentDocumentFactory; use Magento\Sales\Model\Order\Shipment\NotifierInterface; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; -use Magento\Sales\Model\Order\ShipmentQuantityValidator; +use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator; +use Magento\Sales\Model\Order\Shipment\Validation\TrackValidator; use Magento\Sales\Model\Order\Validation\CanShip; use Magento\Sales\Model\Order\Shipment\OrderRegistrarInterface; use Psr\Log\LoggerInterface; @@ -128,7 +129,7 @@ public function __construct( * @param bool $appendComment * @param \Magento\Sales\Api\Data\ShipmentCommentCreationInterface|null $comment * @param \Magento\Sales\Api\Data\ShipmentTrackCreationInterface[] $tracks - * @param \Magento\Sales\Api\Data\ShipmentPackageInterface[] $packages + * @param \Magento\Sales\Api\Data\ShipmentPackageCreationInterface[] $packages * @param \Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface|null $arguments * @return int * @throws \Magento\Sales\Api\Exception\DocumentValidationExceptionInterface @@ -158,19 +159,23 @@ public function execute( $packages, $arguments ); - $errorMessages = array_merge( - $this->orderValidator->validate( - $order, - [CanShip::class] - ), - $this->shipmentValidator->validate( - $shipment, - [ShipmentQuantityValidator::class] - ) + $orderValidationResult = $this->orderValidator->validate( + $order, + [ + CanShip::class + ] + ); + $invoiceValidationResult = $this->shipmentValidator->validate( + $shipment, + [ + QuantityValidator::class, + TrackValidator::class + ] ); - if (!empty($errorMessages)) { + $validationMessages = array_merge($orderValidationResult, $invoiceValidationResult); + if (!empty($validationMessages)) { throw new \Magento\Sales\Exception\DocumentValidationException( - __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) + __("Shipment Document Validation Error(s):\n" . implode("\n", $validationMessages)) ); } $connection->beginTransaction(); diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 10e404631e969..802445bb70b96 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -96,6 +96,7 @@ + From 35c38bf74644d08fc6148cb82da7b68fa9322acc Mon Sep 17 00:00:00 2001 From: Dmytro Voskoboinikov Date: Thu, 18 Aug 2016 15:25:42 +0300 Subject: [PATCH 089/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Shipping/Controller/Adminhtml/Order/ShipmentLoader.php | 4 ---- 1 file changed, 4 deletions(-) diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php index c1ad965393777..c4efe6f6507d5 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/ShipmentLoader.php @@ -1,15 +1,11 @@ Date: Thu, 18 Aug 2016 15:41:40 +0300 Subject: [PATCH 090/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php | 1 - .../Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php | 5 ++--- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php index b01a0af5e50ce..8d800e12a6ff0 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/InvoiceQuantityValidatorTest.php @@ -45,7 +45,6 @@ protected function setUp() $this->orderMock = $this->getMockBuilder(\Magento\Sales\Api\Data\OrderInterface::class) ->disableOriginalConstructor() - ->setMethods(['getStatus']) ->getMockForAbstractClass(); $this->invoiceMock = $this->getMockBuilder(\Magento\Sales\Api\Data\InvoiceInterface::class) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php index 93f6b7408d619..cd8481f75a0d4 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php @@ -63,12 +63,12 @@ class ShipmentDocumentFactoryTest extends \PHPUnit_Framework_TestCase private $trackFactoryMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject|TrackFactory + * @var \PHPUnit_Framework_MockObject_MockObject|HydratorInterface */ private $hydratorMock; /** - * @var \PHPUnit_Framework_MockObject_MockObject|HydratorInterface + * @var \PHPUnit_Framework_MockObject_MockObject|Track */ private $trackMock; @@ -110,7 +110,6 @@ protected function setUp() $this->hydratorMock = $this->getMockBuilder(HydratorInterface::class) ->disableOriginalConstructor() - ->setMethods(['extract']) ->getMockForAbstractClass(); $this->invoiceDocumentFactory = new ShipmentDocumentFactory( From 028496bdaa18d33512ac002540f2d7f24dd80f91 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 18 Aug 2016 15:44:54 +0300 Subject: [PATCH 091/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Validation/QuantityValidatorTest.php | 61 +++++++++++++++++++ .../Validation/TrackValidatorTest.php | 61 +++++++++++++++++++ .../Order/ShipmentDocumentFactoryTest.php | 4 -- 3 files changed, 122 insertions(+), 4 deletions(-) create mode 100644 app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php create mode 100644 app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php new file mode 100644 index 0000000000000..3d59b1d293640 --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php @@ -0,0 +1,61 @@ +shipmentMock = $this->getMockBuilder(ShipmentInterface::class) + ->getMock(); + $this->shipmentItemMock = $this->getMockBuilder(ShipmentItemInterface::class) + ->getMock(); + $this->validator = $objectManagerHelper->getObject(QuantityValidator::class); + } + + public function testValidateTrackWithoutOrderId() + { + $this->shipmentMock->expects($this->once()) + ->method('getOrderId') + ->willReturn(null); + $this->assertEquals([__('Order Id is required for shipment document')], $this->validator->validate($this->shipmentMock)); + } + + public function testValidateTrackWithoutItems() + { + $this->shipmentMock->expects($this->once()) + ->method('getOrderId') + ->willReturn(1); + $this->shipmentMock->expects($this->once()) + ->method('getItems') + ->willReturn(null); + $this->assertEquals([__('You can\'t create a shipment without products.')], $this->validator->validate($this->shipmentMock)); + } +} diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php new file mode 100644 index 0000000000000..69e88bb45306c --- /dev/null +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php @@ -0,0 +1,61 @@ +shipmentMock = $this->getMockBuilder(ShipmentInterface::class) + ->getMock(); + $this->shipmentTrackMock = $this->getMockBuilder(ShipmentTrackInterface::class) + ->getMock(); + $this->shipmentMock->expects($this->once()) + ->method('getTracks') + ->willReturn([$this->shipmentTrackMock]); + $this->validator = $objectManagerHelper->getObject(TrackValidator::class); + } + + public function testValidateTrackWithNumber() + { + $this->shipmentTrackMock->expects($this->once()) + ->method('getTrackNumber') + ->willReturn('12345'); + $this->assertEquals([], $this->validator->validate($this->shipmentMock)); + } + + public function testValidateTrackWithoutNumber() + { + $this->shipmentTrackMock->expects($this->once()) + ->method('getTrackNumber') + ->willReturn(null); + $this->assertEquals([__('Please enter a tracking number.')], $this->validator->validate($this->shipmentMock)); + } +} \ No newline at end of file diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php index 93f6b7408d619..75bc46e5aa3f1 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php @@ -145,10 +145,6 @@ public function testCreate() ) ->willReturn($this->shipmentMock); - $this->trackMock->expects($this->once()) - ->method('getTrackNumber') - ->willReturn($trackNum); - $this->shipmentMock->expects($this->once()) ->method('addTrack') ->willReturnSelf(); From 580d24cc7ba966c5c2c8a4105b4e2d59df6b6d91 Mon Sep 17 00:00:00 2001 From: Dmytro Voskoboinikov Date: Thu, 18 Aug 2016 15:50:49 +0300 Subject: [PATCH 092/115] MAGETWO-54677: Order status change after Shipment creation through API --- app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php index f36889353afad..6e8ce097f411d 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/TrackCreation.php @@ -32,6 +32,8 @@ class TrackCreation implements ShipmentTrackCreationInterface */ private $extensionAttributes; + //@codeCoverageIgnoreStart + /** * {@inheritdoc} */ @@ -100,4 +102,6 @@ public function setExtensionAttributes( $this->extensionAttributes = $extensionAttributes; return $this; } + + //@codeCoverageIgnoreEnd } From 226a61f80e85d41d11db3f68ad83518e5a02065c Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 15:53:12 +0300 Subject: [PATCH 093/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Model/Order/ShipmentQuantityValidator.php | 33 ++++++++++++------- .../Model/Order/Validation/CanInvoice.php | 21 ++++++++---- .../Sales/Model/Order/Validation/CanShip.php | 17 +++++++--- .../Model/Order/Validation/CanInvoiceTest.php | 6 +++- .../Model/Order/Validation/CanShipTest.php | 6 +++- 5 files changed, 59 insertions(+), 24 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php index bce62402ba35b..fa7a60e8a6bba 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentQuantityValidator.php @@ -7,6 +7,7 @@ use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\OrderInterface; +use Magento\Sales\Api\Data\OrderItemInterface; use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\Data\ShipmentItemInterface; use Magento\Sales\Api\OrderRepositoryInterface; @@ -45,22 +46,31 @@ public function validate($entity) return [__('Order Id is required for shipment document')]; } - if ($entity->getItems() === null) { + if (empty($entity->getItems())) { return [__('You can\'t create a shipment without products.')]; } $messages = []; $order = $this->orderRepository->get($entity->getOrderId()); + $orderItemsById = $this->getOrderItems($order); + $totalQuantity = 0; foreach ($entity->getItems() as $item) { - $orderItem = $this->getOrderItemById($order, $item->getOrderItemId()); - if ($orderItem === null) { - $messages[] = __('We can not found item "%1" in order.', $item->getOrderItemId()); + if (!isset($orderItemsById[$item->getOrderItemId()])) { + $messages[] = __( + 'The shipment contains product SKU "%1" that is not part of the original order.', + $item->getSku() + ); continue; } + $orderItem = $orderItemsById[$item->getOrderItemId()]; if (!$this->isQtyAvailable($orderItem, $item->getQty())) { - $messages[] =__('We found an invalid quantity to ship for item "%1".', $item->getName()); + $messages[] =__( + 'The quantity to ship must not be greater than the unshipped quantity' + . ' for product SKU "%1".', + $orderItem->getSku() + ); } else { $totalQuantity += $item->getQty(); } @@ -68,23 +78,22 @@ public function validate($entity) if ($totalQuantity <= 0) { $messages[] = __('You can\'t create a shipment without products.'); } + return $messages; } /** * @param OrderInterface $order - * @param int $id - * @return \Magento\Sales\Api\Data\OrderItemInterface|null + * @return OrderItemInterface[] */ - private function getOrderItemById(OrderInterface $order, $id) + private function getOrderItems(OrderInterface $order) { + $orderItemsById = []; foreach ($order->getItems() as $item) { - if ($item->getItemId() === $id) { - return $item; - } + $orderItemsById[$item->getItemId()] = $item; } - return null; + return $orderItemsById; } /** diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php index 106882418ab9f..c0f2771561e25 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php @@ -21,18 +21,17 @@ class CanInvoice implements ValidatorInterface public function validate($entity) { $messages = []; - if (!$this->canInvoice($entity)) { + + if (!$this->isStateReadyForInvoice($entity)) { + $messages[] = __('An invoice cannot be created when an order has a status of %1', $entity->getStatus()); + } elseif (!$this->canInvoice($entity)) { $messages[] = __('The order does not allow an invoice to be created.'); } return $messages; } - /** - * @param OrderInterface $order - * @return bool - */ - private function canInvoice(OrderInterface $order) + private function isStateReadyForInvoice(OrderInterface $order) { if ($order->getState() === Order::STATE_PAYMENT_REVIEW || $order->getState() === Order::STATE_HOLDED || @@ -42,6 +41,16 @@ private function canInvoice(OrderInterface $order) ) { return false; }; + + return true; + } + + /** + * @param OrderInterface $order + * @return bool + */ + private function canInvoice(OrderInterface $order) + { /** @var \Magento\Sales\Model\Order\Item $item */ foreach ($order->getItems() as $item) { if ($item->getQtyToInvoice() > 0 && !$item->getLockedDoInvoice()) { diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php index cebdc4b0afe28..a2e4da6eee13f 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php @@ -5,9 +5,7 @@ */ namespace Magento\Sales\Model\Order\Validation; -use Magento\Framework\Exception\NoSuchEntityException; use Magento\Sales\Api\Data\OrderInterface; -use Magento\Sales\Exception\DocumentValidationException; use Magento\Sales\Model\Order; use Magento\Sales\Model\ValidatorInterface; @@ -23,7 +21,9 @@ class CanShip implements ValidatorInterface public function validate($entity) { $messages = []; - if (!$this->canShip($entity)) { + if (!$this->isStateReadyForShipment($entity)) { + $messages[] = __('A shipment cannot be created when an order has a status of %1', $entity->getStatus()); + } elseif (!$this->canShip($entity)) { $messages[] = __('The order does not allow a shipment to be created.'); } @@ -34,7 +34,7 @@ public function validate($entity) * @param OrderInterface $order * @return bool */ - private function canShip(OrderInterface $order) + private function isStateReadyForShipment(OrderInterface $order) { if ($order->getState() === Order::STATE_PAYMENT_REVIEW || $order->getState() === Order::STATE_HOLDED || @@ -44,6 +44,15 @@ private function canShip(OrderInterface $order) return false; } + return true; + } + + /** + * @param OrderInterface $order + * @return bool + */ + private function canShip(OrderInterface $order) + { /** @var \Magento\Sales\Model\Order\Item $item */ foreach ($order->getItems() as $item) { if ($item->getQtyToShip() > 0 && !$item->getIsVirtual() && !$item->getLockedDoShip()) { diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php index f3d93d734741b..dd76bc1e52586 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanInvoiceTest.php @@ -62,7 +62,11 @@ public function testCanInvoiceWrongState($state) ->willReturn($state); $this->orderMock->expects($this->never()) ->method('getItems'); - $this->assertNotEmpty( + $this->orderMock->expects($this->once()) + ->method('getStatus') + ->willReturn('status'); + $this->assertEquals( + [__('An invoice cannot be created when an order has a status of %1', 'status')], $this->model->validate($this->orderMock) ); } diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php index 443453726000c..11d99fbb9cced 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Validation/CanShipTest.php @@ -60,9 +60,13 @@ public function testCanShipWrongState($state) $this->orderMock->expects($this->any()) ->method('getState') ->willReturn($state); + $this->orderMock->expects($this->once()) + ->method('getStatus') + ->willReturn('status'); $this->orderMock->expects($this->never()) ->method('getItems'); - $this->assertNotEmpty( + $this->assertEquals( + [__('A shipment cannot be created when an order has a status of %1', 'status')], $this->model->validate($this->orderMock) ); } From d21cc67b777cc722c2dfb76ee88af17f7218c2de Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 15:56:10 +0300 Subject: [PATCH 094/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/Shipment/Validation/QuantityValidator.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php index 192ea5b091f9b..f124fe9c6c04c 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php @@ -11,6 +11,7 @@ use Magento\Sales\Api\Data\ShipmentInterface; use Magento\Sales\Api\OrderRepositoryInterface; use Magento\Sales\Exception\DocumentValidationException; +use Magento\Sales\Model\Order\Item; use Magento\Sales\Model\ValidatorInterface; /** From 168b61c27febb411726b05dbb6b514a9aab2c996 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 16:10:34 +0300 Subject: [PATCH 095/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php | 1 + 1 file changed, 1 insertion(+) diff --git a/app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php b/app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php index 58ad64a0e5816..fa53f72ebcafc 100644 --- a/app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php +++ b/app/code/Magento/Sales/Model/Order/Invoice/CommentCreation.php @@ -93,5 +93,6 @@ public function setExtensionAttributes( \Magento\Sales\Api\Data\InvoiceCommentCreationExtensionInterface $extensionAttributes ) { $this->extensionAttributes = $extensionAttributes; + return $this; } } From 721f3b5a6cdd45b751940cca8b2ff25056b5a0aa Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 16:15:43 +0300 Subject: [PATCH 096/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Sales/Model/Order/Shipment/Validation/QuantityValidator.php | 2 +- .../Sales/Model/Order/Shipment/Validation/TrackValidator.php | 2 +- app/code/Magento/Sales/Model/Order/Validation/CanShip.php | 2 +- app/code/Magento/Sales/Model/ValidatorInterface.php | 2 +- 4 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php index f124fe9c6c04c..20e3712d889ed 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/QuantityValidator.php @@ -36,7 +36,7 @@ public function __construct( /** * @param ShipmentInterface $entity - * @return string[] + * @return array * @throws DocumentValidationException * @throws NoSuchEntityException */ diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php index 01efae2f1e69d..8ba20542da150 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php @@ -15,7 +15,7 @@ class TrackValidator implements ValidatorInterface { /** * @param object|ShipmentInterface $entity - * @return string[] + * @return array */ public function validate($entity) { diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php index a2e4da6eee13f..46638a62483e6 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanShip.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanShip.php @@ -16,7 +16,7 @@ class CanShip implements ValidatorInterface { /** * @param OrderInterface $entity - * @return string[] + * @return array */ public function validate($entity) { diff --git a/app/code/Magento/Sales/Model/ValidatorInterface.php b/app/code/Magento/Sales/Model/ValidatorInterface.php index 6c50af2c685d6..1882782e314f7 100644 --- a/app/code/Magento/Sales/Model/ValidatorInterface.php +++ b/app/code/Magento/Sales/Model/ValidatorInterface.php @@ -15,7 +15,7 @@ interface ValidatorInterface { /** * @param object $entity - * @return string[] + * @return array * @throws DocumentValidationException * @throws NoSuchEntityException */ From 32fd5c5a351a3ebd49e4834311fc4d955ddb1191 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 16:24:26 +0300 Subject: [PATCH 097/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- app/code/Magento/Sales/Model/ShipOrder.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Sales/Model/ShipOrder.php b/app/code/Magento/Sales/Model/ShipOrder.php index e7fd7707a125d..d051144cf73ca 100644 --- a/app/code/Magento/Sales/Model/ShipOrder.php +++ b/app/code/Magento/Sales/Model/ShipOrder.php @@ -165,14 +165,14 @@ public function execute( CanShip::class ] ); - $invoiceValidationResult = $this->shipmentValidator->validate( + $shipmentValidationResult = $this->shipmentValidator->validate( $shipment, [ QuantityValidator::class, TrackValidator::class ] ); - $validationMessages = array_merge($orderValidationResult, $invoiceValidationResult); + $validationMessages = array_merge($orderValidationResult, $shipmentValidationResult); if (!empty($validationMessages)) { throw new \Magento\Sales\Exception\DocumentValidationException( __("Shipment Document Validation Error(s):\n" . implode("\n", $validationMessages)) From 81fd780aead006f62dcfa16a554853c785caecee Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 16:25:27 +0300 Subject: [PATCH 098/115] MAGETWO-54677: Order status change after Shipment creation through API - static fixes --- .../Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php index 69e88bb45306c..8d05097614568 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php @@ -11,7 +11,7 @@ use Magento\Sales\Api\Data\ShipmentTrackInterface; /** - * Class QuantityValidatorTest + * Class TrackValidatorTest */ class TrackValidatorTest extends \PHPUnit_Framework_TestCase { From d4f0d2a5dc830e8c8657c3c534101ddfcf007d6d Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 16:26:38 +0300 Subject: [PATCH 099/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- .../Model/Order/Shipment/Validation/TrackValidatorTest.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php index 8d05097614568..a0c5658e2c96c 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php @@ -34,9 +34,9 @@ protected function setUp() { $objectManagerHelper = new ObjectManager($this); $this->shipmentMock = $this->getMockBuilder(ShipmentInterface::class) - ->getMock(); + ->getMockForAbstractClass(); $this->shipmentTrackMock = $this->getMockBuilder(ShipmentTrackInterface::class) - ->getMock(); + ->getMockForAbstractClass(); $this->shipmentMock->expects($this->once()) ->method('getTracks') ->willReturn([$this->shipmentTrackMock]); From ff0cb4b5917d361d9ed70cf703827f3c530458f7 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 16:36:05 +0300 Subject: [PATCH 100/115] MAGETWO-54677: Order status change after Shipment creation through API. Fix gap --- app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php b/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php index 89c92ad80f7d5..19d06fb0eff32 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/CommentCreation.php @@ -91,6 +91,6 @@ public function getIsVisibleOnFront() public function setIsVisibleOnFront($isVisibleOnFront) { $this->isVisibleOnFront = $isVisibleOnFront; - return $isVisibleOnFront; + return $this; } } From 2421db7e626a702afee967e02e83bf76a6abef97 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 18 Aug 2016 16:47:36 +0300 Subject: [PATCH 101/115] MAGETWO-56496: Introduce and implement new ShipOrderInterface --- .../Framework/EntityManager/CustomAttributesMapper.php | 8 +++----- .../Test/Unit/CustomAttributesMapperTest.php | 3 +++ .../Magento/Framework/EntityManager/TypeResolver.php | 6 +++--- 3 files changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php b/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php index 0d350cc122e3c..fe3a199da86a3 100644 --- a/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php +++ b/lib/internal/Magento/Framework/EntityManager/CustomAttributesMapper.php @@ -55,11 +55,9 @@ public function __construct( */ public function entityToDatabase($entityType, $data) { - if (!$this->metadataPool->hasConfiguration($entityType)) { - return $data; - } - $metadata = $this->metadataPool->getMetadata($entityType); - if (!$metadata->getEavEntityType()) { + if (!$this->metadataPool->hasConfiguration($entityType) + || !$this->metadataPool->getMetadata($entityType)->getEavEntityType() + ) { return $data; } if (isset($data[CustomAttributesDataInterface::CUSTOM_ATTRIBUTES])) { diff --git a/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php b/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php index 4c351efd9365e..56977af1dd6eb 100644 --- a/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php +++ b/lib/internal/Magento/Framework/EntityManager/Test/Unit/CustomAttributesMapperTest.php @@ -50,6 +50,9 @@ public function testEntityToDatabase() ->disableOriginalConstructor() ->setMethods(['getMetadata', 'hasConfiguration']) ->getMock(); + $metadataPool->expects($this->any()) + ->method('hasConfiguration') + ->willReturn(true); $metadataPool->expects($this->any()) ->method('getMetadata') ->with($this->equalTo(\Magento\Customer\Api\Data\AddressInterface::class)) diff --git a/lib/internal/Magento/Framework/EntityManager/TypeResolver.php b/lib/internal/Magento/Framework/EntityManager/TypeResolver.php index e40eb50595cb4..2718162e80d66 100644 --- a/lib/internal/Magento/Framework/EntityManager/TypeResolver.php +++ b/lib/internal/Magento/Framework/EntityManager/TypeResolver.php @@ -20,7 +20,8 @@ class TypeResolver */ private $typeMapping = [ \Magento\SalesRule\Model\Rule::class => \Magento\SalesRule\Api\Data\RuleInterface::class, - \Magento\SalesRule\Model\Rule\Interceptor::class => \Magento\SalesRule\Api\Data\RuleInterface::class + \Magento\SalesRule\Model\Rule\Interceptor::class => \Magento\SalesRule\Api\Data\RuleInterface::class, + \Magento\SalesRule\Model\Rule\Proxy::class => \Magento\SalesRule\Api\Data\RuleInterface::class ]; /** @@ -50,8 +51,7 @@ public function resolve($type) $dataInterfaces = []; foreach ($interfaceNames as $interfaceName) { if (strpos($interfaceName, '\Api\Data\\')) { - $dataInterfaces[] = isset($this->config[$interfaceName]) - ? $this->config[$interfaceName] : $interfaceName; + $dataInterfaces[] = $interfaceName; } } From f89fba5d6d81fbd30faa7085c7cb1a6c13905a1a Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 16:48:20 +0300 Subject: [PATCH 102/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- .../Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 0ca549b9e5080..f510544273aa9 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -3,7 +3,6 @@ * Copyright © 2016 Magento. All rights reserved. * See COPYING.txt for license details. */ - namespace Magento\Sales\Model\Order; use Magento\Sales\Api\Data\ShipmentInterface; @@ -75,7 +74,6 @@ public function create( array $packages = [], ShipmentCreationArgumentsInterface $arguments = null ) { - $shipmentItems = $this->itemsToArray($items); /** @var Shipment $shipment */ $shipment = $this->shipmentFactory->create( @@ -113,12 +111,12 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme } /** - * Convert Items To Array + * Convert items to array * * @param ShipmentItemCreationInterface[] $items * @return array */ - private function itemsToArray($items = []) + private function itemsToArray(array $items = []) { $invoiceItems = []; foreach ($items as $item) { From 4c20a9bdb66f1c1782324bc4c421db8ed2cc796a Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 17:00:18 +0300 Subject: [PATCH 103/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- .../Sales/Model/Order/Shipment/ShipmentValidator.php | 2 +- .../Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php index 75239253d4cf4..816551b8b322c 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/ShipmentValidator.php @@ -18,7 +18,7 @@ class ShipmentValidator implements ShipmentValidatorInterface private $validator; /** - * InvoiceValidatorRunner constructor. + * ShipmentValidator constructor. * @param \Magento\Sales\Model\Validator $validator */ public function __construct(\Magento\Sales\Model\Validator $validator) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php index aabc8a0b08e80..b0677b050f6fb 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/ShipmentDocumentFactoryTest.php @@ -18,7 +18,7 @@ use Magento\Framework\EntityManager\HydratorInterface; /** - * Class InvoiceDocumentFactoryTest + * Class ShipmentDocumentFactoryTest */ class ShipmentDocumentFactoryTest extends \PHPUnit_Framework_TestCase { @@ -50,7 +50,7 @@ class ShipmentDocumentFactoryTest extends \PHPUnit_Framework_TestCase /** * @var ShipmentDocumentFactory */ - private $invoiceDocumentFactory; + private $shipmentDocumentFactory; /** * @var \PHPUnit_Framework_MockObject_MockObject|HydratorPool @@ -112,7 +112,7 @@ protected function setUp() ->disableOriginalConstructor() ->getMockForAbstractClass(); - $this->invoiceDocumentFactory = new ShipmentDocumentFactory( + $this->shipmentDocumentFactory = new ShipmentDocumentFactory( $this->shipmentFactoryMock, $this->hydratorPoolMock, $this->trackFactoryMock @@ -181,7 +181,7 @@ public function testCreate() } $this->assertEquals( - $this->invoiceDocumentFactory->create( + $this->shipmentDocumentFactory->create( $this->orderMock, [$this->itemMock], $tracks, From 90d7f249bec2f20a5f1dffdc932d1ccd7292abb9 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 17:01:36 +0300 Subject: [PATCH 104/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- .../Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index f510544273aa9..8a553f0d27689 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -118,10 +118,10 @@ private function prepareTracks(\Magento\Sales\Api\Data\ShipmentInterface $shipme */ private function itemsToArray(array $items = []) { - $invoiceItems = []; + $shipmentItems = []; foreach ($items as $item) { - $invoiceItems[$item->getOrderItemId()] = $item->getQty(); + $shipmentItems[$item->getOrderItemId()] = $item->getQty(); } - return $invoiceItems; + return $shipmentItems; } } From 5c8ed158aafc30aed037192debfcd5bfdd1fa58d Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 17:03:26 +0300 Subject: [PATCH 105/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php index 8a553f0d27689..d10f84d815543 100644 --- a/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php +++ b/app/code/Magento/Sales/Model/Order/ShipmentDocumentFactory.php @@ -14,8 +14,9 @@ use Magento\Sales\Api\Data\OrderInterface; use Magento\Sales\Api\Data\ShipmentCommentCreationInterface; use Magento\Sales\Api\Data\ShipmentCreationArgumentsInterface; + /** - * Class InvoiceDocumentFactory + * Class ShipmentDocumentFactory * * @api */ From 13ea6aed63bd9e8b6801c73d4e359c9a8ffa7fd7 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 17:20:47 +0300 Subject: [PATCH 106/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- app/code/Magento/Sales/Model/InvoiceOrder.php | 2 +- app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/InvoiceOrder.php b/app/code/Magento/Sales/Model/InvoiceOrder.php index 63f62cf8ff23a..e51b46082d943 100644 --- a/app/code/Magento/Sales/Model/InvoiceOrder.php +++ b/app/code/Magento/Sales/Model/InvoiceOrder.php @@ -24,7 +24,7 @@ use Psr\Log\LoggerInterface; /** - * Class InvoiceService + * Class InvoiceOrder * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ class InvoiceOrder implements InvoiceOrderInterface diff --git a/app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php b/app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php index 5c3283bc1554e..6dfa929acb629 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/InvoiceOrderTest.php @@ -26,6 +26,7 @@ /** * Class InvoiceOrderTest + * * @SuppressWarnings(PHPMD.TooManyFields) * @SuppressWarnings(PHPMD.CouplingBetweenObjects) */ From e4c020fe5840d262730318b277d019ba3999a611 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 18 Aug 2016 17:38:26 +0300 Subject: [PATCH 107/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Sales/Model/Order/Shipment/Validation/TrackValidator.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php index 8ba20542da150..1d7fad1e2aa16 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php @@ -20,6 +20,9 @@ class TrackValidator implements ValidatorInterface public function validate($entity) { $messages = []; + if ($entity->getTracks()) { + return $messages; + } foreach ($entity->getTracks() as $track) { if (!$track->getTrackNumber()) { $messages[] = __('Please enter a tracking number.'); From 5cef44b40afdf2a0900fe29a0b1b45730d348ae7 Mon Sep 17 00:00:00 2001 From: Anton Kaplya Date: Thu, 18 Aug 2016 17:40:19 +0300 Subject: [PATCH 108/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Sales/Model/Order/Shipment/Validation/TrackValidator.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php index 1d7fad1e2aa16..55970d37c597d 100644 --- a/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php +++ b/app/code/Magento/Sales/Model/Order/Shipment/Validation/TrackValidator.php @@ -20,7 +20,7 @@ class TrackValidator implements ValidatorInterface public function validate($entity) { $messages = []; - if ($entity->getTracks()) { + if (!$entity->getTracks()) { return $messages; } foreach ($entity->getTracks() as $track) { From b95b1a617e42577c50f0dc83f3995918f35e23ae Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 18:10:49 +0300 Subject: [PATCH 109/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- app/code/Magento/Sales/etc/di.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index 2fd8f789a358f..f1b2c083b44a3 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -92,7 +92,6 @@ - From 95c6b6ec544b850ecbda8cef893cc67f3b094338 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 18:15:30 +0300 Subject: [PATCH 110/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- .../Shipping/Controller/Adminhtml/Order/Shipment/Save.php | 4 ++-- .../Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index 9be027576c28b..a615ffa5c3ac8 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -8,7 +8,7 @@ use Magento\Backend\App\Action; use Magento\Framework\App\ObjectManager; -use Magento\Sales\Model\Order\ShipmentQuantityValidator; +use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator; /** * Class Save @@ -129,7 +129,7 @@ public function execute() $shipment->setCustomerNote($data['comment_text']); $shipment->setCustomerNoteNotify(isset($data['comment_customer_notify'])); } - $errorMessages = $this->getShipmentValidator()->validate($shipment, [ShipmentQuantityValidator::class]); + $errorMessages = $this->getShipmentValidator()->validate($shipment, [QuantityValidator::class]); if (!empty($errorMessages)) { $this->messageManager->addError( __("Shipment Document Validation Error(s):\n" . implode("\n", $errorMessages)) diff --git a/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php b/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php index bdc4dd17d1088..f8d9c06dc8ec0 100644 --- a/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php +++ b/app/code/Magento/Shipping/Test/Unit/Controller/Adminhtml/Order/Shipment/SaveTest.php @@ -12,7 +12,7 @@ use Magento\Sales\Model\Order\Email\Sender\ShipmentSender; use Magento\Framework\TestFramework\Unit\Helper\ObjectManager as ObjectManagerHelper; use Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface; -use Magento\Sales\Model\Order\ShipmentQuantityValidator; +use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator; /** * Class SaveTest @@ -361,7 +361,7 @@ public function testExecute($formKeyIsValid, $isPost) $this->shipmentValidatorMock->expects($this->once()) ->method('validate') - ->with($shipment, [ShipmentQuantityValidator::class]) + ->with($shipment, [QuantityValidator::class]) ->willReturn([]); $this->saveAction->execute(); From 044a5182bb0aa6b28589c878084840260c5012f3 Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 18:26:28 +0300 Subject: [PATCH 111/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- app/code/Magento/Sales/etc/di.xml | 1 - 1 file changed, 1 deletion(-) diff --git a/app/code/Magento/Sales/etc/di.xml b/app/code/Magento/Sales/etc/di.xml index f1b2c083b44a3..dfdb0f6a261c5 100644 --- a/app/code/Magento/Sales/etc/di.xml +++ b/app/code/Magento/Sales/etc/di.xml @@ -92,7 +92,6 @@ - From 9e9b7aa0e80d735e291d91c72df40a927fa3d45e Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 18:33:13 +0300 Subject: [PATCH 112/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- .../Validation/TrackValidatorTest.php | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php index a0c5658e2c96c..0d8d951ccf18a 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/TrackValidatorTest.php @@ -37,9 +37,6 @@ protected function setUp() ->getMockForAbstractClass(); $this->shipmentTrackMock = $this->getMockBuilder(ShipmentTrackInterface::class) ->getMockForAbstractClass(); - $this->shipmentMock->expects($this->once()) - ->method('getTracks') - ->willReturn([$this->shipmentTrackMock]); $this->validator = $objectManagerHelper->getObject(TrackValidator::class); } @@ -48,6 +45,9 @@ public function testValidateTrackWithNumber() $this->shipmentTrackMock->expects($this->once()) ->method('getTrackNumber') ->willReturn('12345'); + $this->shipmentMock->expects($this->exactly(2)) + ->method('getTracks') + ->willReturn([$this->shipmentTrackMock]); $this->assertEquals([], $this->validator->validate($this->shipmentMock)); } @@ -56,6 +56,19 @@ public function testValidateTrackWithoutNumber() $this->shipmentTrackMock->expects($this->once()) ->method('getTrackNumber') ->willReturn(null); + $this->shipmentMock->expects($this->exactly(2)) + ->method('getTracks') + ->willReturn([$this->shipmentTrackMock]); $this->assertEquals([__('Please enter a tracking number.')], $this->validator->validate($this->shipmentMock)); } -} \ No newline at end of file + + public function testValidateTrackWithEmptyTracks() + { + $this->shipmentTrackMock->expects($this->never()) + ->method('getTrackNumber'); + $this->shipmentMock->expects($this->once()) + ->method('getTracks') + ->willReturn([]); + $this->assertEquals([], $this->validator->validate($this->shipmentMock)); + } +} From 2621d002d264baa3f189e36ac6571051205e6cdf Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Thu, 18 Aug 2016 18:44:58 +0300 Subject: [PATCH 113/115] MAGETWO-54677: Order status change after Shipment creation through API. Static fixes --- app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php index c0f2771561e25..bb14dc1bb5180 100644 --- a/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php +++ b/app/code/Magento/Sales/Model/Order/Validation/CanInvoice.php @@ -31,6 +31,10 @@ public function validate($entity) return $messages; } + /** + * @param OrderInterface $order + * @return bool + */ private function isStateReadyForInvoice(OrderInterface $order) { if ($order->getState() === Order::STATE_PAYMENT_REVIEW || From 450823144272332b002eaf3fe778ba548109e115 Mon Sep 17 00:00:00 2001 From: Olexandr Lysenko Date: Fri, 19 Aug 2016 10:26:27 +0300 Subject: [PATCH 114/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Magento/Sales/Service/V1/OrderInvoiceCreateTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/OrderInvoiceCreateTest.php b/dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/OrderInvoiceCreateTest.php index cb384134a7c68..60c9f54ea132c 100644 --- a/dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/OrderInvoiceCreateTest.php +++ b/dev/tests/api-functional/testsuite/Magento/Sales/Service/V1/OrderInvoiceCreateTest.php @@ -10,7 +10,7 @@ */ class OrderInvoiceCreateTest extends \Magento\TestFramework\TestCase\WebapiAbstract { - const SERVICE_READ_NAME = 'salesOrderInvoiceV1'; + const SERVICE_READ_NAME = 'salesInvoiceOrderV1'; const SERVICE_VERSION = 'V1'; /** From 65abe1db7180be75350349cbb64c3c205cfba46a Mon Sep 17 00:00:00 2001 From: Vladyslav Shcherbyna Date: Fri, 19 Aug 2016 18:19:04 +0300 Subject: [PATCH 115/115] MAGETWO-54677: Order status change after Shipment creation through API --- .../Shipment/Validation/QuantityValidatorTest.php | 10 ++++++++-- .../Controller/Adminhtml/Order/Shipment/Save.php | 3 +-- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php index 3d59b1d293640..01cccd2458695 100644 --- a/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php +++ b/app/code/Magento/Sales/Test/Unit/Model/Order/Shipment/Validation/QuantityValidatorTest.php @@ -45,7 +45,10 @@ public function testValidateTrackWithoutOrderId() $this->shipmentMock->expects($this->once()) ->method('getOrderId') ->willReturn(null); - $this->assertEquals([__('Order Id is required for shipment document')], $this->validator->validate($this->shipmentMock)); + $this->assertEquals( + [__('Order Id is required for shipment document')], + $this->validator->validate($this->shipmentMock) + ); } public function testValidateTrackWithoutItems() @@ -56,6 +59,9 @@ public function testValidateTrackWithoutItems() $this->shipmentMock->expects($this->once()) ->method('getItems') ->willReturn(null); - $this->assertEquals([__('You can\'t create a shipment without products.')], $this->validator->validate($this->shipmentMock)); + $this->assertEquals( + [__('You can\'t create a shipment without products.')], + $this->validator->validate($this->shipmentMock) + ); } } diff --git a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php index a615ffa5c3ac8..d265159bc630b 100644 --- a/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php +++ b/app/code/Magento/Shipping/Controller/Adminhtml/Order/Shipment/Save.php @@ -7,7 +7,6 @@ namespace Magento\Shipping\Controller\Adminhtml\Order\Shipment; use Magento\Backend\App\Action; -use Magento\Framework\App\ObjectManager; use Magento\Sales\Model\Order\Shipment\Validation\QuantityValidator; /** @@ -193,7 +192,7 @@ public function execute() private function getShipmentValidator() { if ($this->shipmentValidator === null) { - $this->shipmentValidator = ObjectManager::getInstance()->get( + $this->shipmentValidator = $this->_objectManager->get( \Magento\Sales\Model\Order\Shipment\ShipmentValidatorInterface::class ); }