Skip to content

Commit

Permalink
Merge pull request #41 from creative-commoners/pulls/1.0/fix-total-it…
Browse files Browse the repository at this point in the history
…ems-with-canview

FIX Total items count in output respects canView on records
  • Loading branch information
NightJar authored Nov 17, 2017
2 parents 4bdd071 + be255c2 commit a737f67
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
12 changes: 7 additions & 5 deletions code/RestfulServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -229,20 +229,22 @@ protected function getHandler($className, $id, $relationName)
$fields = $rawFields ? explode(',', $rawFields) : null;

if ($obj instanceof SS_List) {
$responseFormatter->setTotalSize($obj->dataQuery()->query()->unlimitedRowCount());
$objs = new ArrayList($obj->toArray());
$objs = ArrayList::create($obj->toArray());
foreach ($objs as $obj) {
if (!$obj->canView($this->getMember())) {
$objs->remove($obj);
}
}
$responseFormatter->setTotalSize($objs->count());
return $responseFormatter->convertDataObjectSet($objs, $fields);
} elseif (!$obj) {
}

if (!$obj) {
$responseFormatter->setTotalSize(0);
return $responseFormatter->convertDataObjectSet(new ArrayList(), $fields);
} else {
return $responseFormatter->convertDataObject($obj, $fields);
}

return $responseFormatter->convertDataObject($obj, $fields);
}

/**
Expand Down
5 changes: 5 additions & 0 deletions tests/unit/RestfulServerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -476,6 +476,8 @@ public function testCanViewRespectedInList()
$response = Director::test($url, null, null, 'GET');
$this->assertEquals($response->getStatusCode(), 200);
$this->assertNotContains('Unspeakable', $response->getBody());
$responseArray = Convert::json2array($response->getBody());
$this->assertSame(0, $responseArray['totalSize']);

// With authentication
$_SERVER['PHP_AUTH_USER'] = '[email protected]';
Expand All @@ -484,6 +486,9 @@ public function testCanViewRespectedInList()
$response = Director::test($url, null, null, 'GET');
$this->assertEquals($response->getStatusCode(), 200);
$this->assertContains('Unspeakable', $response->getBody());
// Assumption: default formatter is XML
$responseArray = Convert::xml2array($response->getBody());
$this->assertEquals(1, $responseArray['@attributes']['totalSize']);
unset($_SERVER['PHP_AUTH_USER']);
unset($_SERVER['PHP_AUTH_PW']);
}
Expand Down

0 comments on commit a737f67

Please sign in to comment.