-
Notifications
You must be signed in to change notification settings - Fork 37
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 #13 from henkelund/user-id
User ID tracking
- Loading branch information
Showing
15 changed files
with
720 additions
and
3 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
<?php | ||
/** | ||
* Copyright 2016-2017 Henrik Hedelund | ||
* | ||
* This file is part of Henhed_Piwik. | ||
* | ||
* Henhed_Piwik is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Henhed_Piwik is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Henhed_Piwik. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Henhed\Piwik\CustomerData\Customer; | ||
|
||
/** | ||
* Plugin for \Magento\Customer\CustomerData\Customer | ||
* | ||
*/ | ||
class CustomerPlugin | ||
{ | ||
|
||
/** | ||
* Current customer helper | ||
* | ||
* @var \Magento\Customer\Helper\Session\CurrentCustomer $_currentCustomer | ||
*/ | ||
protected $_currentCustomer; | ||
|
||
/** | ||
* Piwik data helper | ||
* | ||
* @var \Henhed\Piwik\Helper\Data $_dataHelper | ||
*/ | ||
protected $_dataHelper; | ||
|
||
/** | ||
* User ID provider pool | ||
* | ||
* @var \Henhed\Piwik\UserId\Provider\Pool $_uidProviderPool | ||
*/ | ||
protected $_uidProviderPool; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer | ||
* @param \Henhed\Piwik\Helper\Data $dataHelper | ||
* @param \Henhed\Piwik\UserId\Provider\Pool $uidProviderPool | ||
*/ | ||
public function __construct( | ||
\Magento\Customer\Helper\Session\CurrentCustomer $currentCustomer, | ||
\Henhed\Piwik\Helper\Data $dataHelper, | ||
\Henhed\Piwik\UserId\Provider\Pool $uidProviderPool | ||
) { | ||
$this->_currentCustomer = $currentCustomer; | ||
$this->_dataHelper = $dataHelper; | ||
$this->_uidProviderPool = $uidProviderPool; | ||
} | ||
|
||
/** | ||
* Get configured Piwik User ID provider or NULL | ||
* | ||
* @return \Henhed\Piwik\UserId\Provider\ProviderInterface|null | ||
*/ | ||
protected function _getUserIdProvider() | ||
{ | ||
$code = $this->_dataHelper->getUserIdProviderCode(); | ||
return $code ? $this->_uidProviderPool->getProviderByCode($code) : null; | ||
} | ||
|
||
/** | ||
* Get Piwik User ID for current customer | ||
* | ||
* @return string | ||
*/ | ||
protected function _getUserId() | ||
{ | ||
$provider = $this->_getUserIdProvider(); | ||
$customerId = $this->_currentCustomer->getCustomerId(); | ||
return ($provider && $customerId) | ||
? (string) $provider->getUserId($customerId) | ||
: ''; | ||
} | ||
|
||
/** | ||
* Add visitor related tracker information to customer section data. | ||
* | ||
* @param \Magento\Customer\CustomerData\Customer $subject | ||
* @param array $result | ||
* @return array | ||
*/ | ||
public function afterGetSectionData( | ||
\Magento\Customer\CustomerData\Customer $subject, | ||
$result | ||
) { | ||
if ($this->_dataHelper->isTrackingEnabled()) { | ||
$userId = $this->_getUserId(); | ||
if ($userId !== '') { | ||
$result['piwikUserId'] = $userId; | ||
} | ||
} | ||
return $result; | ||
} | ||
} |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
<?php | ||
/** | ||
* Copyright 2016-2017 Henrik Hedelund | ||
* | ||
* This file is part of Henhed_Piwik. | ||
* | ||
* Henhed_Piwik is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Henhed_Piwik is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Henhed_Piwik. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Henhed\Piwik\Model\Config\Source\UserId; | ||
|
||
/** | ||
* User ID provider config source model | ||
* | ||
*/ | ||
class Provider implements \Magento\Framework\Option\ArrayInterface | ||
{ | ||
|
||
/** | ||
* User ID provider pool | ||
* | ||
* @var \Henhed\Piwik\UserId\Provider\Pool $_pool | ||
*/ | ||
protected $_pool; | ||
|
||
/** | ||
* Constructor | ||
* | ||
* @param \Henhed\Piwik\UserId\Provider\Pool $pool | ||
*/ | ||
public function __construct(\Henhed\Piwik\UserId\Provider\Pool $pool) | ||
{ | ||
$this->_pool = $pool; | ||
} | ||
|
||
/** | ||
* Return array of user ID providers as value-label pairs | ||
* | ||
* @return array | ||
*/ | ||
public function toOptionArray() | ||
{ | ||
$options = [['value' => '', 'label' => __('No')]]; | ||
foreach ($this->_pool->getAllProviders() as $code => $provider) { | ||
$options[] = [ | ||
'value' => $code, | ||
'label' => sprintf('%s (%s)', __('Yes'), $provider->getTitle()) | ||
]; | ||
} | ||
return $options; | ||
} | ||
} |
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,175 @@ | ||
<?php | ||
/** | ||
* Copyright 2016-2017 Henrik Hedelund | ||
* | ||
* This file is part of Henhed_Piwik. | ||
* | ||
* Henhed_Piwik is free software: you can redistribute it and/or modify | ||
* it under the terms of the GNU Affero General Public License as published by | ||
* the Free Software Foundation, either version 3 of the License, or | ||
* (at your option) any later version. | ||
* | ||
* Henhed_Piwik is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
* GNU Affero General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Affero General Public License | ||
* along with Henhed_Piwik. If not, see <http://www.gnu.org/licenses/>. | ||
*/ | ||
|
||
namespace Henhed\Piwik\Test\Unit\CustomerData\Customer; | ||
|
||
use Magento\Framework\TestFramework\Unit\Helper\ObjectManager; | ||
|
||
/** | ||
* Test for \Henhed\Piwik\CustomerData\Customer\CustomerPlugin | ||
* | ||
*/ | ||
class CustomerPluginTest extends \PHPUnit_Framework_TestCase | ||
{ | ||
|
||
/** | ||
* Customer data plugin (test subject) instance | ||
* | ||
* @var \Henhed\Piwik\CustomerData\Customer\CustomerPlugin $_customerPlugin | ||
*/ | ||
protected $_customerPlugin; | ||
|
||
/** | ||
* Current customer helper mock object | ||
* | ||
* @var \PHPUnit_Framework_MockObject_MockObject $_currentCustomerMock | ||
*/ | ||
protected $_currentCustomerMock; | ||
|
||
/** | ||
* Piwik data helper mock object | ||
* | ||
* @var \PHPUnit_Framework_MockObject_MockObject $_dataHelperMock | ||
*/ | ||
protected $_dataHelperMock; | ||
|
||
/** | ||
* Piwik user ID provider pool mock object | ||
* | ||
* @var \PHPUnit_Framework_MockObject_MockObject $_uidProviderPoolMock | ||
*/ | ||
protected $_uidProviderPoolMock; | ||
|
||
/** | ||
* Piwik user ID provider mock object | ||
* | ||
* @var \PHPUnit_Framework_MockObject_MockObject $_uidProviderMock | ||
*/ | ||
protected $_uidProviderMock; | ||
|
||
/** | ||
* Customer data mock object | ||
* | ||
* @var \PHPUnit_Framework_MockObject_MockObject $_customerDataMock | ||
*/ | ||
protected $_customerDataMock; | ||
|
||
/** | ||
* Set up | ||
* | ||
* @return void | ||
*/ | ||
public function setUp() | ||
{ | ||
$className = 'Henhed\Piwik\CustomerData\Customer\CustomerPlugin'; | ||
$objectManager = new ObjectManager($this); | ||
$args = $objectManager->getConstructArguments($className); | ||
$this->_customerPlugin = $objectManager->getObject($className, $args); | ||
$this->_currentCustomerMock = $args['currentCustomer']; | ||
$this->_dataHelperMock = $args['dataHelper']; | ||
$this->_uidProviderPoolMock = $args['uidProviderPool']; | ||
$this->_uidProviderMock = $this->getMock( | ||
'Henhed\Piwik\UserId\Provider\ProviderInterface', | ||
['getUserId', 'getTitle'], [], '', false | ||
); | ||
$this->_customerDataMock = $this->getMock( | ||
'Magento\Customer\CustomerData\Customer', [], [], '', false | ||
); | ||
} | ||
|
||
/** | ||
* Data provider for `testafterGetSectionData' | ||
* | ||
* @return array | ||
*/ | ||
public function testafterGetSectionDataDataProvider() | ||
{ | ||
return [ | ||
[false, 1, 'p', 'UID1'], | ||
[true, null, 'p', 'UID2'], | ||
[true, 3, 'p', ''], | ||
[true, 4, null, 'UID4'], | ||
[true, 5, 'p', 'UID5'] | ||
]; | ||
} | ||
|
||
/** | ||
* Test `afterGetSectionData' | ||
* | ||
* @param boolean $enabled | ||
* @param int $customerId | ||
* @param string|null $provider | ||
* @param string $userId | ||
* @return void | ||
* @dataProvider testafterGetSectionDataDataProvider | ||
*/ | ||
public function testafterGetSectionData( | ||
$enabled, $customerId, $provider, $userId | ||
) { | ||
$expectedResult = []; | ||
if ($enabled && $customerId && $provider && $userId) { | ||
$expectedResult['piwikUserId'] = $userId; | ||
} | ||
|
||
$this->_dataHelperMock | ||
->expects($this->once()) | ||
->method('isTrackingEnabled') | ||
->willReturn($enabled); | ||
|
||
$this->_dataHelperMock | ||
->expects($enabled ? $this->once() : $this->never()) | ||
->method('getUserIdProviderCode') | ||
->willReturn($provider); | ||
|
||
$this->_currentCustomerMock | ||
->expects($enabled ? $this->once() : $this->never()) | ||
->method('getCustomerId') | ||
->willReturn($customerId); | ||
|
||
$this->_uidProviderPoolMock | ||
->expects( | ||
($enabled && $provider) | ||
? $this->once() | ||
: $this->never() | ||
) | ||
->method('getProviderByCode') | ||
->with($provider) | ||
->willReturn($this->_uidProviderMock); | ||
|
||
$this->_uidProviderMock | ||
->expects( | ||
($enabled && $customerId && $provider) | ||
? $this->once() | ||
: $this->never() | ||
) | ||
->method('getUserId') | ||
->with($customerId) | ||
->willReturn($userId); | ||
|
||
// Assert that result of plugin equals expected result | ||
$this->assertEquals( | ||
$expectedResult, | ||
$this->_customerPlugin->afterGetSectionData( | ||
$this->_customerDataMock, | ||
[] | ||
) | ||
); | ||
} | ||
} |
Oops, something went wrong.