/** * Add comment to shipment history * * @return void */ public function execute() { try { $this->getRequest()->setParam('shipment_id', $this->getRequest()->getParam('id')); $data = $this->getRequest()->getPost('comment'); if (empty($data['comment'])) { throw new \Magento\Framework\Exception\LocalizedException(__("The comment text field cannot be empty.")); } $this->shipmentLoader->setOrderId($this->getRequest()->getParam('order_id')); $this->shipmentLoader->setShipmentId($this->getRequest()->getParam('shipment_id')); $this->shipmentLoader->setShipment($this->getRequest()->getParam('shipment')); $this->shipmentLoader->setTracking($this->getRequest()->getParam('tracking')); $shipment = $this->shipmentLoader->load(); $shipment->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front'])); $this->shipmentCommentSender->send($shipment, !empty($data['is_customer_notified']), $data['comment']); $shipment->save(); $resultLayout = $this->resultLayoutFactory->create(); $resultLayout->addDefaultHandle(); $response = $resultLayout->getLayout()->getBlock('shipment_comments')->toHtml(); } catch (\Magento\Framework\Exception\LocalizedException $e) { $response = ['error' => true, 'message' => $e->getMessage()]; } catch (\Exception $e) { $response = ['error' => true, 'message' => __('Cannot add new comment.')]; } if (is_array($response)) { $response = $this->_objectManager->get('Magento\\Framework\\Json\\Helper\\Data')->jsonEncode($response); $this->getResponse()->representJson($response); } else { $this->getResponse()->setBody($response); } }
public function testSendVirtualOrder() { $isVirtualOrder = true; $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder); $this->stepAddressFormat($this->addressMock, $isVirtualOrder); $this->identityContainerMock->expects($this->once())->method('isEnabled')->will($this->returnValue(false)); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with($this->equalTo(['order' => $this->orderMock, 'shipment' => $this->shipmentMock, 'billing' => $this->addressMock, 'comment' => '', 'store' => $this->storeMock, 'formattedShippingAddress' => null, 'formattedBillingAddress' => 1])); $this->assertFalse($this->sender->send($this->shipmentMock)); }
public function testSendTrueWithoutCustomerCopy() { $billingAddress = $this->addressMock; $comment = 'comment_test'; $this->orderMock->expects($this->once())->method('getCustomerIsGuest')->will($this->returnValue(false)); $this->stepAddressFormat($billingAddress); $this->identityContainerMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with($this->equalTo(['order' => $this->orderMock, 'shipment' => $this->shipmentMock, 'billing' => $billingAddress, 'comment' => $comment, 'store' => $this->storeMock, 'formattedShippingAddress' => 1, 'formattedBillingAddress' => 1])); $this->stepSendWithCallSendCopyTo(); $result = $this->sender->send($this->shipmentMock, false, $comment); $this->assertTrue($result); }
public function testSendTrueWithoutCustomerCopy() { $billingAddress = 'billing_address'; $comment = 'comment_test'; $this->orderMock->expects($this->once())->method('getCustomerIsGuest')->will($this->returnValue(false)); $this->orderMock->expects($this->any())->method('getBillingAddress')->will($this->returnValue($billingAddress)); $this->identityContainerMock->expects($this->once())->method('isEnabled')->will($this->returnValue(true)); $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with($this->equalTo(['order' => $this->orderMock, 'shipment' => $this->shipmentMock, 'billing' => $billingAddress, 'comment' => $comment, 'store' => $this->storeMock])); $senderMock = $this->getMock('Magento\\Sales\\Model\\Order\\Email\\Sender', ['send', 'sendCopyTo'], [], '', false); $senderMock->expects($this->never())->method('send'); $senderMock->expects($this->once())->method('sendCopyTo'); $this->senderBuilderFactoryMock->expects($this->once())->method('create')->will($this->returnValue($senderMock)); $result = $this->sender->send($this->shipmentMock, false, $comment); $this->assertTrue($result); }
/** * Run test execute method (save exception) */ public function testExecuteExceptionSave() { $data = ['comment' => 'comment']; $orderId = 1; $shipmentId = 1; $shipment = []; $tracking = []; $this->requestMock->expects($this->once())->method('setParam')->with('shipment_id', $shipmentId); $this->requestMock->expects($this->once())->method('getPost')->with('comment')->will($this->returnValue($data)); $this->requestMock->expects($this->any())->method('getParam')->will($this->returnValueMap([['id', null, $shipmentId], ['order_id', null, $orderId], ['shipment_id', null, $shipmentId], ['shipment', null, $shipment], ['tracking', null, $tracking]])); $this->shipmentLoaderMock->expects($this->once())->method('setOrderId')->with($orderId); $this->shipmentLoaderMock->expects($this->once())->method('setShipmentId')->with($shipmentId); $this->shipmentLoaderMock->expects($this->once())->method('setShipment')->with($shipment); $this->shipmentLoaderMock->expects($this->once())->method('setTracking')->with($tracking); $this->shipmentLoaderMock->expects($this->once())->method('load')->will($this->returnValue($this->shipmentMock)); $this->shipmentMock->expects($this->once())->method('addComment'); $this->shipmentCommentSenderMock->expects($this->once())->method('send'); $this->shipmentMock->expects($this->once())->method('save')->will($this->throwException(new \Exception())); $this->exceptionResponse(); $this->assertNull($this->controller->execute()); }