/**
  * @param int $configValue
  * @param bool|null $forceSyncMode
  * @param bool|null $customerNoteNotify
  * @param bool|null $emailSendingResult
  * @dataProvider sendDataProvider
  * @return void
  * @SuppressWarnings(PHPMD.ExcessiveMethodLength)
  */
 public function testSend($configValue, $forceSyncMode, $customerNoteNotify, $emailSendingResult)
 {
     $comment = 'comment_test';
     $address = 'address_test';
     $configPath = 'sales_email/general/async_sending';
     $this->creditmemoMock->expects($this->once())->method('setSendEmail')->with(true);
     $this->globalConfig->expects($this->once())->method('getValue')->with($configPath)->willReturn($configValue);
     if (!$configValue || $forceSyncMode) {
         $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false);
         $this->addressRenderer->expects($this->any())->method('format')->with($addressMock, 'html')->willReturn($address);
         $this->orderMock->expects($this->any())->method('getBillingAddress')->willReturn($addressMock);
         $this->orderMock->expects($this->any())->method('getShippingAddress')->willReturn($addressMock);
         $this->creditmemoMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn($customerNoteNotify);
         $this->creditmemoMock->expects($this->any())->method('getCustomerNote')->willReturn($comment);
         $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'creditmemo' => $this->creditmemoMock, 'comment' => $customerNoteNotify ? $comment : '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $address, 'formattedBillingAddress' => $address]);
         $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->creditmemoMock->expects($this->once())->method('setEmailSent')->with(true);
             $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, ['send_email', 'email_sent']);
             $this->assertTrue($this->sender->send($this->creditmemoMock));
         } else {
             $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email');
             $this->assertFalse($this->sender->send($this->creditmemoMock));
         }
     } else {
         $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email');
         $this->assertFalse($this->sender->send($this->creditmemoMock));
     }
 }
Beispiel #2
0
 /**
  * Handles asynchronous email sending during corresponding
  * cron job.
  *
  * Also method is used in the next events:
  *
  * - config_data_sales_email_general_async_sending_disabled
  *
  * Works only if asynchronous email sending is enabled
  * in global settings.
  *
  * @return void
  */
 public function execute()
 {
     if ($this->globalConfig->getValue('sales_email/general/async_sending')) {
         $this->entityCollection->addFieldToFilter('send_email', ['eq' => 1]);
         $this->entityCollection->addFieldToFilter('email_sent', ['null' => true]);
         /** @var \Magento\Sales\Model\AbstractModel $item */
         foreach ($this->entityCollection->getItems() as $item) {
             if ($this->emailSender->send($item, true)) {
                 $this->entityResource->save($item->setEmailSent(true));
             }
         }
     }
 }
Beispiel #3
0
 /**
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /**@var $object \Magento\Sales\Model\Order\Creditmemo\Item*/
     if (!$object->getParentId() && $object->getCreditmemo()) {
         $object->setParentId($object->getCreditmemo()->getId());
     }
     return parent::_beforeSave($object);
 }
Beispiel #4
0
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Shipment\Item $object */
     if (!$object->getParentId() && $object->getShipment()) {
         $object->setParentId($object->getShipment()->getId());
     }
     return parent::_beforeSave($object);
 }
Beispiel #5
0
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Invoice\Item $object */
     if (null == !$object->getOrderItem()) {
         $object->getOrderItem()->save();
     }
     return parent::_afterSave($object);
 }
Beispiel #6
0
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     parent::_beforeSave($object);
     $warnings = $this->validator->validate($object);
     if (!empty($warnings)) {
         throw new \Magento\Framework\Exception\LocalizedException(__("Cannot save comment:\n%1", implode("\n", $warnings)));
     }
     return $this;
 }
Beispiel #7
0
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Creditmemo $object */
     if (!$object->getOrderId() && $object->getOrder()) {
         $object->setOrderId($object->getOrder()->getId());
         $object->setBillingAddressId($object->getOrder()->getBillingAddress()->getId());
     }
     return parent::_beforeSave($object);
 }
Beispiel #8
0
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /**@var $object \Magento\Sales\Model\Order\Payment */
     parent::_beforeSave($object);
     if (!$object->getParentId() && $object->getOrder()) {
         $object->setParentId($object->getOrder()->getId());
     }
     return $this;
 }
Beispiel #9
0
 /**
  * @param int $configValue
  * @param array|null $collectionItems
  * @param bool|null $emailSendingResult
  * @dataProvider executeDataProvider
  * @return void
  */
 public function testExecute($configValue, $collectionItems, $emailSendingResult)
 {
     $path = 'sales_email/general/async_sending';
     $this->globalConfig->expects($this->once())->method('getValue')->with($path)->willReturn($configValue);
     if ($configValue) {
         $this->entityCollection->expects($this->at(0))->method('addFieldToFilter')->with('send_email', ['eq' => 1]);
         $this->entityCollection->expects($this->at(1))->method('addFieldToFilter')->with('email_sent', ['null' => true]);
         $this->entityCollection->expects($this->any())->method('getItems')->willReturn($collectionItems);
         if ($collectionItems) {
             /** @var \Magento\Sales\Model\AbstractModel|\PHPUnit_Framework_MockObject_MockObject $collectionItem */
             $collectionItem = $collectionItems[0];
             $this->emailSender->expects($this->once())->method('send')->with($collectionItem, true)->willReturn($emailSendingResult);
             if ($emailSendingResult) {
                 $collectionItem->expects($this->once())->method('setEmailSent')->with(true)->willReturn($collectionItem);
                 $this->entityResource->expects($this->once())->method('save')->with($collectionItem);
             }
         }
     }
     $this->object->execute();
 }
Beispiel #10
0
 /**
  * Perform actions before object save
  *
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Shipment $object */
     if ((!$object->getId() || null !== $object->getItems()) && !count($object->getAllItems())) {
         throw new \Magento\Framework\Exception\LocalizedException(__('We cannot create an empty shipment.'));
     }
     if (!$object->getOrderId() && $object->getOrder()) {
         $object->setOrderId($object->getOrder()->getId());
         $object->setShippingAddressId($object->getOrder()->getShippingAddress()->getId());
     }
     return parent::_beforeSave($object);
 }
Beispiel #11
0
 /**
  * Performs validation before save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  * @throws \Magento\Framework\Exception\LocalizedException
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Invoice\Comment $object */
     if (!$object->getParentId() && $object->getInvoice()) {
         $object->setParentId($object->getInvoice()->getId());
     }
     parent::_beforeSave($object);
     $errors = $this->validator->validate($object);
     if (!empty($errors)) {
         throw new \Magento\Framework\Exception\LocalizedException(__("Cannot save comment:\n%1", implode("\n", $errors)));
     }
     return $this;
 }
Beispiel #12
0
 /**
  * @param bool $isVirtualOrder
  * @param int $formatCallCount
  * @param string|null $expectedShippingAddress
  * @dataProvider sendVirtualOrderDataProvider
  */
 public function testSendVirtualOrder($isVirtualOrder, $formatCallCount, $expectedShippingAddress)
 {
     $billingAddress = 'address_test';
     $this->orderMock->setData(\Magento\Sales\Api\Data\OrderInterface::IS_VIRTUAL, $isVirtualOrder);
     $this->creditmemoMock->expects($this->once())->method('setSendEmail')->with(true);
     $this->globalConfig->expects($this->once())->method('getValue')->with('sales_email/general/async_sending')->willReturn(false);
     $addressMock = $this->getMock('Magento\\Sales\\Model\\Order\\Address', [], [], '', false);
     $this->addressRenderer->expects($this->exactly($formatCallCount))->method('format')->with($addressMock, 'html')->willReturn($billingAddress);
     $this->stepAddressFormat($addressMock, $isVirtualOrder);
     $this->creditmemoMock->expects($this->once())->method('getCustomerNoteNotify')->willReturn(true);
     $this->templateContainerMock->expects($this->once())->method('setTemplateVars')->with(['order' => $this->orderMock, 'creditmemo' => $this->creditmemoMock, 'comment' => '', 'billing' => $addressMock, 'payment_html' => 'payment', 'store' => $this->storeMock, 'formattedShippingAddress' => $expectedShippingAddress, 'formattedBillingAddress' => $billingAddress]);
     $this->identityContainerMock->expects($this->once())->method('isEnabled')->willReturn(false);
     $this->creditmemoResourceMock->expects($this->once())->method('saveAttribute')->with($this->creditmemoMock, 'send_email');
     $this->assertFalse($this->sender->send($this->creditmemoMock));
 }
Beispiel #13
0
 /**
  * Perform actions after object save
  *
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function processRelations(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order\Creditmemo $object */
     if (null !== $object->getItems()) {
         foreach ($object->getItems() as $item) {
             $item->setParentId($object->getId());
             $item->save();
         }
     }
     if (null !== $object->getComments()) {
         foreach ($object->getComments() as $comment) {
             $comment->save();
         }
     }
     return parent::processRelations($object);
 }
Beispiel #14
0
 /**
  * Lookup for parent_id in already saved transactions of this payment by the order_id
  * Also serialize additional information, if any
  *
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Sales\Model\Order\Payment\Transaction $transaction
  * @throws \Magento\Framework\Exception\LocalizedException
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $transaction)
 {
     $parentTxnId = $transaction->getData('parent_txn_id');
     $txnId = $transaction->getData('txn_id');
     $orderId = $transaction->getData('order_id');
     $paymentId = $transaction->getData('payment_id');
     $idFieldName = $this->getIdFieldName();
     if ($parentTxnId) {
         if (!$txnId || !$orderId || !$paymentId) {
             throw new \Magento\Framework\Exception\LocalizedException(__('We don\'t have enough information to save the parent transaction ID.'));
         }
         $parentId = (int) $this->_lookupByTxnId($orderId, $paymentId, $parentTxnId, $idFieldName);
         if ($parentId) {
             $transaction->setData('parent_id', $parentId);
         }
     }
     // make sure unique key won't cause trouble
     if ($transaction->isFailsafe()) {
         $autoincrementId = (int) $this->_lookupByTxnId($orderId, $paymentId, $txnId, $idFieldName);
         if ($autoincrementId) {
             $transaction->setData($idFieldName, $autoincrementId)->isObjectNew(false);
         }
     }
     return parent::_beforeSave($transaction);
 }
Beispiel #15
0
 /**
  * Update related grid table after object save
  *
  * @param \Magento\Framework\Model\AbstractModel|\Magento\Framework\Object $object
  * @return \Magento\Framework\Model\Resource\Db\AbstractDb
  */
 protected function _afterSave(\Magento\Framework\Model\AbstractModel $object)
 {
     $resource = parent::_afterSave($object);
     if ($object->getOrderId()) {
         $this->gridPool->refreshByOrderId($object->getOrderId());
     }
     return $resource;
 }
Beispiel #16
0
 /**
  * {@inheritdoc}
  */
 public function save(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order $object */
     $this->stateHandler->check($object);
     return parent::save($object);
 }
Beispiel #17
0
 /**
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function _beforeSave(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order $object */
     $this->stateHandler->check($object);
     if (!$object->getId()) {
         /** @var \Magento\Store\Model\Store $store */
         $store = $object->getStore();
         $name = [$store->getWebsite()->getName(), $store->getGroup()->getName(), $store->getName()];
         $object->setStoreName(implode(PHP_EOL, $name));
         $object->setTotalItemCount($this->calculateItems($object));
     }
     $object->setData('protect_code', substr(md5(uniqid(Random::getRandomNumber(), true) . ':' . microtime(true)), 5, 6));
     $isNewCustomer = !$object->getCustomerId() || $object->getCustomerId() === true;
     if ($isNewCustomer && $object->getCustomer()) {
         $object->setCustomerId($object->getCustomer()->getId());
     }
     return parent::_beforeSave($object);
 }
Beispiel #18
0
 /**
  * @param \Magento\Framework\Model\AbstractModel $object
  * @return $this
  */
 protected function processRelations(\Magento\Framework\Model\AbstractModel $object)
 {
     /** @var \Magento\Sales\Model\Order $object */
     $this->addressHandler->process($object);
     if (null !== $object->getItems()) {
         /** @var \Magento\Sales\Model\Order\Item $item */
         foreach ($object->getItems() as $item) {
             $item->setOrderId($object->getId());
             $item->setOrder($object);
             $item->save();
         }
     }
     if (null !== $object->getPayments()) {
         /** @var \Magento\Sales\Model\Order\Payment $payment */
         foreach ($object->getPayments() as $payment) {
             $payment->setParentId($object->getId());
             $payment->setOrder($object);
             $payment->save();
         }
     }
     if (null !== $object->getStatusHistories()) {
         /** @var \Magento\Sales\Model\Order\Status\History $statusHistory */
         foreach ($object->getStatusHistories() as $statusHistory) {
             $statusHistory->setParentId($object->getId());
             $statusHistory->save();
             $statusHistory->setOrder($object);
         }
     }
     if (null !== $object->getRelatedObjects()) {
         foreach ($object->getRelatedObjects() as $relatedObject) {
             $relatedObject->save();
             $relatedObject->setOrder($object);
         }
     }
     return parent::processRelations($object);
 }