diff options
author | Roeland Jago Douma <roeland@famdouma.nl> | 2017-08-03 15:14:29 +0200 |
---|---|---|
committer | Roeland Jago Douma <roeland@famdouma.nl> | 2017-08-03 15:14:29 +0200 |
commit | 4633717f0494a05a921a30066e47dc71d6002129 (patch) | |
tree | 87ce72d754e3821e5fb091be1a2db1ac59c18b2e /tests/lib/Share20/LegacyHooksTest.php | |
parent | 8fecc336ac78719777c1a11ee62c14f084b56d51 (diff) | |
download | nextcloud-server-4633717f0494a05a921a30066e47dc71d6002129.tar.gz nextcloud-server-4633717f0494a05a921a30066e47dc71d6002129.zip |
Move pre_shared and post_shared events to EventDispatcher
* Now listeners for those events get proper share objects.
* Legacy hooks still fired
* Updated tests
Signed-off-by: Roeland Jago Douma <roeland@famdouma.nl>
Diffstat (limited to 'tests/lib/Share20/LegacyHooksTest.php')
-rw-r--r-- | tests/lib/Share20/LegacyHooksTest.php | 150 |
1 files changed, 150 insertions, 0 deletions
diff --git a/tests/lib/Share20/LegacyHooksTest.php b/tests/lib/Share20/LegacyHooksTest.php index 22d575d26ff..5fc6a447047 100644 --- a/tests/lib/Share20/LegacyHooksTest.php +++ b/tests/lib/Share20/LegacyHooksTest.php @@ -24,6 +24,7 @@ namespace Test\Share20; use OC\Share20\LegacyHooks; use OC\Share20\Manager; +use OCP\Constants; use OCP\Files\File; use Symfony\Component\EventDispatcher\EventDispatcher; use Symfony\Component\EventDispatcher\GenericEvent; @@ -187,4 +188,153 @@ class LegacyHooksTest extends TestCase { $event = new GenericEvent($share); $this->eventDispatcher->dispatch('OCP\Share::postUnshareFromSelf', $event); } + + public function testPreShare() { + $path = $this->createMock(File::class); + $path->method('getId')->willReturn(1); + + $date = new \DateTime(); + + $share = $this->manager->newShare(); + $share->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setSharedWith('awesomeUser') + ->setSharedBy('sharedBy') + ->setNode($path) + ->setTarget('myTarget') + ->setPermissions(Constants::PERMISSION_ALL) + ->setExpirationDate($date) + ->setPassword('password') + ->setToken('token'); + + + $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock(); + \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare'); + + $run = true; + $error = ''; + + $expected = [ + 'itemType' => 'file', + 'itemSource' => 1, + 'shareType' => \OCP\Share::SHARE_TYPE_LINK, + 'shareWith' => 'awesomeUser', + 'uidOwner' => 'sharedBy', + 'fileSource' => 1, + 'itemTarget' => 'myTarget', + 'permissions' => Constants::PERMISSION_ALL, + 'expiration' => $date, + 'token' => 'token', + 'run' => &$run, + 'error' => &$error, + ]; + + $hookListner + ->expects($this->exactly(1)) + ->method('preShare') + ->with($expected); + + $event = new GenericEvent($share); + $this->eventDispatcher->dispatch('OCP\Share::preShare', $event); + } + + public function testPreShareError() { + $path = $this->createMock(File::class); + $path->method('getId')->willReturn(1); + + $date = new \DateTime(); + + $share = $this->manager->newShare(); + $share->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setSharedWith('awesomeUser') + ->setSharedBy('sharedBy') + ->setNode($path) + ->setTarget('myTarget') + ->setPermissions(Constants::PERMISSION_ALL) + ->setExpirationDate($date) + ->setPassword('password') + ->setToken('token'); + + + $hookListner = $this->getMockBuilder('Dummy')->setMethods(['preShare'])->getMock(); + \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'preShare'); + + $run = true; + $error = ''; + + $expected = [ + 'itemType' => 'file', + 'itemSource' => 1, + 'shareType' => \OCP\Share::SHARE_TYPE_LINK, + 'shareWith' => 'awesomeUser', + 'uidOwner' => 'sharedBy', + 'fileSource' => 1, + 'itemTarget' => 'myTarget', + 'permissions' => Constants::PERMISSION_ALL, + 'expiration' => $date, + 'token' => 'token', + 'run' => &$run, + 'error' => &$error, + ]; + + $hookListner + ->expects($this->exactly(1)) + ->method('preShare') + ->with($expected) + ->will($this->returnCallback(function ($data) { + $data['run'] = false; + $data['error'] = 'I error'; + })); + + $event = new GenericEvent($share); + $this->eventDispatcher->dispatch('OCP\Share::preShare', $event); + + $this->assertTrue($event->isPropagationStopped()); + $this->assertSame('I error', $event->getArgument('error')); + } + + public function testPostShare() { + $path = $this->createMock(File::class); + $path->method('getId')->willReturn(1); + + $date = new \DateTime(); + + $share = $this->manager->newShare(); + $share->setId(42) + ->setShareType(\OCP\Share::SHARE_TYPE_LINK) + ->setSharedWith('awesomeUser') + ->setSharedBy('sharedBy') + ->setNode($path) + ->setTarget('myTarget') + ->setPermissions(Constants::PERMISSION_ALL) + ->setExpirationDate($date) + ->setPassword('password') + ->setToken('token'); + + + $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postShare'])->getMock(); + \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'postShare'); + + $expected = [ + 'id' => 42, + 'itemType' => 'file', + 'itemSource' => 1, + 'shareType' => \OCP\Share::SHARE_TYPE_LINK, + 'shareWith' => 'awesomeUser', + 'uidOwner' => 'sharedBy', + 'fileSource' => 1, + 'itemTarget' => 'myTarget', + 'fileTarget' => 'myTarget', + 'permissions' => Constants::PERMISSION_ALL, + 'expiration' => $date, + 'token' => 'token', + ]; + + $hookListner + ->expects($this->exactly(1)) + ->method('postShare') + ->with($expected); + + $event = new GenericEvent($share); + $this->eventDispatcher->dispatch('OCP\Share::postShare', $event); + } } |