Skip to content

Commit

Permalink
Merge pull request #13 from henkelund/user-id
Browse files Browse the repository at this point in the history
User ID tracking
  • Loading branch information
henkelund authored Feb 28, 2017
2 parents d94793a + 3cc9128 commit 3b24cb1
Show file tree
Hide file tree
Showing 15 changed files with 720 additions and 3 deletions.
112 changes: 112 additions & 0 deletions CustomerData/Customer/CustomerPlugin.php
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;
}
}
16 changes: 16 additions & 0 deletions Helper/Data.php
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper
const XML_PATH_SITE_ID = 'piwik/tracking/site_id';
const XML_PATH_LINK_ENABLED = 'piwik/tracking/link_enabled';
const XML_PATH_LINK_DELAY = 'piwik/tracking/link_delay';
const XML_PATH_UID_PROVIDER = 'piwik/tracking/uid_provider';

/**
* Check if Piwik is enabled
Expand Down Expand Up @@ -233,4 +234,19 @@ public function getLinkTrackingDelay($store = null)
$store
);
}

/**
* Get provider code for Piwik user ID tracking
*
* @param null|string|bool|int|Store $store
* @return string
*/
public function getUserIdProviderCode($store = null)
{
return $this->scopeConfig->getValue(
self::XML_PATH_UID_PROVIDER,
\Magento\Store\Model\ScopeInterface::SCOPE_STORE,
$store
);
}
}
63 changes: 63 additions & 0 deletions Model/Config/Source/UserId/Provider.php
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;
}
}
175 changes: 175 additions & 0 deletions Test/Unit/CustomerData/Customer/CustomerPluginTest.php
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,
[]
)
);
}
}
Loading

0 comments on commit 3b24cb1

Please sign in to comment.