Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
OCSController requires DataResponse
The OCS Controller requires a DataResponse object to be returned.
This means that all error handling will have to be done via exceptions
thrown and handling in the middleware.
  • Loading branch information
rullzer committed Aug 10, 2016
commit a54f9c6aef581617953d49eac9aada90e440de56
15 changes: 4 additions & 11 deletions lib/public/AppFramework/OCSController.php
Original file line number Diff line number Diff line change
Expand Up @@ -88,26 +88,19 @@ public function buildResponse($response, $format = 'xml') {
/**
* Unwrap data and build ocs response
* @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
* @return OCSResponse
*/
private function buildOCSResponse($format, $data) {
if ($data instanceof DataResponse) {
$data = $data->getData();
}

private function buildOCSResponse($format, DataResponse $data) {
$params = [
'statuscode' => 100,
'message' => 'OK',
'data' => [],
'data' => $data->getData(),
'itemscount' => '',
'itemsperpage' => ''
];

foreach ($data as $key => $value) {
$params[$key] = $value;
}

return new OCSResponse(
$format, $params['statuscode'],
$params['message'], $params['data'],
Expand Down
57 changes: 5 additions & 52 deletions tests/lib/AppFramework/Controller/OCSControllerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,8 +75,8 @@ public function testXML() {
$expected = "<?xml version=\"1.0\"?>\n" .
"<ocs>\n" .
" <meta>\n" .
" <status>failure</status>\n" .
" <statuscode>400</statuscode>\n" .
" <status>ok</status>\n" .
" <statuscode>100</statuscode>\n" .
" <message>OK</message>\n" .
" <totalitems></totalitems>\n" .
" <itemsperpage></itemsperpage>\n" .
Expand All @@ -86,54 +86,12 @@ public function testXML() {
" </data>\n" .
"</ocs>\n";

$params = [
'data' => [
'test' => 'hi'
],
'statuscode' => 400
];
$params = new DataResponse(['test' => 'hi']);

$out = $controller->buildResponse($params, 'xml')->render();
$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() {
$controller = new ChildOCSController('app', new Request(
[],
Expand All @@ -144,14 +102,9 @@ public function testJSON() {
->disableOriginalConstructor()
->getMock()
));
$expected = '{"ocs":{"meta":{"status":"failure","statuscode":400,"message":"OK",' .
$expected = '{"ocs":{"meta":{"status":"ok","statuscode":100,"message":"OK",' .
'"totalitems":"","itemsperpage":""},"data":{"test":"hi"}}}';
$params = [
'data' => [
'test' => 'hi'
],
'statuscode' => 400
];
$params = new DataResponse(['test' => 'hi']);

$out = $controller->buildResponse($params, 'json')->render();
$this->assertEquals($expected, $out);
Expand Down