From e52994d617708d0310216a6081c4f4d5e3c36881 Mon Sep 17 00:00:00 2001
From: Ostap Smolyar <ostap.smolyar@gmail.com>
Date: Thu, 29 Dec 2016 19:53:21 +0200
Subject: [PATCH] MTA-3952: Refactoring comments history block in Order module

---
 .../Block/Adminhtml/Order/View/Tab/Info.php   |  21 ++++
 .../View/Tab/Info/CommentHistoryBlock.php     | 119 ++++++++++++++++++
 .../AssertAuthorizationInCommentsHistory.php  |  10 +-
 3 files changed, 148 insertions(+), 2 deletions(-)
 create mode 100644 dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info/CommentHistoryBlock.php

diff --git a/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info.php b/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info.php
index 146e3facccb..5000bbed56f 100644
--- a/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info.php
+++ b/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info.php
@@ -7,6 +7,7 @@
 namespace Magento\Sales\Test\Block\Adminhtml\Order\View\Tab;
 
 use Magento\Backend\Test\Block\Widget\Tab;
+use Magento\Sales\Test\Block\Adminhtml\Order\View\Tab\Info\CommentHistoryBlock;
 use Magento\Sales\Test\Block\Adminhtml\Order\View\Tab\Info\PaymentInfoBlock;
 
 /**
@@ -28,6 +29,13 @@ class Info extends Tab
      */
     private $paymentInfoBlockSelector = '.order-payment-method';
 
+    /**
+     * Selector for Comment history block.
+     *
+     * @var string
+     */
+    private $commentHistoryBlockSelector = '#order_history_block';
+
     /**
      * Get order status from info block.
      *
@@ -50,4 +58,17 @@ class Info extends Tab
             ['element' => $this->_rootElement->find($this->paymentInfoBlockSelector)]
         );
     }
+
+    /**
+     * Returns Comment history block.
+     *
+     * @return CommentHistoryBlock
+     */
+    public function getCommentHistoryBlock()
+    {
+        return $this->blockFactory->create(
+            CommentHistoryBlock::class,
+            ['element' => $this->_rootElement->find($this->commentHistoryBlockSelector)]
+        );
+    }
 }
diff --git a/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info/CommentHistoryBlock.php b/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info/CommentHistoryBlock.php
new file mode 100644
index 00000000000..c9223d75115
--- /dev/null
+++ b/dev/tests/functional/tests/app/Magento/Sales/Test/Block/Adminhtml/Order/View/Tab/Info/CommentHistoryBlock.php
@@ -0,0 +1,119 @@
+<?php
+/**
+ * Copyright © 2016 Magento. All rights reserved.
+ * See COPYING.txt for license details.
+ */
+
+namespace Magento\Sales\Test\Block\Adminhtml\Order\View\Tab\Info;
+
+use Magento\Mtf\Block\Block;
+
+/**
+ * Order comments history block.
+ */
+class CommentHistoryBlock extends Block
+{
+    /**
+     * Comment history list locator.
+     *
+     * @var string
+     */
+    protected $commentHistory = '.note-list';
+
+    /**
+     * Comment date.
+     *
+     * @var string
+     */
+    protected $commentHistoryDate = '.note-list-date';
+
+    /**
+     * Comment time.
+     *
+     * @var string
+     */
+    protected $commentHistoryTime = '.note-list-time';
+
+    /**
+     * Comment status.
+     *
+     * @var string
+     */
+    protected $commentHistoryStatus = '.note-list-status';
+
+    /**
+     * Comment notified status.
+     *
+     * @var string
+     */
+    protected $commentHistoryNotifiedStatus = '.note-list-customer';
+
+    /**
+     * Comment locator.
+     *
+     * @var string
+     */
+    protected $comment = '.note-list-comment';
+
+    /**
+     * Authorized Amount.
+     *
+     * @var string
+     */
+    protected $authorizedAmount = '//div[@class="note-list-comment"][contains(text(), "Authorized amount of")]';
+
+    /**
+     * Captured Amount from IPN.
+     *
+     * @var string
+     */
+    protected $capturedAmount = '//div[@class="note-list-comment"][contains(text(), "Captured amount of")]';
+
+    /**
+     * Refunded Amount.
+     *
+     * @var string
+     */
+    protected $refundedAmount = '//div[@class="note-list-comment"][contains(text(), "We refunded")]';
+
+    /**
+     * Voided Amount.
+     *
+     * @var string
+     */
+    protected $voidedAmount = '//div[@class="note-list-comment"][contains(text(), "Voided authorization")]';
+
+    /**
+     * Get comment history block data.
+     *
+     * @return array
+     */
+    public function getComments()
+    {
+        $result = [];
+        $elements = $this->_rootElement->getElements($this->commentHistory);
+        foreach ($elements as $item) {
+            $result['date'] = $item->find($this->commentHistoryDate)->getText();
+            $result['time'] = $item->find($this->commentHistoryTime)->getText();
+            $result['status'] = $item->find($this->commentHistoryStatus)->getText();
+            $result['is_customer_notified'] = $item->find($this->commentHistoryNotifiedStatus)->getText();
+            $result['authorized_amount'] = $item->find($this->authorizedAmount)->getText();
+            $result['captured_amount'] = $item->find($this->capturedAmount)->getText();
+            $result['refunded_amount'] = $item->find($this->refundedAmount)->getText();
+            $result['voided_amount'] = $item->find($this->voidedAmount)->getText();
+        }
+
+        return $result;
+    }
+
+    /**
+     * Get last comment.
+     *
+     * @return array
+     */
+    public function getLatestComment()
+    {
+        $comments = $this->getComments();
+        return end($comments);
+    }
+}
diff --git a/dev/tests/functional/tests/app/Magento/Sales/Test/Constraint/AssertAuthorizationInCommentsHistory.php b/dev/tests/functional/tests/app/Magento/Sales/Test/Constraint/AssertAuthorizationInCommentsHistory.php
index 8b34837bc8d..2b03f14d3f9 100644
--- a/dev/tests/functional/tests/app/Magento/Sales/Test/Constraint/AssertAuthorizationInCommentsHistory.php
+++ b/dev/tests/functional/tests/app/Magento/Sales/Test/Constraint/AssertAuthorizationInCommentsHistory.php
@@ -37,11 +37,17 @@ class AssertAuthorizationInCommentsHistory extends AbstractConstraint
     ) {
         $salesOrder->open();
         $salesOrder->getSalesOrderGrid()->searchAndOpen(['id' => $orderId]);
-        $actualAuthorizedAmount = $salesOrderView->getOrderHistoryBlock()->getAuthorizedAmount();
+
+        /** @var \Magento\Sales\Test\Block\Adminhtml\Order\View\Tab\Info $infoTab */
+        $infoTab = $salesOrderView->getOrderForm()->openTab('info')->getTab('info');
+        $historyBlock = $infoTab->getCommentHistoryBlock();
+        $lastComment = $historyBlock->getLatestComment();
+
+        //$actualAuthorizedAmount = $salesOrderView->getOrderHistoryBlock()->getAuthorizedAmount();
 
         \PHPUnit_Framework_Assert::assertRegExp(
             sprintf(self::AUTHORIZED_AMOUNT_PATTERN, $prices['grandTotal']),
-            $actualAuthorizedAmount,
+            $lastComment['authorized_amount'],
             'Incorrect authorized amount value for the order #' . $orderId
         );
     }
-- 
GitLab