Merge pull request #22234 from owncloud/systemtags-filter-intersect-empty

Fix system tag filter AND condition
This commit is contained in:
Thomas Müller 2016-02-09 15:34:06 +01:00
commit 4659bf9b4a
2 changed files with 113 additions and 19 deletions

View File

@ -211,7 +211,7 @@ class FilesReportPlugin extends ServerPlugin {
*/ */
public function processFilterRules($filterRules) { public function processFilterRules($filterRules) {
$ns = '{' . $this::NS_OWNCLOUD . '}'; $ns = '{' . $this::NS_OWNCLOUD . '}';
$resultFileIds = []; $resultFileIds = null;
$systemTagIds = []; $systemTagIds = [];
foreach ($filterRules as $filterRule) { foreach ($filterRules as $filterRule) {
if ($filterRule['name'] === $ns . 'systemtag') { if ($filterRule['name'] === $ns . 'systemtag') {
@ -239,11 +239,22 @@ class FilesReportPlugin extends ServerPlugin {
foreach ($systemTagIds as $systemTagId) { foreach ($systemTagIds as $systemTagId) {
$fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files'); $fileIds = $this->tagMapper->getObjectIdsForTags($systemTagId, 'files');
if (empty($resultFileIds)) { if (empty($fileIds)) {
// This tag has no files, nothing can ever show up
return [];
}
// first run ?
if ($resultFileIds === null) {
$resultFileIds = $fileIds; $resultFileIds = $fileIds;
} else { } else {
$resultFileIds = array_intersect($resultFileIds, $fileIds); $resultFileIds = array_intersect($resultFileIds, $fileIds);
} }
if (empty($resultFileIds)) {
// Empty intersection, nothing can show up anymore
return [];
}
} }
return $resultFileIds; return $resultFileIds;
} }

View File

@ -30,16 +30,16 @@ use OCP\IGroupManager;
use OCP\SystemTag\ISystemTagManager; use OCP\SystemTag\ISystemTagManager;
class FilesReportPlugin extends \Test\TestCase { class FilesReportPlugin extends \Test\TestCase {
/** @var \Sabre\DAV\Server */ /** @var \Sabre\DAV\Server|\PHPUnit_Framework_MockObject_MockObject */
private $server; private $server;
/** @var \Sabre\DAV\Tree */ /** @var \Sabre\DAV\Tree|\PHPUnit_Framework_MockObject_MockObject */
private $tree; private $tree;
/** @var ISystemTagObjectMapper */ /** @var ISystemTagObjectMapper|\PHPUnit_Framework_MockObject_MockObject */
private $tagMapper; private $tagMapper;
/** @var ISystemTagManager */ /** @var ISystemTagManager|\PHPUnit_Framework_MockObject_MockObject */
private $tagManager; private $tagManager;
/** @var \OCP\IUserSession */ /** @var \OCP\IUserSession */
@ -48,13 +48,13 @@ class FilesReportPlugin extends \Test\TestCase {
/** @var FilesReportPluginImplementation */ /** @var FilesReportPluginImplementation */
private $plugin; private $plugin;
/** @var View **/ /** @var View|\PHPUnit_Framework_MockObject_MockObject **/
private $view; private $view;
/** @var IGroupManager **/ /** @var IGroupManager|\PHPUnit_Framework_MockObject_MockObject **/
private $groupManager; private $groupManager;
/** @var Folder **/ /** @var Folder|\PHPUnit_Framework_MockObject_MockObject **/
private $userFolder; private $userFolder;
public function setUp() { public function setUp() {
@ -254,6 +254,7 @@ class FilesReportPlugin extends \Test\TestCase {
->with('222') ->with('222')
->will($this->returnValue([$filesNode2])); ->will($this->returnValue([$filesNode2]));
/** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit_Framework_MockObject_MockObject $reportTargetNode */
$result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']); $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
$this->assertCount(2, $result); $this->assertCount(2, $result);
@ -304,6 +305,7 @@ class FilesReportPlugin extends \Test\TestCase {
->with('222') ->with('222')
->will($this->returnValue([$filesNode2])); ->will($this->returnValue([$filesNode2]));
/** @var \OCA\DAV\Connector\Sabre\Directory|\PHPUnit_Framework_MockObject_MockObject $reportTargetNode */
$result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']); $result = $this->plugin->findNodesByFileIds($reportTargetNode, ['111', '222']);
$this->assertCount(2, $result); $this->assertCount(2, $result);
@ -361,10 +363,14 @@ class FilesReportPlugin extends \Test\TestCase {
->method('isAdmin') ->method('isAdmin')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$this->tagMapper->expects($this->once()) $this->tagMapper->expects($this->exactly(1))
->method('getObjectIdsForTags') ->method('getObjectIdsForTags')
->with('123') ->withConsecutive(
->will($this->returnValue(['111', '222'])); ['123', 'files']
)
->willReturnMap([
['123', 'files', ['111', '222']],
]);
$rules = [ $rules = [
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
@ -378,14 +384,16 @@ class FilesReportPlugin extends \Test\TestCase {
->method('isAdmin') ->method('isAdmin')
->will($this->returnValue(true)); ->will($this->returnValue(true));
$this->tagMapper->expects($this->at(0)) $this->tagMapper->expects($this->exactly(2))
->method('getObjectIdsForTags') ->method('getObjectIdsForTags')
->with('123') ->withConsecutive(
->will($this->returnValue(['111', '222'])); ['123', 'files'],
$this->tagMapper->expects($this->at(1)) ['456', 'files']
->method('getObjectIdsForTags') )
->with('456') ->willReturnMap([
->will($this->returnValue(['222', '333'])); ['123', 'files', ['111', '222']],
['456', 'files', ['222', '333']],
]);
$rules = [ $rules = [
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'], ['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
@ -395,6 +403,81 @@ class FilesReportPlugin extends \Test\TestCase {
$this->assertEquals(['222'], array_values($this->plugin->processFilterRules($rules))); $this->assertEquals(['222'], array_values($this->plugin->processFilterRules($rules)));
} }
public function testProcessFilterRulesAndConditionWithOneEmptyResult() {
$this->groupManager->expects($this->any())
->method('isAdmin')
->will($this->returnValue(true));
$this->tagMapper->expects($this->exactly(2))
->method('getObjectIdsForTags')
->withConsecutive(
['123', 'files'],
['456', 'files']
)
->willReturnMap([
['123', 'files', ['111', '222']],
['456', 'files', []],
]);
$rules = [
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
];
$this->assertEquals([], array_values($this->plugin->processFilterRules($rules)));
}
public function testProcessFilterRulesAndConditionWithFirstEmptyResult() {
$this->groupManager->expects($this->any())
->method('isAdmin')
->will($this->returnValue(true));
$this->tagMapper->expects($this->exactly(1))
->method('getObjectIdsForTags')
->withConsecutive(
['123', 'files'],
['456', 'files']
)
->willReturnMap([
['123', 'files', []],
['456', 'files', ['111', '222']],
]);
$rules = [
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
];
$this->assertEquals([], array_values($this->plugin->processFilterRules($rules)));
}
public function testProcessFilterRulesAndConditionWithEmptyMidResult() {
$this->groupManager->expects($this->any())
->method('isAdmin')
->will($this->returnValue(true));
$this->tagMapper->expects($this->exactly(2))
->method('getObjectIdsForTags')
->withConsecutive(
['123', 'files'],
['456', 'files'],
['789', 'files']
)
->willReturnMap([
['123', 'files', ['111', '222']],
['456', 'files', ['333']],
['789', 'files', ['111', '222']],
]);
$rules = [
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '123'],
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '456'],
['name' => '{http://owncloud.org/ns}systemtag', 'value' => '789'],
];
$this->assertEquals([], array_values($this->plugin->processFilterRules($rules)));
}
public function testProcessFilterRulesInvisibleTagAsAdmin() { public function testProcessFilterRulesInvisibleTagAsAdmin() {
$this->groupManager->expects($this->any()) $this->groupManager->expects($this->any())
->method('isAdmin') ->method('isAdmin')