2013-08-19 01:35:34 +02:00
|
|
|
<?php
|
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
|
|
|
* Dummy parameter token
|
|
|
|
*/
|
|
|
|
class ParameterConfirmationTokenTest_Token extends ParameterConfirmationToken implements TestOnly {
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2013-08-19 01:35:34 +02:00
|
|
|
public function currentAbsoluteURL() {
|
|
|
|
return parent::currentAbsoluteURL();
|
|
|
|
}
|
2014-07-01 09:07:13 +02:00
|
|
|
}
|
|
|
|
|
2013-08-19 01:35:34 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
/**
|
|
|
|
* A token that always validates a given token
|
|
|
|
*/
|
|
|
|
class ParameterConfirmationTokenTest_ValidToken extends ParameterConfirmationTokenTest_Token {
|
|
|
|
|
|
|
|
protected function checkToken($token) {
|
|
|
|
return true;
|
|
|
|
}
|
2013-08-19 01:35:34 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
class ParameterConfirmationTokenTest extends SapphireTest {
|
|
|
|
|
|
|
|
private function addPart($answer, $slash, $part) {
|
|
|
|
$bare = str_replace('/', '', $part);
|
|
|
|
|
|
|
|
if ($bare) $answer = array_merge($answer, array($bare));
|
|
|
|
if ($part) $slash = (substr($part, -1) == '/') ? '/' : '';
|
|
|
|
|
|
|
|
return array($answer, $slash);
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2015-05-28 00:55:18 +02:00
|
|
|
protected $oldHost = null;
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
2015-05-28 00:55:18 +02:00
|
|
|
$this->oldHost = $_SERVER['HTTP_HOST'];
|
2014-07-01 09:07:13 +02:00
|
|
|
$_GET['parameterconfirmationtokentest_notoken'] = 'value';
|
|
|
|
$_GET['parameterconfirmationtokentest_empty'] = '';
|
|
|
|
$_GET['parameterconfirmationtokentest_withtoken'] = '1';
|
|
|
|
$_GET['parameterconfirmationtokentest_withtokentoken'] = 'dummy';
|
2015-05-22 02:58:20 +02:00
|
|
|
$_GET['parameterconfirmationtokentest_nulltoken'] = '1';
|
|
|
|
$_GET['parameterconfirmationtokentest_nulltokentoken'] = null;
|
|
|
|
$_GET['parameterconfirmationtokentest_emptytoken'] = '1';
|
|
|
|
$_GET['parameterconfirmationtokentest_emptytokentoken'] = '';
|
2014-07-01 09:07:13 +02:00
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
public function tearDown() {
|
2016-07-19 03:17:07 +02:00
|
|
|
foreach($_GET as $param => $value) {
|
|
|
|
if(stripos($param, 'parameterconfirmationtokentest_') === 0) {
|
|
|
|
unset($_GET[$param]);
|
|
|
|
}
|
2014-07-01 09:07:13 +02:00
|
|
|
}
|
2015-05-28 00:55:18 +02:00
|
|
|
$_SERVER['HTTP_HOST'] = $this->oldHost;
|
2014-07-01 09:07:13 +02:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
public function testParameterDetectsParameters() {
|
|
|
|
$withoutToken = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_notoken');
|
|
|
|
$emptyParameter = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_empty');
|
|
|
|
$withToken = new ParameterConfirmationTokenTest_ValidToken('parameterconfirmationtokentest_withtoken');
|
|
|
|
$withoutParameter = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_noparam');
|
2015-05-22 02:58:20 +02:00
|
|
|
$nullToken = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_nulltoken');
|
|
|
|
$emptyToken = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_emptytoken');
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
// Check parameter
|
|
|
|
$this->assertTrue($withoutToken->parameterProvided());
|
|
|
|
$this->assertTrue($emptyParameter->parameterProvided()); // even if empty, it's still provided
|
|
|
|
$this->assertTrue($withToken->parameterProvided());
|
|
|
|
$this->assertFalse($withoutParameter->parameterProvided());
|
2015-05-22 02:58:20 +02:00
|
|
|
$this->assertTrue($nullToken->parameterProvided());
|
|
|
|
$this->assertTrue($emptyToken->parameterProvided());
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
// Check token
|
|
|
|
$this->assertFalse($withoutToken->tokenProvided());
|
|
|
|
$this->assertFalse($emptyParameter->tokenProvided());
|
2015-05-22 02:58:20 +02:00
|
|
|
$this->assertTrue($withToken->tokenProvided()); // Actually forced to true for this test
|
2014-07-01 09:07:13 +02:00
|
|
|
$this->assertFalse($withoutParameter->tokenProvided());
|
2015-05-22 02:58:20 +02:00
|
|
|
$this->assertFalse($nullToken->tokenProvided());
|
|
|
|
$this->assertFalse($emptyToken->tokenProvided());
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
// Check if reload is required
|
|
|
|
$this->assertTrue($withoutToken->reloadRequired());
|
|
|
|
$this->assertTrue($emptyParameter->reloadRequired());
|
|
|
|
$this->assertFalse($withToken->reloadRequired());
|
|
|
|
$this->assertFalse($withoutParameter->reloadRequired());
|
2015-05-22 02:58:20 +02:00
|
|
|
$this->assertTrue($nullToken->reloadRequired());
|
|
|
|
$this->assertTrue($emptyToken->reloadRequired());
|
2016-01-06 00:34:58 +01:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
// Check suppression
|
|
|
|
$this->assertTrue(isset($_GET['parameterconfirmationtokentest_notoken']));
|
|
|
|
$withoutToken->suppress();
|
|
|
|
$this->assertFalse(isset($_GET['parameterconfirmationtokentest_notoken']));
|
|
|
|
}
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2014-07-01 09:07:13 +02:00
|
|
|
public function testPrepareTokens() {
|
|
|
|
// Test priority ordering
|
|
|
|
$token = ParameterConfirmationToken::prepare_tokens(array(
|
|
|
|
'parameterconfirmationtokentest_notoken',
|
|
|
|
'parameterconfirmationtokentest_empty',
|
|
|
|
'parameterconfirmationtokentest_noparam'
|
|
|
|
));
|
|
|
|
// Test no invalid tokens
|
|
|
|
$this->assertEquals('parameterconfirmationtokentest_empty', $token->getName());
|
|
|
|
$token = ParameterConfirmationToken::prepare_tokens(array(
|
|
|
|
'parameterconfirmationtokentest_noparam'
|
|
|
|
));
|
|
|
|
$this->assertEmpty($token);
|
|
|
|
}
|
2013-08-19 01:35:34 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* currentAbsoluteURL needs to handle base or url being missing, or any combination of slashes.
|
2014-08-15 08:53:05 +02:00
|
|
|
*
|
2013-08-19 01:35:34 +02:00
|
|
|
* There should always be exactly one slash between each part in the result, and any trailing slash
|
|
|
|
* should be preserved.
|
|
|
|
*/
|
2014-07-01 09:07:13 +02:00
|
|
|
public function testCurrentAbsoluteURLHandlesSlashes() {
|
2013-08-19 01:35:34 +02:00
|
|
|
global $url;
|
|
|
|
|
|
|
|
$token = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_parameter');
|
|
|
|
|
|
|
|
foreach(array('foo','foo/') as $host) {
|
|
|
|
list($hostAnswer, $hostSlash) = $this->addPart(array(), '', $host);
|
|
|
|
|
|
|
|
foreach(array('', '/', 'bar', 'bar/', '/bar', '/bar/') as $base) {
|
|
|
|
list($baseAnswer, $baseSlash) = $this->addPart($hostAnswer, $hostSlash, $base);
|
|
|
|
|
|
|
|
foreach(array('', '/', 'baz', 'baz/', '/baz', '/baz/') as $url) {
|
|
|
|
list($urlAnswer, $urlSlash) = $this->addPart($baseAnswer, $baseSlash, $url);
|
|
|
|
|
|
|
|
$_SERVER['HTTP_HOST'] = $host;
|
|
|
|
ParameterConfirmationToken::$alternateBaseURL = $base;
|
|
|
|
|
|
|
|
$this->assertEquals('http://'.implode('/', $urlAnswer) . $urlSlash, $token->currentAbsoluteURL());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-01-06 00:34:58 +01:00
|
|
|
}
|