Skip to content

Commit

Permalink
Merge pull request #622 from magento-folks/merchant_beta
Browse files Browse the repository at this point in the history
[Merchant Beta] [Folks] Bugfix
  • Loading branch information
Shkolyarenko, Serhiy(sshkolyarenko) committed Sep 24, 2015
2 parents cb271b5 + e08b407 commit 523c2eb
Show file tree
Hide file tree
Showing 10 changed files with 360 additions and 17 deletions.
4 changes: 2 additions & 2 deletions app/code/Magento/Checkout/Block/Cart/Sidebar.php
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ public function getShoppingCartUrl()
*/
public function getUpdateItemQtyUrl()
{
return $this->getUrl('checkout/sidebar/updateItemQty');
return $this->getUrl('checkout/sidebar/updateItemQty', ['_secure' => $this->getRequest()->isSecure()]);
}

/**
Expand All @@ -116,7 +116,7 @@ public function getUpdateItemQtyUrl()
*/
public function getRemoveItemUrl()
{
return $this->getUrl('checkout/sidebar/removeItem');
return $this->getUrl('checkout/sidebar/removeItem', ['_secure' => $this->getRequest()->isSecure()]);
}

/**
Expand Down
19 changes: 16 additions & 3 deletions app/code/Magento/Checkout/Test/Unit/Block/Cart/SidebarTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,16 @@ class SidebarTest extends \PHPUnit_Framework_TestCase
*/
protected $checkoutSessionMock;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $requestMock;

protected function setUp()
{
$this->_objectManager = new \Magento\Framework\TestFramework\Unit\Helper\ObjectManager($this);

$this->requestMock = $this->getMock('\Magento\Framework\App\RequestInterface');
$this->layoutMock = $this->getMock('\Magento\Framework\View\Layout', [], [], '', false);
$this->checkoutSessionMock = $this->getMock('\Magento\Checkout\Model\Session', [], [], '', false);
$this->urlBuilderMock = $this->getMock('\Magento\Framework\UrlInterface', [], [], '', false);
Expand All @@ -64,7 +70,7 @@ protected function setUp()

$contextMock = $this->getMock(
'\Magento\Framework\View\Element\Template\Context',
['getLayout', 'getUrlBuilder', 'getStoreManager', 'getScopeConfig'],
['getLayout', 'getUrlBuilder', 'getStoreManager', 'getScopeConfig', 'getRequest'],
[],
'',
false
Expand All @@ -81,6 +87,9 @@ protected function setUp()
$contextMock->expects($this->once())
->method('getScopeConfig')
->will($this->returnValue($this->scopeConfigMock));
$contextMock->expects($this->any())
->method('getRequest')
->will($this->returnValue($this->requestMock));

$this->model = $this->_objectManager->getObject(
'Magento\Checkout\Block\Cart\Sidebar',
Expand Down Expand Up @@ -131,10 +140,14 @@ public function testGetConfig()
$valueMap = [
['checkout/cart', [], $shoppingCartUrl],
['checkout', [], $checkoutUrl],
['checkout/sidebar/updateItemQty', [], $updateItemQtyUrl],
['checkout/sidebar/removeItem', [], $removeItemUrl]
['checkout/sidebar/updateItemQty', ['_secure' => false], $updateItemQtyUrl],
['checkout/sidebar/removeItem', ['_secure' => false], $removeItemUrl]
];

$this->requestMock->expects($this->any())
->method('isSecure')
->willReturn(false);

$this->urlBuilderMock->expects($this->exactly(4))
->method('getUrl')
->willReturnMap($valueMap);
Expand Down
3 changes: 3 additions & 0 deletions app/code/Magento/Checkout/etc/frontend/di.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
<argument name="secureUrlList" xsi:type="array">
<item name="checkout_index" xsi:type="string">/checkout</item>
</argument>
<argument name="excludedUrlList" xsi:type="array">
<item name="checkout_sidebar" xsi:type="string">/checkout/sidebar</item>
</argument>
</arguments>
</type>
<type name="Magento\Framework\View\Layout">
Expand Down
14 changes: 13 additions & 1 deletion app/code/Magento/Customer/Controller/Section/Load.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ class Load extends \Magento\Framework\App\Action\Action
*/
protected $resultJsonFactory;

/**
* @var Identifier
*/
protected $sectionIdentifier;

/**
* @var SectionPoolInterface
*/
Expand All @@ -28,15 +33,18 @@ class Load extends \Magento\Framework\App\Action\Action
/**
* @param Context $context
* @param JsonFactory $resultJsonFactory
* @param \Magento\Customer\CustomerData\Section\Identifier $sectionIdentifier
* @param SectionPoolInterface $sectionPool
*/
public function __construct(
Context $context,
JsonFactory $resultJsonFactory,
\Magento\Customer\CustomerData\Section\Identifier $sectionIdentifier,
SectionPoolInterface $sectionPool
) {
parent::__construct($context);
$this->resultJsonFactory = $resultJsonFactory;
$this->sectionIdentifier = $sectionIdentifier;
$this->sectionPool = $sectionPool;
}

Expand All @@ -51,7 +59,11 @@ public function execute()
$sectionNames = $this->getRequest()->getParam('sections');
$sectionNames = $sectionNames ? array_unique(\explode(',', $sectionNames)) : null;

$response = $this->sectionPool->getSectionsData($sectionNames);
$updateSectionId = $this->getRequest()->getParam('update_section_id');
if ('false' == $updateSectionId) {
$updateSectionId = false;
}
$response = $this->sectionPool->getSectionsData($sectionNames, (bool)$updateSectionId);
} catch (\Exception $e) {
$resultJson->setStatusHeader(
\Zend\Http\Response::STATUS_CODE_400,
Expand Down
88 changes: 88 additions & 0 deletions app/code/Magento/Customer/CustomerData/Section/Identifier.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/
namespace Magento\Customer\CustomerData\Section;

/**
* Customer section identifier
*/
class Identifier
{
const COOKIE_KEY = 'storage_data_id';

const SECTION_KEY = 'data_id';

const UPDATE_MARK = 'sections_updated';

/**
* @var int
*/
protected $markId;

/**
* @var \Magento\Framework\Stdlib\Cookie\PhpCookieManager
*/
protected $cookieManager;

/**
* @var \Magento\Framework\Session\Config\ConfigInterface
*/
protected $sessionConfig;

/**
* @param \Magento\Framework\Stdlib\Cookie\PhpCookieManager $cookieManager
*/
public function __construct(
\Magento\Framework\Stdlib\Cookie\PhpCookieManager $cookieManager
) {
$this->cookieManager = $cookieManager;
}

/**
* Init mark(identifier) for sections
*
* @param bool $forceUpdate
* @return int
*/
public function initMark($forceUpdate)
{
if ($forceUpdate) {
$this->markId = time();
return $this->markId;
}

$cookieMarkId = false;
if (!$this->markId) {
$cookieMarkId = $this->cookieManager->getCookie(self::COOKIE_KEY);
}

$this->markId = $cookieMarkId ? $cookieMarkId : time();

return $this->markId;
}

/**
* Mark sections with data id
*
* @param array $sectionsData
* @param null $sectionNames
* @param bool $updateIds
* @return array
*/
public function markSections(array $sectionsData, $sectionNames = null, $updateIds = false)
{
if (!$sectionNames) {
$sectionNames = array_keys($sectionsData);
}
$markId = $this->initMark($updateIds);

foreach ($sectionNames as $name) {
if ($updateIds || !array_key_exists(self::SECTION_KEY, $sectionsData[$name])) {
$sectionsData[$name][self::SECTION_KEY] = $markId;
}
}
return $sectionsData;
}
}
20 changes: 16 additions & 4 deletions app/code/Magento/Customer/CustomerData/SectionPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,24 +27,36 @@ class SectionPool implements SectionPoolInterface
*/
protected $sectionSourceMap;

/**
* @var \Magento\Customer\CustomerData\Section\Identifier
*/
protected $identifier;

/**
* Construct
*
* @param ObjectManagerInterface $objectManager
* @param \Magento\Customer\CustomerData\Section\Identifier $identifier
* @param array $sectionSourceMap
*/
public function __construct(ObjectManagerInterface $objectManager, array $sectionSourceMap = [])
{
public function __construct(
ObjectManagerInterface $objectManager,
\Magento\Customer\CustomerData\Section\Identifier $identifier,
array $sectionSourceMap = []
) {
$this->objectManager = $objectManager;
$this->identifier = $identifier;
$this->sectionSourceMap = $sectionSourceMap;
}

/**
* {@inheritdoc}
*/
public function getSectionsData(array $sectionNames = null)
public function getSectionsData(array $sectionNames = null, $updateIds = false)
{
return $sectionNames ? $this->getSectionDataByNames($sectionNames) : $this->getAllSectionData();
$sectionsData = $sectionNames ? $this->getSectionDataByNames($sectionNames) : $this->getAllSectionData();
$sectionsData = $this->identifier->markSections($sectionsData, $sectionNames, $updateIds);
return $sectionsData;
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ interface SectionPoolInterface
* Get section data by section names. If $sectionNames is null then return all sections data
*
* @param array $sectionNames
* @param bool $updateIds
* @return array
*/
public function getSectionsData(array $sectionNames = null);
public function getSectionsData(array $sectionNames = null, $updateIds = false);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* Copyright © 2015 Magento. All rights reserved.
* See COPYING.txt for license details.
*/

namespace Magento\Customer\Test\Unit\CustomerData\Section;

use \Magento\Customer\CustomerData\Section\Identifier;

class IdentifierTest extends \PHPUnit_Framework_TestCase
{
/**
* @var \Magento\Customer\CustomerData\Section\Identifier
*/
protected $model;

/**
* @var \PHPUnit_Framework_MockObject_MockObject
*/
protected $cookieManMock;

/**
* @var string
*/
protected $cookieMarkId;

protected function setUp()
{
$this->cookieManMock = $this->getMock('Magento\Framework\Stdlib\Cookie\PhpCookieManager', [], [], '', false);
$this->cookieMarkId = '123456';
$this->model = new Identifier(
$this->cookieManMock
);
}

public function testInitMark()
{
$this->cookieManMock->expects($this->once())
->method('getCookie')
->with(Identifier::COOKIE_KEY)
->willReturn($this->cookieMarkId);
$this->assertEquals($this->cookieMarkId, $this->model->initMark(false));
}

public function testMarkSectionsDontUpdate()
{
$sectionsData = [
'section1' => [1],
'section2' => [2],
'section3' => [3],
];

$expectedData = [
'section1' => [1, 'data_id' => $this->cookieMarkId],
'section2' => [2, 'data_id' => $this->cookieMarkId],
'section3' => [3],
];
$sectionNames = ['section1', 'section2'];

$this->cookieManMock->expects($this->once())
->method('getCookie')
->with(Identifier::COOKIE_KEY)
->willReturn($this->cookieMarkId);

// third parameter is true to avoid diving deeply into initMark()
$result = $this->model->markSections($sectionsData, $sectionNames, false);
$this->assertEquals($expectedData, $result);
}

public function testMarkSectionsUpdate()
{
$sectionsData = [
'section1' => [1, 'data_id' => 0],
'section2' => [2, 'data_id' => 0],
'section3' => [3],
];
$sectionNames = ['section1', 'section2'];

// third parameter is true to avoid diving deeply into initMark()
$result = $this->model->markSections($sectionsData, $sectionNames, true);
$this->assertArrayHasKey('data_id', $result['section1']);
$this->assertNotEquals(0, $result['section1']['data_id']);
$this->assertArrayHasKey('data_id', $result['section2']);
$this->assertNotEquals(0, $result['section2']['data_id']);
}
}
Loading

0 comments on commit 523c2eb

Please sign in to comment.