Skip to content

Commit

Permalink
Update data helper unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
henkelund committed Oct 30, 2016
1 parent 2c14686 commit 6e5ab45
Showing 1 changed file with 126 additions and 15 deletions.
141 changes: 126 additions & 15 deletions Test/Unit/Helper/DataTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down Expand Up @@ -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
]
]));
}
Expand All @@ -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]
];
Expand All @@ -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'
Expand All @@ -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());
}

/**
Expand All @@ -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]
];
Expand Down

0 comments on commit 6e5ab45

Please sign in to comment.