/**
  * Add comment to creditmemo history
  *
  * @return \Magento\Framework\Controller\Result\Raw|\Magento\Framework\Controller\Result\Json
  */
 public function execute()
 {
     try {
         $this->getRequest()->setParam('creditmemo_id', $this->getRequest()->getParam('id'));
         $data = $this->getRequest()->getPost('comment');
         if (empty($data['comment'])) {
             throw new \Magento\Framework\Exception\LocalizedException(__('Please enter a comment.'));
         }
         $this->creditmemoLoader->setOrderId($this->getRequest()->getParam('order_id'));
         $this->creditmemoLoader->setCreditmemoId($this->getRequest()->getParam('creditmemo_id'));
         $this->creditmemoLoader->setCreditmemo($this->getRequest()->getParam('creditmemo'));
         $this->creditmemoLoader->setInvoiceId($this->getRequest()->getParam('invoice_id'));
         $creditmemo = $this->creditmemoLoader->load();
         $comment = $creditmemo->addComment($data['comment'], isset($data['is_customer_notified']), isset($data['is_visible_on_front']));
         $comment->save();
         $this->creditmemoCommentSender->send($creditmemo, !empty($data['is_customer_notified']), $data['comment']);
         $resultPage = $this->resultPageFactory->create();
         $response = $resultPage->getLayout()->getBlock('creditmemo_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)) {
         $resultJson = $this->resultJsonFactory->create();
         $resultJson->setData($response);
         return $resultJson;
     } else {
         $resultRaw = $this->resultRawFactory->create();
         $resultRaw->setContents($response);
         return $resultRaw;
     }
 }
 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, 'creditmemo' => $this->creditmemoMock, 'billing' => $billingAddress, 'comment' => $comment, 'store' => $this->storeMock, 'formattedShippingAddress' => 1, 'formattedBillingAddress' => 1]));
     $this->stepSendWithCallSendCopyTo();
     $result = $this->sender->send($this->creditmemoMock, false, $comment);
     $this->assertTrue($result);
 }