Skip to content

Commit

Permalink
Merge pull request #5336 from magento-tsg/2.4-develop-pr9
Browse files Browse the repository at this point in the history
[TSG] Fixes for 2.4 (pr9) (2.4-develop)
  • Loading branch information
zakdma authored Feb 15, 2020
2 parents 5c5b60c + f4bd829 commit a25c107
Show file tree
Hide file tree
Showing 12 changed files with 448 additions and 166 deletions.
41 changes: 15 additions & 26 deletions app/code/Magento/Backend/Block/Dashboard/Graph.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,6 @@
*/
class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard
{
/**
* Api URL
*/
const API_URL = 'https://image-charts.com/chart';

/**
Expand Down Expand Up @@ -190,8 +187,8 @@ public function getChartUrl($directUrl = true)
$params = [
'cht' => 'lc',
'chls' => '7',
'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
'chm' => 'B,f4d4b2,0,0,0',
'chf' => 'bg,s,f4f4f4|c,lg,90,ffffff,0.1,ededed,0',
'chm' => 'B,f4d4b2,0,0,0',
'chco' => 'db4814',
'chxs' => '0,0,11|1,0,11',
'chma' => '15,15,15,15'
Expand Down Expand Up @@ -237,7 +234,7 @@ public function getChartUrl($directUrl = true)
case '1y':
case '2y':
$d = $dateStart->format('Y-m');
$dateStart->modify('+1 month');
$dateStart->modify('first day of next month');
break;
default:
$d = $dateStart->format('Y-m-d H:00');
Expand Down Expand Up @@ -300,20 +297,23 @@ public function getChartUrl($directUrl = true)
$minvalue = min($localminvalue);

// default values
$yLabels = [];
$miny = 0;
$maxy = 0;
$yorigin = 0;
$xAxis = 'x';
$xAxisIndex = 0;
$yAxisIndex = 1;

if ($minvalue >= 0 && $maxvalue >= 0) {
if ($maxvalue > 10) {
$p = pow(10, $this->_getPow((int) $maxvalue));
$p = pow(10, $this->_getPow((int)$maxvalue));
$maxy = ceil($maxvalue / $p) * $p;
$yLabels = range($miny, $maxy, $p);
$yRange = "$yAxisIndex,$miny,$maxy,$p";
} else {
$maxy = ceil($maxvalue + 1);
$yLabels = range($miny, $maxy, 1);
$yRange = "$yAxisIndex,$miny,$maxy,1";
}
$params['chxr'] = $yRange;
$yorigin = 0;
}

Expand Down Expand Up @@ -341,22 +341,11 @@ public function getChartUrl($directUrl = true)

$params['chd'] .= $buffer;

$valueBuffer = [];

if (count($this->_axisLabels) > 0) {
$params['chxt'] = implode(',', array_keys($this->_axisLabels));
$indexid = 0;
foreach ($this->_axisLabels as $idx => $labels) {
if ($idx == 'x') {
$this->formatAxisLabelDate((string) $idx, (string) $timezoneLocal);
$tmpstring = implode('|', $this->_axisLabels[$idx]);
$valueBuffer[] = $indexid . ":|" . $tmpstring;
} elseif ($idx == 'y') {
$valueBuffer[] = $indexid . ":|" . implode('|', $yLabels);
}
$indexid++;
}
$params['chxl'] = implode('|', $valueBuffer);
$this->formatAxisLabelDate($xAxis, (string)$timezoneLocal);
$customAxisLabels = $xAxisIndex . ":|" . implode('|', $this->_axisLabels[$xAxis]);
$params['chxl'] = $customAxisLabels . $dataSetdelimiter;
}

// chart size
Expand All @@ -368,7 +357,7 @@ public function getChartUrl($directUrl = true)
foreach ($params as $name => $value) {
$p[] = $name . '=' . urlencode($value);
}
return (string) self::API_URL . '?' . implode('&', $p);
return (string)self::API_URL . '?' . implode('&', $p);
}
$gaData = urlencode(base64_encode(json_encode($params)));
$gaHash = $this->_dashboardData->getChartDataHash($gaData);
Expand All @@ -392,7 +381,7 @@ private function formatAxisLabelDate($idx, $timezoneLocal)
switch ($this->getDataHelper()->getParam('period')) {
case '24h':
$this->_axisLabels[$idx][$_index] = $this->_localeDate->formatDateTime(
$period->setTime((int) $period->format('H'), 0, 0),
$period->setTime((int)$period->format('H'), 0, 0),
\IntlDateFormatter::NONE,
\IntlDateFormatter::SHORT
);
Expand Down
2 changes: 1 addition & 1 deletion app/code/Magento/Customer/Model/CustomerRegistry.php
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ public function removeByEmail($customerEmail, $websiteId = null)
$websiteId = $this->storeManager->getStore()->getWebsiteId();
}
$emailKey = $this->getEmailKey($customerEmail, $websiteId);
if ($emailKey) {
if (isset($this->customerRegistryByEmail[$emailKey])) {
/** @var Customer $customer */
$customer = $this->customerRegistryByEmail[$emailKey];
unset($this->customerRegistryByEmail[$emailKey]);
Expand Down
10 changes: 7 additions & 3 deletions app/code/Magento/PageCache/Model/App/Response/HttpPlugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@

namespace Magento\PageCache\Model\App\Response;

use Magento\Framework\App\PageCache\NotCacheableInterface;
use Magento\Framework\App\Response\Http as HttpResponse;

/**
* HTTP response plugin for frontend.
*/
Expand All @@ -14,14 +17,15 @@ class HttpPlugin
/**
* Set proper value of X-Magento-Vary cookie.
*
* @param \Magento\Framework\App\Response\Http $subject
* @param HttpResponse $subject
* @return void
*/
public function beforeSendResponse(\Magento\Framework\App\Response\Http $subject)
public function beforeSendResponse(HttpResponse $subject)
{
if ($subject instanceof \Magento\Framework\App\PageCache\NotCacheableInterface) {
if ($subject instanceof NotCacheableInterface || $subject->headersSent()) {
return;
}

$subject->sendVary();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,63 @@
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\PageCache\Test\Unit\Model\App\Response;

use Magento\Framework\App\Response\Http as HttpResponse;
use Magento\MediaStorage\Model\File\Storage\Response as FileResponse;
use Magento\PageCache\Model\App\Response\HttpPlugin;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;

class HttpPluginTest extends \PHPUnit\Framework\TestCase
/**
* Tests \Magento\PageCache\Model\App\Response\HttpPlugin.
*/
class HttpPluginTest extends TestCase
{
/**
* @param \Magento\Framework\App\Response\FileInterface $responseInstanceClass
* @var HttpPlugin
*/
private $httpPlugin;

/**
* @inheritdoc
*/
protected function setUp()
{
parent::setUp();
$this->httpPlugin = new HttpPlugin();
}

/**
* @param string $responseClass
* @param bool $headersSent
* @param int $sendVaryCalled
* @return void
*
* @dataProvider beforeSendResponseDataProvider
*/
public function testBeforeSendResponse($responseInstanceClass, $sendVaryCalled)
public function testBeforeSendResponse(string $responseClass, bool $headersSent, int $sendVaryCalled): void
{
/** @var \Magento\Framework\App\Response\Http | \PHPUnit_Framework_MockObject_MockObject $responseMock */
$responseMock = $this->createMock($responseInstanceClass);
$responseMock->expects($this->exactly($sendVaryCalled))
->method('sendVary');
$plugin = new HttpPlugin();
$plugin->beforeSendResponse($responseMock);
/** @var HttpResponse|MockObject $responseMock */
$responseMock = $this->createMock($responseClass);
$responseMock->expects($this->any())->method('headersSent')->willReturn($headersSent);
$responseMock->expects($this->exactly($sendVaryCalled))->method('sendVary');

$this->httpPlugin->beforeSendResponse($responseMock);
}

/**
* @return array
*/
public function beforeSendResponseDataProvider()
public function beforeSendResponseDataProvider(): array
{
return [
[\Magento\Framework\App\Response\Http::class, 1],
[\Magento\MediaStorage\Model\File\Storage\Response::class, 0]
'http_response_headers_not_sent' => [HttpResponse::class, false, 1],
'http_response_headers_sent' => [HttpResponse::class, true, 0],
'file_response_headers_not_sent' => [FileResponse::class, false, 0],
'file_response_headers_sent' => [FileResponse::class, true, 0],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -461,6 +461,7 @@ public function getDateRange($range, $customStart, $customEnd, $returnObjects =
$startMonth = isset($startMonthDay[0]) ? (int)$startMonthDay[0] : 1;
$startDay = isset($startMonthDay[1]) ? (int)$startMonthDay[1] : 1;
$dateStart->setDate($dateStart->format('Y'), $startMonth, $startDay);
$dateStart->modify('-1 year');
if ($range == '2y') {
$dateStart->modify('-1 year');
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Backend\Block\Dashboard\Tab;

use Magento\Backend\Block\Dashboard\Graph;
use Magento\Framework\ObjectManagerInterface;
use Magento\Framework\View\LayoutInterface;
use Magento\TestFramework\Helper\Bootstrap;
use PHPUnit\Framework\TestCase;

/**
* Test for \Magento\Backend\Block\Dashboard\Tab\Orders class.
*
* @magentoAppArea adminhtml
*/
class OrdersTest extends TestCase
{
/**
* @var ObjectManagerInterface
*/
private $objectManager;

/**
* @var LayoutInterface
*/
private $layout;

/**
* @var Graph
*/
private $graphBlock;

/**
* @inheritdoc
*/
protected function setUp()
{
$this->objectManager = Bootstrap::getObjectManager();
$this->layout = $this->objectManager->get(LayoutInterface::class);
$this->graphBlock = $this->layout->createBlock(Graph::class);
}

/**
* @magentoDataFixture Magento/Sales/_files/order_list_with_invoice.php
* @dataProvider chartUrlDataProvider
* @param string $period
* @param string $expectedAxisRange
* @return void
*/
public function testGetChartUrl(string $period, string $expectedAxisRange): void
{
$this->graphBlock->getRequest()->setParams(['period' => $period]);
$ordersBlock = $this->layout->createBlock(Orders::class);
$decodedChartUrl = urldecode($ordersBlock->getChartUrl());
$this->assertEquals($expectedAxisRange, $this->getUrlParamData($decodedChartUrl, 'chxr'));
}

/**
* @return array
*/
public function chartUrlDataProvider(): array
{
return [
'Last 24 Hours' => ['24h', '1,0,2,1'],
'Last 7 Days' => ['7d', '1,0,3,1'],
'Current Month' => ['1m', '1,0,3,1'],
'YTD' => ['1y', '1,0,4,1'],
'2YTD' => ['2y', '1,0,4,1'],
];
}

/**
* @param string $chartUrl
* @param string $paramName
* @return string
*/
private function getUrlParamData(string $chartUrl, string $paramName): string
{
$chartUrlSegments = explode('&', $chartUrl);
foreach ($chartUrlSegments as $chartUrlSegment) {
[$paramKey, $paramValue] = explode('=', $chartUrlSegment);
if ($paramKey === $paramName) {
return $paramValue;
}
}

return '';
}
}
Loading

0 comments on commit a25c107

Please sign in to comment.