use OCP\SystemTag\TagNotFoundException;
use OCP\SystemTag\TagAlreadyExistsException;
+/**
+ * DAV node representing a system tag, with the name being the tag id.
+ */
class SystemTagNode implements \Sabre\DAV\INode {
/**
use Sabre\HTTP\RequestInterface;
use Sabre\HTTP\ResponseInterface;
+/**
+ * Sabre plugin to handle system tags:
+ *
+ * - makes it possible to create new tags with POST operation
+ * - get/set Webdav properties for tags
+ *
+ */
class SystemTagPlugin extends \Sabre\DAV\ServerPlugin {
// namespace
}
/**
- * We intercept this to handle POST requests on calendars.
+ * POST operation on system tag collections
*
* @param RequestInterface $request request object
* @param ResponseInterface $response response object
/**
* Creates a new tag
*
- * @param string $data
+ * @param string $data JSON encoded string containing the properties of the tag to create
* @param string $contentType content type of the data
* @return ISystemTag newly created system tag
*
* @throws BadRequest if a field was missing
- * @throws Conflict
+ * @throws Conflict if a tag with the same properties already exists
* @throws UnsupportedMediaType if the content type is not supported
*/
private function createTag($data, $contentType = 'application/json') {
if ($contentType === 'application/json') {
$data = json_decode($data, true);
- // TODO: application/x-www-form-urlencoded ?
} else {
throw new UnsupportedMediaType();
}
try {
return $this->tagManager->createTag($tagName, $userVisible, $userAssignable);
} catch (TagAlreadyExistsException $e) {
- throw new Conflict('Tag already exists');
+ throw new Conflict('Tag already exists', 0, $e);
}
}
function getChild($name) {
try {
- $tags = $this->tagManager->getTagsById($name);
+ $tags = $this->tagManager->getTagsByIds([$name]);
return $this->makeNode(current($tags));
} catch (\InvalidArgumentException $e) {
throw new BadRequest('Invalid tag id', 0, $e);
}
function getChildren() {
- // TODO: set visibility filter based on principal/permissions ?
$tags = $this->tagManager->getAllTags(true);
return array_map(function($tag) {
return $this->makeNode($tag);
function childExists($name) {
try {
- $this->tagManager->getTagsById($name);
+ $this->tagManager->getTagsByIds([$name]);
return true;
} catch (\InvalidArgumentException $e) {
throw new BadRequest('Invalid tag id', 0, $e);
function getChild($tagId) {
try {
- if ($this->tagMapper->haveTag($this->objectId, $this->objectType, $tagId, true)) {
- $tag = $this->tagManager->getTagsById($tagId);
+ if ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true)) {
+ $tag = $this->tagManager->getTagsByIds([$tagId]);
return $this->makeNode(current($tag));
}
throw new NotFound('Tag with id ' . $tagId . ' not present for object ' . $this->objectId);
}
function getChildren() {
- $tagIds = current($this->tagMapper->getTagIdsForObjects($this->objectId, $this->objectType));
+ $tagIds = current($this->tagMapper->getTagIdsForObjects([$this->objectId], $this->objectType));
if (empty($tagIds)) {
return [];
}
- $tags = $this->tagManager->getTagsById($tagIds);
+ $tags = $this->tagManager->getTagsByIds($tagIds);
return array_values(array_map(function($tag) {
return $this->makeNode($tag);
}, $tags));
function childExists($tagId) {
try {
- return ($this->tagMapper->haveTag($this->objectId, $this->objectType, $tagId, true));
+ return ($this->tagMapper->haveTag([$this->objectId], $this->objectType, $tagId, true));
} catch (\InvalidArgumentException $e) {
throw new BadRequest('Invalid tag id', 0, $e);
} catch (TagNotFoundException $e) {
}
/**
- * Create a sabre node for the given system tag
+ * Create a sabre node for the mapping of the
+ * given system tag to the collection's object
*
* @param ISystemTag $tag
*
}
function createFile($name, $data = null) {
- throw new Forbidden('Permission denied to create collections');
+ throw new Forbidden('Permission denied to create nodes');
}
function createDirectory($name) {
$tag = new SystemTag(123, 'Test', true, false);
$this->tagManager->expects($this->once())
- ->method('getTagsById')
- ->with('123')
+ ->method('getTagsByIds')
+ ->with(['123'])
->will($this->returnValue([$tag]));
$childNode = $this->node->getChild('123');
*/
public function testGetChildInvalidName() {
$this->tagManager->expects($this->once())
- ->method('getTagsById')
- ->with('invalid')
+ ->method('getTagsByIds')
+ ->with(['invalid'])
->will($this->throwException(new \InvalidArgumentException()));
$this->node->getChild('invalid');
*/
public function testGetChildNotFound() {
$this->tagManager->expects($this->once())
- ->method('getTagsById')
- ->with('444')
+ ->method('getTagsByIds')
+ ->with(['444'])
->will($this->throwException(new TagNotFoundException()));
$this->node->getChild('444');
$tag = new SystemTag(123, 'One', true, false);
$this->tagManager->expects($this->once())
- ->method('getTagsById')
- ->with('123')
+ ->method('getTagsByIds')
+ ->with(['123'])
->will($this->returnValue([$tag]));
$this->assertTrue($this->node->childExists('123'));
public function testChildExistsNotFound() {
$this->tagManager->expects($this->once())
- ->method('getTagsById')
- ->with('123')
+ ->method('getTagsByIds')
+ ->with(['123'])
->will($this->throwException(new TagNotFoundException()));
$this->assertFalse($this->node->childExists('123'));
*/
public function testChildExistsBadRequest() {
$this->tagManager->expects($this->once())
- ->method('getTagsById')
- ->with('invalid')
+ ->method('getTagsByIds')
+ ->with(['invalid'])
->will($this->throwException(new \InvalidArgumentException()));
$this->node->childExists('invalid');
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', '555', true)
+ ->with([111], 'files', '555', true)
->will($this->returnValue(true));
$this->tagManager->expects($this->once())
- ->method('getTagsById')
- ->with('555')
- ->will($this->returnValue([$tag]));
+ ->method('getTagsByIds')
+ ->with(['555'])
+ ->will($this->returnValue(['555' => $tag]));
$childNode = $this->node->getChild('555');
public function testGetChildRelationNotFound() {
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', '777')
+ ->with([111], 'files', '777')
->will($this->returnValue(false));
$this->node->getChild('777');
public function testGetChildInvalidId() {
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', 'badid')
+ ->with([111], 'files', 'badid')
->will($this->throwException(new \InvalidArgumentException()));
$this->node->getChild('badid');
public function testGetChildTagDoesNotExist() {
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', '777')
+ ->with([111], 'files', '777')
->will($this->throwException(new TagNotFoundException()));
$this->node->getChild('777');
$this->tagMapper->expects($this->once())
->method('getTagIdsForObjects')
- ->with(111, 'files')
+ ->with([111], 'files')
->will($this->returnValue(['111' => ['555', '556']]));
$this->tagManager->expects($this->once())
- ->method('getTagsById')
+ ->method('getTagsByIds')
->with(['555', '556'])
->will($this->returnValue(['555' => $tag1, '666' => $tag2]));
public function testChildExists() {
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', '555')
+ ->with([111], 'files', '555')
->will($this->returnValue(true));
$this->assertTrue($this->node->childExists('555'));
public function testChildExistsNotFound() {
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', '555')
+ ->with([111], 'files', '555')
->will($this->returnValue(false));
$this->assertFalse($this->node->childExists('555'));
public function testChildExistsTagNotFound() {
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', '555')
+ ->with([111], 'files', '555')
->will($this->throwException(new TagNotFoundException()));
$this->assertFalse($this->node->childExists('555'));
public function testChildExistsInvalidId() {
$this->tagMapper->expects($this->once())
->method('haveTag')
- ->with(111, 'files', '555')
+ ->with([111], 'files', '555')
->will($this->throwException(new \InvalidArgumentException()));
$this->node->childExists('555');
/**
* {@inheritdoc}
*/
- public function getTagsById($tagIds) {
+ public function getTagsByIds($tagIds) {
if (!is_array($tagIds)) {
$tagIds = [$tagIds];
}
$tagNotFoundException = null;
try {
- $this->getTagsById($tagIds);
+ $this->getTagsByIds($tagIds);
} catch (TagNotFoundException $e) {
$tagNotFoundException = $e;
}
* @throws \OCP\SystemTag\TagNotFoundException if at least one tag did not exist
*/
private function assertTagsExist($tagIds) {
- $tags = $this->tagManager->getTagsById($tagIds);
+ $tags = $this->tagManager->getTagsByIds($tagIds);
if (count($tags) !== count($tagIds)) {
// at least one tag missing, bail out
$foundTagIds = array_map(
*
* @since 9.0.0
*/
- public function getTagsById($tagIds);
+ public function getTagsByIds($tagIds);
/**
* Returns the tag object matching the given attributes.
$tag1 = $this->tagManager->createTag('one', true, false);
$tag2 = $this->tagManager->createTag('two', false, true);
- $tagList = $this->tagManager->getTagsById([$tag1->getId(), $tag2->getId()]);
+ $tagList = $this->tagManager->getTagsByIds([$tag1->getId(), $tag2->getId()]);
$this->assertCount(2, $tagList);
*/
public function testGetNonExistingTagsById() {
$tag1 = $this->tagManager->createTag('one', true, false);
- $this->tagManager->getTagsById([$tag1->getId(), 100, 101]);
+ $this->tagManager->getTagsByIds([$tag1->getId(), 100, 101]);
}
/**
*/
public function testGetInvalidTagIdFormat() {
$tag1 = $this->tagManager->createTag('one', true, false);
- $this->tagManager->getTagsById([$tag1->getId() . 'suffix']);
+ $this->tagManager->getTagsByIds([$tag1->getId() . 'suffix']);
}
public function updateTagProvider() {
$this->tag3 = new SystemTag(3, 'testtag3', false, false);
$this->tagManager->expects($this->any())
- ->method('getTagsById')
+ ->method('getTagsByIds')
->will($this->returnCallback(function($tagIds) {
$result = [];
if (in_array(1, $tagIds)) {