public function testUpdateFileTags() { $tag1 = 'tag1'; $tag2 = 'tag2'; $subdir = $this->root->newFolder('subdir'); $testFile = $subdir->newFile('test.txt'); $testFile->putContent('test contents'); $fileId = $testFile->getId(); // set tags $this->tagService->updateFileTags('subdir/test.txt', array($tag1, $tag2)); $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag1)); $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); // remove tag $result = $this->tagService->updateFileTags('subdir/test.txt', array($tag2)); $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); $this->assertEquals(array($fileId), $this->tagger->getIdsForTag($tag2)); // clear tags $result = $this->tagService->updateFileTags('subdir/test.txt', array()); $this->assertEquals(array(), $this->tagger->getIdsForTag($tag1)); $this->assertEquals(array(), $this->tagger->getIdsForTag($tag2)); // non-existing file $caught = false; try { $this->tagService->updateFileTags('subdir/unexist.txt', array($tag1)); } catch (\OCP\Files\NotFoundException $e) { $caught = true; } $this->assertTrue($caught); $subdir->delete(); }
public function testUpdateFav() { // this test will replace the existing tags "tagremove" with "tag1" and "tag2" // and keep "tagkeep" $node = $this->getMockBuilder('\\OC_Connector_Sabre_Node')->disableOriginalConstructor()->getMock(); $node->expects($this->any())->method('getId')->will($this->returnValue(123)); // set favorite tag $this->tagger->expects($this->once())->method('tagAs')->with(123, self::TAG_FAVORITE); // properties to set $properties = array(self::FAVORITE_PROPERTYNAME => true); $result = array(); $this->plugin->updateProperties($properties, $result, $node); // all requested properties removed, as they were processed already $this->assertEmpty($properties); $this->assertTrue($result[200][self::FAVORITE_PROPERTYNAME]); $this->assertFalse(isset($result[200][self::TAGS_PROPERTYNAME])); // unfavorite now // set favorite tag $this->tagger->expects($this->once())->method('unTag')->with(123, self::TAG_FAVORITE); $properties = array(self::FAVORITE_PROPERTYNAME => false); $result = array(); $this->plugin->updateProperties($properties, $result, $node); $this->assertFalse($result[200][self::FAVORITE_PROPERTYNAME]); $this->assertFalse(isset($result[200][self::TAGS_PROPERTYNAME])); }
public function testUpdateFav() { // this test will replace the existing tags "tagremove" with "tag1" and "tag2" // and keep "tagkeep" $node = $this->getMockBuilder('\\OCA\\DAV\\Connector\\Sabre\\Node')->disableOriginalConstructor()->getMock(); $node->expects($this->any())->method('getId')->will($this->returnValue(123)); $this->tree->expects($this->any())->method('getNodeForPath')->with('/dummypath')->will($this->returnValue($node)); // set favorite tag $this->tagger->expects($this->once())->method('tagAs')->with(123, self::TAG_FAVORITE); // properties to set $propPatch = new \Sabre\DAV\PropPatch(array(self::FAVORITE_PROPERTYNAME => true)); $this->plugin->handleUpdateProperties('/dummypath', $propPatch); $propPatch->commit(); // all requested properties removed, as they were processed already $this->assertEmpty($propPatch->getRemainingMutations()); $result = $propPatch->getResult(); $this->assertFalse(false, isset($result[self::TAGS_PROPERTYNAME])); $this->assertEquals(200, isset($result[self::FAVORITE_PROPERTYNAME])); // unfavorite now // set favorite tag $this->tagger->expects($this->once())->method('unTag')->with(123, self::TAG_FAVORITE); // properties to set $propPatch = new \Sabre\DAV\PropPatch(array(self::FAVORITE_PROPERTYNAME => false)); $this->plugin->handleUpdateProperties('/dummypath', $propPatch); $propPatch->commit(); // all requested properties removed, as they were processed already $this->assertEmpty($propPatch->getRemainingMutations()); $result = $propPatch->getResult(); $this->assertFalse(false, isset($result[self::TAGS_PROPERTYNAME])); $this->assertEquals(200, isset($result[self::FAVORITE_PROPERTYNAME])); }
/** * Get all files for the given tag * * @param array $tagName tag name to filter by * @return FileInfo[] list of matching files * @throws \Exception if the tag does not exist */ public function getFilesByTag($tagName) { try { $fileIds = $this->tagger->getIdsForTag($tagName); } catch (\Exception $e) { return []; } $fileInfos = []; foreach ($fileIds as $fileId) { $nodes = $this->homeFolder->getById((int) $fileId); foreach ($nodes as $node) { /** @var \OC\Files\Node\Node $node */ $fileInfos[] = $node->getFileInfo(); } } return $fileInfos; }
/** * Updates the tags of the specified file path. * The passed tags are absolute, which means they will * replace the actual tag selection. * * @param string $path path * @param array $tags array of tags * @return array list of tags * @throws \OCP\NotFoundException if the file does not exist */ public function updateFileTags($path, $tags) { $fileId = $this->homeFolder->get($path)->getId(); $currentTags = $this->tagger->getTagsForObjects(array($fileId)); if (!empty($currentTags)) { $currentTags = current($currentTags); } $newTags = array_diff($tags, $currentTags); foreach ($newTags as $tag) { $this->tagger->tagAs($fileId, $tag); } $deletedTags = array_diff($currentTags, $tags); foreach ($deletedTags as $tag) { $this->tagger->unTag($fileId, $tag); } // TODO: re-read from tagger to make sure the // list is up to date, in case of concurrent changes ? return $tags; }
/** * Get all files for the given tag * * @param string $tagName tag name to filter by * @return Node[] list of matching files * @throws \Exception if the tag does not exist */ public function getFilesByTag($tagName) { try { $fileIds = $this->tagger->getIdsForTag($tagName); } catch (\Exception $e) { return []; } $allNodes = []; foreach ($fileIds as $fileId) { $allNodes = array_merge($allNodes, $this->homeFolder->getById((int) $fileId)); } return $allNodes; }
/** * Checks whether the given file has the given tag. * * @param \OCP\ITags $tagger * @param array $fileData file data * @param string $tag tag to check for * @return boolean true if the given file has the expected tag, * false otherwise */ private function hasTag($tagger, $fileData, $tag) { $tags = $tagger->getTagsForObjects(array((int) $fileData['fileid'])); return !empty($tags) && in_array($tag, current($tags)); }