Skip to content

Sort Order "Comments History" tab comments correctly when same timestamp #34306

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 51 commits into
base: 2.4-develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 42 commits
Commits
Show all changes
51 commits
Select commit Hold shift + click to select a range
b7af8a5
Correctly label option for Protection Type
PromInc Jul 8, 2021
242c12f
Correctly label option for Protection Type
PromInc Jul 8, 2021
08d20be
Correctly label option for Protection Type
PromInc Jul 8, 2021
6c87486
Correctly label option for Protection Type
PromInc Jul 8, 2021
692f96d
Correctly label option for Protection Type
PromInc Jul 8, 2021
4febd7c
Correctly label option for Protection Type
PromInc Jul 8, 2021
41188db
Correctly label option for Protection Type
PromInc Jul 8, 2021
bf1c340
Correctly label option for Protection Type
PromInc Jul 8, 2021
ba24475
Sort by timestamp and id
PromInc Oct 11, 2021
836a325
Merge branch 'magento:2.4-develop' into 2.4-develop
PromInc Oct 11, 2021
eba21cf
Update ResetMethod.php
PromInc Oct 11, 2021
7947440
Update ResetMethod.php
PromInc Oct 11, 2021
36a2245
Update CollectionFactory.php
PromInc Oct 11, 2021
e46991f
Update ResetMethodTest.php
PromInc Oct 11, 2021
81e3cbc
Update ResetMethodTest.php
PromInc Oct 11, 2021
c1fb73c
Update CollectionFactoryTest.php
PromInc Oct 11, 2021
5174e50
Update FrequencyTest.php
PromInc Oct 11, 2021
c81212c
Update QuantityTest.php
PromInc Oct 11, 2021
75999e0
Update SecurityManagerTest.php
PromInc Oct 11, 2021
47e17a5
Update en_US.csv
PromInc Oct 11, 2021
1875102
Update ResetMethodTest.php
PromInc Oct 11, 2021
f597b7a
Force invoices to be after other records
PromInc Oct 11, 2021
6e79dc6
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Oct 12, 2021
a5b81c2
Add type for sorting
PromInc Nov 8, 2021
a23b463
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
70cf0a4
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
4109ff2
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
d4c0ba5
define arguments
PromInc Nov 9, 2021
1e8d5c6
Update app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
PromInc Nov 9, 2021
e65dea4
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
Nov 16, 2021
451831a
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
Dec 8, 2021
5ce6523
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
Dec 10, 2021
84cd2f4
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
engcom-Lima Dec 10, 2021
16ecc87
Merge branch '2.4-develop' of https://github.com/PromInc/magento2 int…
engcom-Lima Dec 10, 2021
30a9d66
Merge branch '2.4-develop' of https://github.com/magento/magento2 int…
engcom-Lima Dec 14, 2021
284dad8
[BUGFIX] - Fixed static test failures
engcom-Lima Dec 14, 2021
d4d00a3
Merge branch 'magento:2.4-develop' into 2.4-develop
PromInc Dec 22, 2021
f7a746f
Use configuration for mage-messages cookie
PromInc Dec 22, 2021
30dd1a4
Create Config.php
PromInc Dec 22, 2021
0a9223d
Pass the configured the cookie domain to Javascript
PromInc Dec 22, 2021
496302d
set the cookieDomain via the config value
PromInc Dec 22, 2021
fc46a6b
Delete Config.php
PromInc Dec 22, 2021
78c55d6
Revert "set the cookieDomain via the config value"
PromInc Dec 28, 2021
ceede4b
Revert "Pass the configured the cookie domain to Javascript"
PromInc Dec 28, 2021
2ade2a7
Revert "Use configuration for mage-messages cookie"
PromInc Dec 28, 2021
54a7be0
Update function name
PromInc Dec 28, 2021
170e2fb
Merge remote-tracking branch 'magento2/2.4-develop' into 2.4-develop
engcom-Charlie Feb 24, 2022
caf892c
Issue-34304 Fixed multiple tests failurs.
engcom-Charlie Mar 1, 2022
7130231
Merge remote-tracking branch 'magento2/2.4-develop' into 2.4-develop
engcom-Charlie Mar 1, 2022
76e55e0
Issue-34304 Fixed tests failurs.
engcom-Charlie Mar 2, 2022
259be51
Issue-34304 Fixed tests failurs.
engcom-Charlie Mar 4, 2022
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
113 changes: 96 additions & 17 deletions app/code/Magento/Sales/Block/Adminhtml/Order/View/Tab/History.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,15 @@
*/
class History extends \Magento\Backend\Block\Template implements \Magento\Backend\Block\Widget\Tab\TabInterface
{
private const HISTORY_TYPE_INVOICE = 1;
private const HISTORY_TYPE_INVOICE_COMMENT = 2;
private const HISTORY_TYPE_ORDER = 3;
private const HISTORY_TYPE_CREDIT_MEMO = 4;
private const HISTORY_TYPE_CREDIT_MEMO_COMMENT = 5;
private const HISTORY_TYPE_SHIPMENT = 6;
private const HISTORY_TYPE_SHIPMENT_COMMENT = 7;
private const HISTORY_TYPE_TRACKING_NUM = 8;

/**
* Template
*
Expand Down Expand Up @@ -75,74 +84,93 @@ public function getFullHistory()

$history = [];
foreach ($order->getAllStatusHistory() as $orderComment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$orderComment->getId(),
$orderComment->getStatusLabel(),
$orderComment->getIsCustomerNotified(),
$this->getOrderAdminDate($orderComment->getCreatedAt()),
$orderComment->getComment()
$orderComment->getComment(),
self::HISTORY_TYPE_ORDER
);
}

foreach ($order->getCreditmemosCollection() as $_memo) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$_memo->getId(),
__('Credit memo #%1 created', $_memo->getIncrementId()),
$_memo->getEmailSent(),
$this->getOrderAdminDate($_memo->getCreatedAt())
$this->getOrderAdminDate($_memo->getCreatedAt()),
'Credit memo created',
self::HISTORY_TYPE_CREDIT_MEMO
);

foreach ($_memo->getCommentsCollection() as $_comment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$_comment->getId(),
__('Credit memo #%1 comment added', $_memo->getIncrementId()),
$_comment->getIsCustomerNotified(),
$this->getOrderAdminDate($_comment->getCreatedAt()),
$_comment->getComment()
$_comment->getComment(),
self::HISTORY_TYPE_CREDIT_MEMO_COMMENT
);
}
}

foreach ($order->getShipmentsCollection() as $_shipment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$_shipment->getId(),
__('Shipment #%1 created', $_shipment->getIncrementId()),
$_shipment->getEmailSent(),
$this->getOrderAdminDate($_shipment->getCreatedAt())
$this->getOrderAdminDate($_shipment->getCreatedAt()),
self::HISTORY_TYPE_SHIPMENT
);

foreach ($_shipment->getCommentsCollection() as $_comment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$_comment->getId(),
__('Shipment #%1 comment added', $_shipment->getIncrementId()),
$_comment->getIsCustomerNotified(),
$this->getOrderAdminDate($_comment->getCreatedAt()),
$_comment->getComment()
$_comment->getComment(),
self::HISTORY_TYPE_SHIPMENT_COMMENT
);
}
}

foreach ($order->getInvoiceCollection() as $_invoice) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$_invoice->getId(),
__('Invoice #%1 created', $_invoice->getIncrementId()),
$_invoice->getEmailSent(),
$this->getOrderAdminDate($_invoice->getCreatedAt())
$this->getOrderAdminDate($_invoice->getCreatedAt()),
'Invoice created',
self::HISTORY_TYPE_INVOICE
);

foreach ($_invoice->getCommentsCollection() as $_comment) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$_comment->getId(),
__('Invoice #%1 comment added', $_invoice->getIncrementId()),
$_comment->getIsCustomerNotified(),
$this->getOrderAdminDate($_comment->getCreatedAt()),
$_comment->getComment()
$_comment->getComment(),
self::HISTORY_TYPE_INVOICE_COMMENT
);
}
}

foreach ($order->getTracksCollection() as $_track) {
$history[] = $this->_prepareHistoryItem(
$history[] = $this->_prepareHistoryItemWithId(
$_track->getId(),
__('Tracking number %1 for %2 assigned', $_track->getNumber(), $_track->getTitle()),
false,
$this->getOrderAdminDate($_track->getCreatedAt())
$this->getOrderAdminDate($_track->getCreatedAt()),
'Tracking number assigned',
self::HISTORY_TYPE_TRACKING_NUM
);
}

usort($history, [__CLASS__, 'sortHistoryByTimestamp']);
usort($history, [__CLASS__, 'sortHistory']);
return $history;
}

Expand Down Expand Up @@ -218,6 +246,35 @@ protected function _prepareHistoryItem($label, $notified, $created, $comment = '
return ['title' => $label, 'notified' => $notified, 'comment' => $comment, 'created_at' => $created];
}

/**
* Map history items as array with id and type
*
* @param int $id
* @param string $label
* @param bool $notified
* @param \DateTimeInterface $created
* @param string $comment
* @param int $type
* @return array
*/
private function _prepareHistoryItemWithId(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please remove the underscore from the method name, it was an old practice from Magento 1, the new code shouldn't contain it

Suggested change
private function _prepareHistoryItemWithId(
private function prepareHistoryItemWithId(

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ihor-sviziev I have updated this function name and addressed the other un-related files that creeped into this pull request. Shame on me for not following git best-practices... Sorry this got a bit messy. I think this is all straightened out now.

int $id,
string $label,
bool $notified,
\DateTimeInterface $created,
string $comment = '',
int $type = 0
): array {
return [
'entity_id' => $id,
'title' => $label,
'notified' => $notified,
'comment' => $comment,
'created_at' => $created,
'type' => $type
];
}

/**
* @inheritdoc
*/
Expand Down Expand Up @@ -307,6 +364,28 @@ public static function sortHistoryByTimestamp($a, $b)
return $createdAtA->getTimestamp() <=> $createdAtB->getTimestamp();
}

/**
* Comparison For Sorting History By Timestamp and entity_id
*
* @param array $a
* @param array $b
* @return int
*/
private function sortHistory(array $a, array $b): int
{
$result = $this->sortHistoryByTimestamp($a, $b);
if (0 !== $result) {
return $result;
}

$result = $a['type'] <=> $b['type'];
if (0 !== $result) {
return $result;
}

return $a['entity_id'] <=> $b['entity_id'];
}

/**
* Get order admin date
*
Expand Down
40 changes: 37 additions & 3 deletions app/code/Magento/Theme/Controller/Result/MessagePlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Magento\Framework\Translate\Inline\ParserInterface;
use Magento\Framework\Translate\InlineInterface;
use Magento\Framework\Session\Config\ConfigInterface;
use Magento\Framework\App\Config\ScopeConfigInterface;

/**
* Plugin for putting messages to cookies
Expand Down Expand Up @@ -59,6 +60,11 @@ class MessagePlugin
*/
protected $sessionConfig;

/**
* @var ScopeConfigInterface
*/
protected $scopeConfig;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please make it private

Suggested change
protected $scopeConfig;
private $scopeConfig;


/**
* @param \Magento\Framework\Stdlib\CookieManagerInterface $cookieManager
* @param \Magento\Framework\Stdlib\Cookie\CookieMetadataFactory $cookieMetadataFactory
Expand All @@ -67,6 +73,7 @@ class MessagePlugin
* @param \Magento\Framework\Serialize\Serializer\Json $serializer
* @param InlineInterface $inlineTranslate
* @param ConfigInterface $sessionConfig
* @param ScopeConfigInterface $scopeConfig
*/
public function __construct(
\Magento\Framework\Stdlib\CookieManagerInterface $cookieManager,
Expand All @@ -75,7 +82,8 @@ public function __construct(
\Magento\Framework\View\Element\Message\InterpretationStrategyInterface $interpretationStrategy,
\Magento\Framework\Serialize\Serializer\Json $serializer,
InlineInterface $inlineTranslate,
ConfigInterface $sessionConfig
ConfigInterface $sessionConfig,
ScopeConfigInterface $scopeConfig
) {
$this->cookieManager = $cookieManager;
$this->cookieMetadataFactory = $cookieMetadataFactory;
Expand All @@ -84,6 +92,7 @@ public function __construct(
$this->interpretationStrategy = $interpretationStrategy;
$this->inlineTranslate = $inlineTranslate;
$this->sessionConfig = $sessionConfig;
$this->scopeConfig = $scopeConfig;
}

/**
Expand Down Expand Up @@ -171,11 +180,36 @@ private function setCookie(array $messages)
}

$publicCookieMetadata = $this->cookieMetadataFactory->createPublicCookieMetadata();
$publicCookieMetadata->setDurationOneYear();
$publicCookieMetadata->setPath($this->sessionConfig->getCookiePath());

if( $configLifetime = $this->scopeConfig->getValue(
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is related to the issue you're trying to fix. Could you please revert it?

'web/cookie/cookie_lifetime',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setDuration($configLifetime);
} else {
$publicCookieMetadata->setDurationOneYear();
}

if( $configPath = $this->scopeConfig->getValue(
'web/cookie/cookie_path',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setPath($configPath);
} else {
$publicCookieMetadata->setPath($this->sessionConfig->getCookiePath());
}

Copy link
Contributor

@ihor-sviziev ihor-sviziev Dec 24, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think this change is related to the issue you're trying to fix. Could you please revert it?

Suggested change
if( $configLifetime = $this->scopeConfig->getValue(
'web/cookie/cookie_lifetime',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setDuration($configLifetime);
} else {
$publicCookieMetadata->setDurationOneYear();
}
if( $configPath = $this->scopeConfig->getValue(
'web/cookie/cookie_path',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setPath($configPath);
} else {
$publicCookieMetadata->setPath($this->sessionConfig->getCookiePath());
}

PS: you can create a new branch and add them to it, so they won't affect this PR.

$publicCookieMetadata->setHttpOnly(false);

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

and this

Suggested change

$publicCookieMetadata->setSameSite('Strict');

if( $configDomain = $this->scopeConfig->getValue(
'web/cookie/cookie_domain',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setDomain($configDomain);
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this

Suggested change
if( $configDomain = $this->scopeConfig->getValue(
'web/cookie/cookie_domain',
\Magento\Store\Model\ScopeInterface::SCOPE_STORE
) ) {
$publicCookieMetadata->setDomain($configDomain);
}

$this->cookieManager->setPublicCookie(
self::MESSAGES_COOKIES_NAME,
$this->serializer->serialize($messages),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
* See COPYING.txt for license details.
*/
?>
<?php $helperConfig = $this->helper('Magento\Theme\Helper\Data'); ?>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this

Suggested change
<?php $helperConfig = $this->helper('Magento\Theme\Helper\Data'); ?>

<div data-bind="scope: 'messages'">
<!-- ko if: cookieMessages && cookieMessages.length > 0 -->
<div aria-atomic="true" role="alert" data-bind="foreach: { data: cookieMessages, as: 'message' }" class="messages">
Expand Down Expand Up @@ -35,7 +36,8 @@
"Magento_Ui/js/core/app": {
"components": {
"messages": {
"component": "Magento_Theme/js/view/messages"
"component": "Magento_Theme/js/view/messages",
"cookieDomain": "<?= $helperConfig->getConfig( 'web/cookie/cookie_domain' ) ?>"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this

Suggested change
"component": "Magento_Theme/js/view/messages",
"cookieDomain": "<?= $helperConfig->getConfig( 'web/cookie/cookie_domain' ) ?>"
"component": "Magento_Theme/js/view/messages"

}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ define([

$.mage.cookies.set('mage-messages', '', {
samesite: 'strict',
domain: ''
domain: this.cookieDomain
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

And this

Suggested change
domain: this.cookieDomain
domain: ''

});
},

Expand Down