-
Notifications
You must be signed in to change notification settings - Fork 9.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #622 from magento-folks/merchant_beta
[Merchant Beta] [Folks] Bugfix
- Loading branch information
Showing
10 changed files
with
360 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
app/code/Magento/Customer/CustomerData/Section/Identifier.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
87 changes: 87 additions & 0 deletions
87
app/code/Magento/Customer/Test/Unit/CustomerData/Section/IdentifierTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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']); | ||
} | ||
} |
Oops, something went wrong.