Merge pull request #823 from nextcloud/stable10_807

[Stable10] OCSController requires DataResponse
This commit is contained in:
Björn Schießle 2016-08-10 15:26:23 +02:00 committed by GitHub
commit 4128c9986c
2 changed files with 9 additions and 63 deletions

View File

@ -88,26 +88,19 @@ abstract class OCSController extends ApiController {
/** /**
* Unwrap data and build ocs response * Unwrap data and build ocs response
* @param string $format json or xml * @param string $format json or xml
* @param array|DataResponse $data the data which should be transformed * @param DataResponse $data the data which should be transformed
* @since 8.1.0 * @since 8.1.0
* @return OCSResponse
*/ */
private function buildOCSResponse($format, $data) { private function buildOCSResponse($format, DataResponse $data) {
if ($data instanceof DataResponse) {
$data = $data->getData();
}
$params = [ $params = [
'statuscode' => 100, 'statuscode' => 100,
'message' => 'OK', 'message' => 'OK',
'data' => [], 'data' => $data->getData(),
'itemscount' => '', 'itemscount' => '',
'itemsperpage' => '' 'itemsperpage' => ''
]; ];
foreach ($data as $key => $value) {
$params[$key] = $value;
}
return new OCSResponse( return new OCSResponse(
$format, $params['statuscode'], $format, $params['statuscode'],
$params['message'], $params['data'], $params['message'], $params['data'],

View File

@ -75,8 +75,8 @@ class OCSControllerTest extends \Test\TestCase {
$expected = "<?xml version=\"1.0\"?>\n" . $expected = "<?xml version=\"1.0\"?>\n" .
"<ocs>\n" . "<ocs>\n" .
" <meta>\n" . " <meta>\n" .
" <status>failure</status>\n" . " <status>ok</status>\n" .
" <statuscode>400</statuscode>\n" . " <statuscode>100</statuscode>\n" .
" <message>OK</message>\n" . " <message>OK</message>\n" .
" <totalitems></totalitems>\n" . " <totalitems></totalitems>\n" .
" <itemsperpage></itemsperpage>\n" . " <itemsperpage></itemsperpage>\n" .
@ -86,54 +86,12 @@ class OCSControllerTest extends \Test\TestCase {
" </data>\n" . " </data>\n" .
"</ocs>\n"; "</ocs>\n";
$params = [ $params = new DataResponse(['test' => 'hi']);
'data' => [
'test' => 'hi'
],
'statuscode' => 400
];
$out = $controller->buildResponse($params, 'xml')->render(); $out = $controller->buildResponse($params, 'xml')->render();
$this->assertEquals($expected, $out); $this->assertEquals($expected, $out);
} }
public function testXMLDataResponse() {
$controller = new ChildOCSController('app', new Request(
[],
$this->getMockBuilder('\OCP\Security\ISecureRandom')
->disableOriginalConstructor()
->getMock(),
$this->getMockBuilder('\OCP\IConfig')
->disableOriginalConstructor()
->getMock()
));
$expected = "<?xml version=\"1.0\"?>\n" .
"<ocs>\n" .
" <meta>\n" .
" <status>failure</status>\n" .
" <statuscode>400</statuscode>\n" .
" <message>OK</message>\n" .
" <totalitems></totalitems>\n" .
" <itemsperpage></itemsperpage>\n" .
" </meta>\n" .
" <data>\n" .
" <test>hi</test>\n" .
" </data>\n" .
"</ocs>\n";
$params = new DataResponse([
'data' => [
'test' => 'hi'
],
'statuscode' => 400
]);
$out = $controller->buildResponse($params, 'xml')->render();
$this->assertEquals($expected, $out);
}
public function testJSON() { public function testJSON() {
$controller = new ChildOCSController('app', new Request( $controller = new ChildOCSController('app', new Request(
[], [],
@ -144,14 +102,9 @@ class OCSControllerTest extends \Test\TestCase {
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock() ->getMock()
)); ));
$expected = '{"ocs":{"meta":{"status":"failure","statuscode":400,"message":"OK",' . $expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' .
'"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}'; '"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}';
$params = [ $params = new DataResponse(['test' => 'hi']);
'data' => [
'test' => 'hi'
],
'statuscode' => 400
];
$out = $controller->buildResponse($params, 'json')->render(); $out = $controller->buildResponse($params, 'json')->render();
$this->assertEquals($expected, $out); $this->assertEquals($expected, $out);