Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
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
19 changes: 16 additions & 3 deletions app/code/Magento/Checkout/Block/Cart/Shipping.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
namespace Magento\Checkout\Block\Cart;

/**
* Cart shipping block.
*
* @api
* @SuppressWarnings(PHPMD.CouplingBetweenObjects)
*/
Expand All @@ -25,6 +27,11 @@ class Shipping extends \Magento\Checkout\Block\Cart\AbstractCart
* @var \Magento\Framework\Serialize\Serializer\Json
*/
private $serializer;

/**
* @var \Magento\Checkout\Model\Cart\ConfigProvider
*/
private $cartConfig;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
Expand All @@ -34,6 +41,7 @@ class Shipping extends \Magento\Checkout\Block\Cart\AbstractCart
* @param array $layoutProcessors
* @param array $data
* @param \Magento\Framework\Serialize\Serializer\Json|null $serializer
* @param \Magento\Checkout\Model\Cart\ConfigProvider|null $cartConfig
* @throws \RuntimeException
*/
public function __construct(
Expand All @@ -43,14 +51,17 @@ public function __construct(
\Magento\Checkout\Model\CompositeConfigProvider $configProvider,
array $layoutProcessors = [],
array $data = [],
\Magento\Framework\Serialize\Serializer\Json $serializer = null
\Magento\Framework\Serialize\Serializer\Json $serializer = null,
\Magento\Checkout\Model\Cart\ConfigProvider $cartConfig = null
Copy link
Member

Choose a reason for hiding this comment

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

Please inject this class via block argument in layout

Copy link
Contributor

Choose a reason for hiding this comment

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

Hi @sivaschenko

Based on the discussion in #23733 I think it's not backwards compatible to add block arguments via layout xml?
If custom modules try to instantiate the same block class in some custom xml file, then after upgrading Magento the module might crash as it's missing some arguments which are then undefined but still being used by the phtml file.

Can you double check this?

) {
$this->configProvider = $configProvider;
$this->layoutProcessors = $layoutProcessors;
parent::__construct($context, $customerSession, $checkoutSession, $data);
$this->_isScopePrivate = true;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\Serializer\Json::class);
$this->cartConfig = $cartConfig ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Checkout\Model\Cart\ConfigProvider::class);
}

/**
Expand All @@ -61,7 +72,7 @@ public function __construct(
*/
public function getCheckoutConfig()
{
return $this->configProvider->getConfig();
return $this->cartConfig->getCheckoutConfig();
}

/**
Expand Down Expand Up @@ -90,11 +101,13 @@ public function getBaseUrl()
}

/**
* Get serialized checkout config.
*
* @return bool|string
* @since 100.2.0
*/
public function getSerializedCheckoutConfig()
{
return json_encode($this->getCheckoutConfig(), JSON_HEX_TAG);
return $this->cartConfig->getSerializedCheckoutConfig();
}
}
15 changes: 12 additions & 3 deletions app/code/Magento/Checkout/Block/Cart/Totals.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@

namespace Magento\Checkout\Block\Cart;

use Magento\Framework\View\Element\BlockInterface;
use Magento\Checkout\Block\Checkout\LayoutProcessorInterface;
use Magento\Framework\View\Element\BlockInterface;

/**
* Totals cart block.
Expand Down Expand Up @@ -41,13 +41,19 @@ class Totals extends \Magento\Checkout\Block\Cart\AbstractCart
*/
protected $layoutProcessors;

/**
* @var \Magento\Framework\Serialize\SerializerInterface
*/
private $serializer;

/**
* @param \Magento\Framework\View\Element\Template\Context $context
* @param \Magento\Customer\Model\Session $customerSession
* @param \Magento\Checkout\Model\Session $checkoutSession
* @param \Magento\Sales\Model\Config $salesConfig
* @param array $layoutProcessors
* @param array $data
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
* @codeCoverageIgnore
*/
public function __construct(
Expand All @@ -56,12 +62,15 @@ public function __construct(
\Magento\Checkout\Model\Session $checkoutSession,
\Magento\Sales\Model\Config $salesConfig,
array $layoutProcessors = [],
array $data = []
array $data = [],
\Magento\Framework\Serialize\SerializerInterface $serializer = null
Copy link
Member

Choose a reason for hiding this comment

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

Please inject this dependency via block argument in layout

) {
$this->_salesConfig = $salesConfig;
parent::__construct($context, $customerSession, $checkoutSession, $data);
$this->_isScopePrivate = true;
$this->layoutProcessors = $layoutProcessors;
$this->serializer = $serializer ?: \Magento\Framework\App\ObjectManager::getInstance()
->get(\Magento\Framework\Serialize\SerializerInterface::class);
}

/**
Expand All @@ -75,7 +84,7 @@ public function getJsLayout()
$this->jsLayout = $processor->process($this->jsLayout);
}

return json_encode($this->jsLayout, JSON_HEX_TAG);
return $this->serializer->serialize($this->jsLayout);
}

/**
Expand Down
67 changes: 67 additions & 0 deletions app/code/Magento/Checkout/Model/Cart/ConfigProvider.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
declare(strict_types=1);

namespace Magento\Checkout\Model\Cart;

/**
* Provides the checkout configuration.
*/
class ConfigProvider implements \Magento\Framework\View\Element\Block\ArgumentInterface
{
/**
* @var \Magento\Checkout\Model\CompositeConfigProvider
*/
private $configProvider;

/**
* @var \Magento\Framework\Serialize\SerializerInterface
*/
private $serializer;

/**
* @var array
*/
private $providedConfig = null;

/**
* Config constructor.
*
* @param \Magento\Checkout\Model\CompositeConfigProvider $configProvider
* @param \Magento\Framework\Serialize\SerializerInterface $serializer
*/
public function __construct(
\Magento\Checkout\Model\CompositeConfigProvider $configProvider,
\Magento\Framework\Serialize\SerializerInterface $serializer
) {
$this->configProvider = $configProvider;
$this->serializer = $serializer;
}

/**
* Retrieve checkout configuration
*
* @return array
*/
public function getCheckoutConfig()
{
if (null === $this->providedConfig) {
$this->providedConfig = $this->configProvider->getConfig();
}

return $this->providedConfig;
}

/**
* Retrieve checkout serialized configuration
*
* @return bool|string
*/
public function getSerializedCheckoutConfig()
{
return $this->serializer->serialize($this->getCheckoutConfig());
}
}
19 changes: 16 additions & 3 deletions app/code/Magento/Checkout/Test/Unit/Block/Cart/ShippingTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
*/
namespace Magento\Checkout\Test\Unit\Block\Cart;

/**
* Test for Magento\Checkout\Block\Cart\Shipping class.
*/
class ShippingTest extends \PHPUnit\Framework\TestCase
{
/**
Expand Down Expand Up @@ -51,6 +54,11 @@ class ShippingTest extends \PHPUnit\Framework\TestCase
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $cartConfig;

protected function setUp()
{
Expand All @@ -69,6 +77,7 @@ protected function setUp()
$this->storeManager = $this->createMock(\Magento\Store\Model\StoreManagerInterface::class);
$this->context->expects($this->once())->method('getStoreManager')->willReturn($this->storeManager);
$this->serializer = $this->createMock(\Magento\Framework\Serialize\Serializer\Json::class);
$this->cartConfig = $this->createMock(\Magento\Checkout\Model\Cart\ConfigProvider::class);

$this->model = new \Magento\Checkout\Block\Cart\Shipping(
$this->context,
Expand All @@ -77,14 +86,15 @@ protected function setUp()
$this->configProvider,
[$this->layoutProcessor],
['jsLayout' => $this->layout],
$this->serializer
$this->serializer,
$this->cartConfig
);
}

public function testGetCheckoutConfig()
{
$config = ['param' => 'value'];
$this->configProvider->expects($this->once())->method('getConfig')->willReturn($config);
$this->cartConfig->expects($this->once())->method('getCheckoutConfig')->willReturn($config);
$this->assertEquals($config, $this->model->getCheckoutConfig());
}

Expand Down Expand Up @@ -117,7 +127,10 @@ public function testGetBaseUrl()
public function testGetSerializedCheckoutConfig()
{
$checkoutConfig = ['checkout', 'config'];
$this->configProvider->expects($this->once())->method('getConfig')->willReturn($checkoutConfig);
$this->cartConfig
->expects($this->once())
->method('getSerializedCheckoutConfig')
->willReturn(json_encode($checkoutConfig));

$this->assertEquals(json_encode($checkoutConfig), $this->model->getSerializedCheckoutConfig());
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
<?php
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Checkout\Test\Unit\Model\Cart;

/**
* Test for Magento\Checkout\Model\Cart\ConfigProvider class.
*/
class ConfigProviderTest extends \PHPUnit\Framework\TestCase
{
/**
* @var \Magento\Checkout\Model\Cart\ConfigProvider
*/
private $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $cartConfigProvider;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $compositeConfigProvider;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
private $serializer;

protected function setUp()
{
$this->cartConfigProvider = $this->createMock(\Magento\Checkout\Model\Cart\ConfigProvider::class);
$this->compositeConfigProvider = $this->createMock(\Magento\Checkout\Model\CompositeConfigProvider::class);
$this->serializer = $this->createMock(\Magento\Framework\Serialize\SerializerInterface::class);

$this->model = new \Magento\Checkout\Model\Cart\ConfigProvider(
$this->compositeConfigProvider,
$this->serializer
);
}

public function testGetCheckoutConfig()
{
$config = ['param' => 'value'];
$this->compositeConfigProvider
->expects($this->once())
->method('getConfig')
->willReturn($config);

$this->assertEquals($config, $this->model->getCheckoutConfig());
}

public function testGetSerializedCheckoutConfig()
{
$config = '{"param":"value"}';

$this->serializer
->expects($this->once())
->method('serialize')
->willReturn($config);

$this->assertEquals($config, $this->model->getSerializedCheckoutConfig());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,7 @@
<container name="checkout.cart.totals.container" as="totals" label="Shopping Cart Totals">
<block class="Magento\Checkout\Block\Cart\Totals" name="checkout.cart.totals" template="Magento_Checkout::cart/totals.phtml">
<arguments>
<argument name="cart_config" xsi:type="object">Magento\Checkout\Model\Cart\ConfigProvider</argument>
Copy link
Member

Choose a reason for hiding this comment

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

Please apply the same approach for other blocks' dependencies as well

<argument name="jsLayout" xsi:type="array">
<item name="components" xsi:type="array">
<item name="block-totals" xsi:type="array">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,14 @@
<script type="text/x-magento-init">
{
"#cart-totals": {
"Magento_Ui/js/core/app": <?= /* @escapeNotVerified */ $block->getJsLayout() ?>
"Magento_Ui/js/core/app": <?= $block->getJsLayout() ?>,
"Magento_Checkout/js/totals": {
<?php // phpcs:disable Magento2.Security.XssTemplate?>
"checkoutConfig":<?= $block->getCartConfig()->getSerializedCheckoutConfig() ?>,
"loaderFile":"<?= $block->getViewFileUrl('images/loader-1.gif') ?>",
"baseUrl":"<?= $block->getBaseUrl() ?>"
<?php // phpcs:enable?>
}
}
}
</script>
Expand Down
20 changes: 20 additions & 0 deletions app/code/Magento/Checkout/view/frontend/web/js/totals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/**
* Copyright © Magento, Inc. All rights reserved.
* See COPYING.txt for license details.
*/

define([
'mage/url',
'Magento_Ui/js/block-loader'
], function (url, blockLoader) {
'use strict';

return function (config) {
window.checkoutConfig = config.checkoutConfig;
window.customerData = window.checkoutConfig.customerData;
window.isCustomerLoggedIn = window.checkoutConfig.isCustomerLoggedIn;

blockLoader(config.loaderFile);
url.setBaseUrl(config.baseUrl);
};
});