2012-03-24 03:19:02 +01:00
|
|
|
<?php
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-10-14 03:30:05 +02:00
|
|
|
namespace SilverStripe\Control\Tests;
|
|
|
|
|
2016-09-09 08:43:05 +02:00
|
|
|
use SilverStripe\Control\HTTPRequest;
|
2017-06-22 12:50:45 +02:00
|
|
|
use SilverStripe\Control\PjaxResponseNegotiator;
|
|
|
|
use SilverStripe\Control\Session;
|
|
|
|
use SilverStripe\Dev\SapphireTest;
|
2016-08-19 00:51:35 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
class PjaxResponseNegotiatorTest extends SapphireTest
|
|
|
|
{
|
2014-08-15 08:53:05 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testDefaultCallbacks()
|
|
|
|
{
|
|
|
|
$negotiator = new PjaxResponseNegotiator(
|
2018-09-27 00:32:35 +02:00
|
|
|
[
|
|
|
|
'default' => function () {
|
|
|
|
return 'default response';
|
|
|
|
},
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$request = new HTTPRequest('GET', '/'); // not setting pjax header
|
2017-06-22 12:50:45 +02:00
|
|
|
$request->setSession(new Session([]));
|
2016-12-16 05:34:21 +01:00
|
|
|
$response = $negotiator->respond($request);
|
|
|
|
$this->assertEquals('default response', $response->getBody());
|
|
|
|
}
|
2012-03-24 03:19:02 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testSelectsFragmentByHeader()
|
|
|
|
{
|
|
|
|
$negotiator = new PjaxResponseNegotiator(
|
2018-09-27 00:32:35 +02:00
|
|
|
[
|
|
|
|
'default' => function () {
|
|
|
|
return 'default response';
|
|
|
|
},
|
|
|
|
'myfragment' => function () {
|
|
|
|
return 'myfragment response';
|
|
|
|
},
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$request = new HTTPRequest('GET', '/');
|
2017-06-22 12:50:45 +02:00
|
|
|
$request->setSession(new Session([]));
|
2016-12-16 05:34:21 +01:00
|
|
|
$request->addHeader('X-Pjax', 'myfragment');
|
|
|
|
$response = $negotiator->respond($request);
|
|
|
|
$this->assertEquals('{"myfragment":"myfragment response"}', $response->getBody());
|
|
|
|
}
|
2012-03-24 03:19:02 +01:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testMultipleFragments()
|
|
|
|
{
|
|
|
|
$negotiator = new PjaxResponseNegotiator(
|
2018-09-27 00:32:35 +02:00
|
|
|
[
|
|
|
|
'default' => function () {
|
|
|
|
return 'default response';
|
|
|
|
},
|
|
|
|
'myfragment' => function () {
|
|
|
|
return 'myfragment response';
|
|
|
|
},
|
|
|
|
'otherfragment' => function () {
|
|
|
|
return 'otherfragment response';
|
|
|
|
},
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
|
|
|
$request = new HTTPRequest('GET', '/');
|
2017-06-22 12:50:45 +02:00
|
|
|
$request->setSession(new Session([]));
|
2016-12-16 05:34:21 +01:00
|
|
|
$request->addHeader('X-Pjax', 'myfragment,otherfragment');
|
2018-08-15 13:10:39 +02:00
|
|
|
$request->addHeader('Accept', 'application/json');
|
2016-12-16 05:34:21 +01:00
|
|
|
$response = $negotiator->respond($request);
|
2022-04-14 03:12:59 +02:00
|
|
|
$json = json_decode($response->getBody() ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertObjectHasAttribute('myfragment', $json);
|
|
|
|
$this->assertEquals('myfragment response', $json->myfragment);
|
|
|
|
$this->assertObjectHasAttribute('otherfragment', $json);
|
|
|
|
$this->assertEquals('otherfragment response', $json->otherfragment);
|
|
|
|
}
|
2012-04-18 21:15:31 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
public function testFragmentsOverride()
|
|
|
|
{
|
|
|
|
$negotiator = new PjaxResponseNegotiator(
|
2018-09-27 00:32:35 +02:00
|
|
|
[
|
|
|
|
'alpha' => function () {
|
|
|
|
return 'alpha response';
|
|
|
|
},
|
|
|
|
'beta' => function () {
|
|
|
|
return 'beta response';
|
|
|
|
},
|
|
|
|
]
|
2016-12-16 05:34:21 +01:00
|
|
|
);
|
2012-06-07 00:26:27 +02:00
|
|
|
|
2016-12-16 05:34:21 +01:00
|
|
|
$request = new HTTPRequest('GET', '/');
|
2017-06-22 12:50:45 +02:00
|
|
|
$request->setSession(new Session([]));
|
2016-12-16 05:34:21 +01:00
|
|
|
$request->addHeader('X-Pjax', 'alpha');
|
2018-08-15 13:10:39 +02:00
|
|
|
$request->addHeader('Accept', 'application/json');
|
2012-06-07 00:26:27 +02:00
|
|
|
|
2018-09-27 00:32:35 +02:00
|
|
|
$response = $negotiator->setFragmentOverride(['beta'])->respond($request);
|
2022-04-14 03:12:59 +02:00
|
|
|
$json = json_decode($response->getBody() ?? '');
|
2016-12-16 05:34:21 +01:00
|
|
|
$this->assertFalse(isset($json->alpha));
|
|
|
|
$this->assertObjectHasAttribute('beta', $json);
|
|
|
|
}
|
2012-06-07 00:26:27 +02:00
|
|
|
}
|