Update API usages

This commit is contained in:
Damian Mooyman 2017-06-15 18:56:48 +12:00
parent d1d4375c95
commit b65c21241b
3 changed files with 78 additions and 64 deletions

View File

@ -518,7 +518,7 @@ class DirectorTest extends SapphireTest
'Content-Length' => '10'
);
$this->assertEquals($headers, Director::extract_request_headers($request));
$this->assertEquals($headers, HTTPRequest::extractRequestHeaders($request));
}
public function testUnmatchedRequestReturns404()
@ -622,7 +622,7 @@ class DirectorTest extends SapphireTest
$filter->failPost = true;
$this->setExpectedException(HTTPResponse_Exception::class);
$this->expectException(HTTPResponse_Exception::class);
Director::test('some-dummy-url');

View File

@ -2,10 +2,11 @@
namespace SilverStripe\Control\Tests;
use SilverStripe\Control\Director;
use SilverStripe\Dev\FunctionalTest;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Control\HTTP;
use SilverStripe\Control\HTTPResponse;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Core\Kernel;
use SilverStripe\Dev\FunctionalTest;
/**
* Tests the {@link HTTP} class
@ -27,13 +28,15 @@ class HTTPTest extends FunctionalTest
$this->assertNotEmpty($response->getHeader('Cache-Control'));
// Ensure max-age is zero for development.
Director::set_environment_type('dev');
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$kernel->setEnvironment(Kernel::DEV);
$response = new HTTPResponse($body, 200);
HTTP::add_cache_headers($response);
$this->assertContains('max-age=0', $response->getHeader('Cache-Control'));
// Ensure max-age setting is respected in production.
Director::set_environment_type('live');
$kernel->setEnvironment(Kernel::LIVE);
$response = new HTTPResponse($body, 200);
HTTP::add_cache_headers($response);
$this->assertContains('max-age=30', explode(', ', $response->getHeader('Cache-Control')));
@ -57,9 +60,11 @@ class HTTPTest extends FunctionalTest
public function testConfigVary()
{
/** @var Kernel $kernel */
$kernel = Injector::inst()->get(Kernel::class);
$body = "<html><head></head><body><h1>Mysite</h1></body></html>";
$response = new HTTPResponse($body, 200);
Director::set_environment_type('live');
$kernel->setEnvironment(Kernel::LIVE);
HTTP::set_cache_age(30);
HTTP::add_cache_headers($response);
@ -206,30 +211,30 @@ class HTTPTest extends FunctionalTest
{
$this->withBaseURL(
'http://www.silverstripe.org/',
function ($test) {
function () {
// background-image
// Note that using /./ in urls is absolutely acceptable
$test->assertEquals(
$this->assertEquals(
'<div style="background-image: url(\'http://www.silverstripe.org/./images/mybackground.gif\');">'.
'Content</div>',
HTTP::absoluteURLs('<div style="background-image: url(\'./images/mybackground.gif\');">Content</div>')
);
// background
$test->assertEquals(
$this->assertEquals(
'<div style="background: url(\'http://www.silverstripe.org/images/mybackground.gif\');">Content</div>',
HTTP::absoluteURLs('<div style="background: url(\'images/mybackground.gif\');">Content</div>')
);
// list-style-image
$test->assertEquals(
$this->assertEquals(
'<div style=\'background: url(http://www.silverstripe.org/list.png);\'>Content</div>',
HTTP::absoluteURLs('<div style=\'background: url(list.png);\'>Content</div>')
);
// list-style
$test->assertEquals(
$this->assertEquals(
'<div style=\'background: url("http://www.silverstripe.org/./assets/list.png");\'>Content</div>',
HTTP::absoluteURLs('<div style=\'background: url("./assets/list.png");\'>Content</div>')
);
@ -244,37 +249,37 @@ class HTTPTest extends FunctionalTest
{
$this->withBaseURL(
'http://www.silverstripe.org/',
function ($test) {
function () {
//empty links
$test->assertEquals(
$this->assertEquals(
'<a href="http://www.silverstripe.org/">test</a>',
HTTP::absoluteURLs('<a href="">test</a>')
);
$test->assertEquals(
$this->assertEquals(
'<a href="http://www.silverstripe.org/">test</a>',
HTTP::absoluteURLs('<a href="/">test</a>')
);
//relative
$test->assertEquals(
$this->assertEquals(
'<a href="http://www.silverstripe.org/">test</a>',
HTTP::absoluteURLs('<a href="./">test</a>')
);
$test->assertEquals(
$this->assertEquals(
'<a href="http://www.silverstripe.org/">test</a>',
HTTP::absoluteURLs('<a href=".">test</a>')
);
// links
$test->assertEquals(
$this->assertEquals(
'<a href=\'http://www.silverstripe.org/blog/\'>SS Blog</a>',
HTTP::absoluteURLs('<a href=\'/blog/\'>SS Blog</a>')
);
// background
// Note that using /./ in urls is absolutely acceptable
$test->assertEquals(
$this->assertEquals(
'<div background="http://www.silverstripe.org/./themes/silverstripe/images/nav-bg-repeat-2.png">'.
'SS Blog</div>',
HTTP::absoluteURLs('<div background="./themes/silverstripe/images/nav-bg-repeat-2.png">SS Blog</div>')
@ -283,25 +288,25 @@ class HTTPTest extends FunctionalTest
//check dot segments
// Assumption: dots are not removed
//if they were, the url should be: http://www.silverstripe.org/abc
$test->assertEquals(
$this->assertEquals(
'<a href="http://www.silverstripe.org/test/page/../../abc">Test</a>',
HTTP::absoluteURLs('<a href="test/page/../../abc">Test</a>')
);
// image
$test->assertEquals(
$this->assertEquals(
'<img src=\'http://www.silverstripe.org/themes/silverstripe/images/logo-org.png\' />',
HTTP::absoluteURLs('<img src=\'themes/silverstripe/images/logo-org.png\' />')
);
// link
$test->assertEquals(
$this->assertEquals(
'<link href=http://www.silverstripe.org/base.css />',
HTTP::absoluteURLs('<link href=base.css />')
);
// Test special characters are retained
$test->assertEquals(
$this->assertEquals(
'<a href="http://www.silverstripe.org/Security/changepassword?m=3&amp;t=7214fdfde">password reset link</a>',
HTTP::absoluteURLs('<a href="/Security/changepassword?m=3&amp;t=7214fdfde">password reset link</a>')
);
@ -319,14 +324,14 @@ class HTTPTest extends FunctionalTest
function ($test) {
// mailto
$test->assertEquals(
$this->assertEquals(
'<a href=\'mailto:admin@silverstripe.org\'>Email Us</a>',
HTTP::absoluteURLs('<a href=\'mailto:admin@silverstripe.org\'>Email Us</a>'),
'Email links are not rewritten'
);
// data uri
$test->assertEquals(
$this->assertEquals(
'<img src="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAYAAACNbyblAAAAHElEQVQI12P4//8/w38'.
'GIAXDIBKE0DHxgljNBAAO9TXL0Y4OHwAAAABJRU5ErkJggg==" alt="Red dot" />',
HTTP::absoluteURLs(
@ -337,7 +342,7 @@ class HTTPTest extends FunctionalTest
);
// call
$test->assertEquals(
$this->assertEquals(
'<a href="callto:12345678" />',
HTTP::absoluteURLs('<a href="callto:12345678" />'),
'Call to links are not rewritten'
@ -352,7 +357,7 @@ class HTTPTest extends FunctionalTest
'http://www.silverstripe.org/',
function ($test) {
$frameworkTests = ltrim(FRAMEWORK_DIR . '/tests', '/');
$test->assertEquals(
$this->assertEquals(
"http://www.silverstripe.org/$frameworkTests/php/Control/HTTPTest.php",
HTTP::filename2url(__FILE__)
);

View File

@ -2,53 +2,62 @@
namespace SilverStripe\Control\Tests;
use SilverStripe\Core\Config\Config;
use SilverStripe\Core\Injector\Injector;
use SilverStripe\Dev\SapphireTest;
use SilverStripe\Control\Session;
use SilverStripe\Core\Config\Config;
use SilverStripe\Dev\SapphireTest;
/**
* Tests to cover the {@link Session} class
*/
class SessionTest extends SapphireTest
{
/**
* @var Session
*/
protected $session = null;
protected function setUp()
{
$this->session = new Session([]);
return parent::setUp();
}
public function testGetSetBasics()
{
Session::set('Test', 'Test');
$this->session->set('Test', 'Test');
$this->assertEquals(Session::get('Test'), 'Test');
$this->assertEquals($this->session->get('Test'), 'Test');
}
public function testClearElement()
{
Session::set('Test', 'Test');
Session::clear('Test');
$this->session->set('Test', 'Test');
$this->session->clear('Test');
$this->assertEquals(Session::get('Test'), '');
$this->assertEquals($this->session->get('Test'), '');
}
public function testClearAllElements()
{
Session::set('Test', 'Test');
Session::set('Test-1', 'Test-1');
$this->session->set('Test', 'Test');
$this->session->set('Test-1', 'Test-1');
Session::clear_all();
$this->session->clearAll();
// should session get return null? The array key should probably be
// unset from the data array
$this->assertEquals(Session::get('Test'), '');
$this->assertEquals(Session::get('Test-1'), '');
$this->assertEquals($this->session->get('Test'), '');
$this->assertEquals($this->session->get('Test-1'), '');
}
public function testGetAllElements()
{
Session::clear_all(); // Remove all session that might've been set by the test harness
$this->session->clearAll(); // Remove all session that might've been set by the test harness
Session::set('Test', 'Test');
Session::set('Test-2', 'Test-2');
$this->session->set('Test', 'Test');
$this->session->set('Test-2', 'Test-2');
$session = Session::get_all();
$session = $this->session->getAll();
unset($session['HTTP_USER_AGENT']);
$this->assertEquals($session, array('Test' => 'Test', 'Test-2' => 'Test-2'));
@ -56,10 +65,10 @@ class SessionTest extends SapphireTest
public function testSettingExistingDoesntClear()
{
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
$s = new Session(array('something' => array('does' => 'exist')));
$s->inst_set('something.does', 'exist');
$result = $s->inst_changedData();
$s->set('something.does', 'exist');
$result = $s->changedData();
unset($result['HTTP_USER_AGENT']);
$this->assertEquals(array(), $result);
}
@ -69,16 +78,16 @@ class SessionTest extends SapphireTest
*/
public function testClearElementThatDoesntExist()
{
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
$s = new Session(array('something' => array('does' => 'exist')));
$s->inst_clear('something.doesnt.exist');
$result = $s->inst_changedData();
$s->clear('something.doesnt.exist');
$result = $s->changedData();
unset($result['HTTP_USER_AGENT']);
$this->assertEquals(array(), $result);
$s->inst_set('something-else', 'val');
$s->inst_clear('something-new');
$result = $s->inst_changedData();
$s->set('something-else', 'val');
$s->clear('something-new');
$result = $s->changedData();
unset($result['HTTP_USER_AGENT']);
$this->assertEquals(array('something-else' => 'val'), $result);
}
@ -88,18 +97,18 @@ class SessionTest extends SapphireTest
*/
public function testClearElementThatDoesExist()
{
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array('something' => array('does' => 'exist')));
$s = new Session(array('something' => array('does' => 'exist')));
$s->inst_clear('something.does');
$result = $s->inst_changedData();
$s->clear('something.does');
$result = $s->changedData();
unset($result['HTTP_USER_AGENT']);
$this->assertEquals(array('something' => array('does' => null)), $result);
}
public function testNonStandardPath()
{
Config::inst()->update('SilverStripe\\Control\\Session', 'store_path', (realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session')));
Session::start();
Session::config()->set('store_path', (realpath(dirname($_SERVER['DOCUMENT_ROOT']) . '/../session')));
$this->session->start();
$this->assertEquals(Config::inst()->get('SilverStripe\\Control\\Session', 'store_path'), '');
}
@ -110,15 +119,15 @@ class SessionTest extends SapphireTest
$_SERVER['HTTP_USER_AGENT'] = 'Test Agent';
// Generate our session
$s = Injector::inst()->create('SilverStripe\\Control\\Session', array());
$s->inst_set('val', 123);
$s->inst_finalize();
$s = new Session(array());
$s->set('val', 123);
$s->finalize();
// Change our UA
$_SERVER['HTTP_USER_AGENT'] = 'Fake Agent';
// Verify the new session reset our values
$s2 = Injector::inst()->create('SilverStripe\\Control\\Session', $s);
$this->assertNotEquals($s2->inst_get('val'), 123);
$s2 = new Session($s);
$this->assertNotEquals($s2->get('val'), 123);
}
}