diff --git a/.travis.yml b/.travis.yml index 6528da8..0a52492 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,17 +3,18 @@ php: - 7.0 - 5.6 env: + - M2_VERSION=2.1.5 + - M2_VERSION=2.1.3 - M2_VERSION=2.1.2 - M2_VERSION=2.1.1 - M2_VERSION=2.1.0 - - M2_VERSION=2.0.10 + - M2_VERSION=2.0.13 matrix: include: - php: 5.5 - env: M2_VERSION=2.0.10 + env: M2_VERSION=2.0.13 cache: directories: - $HOME/.composer/cache script: - dev/ci/build.sh - diff --git a/Block/Piwik.php b/Block/Piwik.php index 5bbf394..e5a13a6 100644 --- a/Block/Piwik.php +++ b/Block/Piwik.php @@ -1,6 +1,6 @@ . + */ + +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; + } +} diff --git a/Helper/Data.php b/Helper/Data.php index 382d134..d192691 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -1,6 +1,6 @@ scopeConfig->getValue( + self::XML_PATH_UID_PROVIDER, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store + ); + } } diff --git a/Helper/Tracker.php b/Helper/Tracker.php index eb96fc6..24d3887 100644 --- a/Helper/Tracker.php +++ b/Helper/Tracker.php @@ -1,6 +1,6 @@ . + */ + +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; + } +} diff --git a/Model/Tracker.php b/Model/Tracker.php index a547cb8..b9a1a07 100644 --- a/Model/Tracker.php +++ b/Model/Tracker.php @@ -1,6 +1,6 @@ . + */ + +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, + [] + ) + ); + } +} diff --git a/Test/Unit/Helper/DataTest.php b/Test/Unit/Helper/DataTest.php index 91f05c6..fbd9a91 100644 --- a/Test/Unit/Helper/DataTest.php +++ b/Test/Unit/Helper/DataTest.php @@ -1,6 +1,6 @@ . + */ + +namespace Henhed\Piwik\UserId\Provider; + +use Magento\Customer\Api\CustomerRepositoryInterface; + +/** + * Customer email provider + * + */ +class EmailProvider implements ProviderInterface +{ + + /** + * Customer repository + * + * @var CustomerRepositoryInterface $_customerRepository + */ + protected $_customerRepository; + + /** + * Constructor + * + * @param CustomerRepositoryInterface $customerRepository + */ + public function __construct(CustomerRepositoryInterface $customerRepository) + { + $this->_customerRepository = $customerRepository; + } + + /** + * {@inheritDoc} + */ + public function getUserId($customerId) + { + try { + return $this->_customerRepository->getById($customerId)->getEmail(); + } catch (\Exception $e) { + return false; + } + } + + /** + * {@inheritDoc} + */ + public function getTitle() + { + return __('Customer E-mail'); + } +} diff --git a/UserId/Provider/EntityIdProvider.php b/UserId/Provider/EntityIdProvider.php new file mode 100644 index 0000000..5bce075 --- /dev/null +++ b/UserId/Provider/EntityIdProvider.php @@ -0,0 +1,45 @@ +. + */ + +namespace Henhed\Piwik\UserId\Provider; + +/** + * Customer entity ID provider + * + */ +class EntityIdProvider implements ProviderInterface +{ + + /** + * {@inheritDoc} + */ + public function getUserId($customerId) + { + return (string) $customerId; + } + + /** + * {@inheritDoc} + */ + public function getTitle() + { + return __('Customer Entity ID'); + } +} diff --git a/UserId/Provider/Pool.php b/UserId/Provider/Pool.php new file mode 100644 index 0000000..1e84b86 --- /dev/null +++ b/UserId/Provider/Pool.php @@ -0,0 +1,79 @@ +. + */ + +namespace Henhed\Piwik\UserId\Provider; + +/** + * User ID provider pool + * + */ +class Pool +{ + + /** + * User ID providers + * + * @var ProviderInterface[] $_providers + */ + protected $_providers = []; + + /** + * Constructor + * + * @param ProviderInterface[] $providers + * @throws \LogicException + */ + public function __construct(array $providers = []) + { + foreach ($providers as $code => $provider) { + if ($provider instanceof ProviderInterface) { + $this->_providers[$code] = $provider; + } else { + throw new \LogicException(sprintf( + '%s must implement %s', + get_class($provider), ProviderInterface::class + )); + } + } + } + + /** + * Get User ID provider by code + * + * @param string $code + * @return ProviderInterface|null + */ + public function getProviderByCode($code) + { + return isset($this->_providers[$code]) + ? $this->_providers[$code] + : null; + } + + /** + * Get all User ID providers added to this pool + * + * @return ProviderInterface[] + */ + public function getAllProviders() + { + return $this->_providers; + } +} diff --git a/UserId/Provider/ProviderInterface.php b/UserId/Provider/ProviderInterface.php new file mode 100644 index 0000000..604279a --- /dev/null +++ b/UserId/Provider/ProviderInterface.php @@ -0,0 +1,44 @@ +. + */ + +namespace Henhed\Piwik\UserId\Provider; + +/** + * User ID provider interface + * + */ +interface ProviderInterface +{ + + /** + * Returns Piwik user ID for given Magento customer ID + * + * @param int $customerId + * @return string + */ + public function getUserId($customerId); + + /** + * Get User ID provider title + * + * @return string + */ + public function getTitle(); +} diff --git a/etc/acl.xml b/etc/acl.xml index 5f55e29..c759a08 100644 --- a/etc/acl.xml +++ b/etc/acl.xml @@ -1,7 +1,7 @@ + + + + + + Henhed\Piwik\UserId\Provider\EntityIdProvider + Henhed\Piwik\UserId\Provider\EmailProvider + + + + + diff --git a/etc/frontend/di.xml b/etc/frontend/di.xml index 963ad69..8d0a059 100644 --- a/etc/frontend/di.xml +++ b/etc/frontend/di.xml @@ -1,7 +1,7 @@ + + + + + + diff --git a/etc/frontend/events.xml b/etc/frontend/events.xml index a58cb4b..fc2d6a1 100644 --- a/etc/frontend/events.xml +++ b/etc/frontend/events.xml @@ -1,7 +1,7 @@