2013-08-19 01:35:34 +02:00
|
|
|
<?php
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
namespace SilverStripe\Core\Tests\Startup;
|
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\Controller;
|
|
|
|
use SilverStripe\Control\HTTPRequest;
|
|
|
|
use SilverStripe\Control\Session;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Core\Startup\ParameterConfirmationToken;
|
2016-12-16 05:34:21 +01:00
|
|
|
use SilverStripe\Core\Tests\Startup\ParameterConfirmationTokenTest\ParameterConfirmationTokenTest_Token;
|
|
|
|
use SilverStripe\Core\Tests\Startup\ParameterConfirmationTokenTest\ParameterConfirmationTokenTest_ValidToken;
|
2016-08-19 00:51:35 +02:00
|
|
|
use SilverStripe\Dev\SapphireTest;
|
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class ParameterConfirmationTokenTest extends SapphireTest
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
/**
|
|
|
|
* @var HTTPRequest
|
|
|
|
*/
|
|
|
|
protected $request = null;
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-03-24 04:00:54 +01:00
|
|
|
protected function setUp()
|
2016-12-16 05:34:21 +01:00
|
|
|
{
|
|
|
|
parent::setUp();
|
2017-06-22 12:50:45 +02:00
|
|
|
$get = [];
|
|
|
|
$get['parameterconfirmationtokentest_notoken'] = 'value';
|
|
|
|
$get['parameterconfirmationtokentest_empty'] = '';
|
|
|
|
$get['parameterconfirmationtokentest_withtoken'] = '1';
|
|
|
|
$get['parameterconfirmationtokentest_withtokentoken'] = 'dummy';
|
|
|
|
$get['parameterconfirmationtokentest_nulltoken'] = '1';
|
|
|
|
$get['parameterconfirmationtokentest_nulltokentoken'] = null;
|
|
|
|
$get['parameterconfirmationtokentest_emptytoken'] = '1';
|
|
|
|
$get['parameterconfirmationtokentest_emptytokentoken'] = '';
|
|
|
|
$this->request = new HTTPRequest('GET', '/', $get);
|
|
|
|
$this->request->setSession(new Session([]));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testParameterDetectsParameters()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$withoutToken = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_notoken', $this->request);
|
|
|
|
$emptyParameter = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_empty', $this->request);
|
|
|
|
$withToken = new ParameterConfirmationTokenTest_ValidToken('parameterconfirmationtokentest_withtoken', $this->request);
|
|
|
|
$withoutParameter = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_noparam', $this->request);
|
|
|
|
$nullToken = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_nulltoken', $this->request);
|
|
|
|
$emptyToken = new ParameterConfirmationTokenTest_Token('parameterconfirmationtokentest_emptytoken', $this->request);
|
2016-12-16 05:34:21 +01: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());
|
|
|
|
$this->assertTrue($nullToken->parameterProvided());
|
|
|
|
$this->assertTrue($emptyToken->parameterProvided());
|
|
|
|
|
|
|
|
// Check token
|
|
|
|
$this->assertFalse($withoutToken->tokenProvided());
|
|
|
|
$this->assertFalse($emptyParameter->tokenProvided());
|
|
|
|
$this->assertTrue($withToken->tokenProvided()); // Actually forced to true for this test
|
|
|
|
$this->assertFalse($withoutParameter->tokenProvided());
|
|
|
|
$this->assertFalse($nullToken->tokenProvided());
|
|
|
|
$this->assertFalse($emptyToken->tokenProvided());
|
|
|
|
|
|
|
|
// Check if reload is required
|
|
|
|
$this->assertTrue($withoutToken->reloadRequired());
|
|
|
|
$this->assertTrue($emptyParameter->reloadRequired());
|
|
|
|
$this->assertFalse($withToken->reloadRequired());
|
|
|
|
$this->assertFalse($withoutParameter->reloadRequired());
|
|
|
|
$this->assertTrue($nullToken->reloadRequired());
|
|
|
|
$this->assertTrue($emptyToken->reloadRequired());
|
|
|
|
|
|
|
|
// Check suppression
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertEquals('value', $this->request->getVar('parameterconfirmationtokentest_notoken'));
|
2016-12-16 05:34:21 +01:00
|
|
|
$withoutToken->suppress();
|
2017-06-22 12:50:45 +02:00
|
|
|
$this->assertNull($this->request->getVar('parameterconfirmationtokentest_notoken'));
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testPrepareTokens()
|
|
|
|
{
|
|
|
|
// Test priority ordering
|
|
|
|
$token = ParameterConfirmationToken::prepare_tokens(
|
2017-06-22 12:50:45 +02:00
|
|
|
[
|
|
|
|
'parameterconfirmationtokentest_notoken',
|
|
|
|
'parameterconfirmationtokentest_empty',
|
|
|
|
'parameterconfirmationtokentest_noparam'
|
|
|
|
],
|
|
|
|
$this->request
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
// Test no invalid tokens
|
|
|
|
$this->assertEquals('parameterconfirmationtokentest_empty', $token->getName());
|
|
|
|
$token = ParameterConfirmationToken::prepare_tokens(
|
2017-06-22 12:50:45 +02:00
|
|
|
[ 'parameterconfirmationtokentest_noparam' ],
|
|
|
|
$this->request
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$this->assertEmpty($token);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* currentAbsoluteURL needs to handle base or url being missing, or any combination of slashes.
|
|
|
|
*
|
|
|
|
* There should always be exactly one slash between each part in the result, and any trailing slash
|
|
|
|
* should be preserved.
|
|
|
|
*/
|
|
|
|
public function testCurrentAbsoluteURLHandlesSlashes()
|
|
|
|
{
|
2017-06-22 12:50:45 +02:00
|
|
|
$token = new ParameterConfirmationTokenTest_Token(
|
|
|
|
'parameterconfirmationtokentest_parameter',
|
|
|
|
$this->request
|
|
|
|
);
|
2016-12-16 05:34:21 +01:00
|
|
|
|
2017-06-22 12:50:45 +02:00
|
|
|
foreach (array('', '/', 'bar', 'bar/', '/bar', '/bar/') as $url) {
|
|
|
|
$this->request->setUrl($url);
|
|
|
|
$expected = rtrim(Controller::join_links(BASE_URL, '/', $url), '/') ?: '/';
|
|
|
|
$this->assertEquals($expected, $token->currentURL(), "Invalid redirect for request url $url");
|
2016-12-16 05:34:21 +01:00
|
|
|
}
|
|
|
|
}
|
2016-01-06 00:34:58 +01:00
|
|
|
}
|