diff --git a/.editorconfig b/.editorconfig new file mode 100644 index 0000000..af725c6 --- /dev/null +++ b/.editorconfig @@ -0,0 +1,15 @@ +root = true + +[*] +charset = utf-8 +end_of_line = lf +insert_final_newline = true +trim_trailing_whitespace = true +indent_style = space +indent_size = 4 + +[*.md] +trim_trailing_whitespace = false + +[.travis.yml] +indent_size = 2 diff --git a/.travis.yml b/.travis.yml index 7999938..6528da8 100644 --- a/.travis.yml +++ b/.travis.yml @@ -2,11 +2,15 @@ language: php php: - 7.0 - 5.6 - - 5.5 env: - - M2_VERSION=2.0.2 - - M2_VERSION=2.0.1 - - M2_VERSION=2.0.0 + - M2_VERSION=2.1.2 + - M2_VERSION=2.1.1 + - M2_VERSION=2.1.0 + - M2_VERSION=2.0.10 +matrix: + include: + - php: 5.5 + env: M2_VERSION=2.0.10 cache: directories: - $HOME/.composer/cache diff --git a/Block/Piwik.php b/Block/Piwik.php index 16a194c..5bbf394 100644 --- a/Block/Piwik.php +++ b/Block/Piwik.php @@ -126,7 +126,7 @@ public function getJsOptions() */ public function getScriptUrl() { - return $this->_dataHelper->getBaseUrl() . 'piwik.js'; + return $this->_dataHelper->getJsScriptUrl(); } /** @@ -136,7 +136,7 @@ public function getScriptUrl() */ public function getTrackerUrl() { - return $this->_dataHelper->getBaseUrl() . 'piwik.php'; + return $this->_dataHelper->getPhpScriptUrl(); } /** diff --git a/Helper/Data.php b/Helper/Data.php index e49e286..382d134 100644 --- a/Helper/Data.php +++ b/Helper/Data.php @@ -34,6 +34,9 @@ class Data extends \Magento\Framework\App\Helper\AbstractHelper */ const XML_PATH_ENABLED = 'piwik/tracking/enabled'; const XML_PATH_HOSTNAME = 'piwik/tracking/hostname'; + const XML_PATH_CDN_HOSTNAME = 'piwik/tracking/cdn_hostname'; + const XML_PATH_JS_SCRIPT_PATH = 'piwik/tracking/js_script_path'; + const XML_PATH_PHP_SCRIPT_PATH = 'piwik/tracking/php_script_path'; 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'; @@ -63,27 +66,127 @@ public function isTrackingEnabled($store = null) */ public function getHostname($store = null) { - return $this->scopeConfig->getValue( + return trim($this->scopeConfig->getValue( self::XML_PATH_HOSTNAME, \Magento\Store\Model\ScopeInterface::SCOPE_STORE, $store - ); + )); } /** - * Retrieve Piwik base URL + * Retrieve Piwik CDN hostname * * @param null|string|bool|int|Store $store + * @return string + */ + public function getCdnHostname($store = null) + { + return trim($this->scopeConfig->getValue( + self::XML_PATH_CDN_HOSTNAME, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store + )); + } + + /** + * Retrieve base URL for given hostname + * + * @param string $host * @param null|bool $secure * @return string */ - public function getBaseUrl($store = null, $secure = null) + protected function _getBaseUrl($host, $secure = null) { if (is_null($secure)) { - $secure = $this->_request->isSecure(); + $secure = $this->_getRequest()->isSecure(); } - $host = rtrim($this->getHostname($store), '/'); - return ($secure ? 'https://' : 'http://') . $host . '/'; + if (false !== ($scheme = strpos($host, '://'))) { + $host = substr($host, $scheme + 3); + } + return ($secure ? 'https://' : 'http://') . rtrim($host, '/') . '/'; + } + + /** + * Retrieve Piwik base URL + * + * @param null|string|bool|int|Store $store + * @param null|bool $secure + * @return string + */ + public function getBaseUrl($store = null, $secure = null) + { + return $this->_getBaseUrl($this->getHostname($store), $secure); + } + + /** + * Retrieve Piwik CDN URL + * + * @param null|string|bool|int|Store $store + * @param null|bool $secure + * @return string + */ + public function getCdnBaseUrl($store = null, $secure = null) + { + $host = $this->getCdnHostname($store); + return ('' !== $host) + ? $this->_getBaseUrl($host, $secure) + : $this->getBaseUrl($store, $secure); + } + + /** + * Retrieve Piwik tracker JS script path + * + * @param null|string|bool|int|Store $store + * @return string + */ + public function getJsScriptPath($store = null) + { + return trim($this->scopeConfig->getValue( + self::XML_PATH_JS_SCRIPT_PATH, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store + ), ' /') ?: 'piwik.js'; + } + + /** + * Retrieve Piwik tracker JS script URL + * + * @param null|string|bool|int|Store $store + * @param null|bool $secure + * @return string + */ + public function getJsScriptUrl($store = null, $secure = null) + { + return $this->getCdnBaseUrl($store, $secure) + . $this->getJsScriptPath($store); + } + + /** + * Retrieve Piwik tracker PHP script path + * + * @param null|string|bool|int|Store $store + * @return string + */ + public function getPhpScriptPath($store = null) + { + return trim($this->scopeConfig->getValue( + self::XML_PATH_PHP_SCRIPT_PATH, + \Magento\Store\Model\ScopeInterface::SCOPE_STORE, + $store + ), ' /') ?: 'piwik.php'; + } + + /** + * Retrieve Piwik tracker PHP script URL + * + * @param null|string|bool|int|Store $store + * @param null|bool $secure + * @return string + */ + public function getPhpScriptUrl($store = null, $secure = null) + { + return $this->getBaseUrl($store, $secure) + . $this->getPhpScriptPath($store); } /** diff --git a/README.md b/README.md index 2eef36c..29a6777 100644 --- a/README.md +++ b/README.md @@ -33,8 +33,7 @@ git clone https://github.com/henkelund/magento2-henhed-piwik.git app/code/Henhed Or, if you prefer, install it using [Composer][composer]. ```sh -composer config repositories.henhedpiwik git https://github.com/henkelund/magento2-henhed-piwik.git -composer require henhed/module-piwik:dev-master +composer require henhed/module-piwik ``` Finally, enable the module with the Magento CLI tool. diff --git a/Test/Unit/Helper/DataTest.php b/Test/Unit/Helper/DataTest.php index 761952e..91f05c6 100644 --- a/Test/Unit/Helper/DataTest.php +++ b/Test/Unit/Helper/DataTest.php @@ -77,12 +77,16 @@ public function setUp() * @param string $siteId * @param string $linkEnabled * @param string $linkDelay + * @param string $phpScriptPath + * @param string $jsScriptPath + * @param string $cdnHostname * @param string $scope * @param null|string|bool|int|Store $store */ protected function _prepareScopeConfigMock($enabled = null, $hostname = null, $siteId = null, $linkEnabled = null, - $linkDelay = null, $scope = ScopeInterface::SCOPE_STORE, $store = null + $linkDelay = null, $phpScriptPath = null, $jsScriptPath = null, + $cdnHostname = null, $scope = ScopeInterface::SCOPE_STORE, $store = null ) { $this->_scopeConfigMock ->expects($this->any()) @@ -113,6 +117,18 @@ protected function _prepareScopeConfigMock($enabled = null, [ \Henhed\Piwik\Helper\Data::XML_PATH_LINK_DELAY, $scope, $store, $linkDelay + ], + [ + \Henhed\Piwik\Helper\Data::XML_PATH_PHP_SCRIPT_PATH, + $scope, $store, $phpScriptPath + ], + [ + \Henhed\Piwik\Helper\Data::XML_PATH_JS_SCRIPT_PATH, + $scope, $store, $jsScriptPath + ], + [ + \Henhed\Piwik\Helper\Data::XML_PATH_CDN_HOSTNAME, + $scope, $store, $cdnHostname ] ])); } @@ -127,6 +143,7 @@ public function isTrackingEnabledDataProvider() return [ [true, 'piwik.example.org', 1, true], [true, '', 1, false], + [true, ' ', 1, false], [true, 'example.org/piwik', 0, false], [false, 'piwik.org', 1, false] ]; @@ -152,38 +169,131 @@ public function testIsTrackingEnabled($enabled, $hostname, $siteId, } /** - * Data provider for `testGetBaseUrl' + * Data provider for `testGetPhpScriptUrl' * * @return array */ - public function baseUrlDataProvider() + public function phpScriptUrlDataProvider() { return [ - ['piwik.org', false, 'http://piwik.org/'], - ['piwik.org', true, 'https://piwik.org/'], - ['example.org/piwik', false, 'http://example.org/piwik/'], - ['example.org/piwik/', true, 'https://example.org/piwik/'] + [ + 'piwik.org', + false, // should prepend `http://' + null, // should fall back on `piwik.php' + // Expected result + 'http://piwik.org/piwik.php' + ], + [ + 'example.com/piwik', + true, // should prepend `https://' + 'tracker.php', // should override `piwik.php' + // Expected result + 'https://example.com/piwik/tracker.php' + ], + [ + ' https://example.com/ ', // should be trimmed + false, // should replace `https://' with `http://' + ' /piwik/tracker.php ', // should be trimmed + // Expected result + 'http://example.com/piwik/tracker.php' + ] ]; } /** - * Test \Henhed\Piwik\Helper\Data::getBaseUrl - * - * Also covers `getHostname' + * Test \Henhed\Piwik\Helper\Data::getPhpScriptUrl * * @param string $hostname * @param bool $isSecure + * @param string $phpScriptPath * @param string $returnValue - * @dataProvider baseUrlDataProvider + * @dataProvider phpScriptUrlDataProvider + */ + public function testGetPhpScriptUrl($hostname, $isSecure, $phpScriptPath, + $returnValue + ) { + $this->_prepareScopeConfigMock( + null, + $hostname, + null, null, null, + $phpScriptPath + ); + + // Test explicit `isSecure' + $this->assertEquals( + $returnValue, + $this->_helper->getPhpScriptUrl(null, $isSecure) + ); + + // Test implicit `isSecure' + $this->_requestMock + ->expects($this->once()) + ->method('isSecure') + ->will($this->returnValue($isSecure)); + + $this->assertEquals($returnValue, $this->_helper->getPhpScriptUrl()); + } + + /** + * Data provider for `testGetJsScriptUrl' + * + * @return array */ - public function testGetBaseUrl($hostname, $isSecure, $returnValue) + public function jsScriptUrlDataProvider() { - $this->_prepareScopeConfigMock(null, $hostname); + return [ + [ + 'piwik.org', + false, // should prepend `http://' + null, // should fall back on `piwik.js' + null, // should fall back on regular hostname + // Expected result + 'http://piwik.org/piwik.js' + ], + [ + ' piwik.org/path/ ', // should be trimmed + true, // should prepend `https://' + 'example.js', // should override `piwik.js' + null, // should fall back on hostname + // Expected result + 'https://piwik.org/path/example.js' + ], + [ + 'piwik.org', // should be ignored + true, // should replace `http://' with `https://'' + ' /to/tracker.js ', // should be trimmed + 'http://cdn.example.com/path/', // should override hostname + // Expected result + 'https://cdn.example.com/path/to/tracker.js' + ] + ]; + } + + /** + * Test \Henhed\Piwik\Helper\Data::getJsScriptUrl + * + * @param string $hostname + * @param bool $isSecure + * @param string $jsScriptPath + * @param string $cdnHostname + * @param string $returnValue + * @dataProvider jsScriptUrlDataProvider + */ + public function testGetJsScriptUrl($hostname, $isSecure, $jsScriptPath, + $cdnHostname, $returnValue + ) { + $this->_prepareScopeConfigMock( + null, + $hostname, + null, null, null, null, + $jsScriptPath, + $cdnHostname + ); // Test explicit `isSecure' $this->assertEquals( $returnValue, - $this->_helper->getBaseUrl(null, $isSecure) + $this->_helper->getJsScriptUrl(null, $isSecure) ); // Test implicit `isSecure' @@ -192,7 +302,7 @@ public function testGetBaseUrl($hostname, $isSecure, $returnValue) ->method('isSecure') ->will($this->returnValue($isSecure)); - $this->assertEquals($returnValue, $this->_helper->getBaseUrl()); + $this->assertEquals($returnValue, $this->_helper->getJsScriptUrl()); } /** @@ -206,6 +316,7 @@ public function isLinkTrackingEnabledDataProvider() [true, true, 'piwik.example.org', 1, true], [false, true, 'piwik.example.org', 2, false], [true, true, '', 1, false], + [true, true, ' ', 1, false], [false, true, 'example.org/piwik', 0, false], [true, false, 'piwik.org', 1, false] ]; diff --git a/etc/adminhtml/system.xml b/etc/adminhtml/system.xml index 547eaba..23d30f0 100644 --- a/etc/adminhtml/system.xml +++ b/etc/adminhtml/system.xml @@ -107,6 +107,53 @@ validate-digits validate-zero-or-greater + + + + 1 + + + + piwik/tracking/php_script_path + Path to the Piwik tracker PHP script. Usually "piwik.php". + required-entry + + + + piwik/tracking/js_script_path + Path to the Piwik tracker Javascript. Usually "piwik.js". + required-entry + + + + piwik/tracking/cdn_hostname + Hostname for serving the Piwik tracker Javascript. May be left empty in which case the regular hostname will be used. + + diff --git a/etc/config.xml b/etc/config.xml index d75777e..e7e555d 100644 --- a/etc/config.xml +++ b/etc/config.xml @@ -29,6 +29,9 @@ 1 1 500 + piwik.php + piwik.js + diff --git a/i18n/en_US.csv b/i18n/en_US.csv index 235445b..702209f 100644 --- a/i18n/en_US.csv +++ b/i18n/en_US.csv @@ -11,3 +11,10 @@ "Enable tracking of outlinks and downloads","Enable tracking of outlinks and downloads" "Link Tracking Timer","Link Tracking Timer" "Delay for link tracking in milliseconds","Delay for link tracking in milliseconds" +"Advanced Options","Advanced Options" +"Javascript Path","Javascript Path" +"Path to the Piwik tracker Javascript. Usually ""piwik.js"".","Path to the Piwik tracker Javascript. Usually ""piwik.js""." +"PHP Script Path","PHP Script Path" +"Path to the Piwik tracker PHP script. Usually ""piwik.php"".","Path to the Piwik tracker PHP script. Usually ""piwik.php""." +"CDN Hostname","CDN Hostname" +"Hostname for serving the Piwik tracker Javascript. May be left empty in which case the regular hostname will be used.","Hostname for serving the Piwik tracker Javascript. May be left empty in which case the regular hostname will be used." diff --git a/i18n/sv_SE.csv b/i18n/sv_SE.csv index dad6884..c2dd210 100644 --- a/i18n/sv_SE.csv +++ b/i18n/sv_SE.csv @@ -11,3 +11,10 @@ "Enable tracking of outlinks and downloads","Aktivera spårning av utlänkar och nedladdningar" "Link Tracking Timer","Länkspårningstimer" "Delay for link tracking in milliseconds","Fördröjning av länkspårning i millisekunder" +"Advanced Options","Avancerade inställningar" +"Javascript Path","Sökväg till javascript" +"Path to the Piwik tracker Javascript. Usually ""piwik.js"".","Sökväg till Piwik-spårarens javascript. Vanligtvis ""piwik.js""." +"PHP Script Path","Sökväg till PHP-skript" +"Path to the Piwik tracker PHP script. Usually ""piwik.php"".","Sökväg till Piwik-spårarens PHP-skript. Vanligtvis ""piwik.php""." +"CDN Hostname","CDN-värdnamn" +"Hostname for serving the Piwik tracker Javascript. May be left empty in which case the regular hostname will be used.","Värdnamn för Piwik-spårarens javascript. Lämna tomt för att använda det allmänna värdnamnet."