diff --git a/app/code/Magento/Backend/Block/Dashboard/Graph.php b/app/code/Magento/Backend/Block/Dashboard/Graph.php index b76421e4e6f67..527bb2136b4c5 100644 --- a/app/code/Magento/Backend/Block/Dashboard/Graph.php +++ b/app/code/Magento/Backend/Block/Dashboard/Graph.php @@ -14,9 +14,6 @@ */ class Graph extends \Magento\Backend\Block\Dashboard\AbstractDashboard { - /** - * Api URL - */ const API_URL = 'https://image-charts.com/chart'; /** @@ -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' @@ -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'); @@ -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; } @@ -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 @@ -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); @@ -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 ); diff --git a/app/code/Magento/Customer/Model/CustomerRegistry.php b/app/code/Magento/Customer/Model/CustomerRegistry.php index b89cce8957487..d68904f6d1645 100644 --- a/app/code/Magento/Customer/Model/CustomerRegistry.php +++ b/app/code/Magento/Customer/Model/CustomerRegistry.php @@ -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]); diff --git a/app/code/Magento/PageCache/Model/App/Response/HttpPlugin.php b/app/code/Magento/PageCache/Model/App/Response/HttpPlugin.php index bf958707dde49..a6949cccc1add 100644 --- a/app/code/Magento/PageCache/Model/App/Response/HttpPlugin.php +++ b/app/code/Magento/PageCache/Model/App/Response/HttpPlugin.php @@ -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. */ @@ -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(); } } diff --git a/app/code/Magento/PageCache/Test/Unit/Model/App/Response/HttpPluginTest.php b/app/code/Magento/PageCache/Test/Unit/Model/App/Response/HttpPluginTest.php index 59591c8ee957f..6f8f12098dda8 100644 --- a/app/code/Magento/PageCache/Test/Unit/Model/App/Response/HttpPluginTest.php +++ b/app/code/Magento/PageCache/Test/Unit/Model/App/Response/HttpPluginTest.php @@ -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], ]; } } diff --git a/app/code/Magento/Reports/Model/ResourceModel/Order/Collection.php b/app/code/Magento/Reports/Model/ResourceModel/Order/Collection.php index d89a118bff94b..0a74c23fad991 100644 --- a/app/code/Magento/Reports/Model/ResourceModel/Order/Collection.php +++ b/app/code/Magento/Reports/Model/ResourceModel/Order/Collection.php @@ -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'); } diff --git a/dev/tests/integration/testsuite/Magento/Backend/Block/Dashboard/Tab/OrdersTest.php b/dev/tests/integration/testsuite/Magento/Backend/Block/Dashboard/Tab/OrdersTest.php new file mode 100644 index 0000000000000..1ac68b8d7ff57 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Backend/Block/Dashboard/Tab/OrdersTest.php @@ -0,0 +1,94 @@ +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 ''; + } +} diff --git a/dev/tests/integration/testsuite/Magento/Framework/Session/SaveHandlerTest.php b/dev/tests/integration/testsuite/Magento/Framework/Session/SaveHandlerTest.php index c420f6fa9dd75..dd493a499e855 100644 --- a/dev/tests/integration/testsuite/Magento/Framework/Session/SaveHandlerTest.php +++ b/dev/tests/integration/testsuite/Magento/Framework/Session/SaveHandlerTest.php @@ -3,18 +3,25 @@ * Copyright © Magento, Inc. All rights reserved. * See COPYING.txt for license details. */ + namespace Magento\Framework\Session; use Magento\Framework\App\DeploymentConfig; use Magento\Framework\Exception\SessionException; use Magento\Framework\Phrase; use Magento\Framework\Session\Config\ConfigInterface; -use Magento\Framework\App\ObjectManager; +use Magento\TestFramework\Helper\Bootstrap; +use Magento\TestFramework\ObjectManager; +/** + * Tests \Magento\Framework\Session\SaveHandler functionality. + * + * @SuppressWarnings(PHPMD.CouplingBetweenObjects) + */ class SaveHandlerTest extends \PHPUnit\Framework\TestCase { /** - * @var \Magento\TestFramework\ObjectManager + * @var ObjectManager */ private $objectManager; @@ -28,15 +35,21 @@ class SaveHandlerTest extends \PHPUnit\Framework\TestCase */ private $saveHandlerFactoryMock; + /** + * @inheritdoc + */ protected function setUp() { - $this->objectManager = \Magento\TestFramework\Helper\Bootstrap::getObjectManager(); + $this->objectManager = Bootstrap::getObjectManager(); $this->deploymentConfigMock = $this->createMock(DeploymentConfig::class); $this->objectManager->addSharedInstance($this->deploymentConfigMock, DeploymentConfig::class); $this->saveHandlerFactoryMock = $this->createMock(SaveHandlerFactory::class); $this->objectManager->addSharedInstance($this->saveHandlerFactoryMock, SaveHandlerFactory::class); } + /** + * @inheritdoc + */ protected function tearDown() { $this->objectManager->removeSharedInstance(DeploymentConfig::class); @@ -44,99 +57,74 @@ protected function tearDown() } /** - * Tests that the session handler is correctly set when object is created. - * - * @dataProvider saveHandlerProvider - * @param string $deploymentConfigHandler + * @return void */ - public function testConstructor($deploymentConfigHandler) + public function testRedisSaveHandler(): void { - $expected = $this->getExpectedSaveHandler($deploymentConfigHandler, ini_get('session.save_handler')); - $this->deploymentConfigMock->method('get') - ->willReturnCallback(function ($configPath) use ($deploymentConfigHandler) { - switch ($configPath) { - case Config::PARAM_SESSION_SAVE_METHOD: - return $deploymentConfigHandler; - case Config::PARAM_SESSION_CACHE_LIMITER: - return 'private_no_expire'; - case Config::PARAM_SESSION_SAVE_PATH: - return 'explicit_save_path'; - default: - return null; - } - }); - - $this->saveHandlerFactoryMock->expects($this->once()) - ->method('create') - ->with($expected); - $sessionConfig = $this->objectManager->create(ConfigInterface::class); - $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]); + ->willReturnMap( + [ + [Config::PARAM_SESSION_SAVE_METHOD, null, 'redis'], + [Config::PARAM_SESSION_SAVE_PATH, null, 'explicit_save_path'], + ] + ); - // Test expectation - $this->assertEquals( - $expected, - $sessionConfig->getOption('session.save_handler') - ); - } + $redisHandlerMock = $this->getMockBuilder(SaveHandler\Redis::class) + ->disableOriginalConstructor() + ->getMock(); + $redisHandlerMock->method('open') + ->with('explicit_save_path', 'test_session_id') + ->willReturn(true); - public function saveHandlerProvider() - { - return [ - ['db'], - ['redis'], - ['files'], - [false], - ]; + $this->saveHandlerFactoryMock->expects($this->exactly(1)) + ->method('create') + ->with('redis') + ->willReturn($redisHandlerMock); + + $sessionConfig = $this->objectManager->create(ConfigInterface::class); + /** @var SaveHandler $saveHandler */ + $saveHandler = $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]); + $result = $saveHandler->open('explicit_save_path', 'test_session_id'); + $this->assertTrue($result); } /** - * Retrieve expected session.save_handler - * - * @param string $deploymentConfigHandler - * @param string $iniHandler - * @return string + * @return void */ - private function getExpectedSaveHandler($deploymentConfigHandler, $iniHandler) - { - if ($deploymentConfigHandler) { - return $deploymentConfigHandler; - } elseif ($iniHandler) { - return $iniHandler; - } else { - return SaveHandlerInterface::DEFAULT_HANDLER; - } - } - - public function testConstructorWithException() + public function testRedisSaveHandlerFallbackToDefaultOnSessionException(): void { $this->deploymentConfigMock->method('get') - ->willReturnCallback(function ($configPath) { - switch ($configPath) { - case Config::PARAM_SESSION_SAVE_METHOD: - return 'db'; - case Config::PARAM_SESSION_CACHE_LIMITER: - return 'private_no_expire'; - case Config::PARAM_SESSION_SAVE_PATH: - return 'explicit_save_path'; - default: - return null; - } - }); + ->willReturnMap( + [ + [Config::PARAM_SESSION_SAVE_METHOD, null, 'redis'], + [Config::PARAM_SESSION_SAVE_PATH, null, 'explicit_save_path'], + ] + ); + + $redisHandlerMock = $this->getMockBuilder(SaveHandler\Redis::class) + ->disableOriginalConstructor() + ->getMock(); + $redisHandlerMock->method('open') + ->with('explicit_save_path', 'test_session_id') + ->willThrowException(new SessionException(new Phrase('Session Exception'))); + + $defaultHandlerMock = $this->getMockBuilder(SaveHandler\Native::class) + ->disableOriginalConstructor() + ->getMock(); + $defaultHandlerMock->expects($this->once())->method('open')->with('explicit_save_path', 'test_session_id'); $this->saveHandlerFactoryMock->expects($this->at(0)) ->method('create') - ->willThrowException(new SessionException(new Phrase('Session Exception'))); + ->with('redis') + ->willReturn($redisHandlerMock); $this->saveHandlerFactoryMock->expects($this->at(1)) ->method('create') - ->with(SaveHandlerInterface::DEFAULT_HANDLER); - $sessionConfig = $this->objectManager->create(ConfigInterface::class); - $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]); + ->with(SaveHandlerInterface::DEFAULT_HANDLER) + ->willReturn($defaultHandlerMock); - // Test expectation - $this->assertEquals( - 'db', - $sessionConfig->getOption('session.save_handler') - ); + $sessionConfig = $this->objectManager->create(ConfigInterface::class); + /** @var SaveHandler $saveHandler */ + $saveHandler = $this->objectManager->create(SaveHandler::class, ['sessionConfig' => $sessionConfig]); + $saveHandler->open('explicit_save_path', 'test_session_id'); } } diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/order_list_with_invoice.php b/dev/tests/integration/testsuite/Magento/Sales/_files/order_list_with_invoice.php new file mode 100644 index 0000000000000..06ddb18b009d1 --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/order_list_with_invoice.php @@ -0,0 +1,103 @@ +get(OrderInterfaceFactory::class); +/** @var OrderRepositoryInterface $orderRepository */ +$orderRepository = $objectManager->get(OrderRepositoryInterface::class); +/** @var InvoiceManagementInterface $invoiceManagement */ +$invoiceManagement = $objectManager->get(InvoiceManagementInterface::class); +/** @var Transaction $transaction */ +$transaction = $objectManager->get(Transaction::class); + +$dateTime = new \DateTimeImmutable(); +$ordersData = [ + [ + 'increment_id' => '100000002', + 'state' => Order::STATE_PROCESSING, + 'status' => 'processing', + 'base_to_global_rate' => 1, + 'base_grand_total' => 120.00, + 'grand_total' => 120.00, + 'subtotal' => 120.00, + 'created_at' => $dateTime->modify('-1 hour')->format(DateTime::DATETIME_PHP_FORMAT), + ], + [ + 'increment_id' => '100000003', + 'state' => Order::STATE_PROCESSING, + 'status' => 'processing', + 'base_to_global_rate' => 1, + 'base_grand_total' => 130.00, + 'grand_total' => 130.00, + 'subtotal' => 130.00, + 'created_at' => $dateTime->modify('-1 day')->format(DateTime::DATETIME_PHP_FORMAT), + ], + [ + 'increment_id' => '100000004', + 'state' => Order::STATE_PROCESSING, + 'status' => 'processing', + 'base_to_global_rate' => 1, + 'base_grand_total' => 140.00, + 'grand_total' => 140.00, + 'subtotal' => 140.00, + 'created_at' => $dateTime->modify('-1 month')->format(DateTime::DATETIME_PHP_FORMAT), + ], + [ + 'increment_id' => '100000005', + 'state' => Order::STATE_PROCESSING, + 'status' => 'processing', + 'base_to_global_rate' => 1, + 'base_grand_total' => 150.00, + 'grand_total' => 150.00, + 'subtotal' => 150.00, + 'created_at' => $dateTime->modify('-1 year')->format(DateTime::DATETIME_PHP_FORMAT), + ], + [ + 'increment_id' => '100000006', + 'state' => Order::STATE_PROCESSING, + 'status' => 'processing', + 'base_to_global_rate' => 1, + 'base_grand_total' => 160.00, + 'grand_total' => 160.00, + 'subtotal' => 160.00, + 'created_at' => $dateTime->modify('-2 year')->format(DateTime::DATETIME_PHP_FORMAT), + ], +]; + +foreach ($ordersData as $orderData) { + /** @var Order $order */ + $order = $orderFactory->create(); + $order + ->setData($orderData) + ->addItem($orderItem) + ->setCustomerIsGuest(true) + ->setCustomerEmail('customer@null.com') + ->setBillingAddress($billingAddress) + ->setShippingAddress($shippingAddress) + ->setPayment($payment); + $orderRepository->save($order); + + /** @var Invoice $invoice */ + $invoice = $invoiceManagement->prepareInvoice($order); + $invoice->register(); + $order->setIsInProcess(true); + $transaction + ->addObject($order) + ->addObject($invoice) + ->save(); +} diff --git a/dev/tests/integration/testsuite/Magento/Sales/_files/order_list_with_invoice_rollback.php b/dev/tests/integration/testsuite/Magento/Sales/_files/order_list_with_invoice_rollback.php new file mode 100644 index 0000000000000..2595d6bf4084a --- /dev/null +++ b/dev/tests/integration/testsuite/Magento/Sales/_files/order_list_with_invoice_rollback.php @@ -0,0 +1,8 @@ +getOption('session.save_handler') ?: $default; - - try { - $this->saveHandlerAdapter = $saveHandlerFactory->create($saveMethod); - } catch (SessionException $e) { - $this->saveHandlerAdapter = $saveHandlerFactory->create($default); - } + $this->saveHandlerFactory = $saveHandlerFactory; + $this->sessionConfig = $sessionConfig; + $this->defaultHandler = $default; } /** - * Open Session - retrieve resources + * Open Session - retrieve resources. * * @param string $savePath * @param string $name @@ -56,32 +60,32 @@ public function __construct( */ public function open($savePath, $name) { - return $this->saveHandlerAdapter->open($savePath, $name); + return $this->callSafely('open', $savePath, $name); } /** - * Close Session - free resources + * Close Session - free resources. * * @return bool */ public function close() { - return $this->saveHandlerAdapter->close(); + return $this->callSafely('close'); } /** - * Read session data + * Read session data. * * @param string $sessionId * @return string */ public function read($sessionId) { - return $this->saveHandlerAdapter->read($sessionId); + return $this->callSafely('read', $sessionId); } /** - * Write Session - commit data to resource + * Write Session - commit data to resource. * * @param string $sessionId * @param string $data @@ -89,23 +93,22 @@ public function read($sessionId) */ public function write($sessionId, $data) { - return $this->saveHandlerAdapter->write($sessionId, $data); + return $this->callSafely('write', $sessionId, $data); } /** - * Destroy Session - remove data from resource for given session id + * Destroy Session - remove data from resource for given session id. * * @param string $sessionId * @return bool */ public function destroy($sessionId) { - return $this->saveHandlerAdapter->destroy($sessionId); + return $this->callSafely('destroy', $sessionId); } /** - * Garbage Collection - remove old session data older - * than $maxLifetime (in seconds) + * Garbage Collection - remove old session data older than $maxLifetime (in seconds). * * @param int $maxLifetime * @return bool @@ -113,6 +116,30 @@ public function destroy($sessionId) */ public function gc($maxLifetime) { - return $this->saveHandlerAdapter->gc($maxLifetime); + return $this->callSafely('gc', $maxLifetime); + } + + /** + * Call save handler adapter method. + * + * In case custom handler failed, default files handler is used. + * + * @param string $method + * @param mixed $arguments + * + * @return mixed + */ + private function callSafely(string $method, ...$arguments) + { + try { + if ($this->saveHandlerAdapter === null) { + $saveMethod = $this->sessionConfig->getOption('session.save_handler') ?: $this->defaultHandler; + $this->saveHandlerAdapter = $this->saveHandlerFactory->create($saveMethod); + } + return $this->saveHandlerAdapter->{$method}(...$arguments); + } catch (SessionException $exception) { + $this->saveHandlerAdapter = $this->saveHandlerFactory->create($this->defaultHandler); + return $this->saveHandlerAdapter->{$method}(...$arguments); + } } } diff --git a/setup/src/Magento/Setup/Model/Installer.php b/setup/src/Magento/Setup/Model/Installer.php index 23f8a13c8bfe8..535040f942b89 100644 --- a/setup/src/Magento/Setup/Model/Installer.php +++ b/setup/src/Magento/Setup/Model/Installer.php @@ -7,6 +7,8 @@ namespace Magento\Setup\Model; use Magento\Backend\Setup\ConfigOptionsList as BackendConfigOptionsList; +use Magento\Framework\App\Cache\Type\Block as BlockCache; +use Magento\Framework\App\Cache\Type\Layout as LayoutCache; use Magento\Framework\App\DeploymentConfig\Reader; use Magento\Framework\App\DeploymentConfig\Writer; use Magento\Framework\App\Filesystem\DirectoryList; @@ -35,6 +37,7 @@ use Magento\Framework\Setup\SchemaSetupInterface; use Magento\Framework\Setup\UpgradeDataInterface; use Magento\Framework\Setup\UpgradeSchemaInterface; +use Magento\PageCache\Model\Cache\Type as PageCache; use Magento\Setup\Console\Command\InstallCommand; use Magento\Setup\Controller\ResponseTypeInterface; use Magento\Setup\Model\ConfigModel as SetupConfigModel; @@ -336,7 +339,7 @@ public function install($request) } $script[] = ['Installing database schema:', 'installSchema', [$request]]; $script[] = ['Installing user configuration...', 'installUserConfig', [$request]]; - $script[] = ['Enabling caches:', 'enableCaches', []]; + $script[] = ['Enabling caches:', 'updateCaches', [true]]; $script[] = ['Installing data...', 'installDataFixtures', [$request]]; if (!empty($request[InstallCommand::INPUT_KEY_SALES_ORDER_INCREMENT_PREFIX])) { $script[] = [ @@ -866,6 +869,12 @@ private function convertationOfOldScriptsIsAllowed(array $request) */ public function installDataFixtures(array $request = []) { + $frontendCaches = [ + PageCache::TYPE_IDENTIFIER, + BlockCache::TYPE_IDENTIFIER, + LayoutCache::TYPE_IDENTIFIER, + ]; + /** @var \Magento\Framework\Registry $registry */ $registry = $this->objectManagerProvider->get()->get(\Magento\Framework\Registry::class); //For backward compatibility in install and upgrade scripts with enabled parallelization. @@ -876,7 +885,11 @@ public function installDataFixtures(array $request = []) $setup = $this->dataSetupFactory->create(); $this->checkFilePermissionsForDbUpgrade(); $this->log->log('Data install/update:'); + $this->log->log('Disabling caches:'); + $this->updateCaches(false, $frontendCaches); $this->handleDBSchemaData($setup, 'data', $request); + $this->log->log('Enabling caches:'); + $this->updateCaches(true, $frontendCaches); $registry->unregister('setup-mode-enabled'); } @@ -1248,23 +1261,39 @@ public function uninstall() } /** - * Enables caches after installing application + * Enable or disable caches for specific types that are available * - * @return void + * If no types are specified then it will enable or disable all available types + * Note this is called by install() via callback. * - * @SuppressWarnings(PHPMD.UnusedPrivateMethod) Called by install() via callback. + * @param bool $isEnabled + * @param array $types + * @return void */ - private function enableCaches() + private function updateCaches($isEnabled, $types = []) { /** @var \Magento\Framework\App\Cache\Manager $cacheManager */ $cacheManager = $this->objectManagerProvider->get()->create(\Magento\Framework\App\Cache\Manager::class); - $types = $cacheManager->getAvailableTypes(); - $enabledTypes = $cacheManager->setEnabled($types, true); - $cacheManager->clean($enabledTypes); + + $availableTypes = $cacheManager->getAvailableTypes(); + $types = empty($types) ? $availableTypes : array_intersect($availableTypes, $types); + $enabledTypes = $cacheManager->setEnabled($types, $isEnabled); + if ($isEnabled) { + $cacheManager->clean($enabledTypes); + } + + // Only get statuses of specific cache types + $cacheStatus = array_filter( + $cacheManager->getStatus(), + function (string $key) use ($types) { + return in_array($key, $types); + }, + ARRAY_FILTER_USE_KEY + ); $this->log->log('Current status:'); // phpcs:ignore Magento2.Functions.DiscouragedFunction - $this->log->log(print_r($cacheManager->getStatus(), true)); + $this->log->log(print_r($cacheStatus, true)); } /** diff --git a/setup/src/Magento/Setup/Test/Unit/Model/InstallerTest.php b/setup/src/Magento/Setup/Test/Unit/Model/InstallerTest.php index e600002d53560..2b992c30615c2 100644 --- a/setup/src/Magento/Setup/Test/Unit/Model/InstallerTest.php +++ b/setup/src/Magento/Setup/Test/Unit/Model/InstallerTest.php @@ -307,8 +307,9 @@ public function testInstall(array $request, array $logMessages) $dataSetup->expects($this->any())->method('getConnection')->willReturn($connection); $cacheManager = $this->createMock(\Magento\Framework\App\Cache\Manager::class); $cacheManager->expects($this->any())->method('getAvailableTypes')->willReturn(['foo', 'bar']); - $cacheManager->expects($this->once())->method('setEnabled')->willReturn(['foo', 'bar']); - $cacheManager->expects($this->any())->method('clean'); + $cacheManager->expects($this->exactly(3))->method('setEnabled')->willReturn(['foo', 'bar']); + $cacheManager->expects($this->exactly(3))->method('clean'); + $cacheManager->expects($this->exactly(3))->method('getStatus')->willReturn(['foo' => 1, 'bar' => 1]); $appState = $this->getMockBuilder(\Magento\Framework\App\State::class) ->disableOriginalConstructor() ->disableArgumentCloning() @@ -382,6 +383,7 @@ public function testInstall(array $request, array $logMessages) /** * @return array + * @SuppressWarnings(PHPMD.ExcessiveMethodLength) */ public function installDataProvider() { @@ -410,15 +412,20 @@ public function installDataProvider() ['Installing user configuration...'], ['Enabling caches:'], ['Current status:'], - [''], + [print_r(['foo' => 1, 'bar' => 1], true)], ['Installing data...'], ['Data install/update:'], + ['Disabling caches:'], + ['Current status:'], + [print_r([], true)], ['Module \'Foo_One\':'], ['Module \'Bar_Two\':'], ['Data post-updates:'], ['Module \'Foo_One\':'], ['Module \'Bar_Two\':'], - //['Installing admin user...'], + ['Enabling caches:'], + ['Current status:'], + [print_r([], true)], ['Caches clearing:'], ['Cache cleared successfully'], ['Disabling Maintenance Mode:'], @@ -456,14 +463,20 @@ public function installDataProvider() ['Installing user configuration...'], ['Enabling caches:'], ['Current status:'], - [''], + [print_r(['foo' => 1, 'bar' => 1], true)], ['Installing data...'], ['Data install/update:'], + ['Disabling caches:'], + ['Current status:'], + [print_r([], true)], ['Module \'Foo_One\':'], ['Module \'Bar_Two\':'], ['Data post-updates:'], ['Module \'Foo_One\':'], ['Module \'Bar_Two\':'], + ['Enabling caches:'], + ['Current status:'], + [print_r([], true)], ['Installing admin user...'], ['Caches clearing:'], ['Cache cleared successfully'],