From: Vincent Petry Date: Thu, 24 Nov 2016 16:17:15 +0000 (+0100) Subject: Add tests for move/copy hooks in Node API X-Git-Tag: v12.0.0beta1~643^2 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=269b6527544a4a91687e1e443c6de3f2d42e05dd;p=nextcloud-server.git Add tests for move/copy hooks in Node API Signed-off-by: Vincent Petry --- diff --git a/tests/lib/Files/Node/NodeTest.php b/tests/lib/Files/Node/NodeTest.php index 03f9118d9e4..5e18caa2014 100644 --- a/tests/lib/Files/Node/NodeTest.php +++ b/tests/lib/Files/Node/NodeTest.php @@ -579,6 +579,87 @@ abstract class NodeTest extends \Test\TestCase { $this->assertEquals('/bar/asd', $node->getPath()); } + public function moveOrCopyProvider() { + return [ + ['move', 'rename', 'preRename', 'postRename'], + ['copy', 'copy', 'preCopy', 'postCopy'], + ]; + } + + /** + * @dataProvider moveOrCopyProvider + * @param string $operationMethod + * @param string $viewMethod + * @param string $preHookName + * @param string $postHookName + */ + public function testMoveCopyHooks($operationMethod, $viewMethod, $preHookName, $postHookName) { + /** @var IRootFolder|\PHPUnit_Framework_MockObject_MockObject $root */ + $root = $this->getMockBuilder('\OC\Files\Node\Root') + ->setConstructorArgs([$this->manager, $this->view, $this->user, $this->userMountCache, $this->logger, $this->userManager]) + ->setMethods(['get']) + ->getMock(); + + $this->view->expects($this->any()) + ->method($viewMethod) + ->with('/bar/foo', '/bar/asd') + ->will($this->returnValue(true)); + + $this->view->expects($this->any()) + ->method('getFileInfo') + ->will($this->returnValue($this->getFileInfo(['permissions' => \OCP\Constants::PERMISSION_ALL, 'fileid' => 1]))); + + /** + * @var \OC\Files\Node\File|\PHPUnit_Framework_MockObject_MockObject $node + */ + $node = $this->createTestNode($root, $this->view, '/bar/foo'); + $parentNode = new \OC\Files\Node\Folder($root, $this->view, '/bar'); + $targetTestNode = $this->createTestNode($root, $this->view, '/bar/asd'); + + $root->expects($this->any()) + ->method('get') + ->will($this->returnValueMap([['/bar', $parentNode], ['/bar/asd', $targetTestNode]])); + + $hooksRun = 0; + + $preListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node) { + $this->assertSame($node, $sourceNode); + $this->assertInstanceOf($this->getNodeClass(), $sourceNode); + $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode); + $this->assertEquals('/bar/asd', $targetNode->getPath()); + $hooksRun++; + }; + + $postListener = function (Node $sourceNode, Node $targetNode) use (&$hooksRun, $node, $targetTestNode) { + $this->assertSame($node, $sourceNode); + $this->assertNotSame($node, $targetNode); + $this->assertSame($targetTestNode, $targetNode); + $this->assertInstanceOf($this->getNodeClass(), $sourceNode); + $this->assertInstanceOf($this->getNodeClass(), $targetNode); + $hooksRun++; + }; + + $preWriteListener = function (Node $targetNode) use (&$hooksRun) { + $this->assertInstanceOf($this->getNonExistingNodeClass(), $targetNode); + $this->assertEquals('/bar/asd', $targetNode->getPath()); + $hooksRun++; + }; + + $postWriteListener = function (Node $targetNode) use (&$hooksRun, $targetTestNode) { + $this->assertSame($targetTestNode, $targetNode); + $hooksRun++; + }; + + $root->listen('\OC\Files', $preHookName, $preListener); + $root->listen('\OC\Files', 'preWrite', $preWriteListener); + $root->listen('\OC\Files', $postHookName, $postListener); + $root->listen('\OC\Files', 'postWrite', $postWriteListener); + + $node->$operationMethod('/bar/asd'); + + $this->assertEquals(4, $hooksRun); + } + /** * @expectedException \OCP\Files\NotPermittedException */