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 | |
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')
-rw-r--r-- | tests/lib/Share20/LegacyHooksTest.php | 150 | ||||
-rw-r--r-- | tests/lib/Share20/ManagerTest.php | 173 |
2 files changed, 237 insertions, 86 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); + } } diff --git a/tests/lib/Share20/ManagerTest.php b/tests/lib/Share20/ManagerTest.php index 01d594de7ae..174aaffd0b6 100644 --- a/tests/lib/Share20/ManagerTest.php +++ b/tests/lib/Share20/ManagerTest.php @@ -1705,46 +1705,45 @@ class ManagerTest extends \Test\TestCase { return $share->setId(42); })); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre'); - \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'post'); - - $hookListnerExpectsPre = [ - 'itemType' => 'file', - 'itemSource' => 1, - 'shareType' => \OCP\Share::SHARE_TYPE_LINK, - 'uidOwner' => 'sharedBy', - 'permissions' => 31, - 'fileSource' => 1, - 'expiration' => $date, - 'token' => 'token', - 'run' => true, - 'error' => '', - 'itemTarget' => '/target', - 'shareWith' => null, - ]; - - $hookListnerExpectsPost = [ - 'itemType' => 'file', - 'itemSource' => 1, - 'shareType' => \OCP\Share::SHARE_TYPE_LINK, - 'uidOwner' => 'sharedBy', - 'permissions' => 31, - 'fileSource' => 1, - 'expiration' => $date, - 'token' => 'token', - 'id' => 42, - 'itemTarget' => '/target', - 'fileTarget' => '/target', - 'shareWith' => null, - ]; + // Pre share + $this->eventDispatcher->expects($this->at(0)) + ->method('dispatch') + ->with( + $this->equalTo('OCP\Share::preShare'), + $this->callback(function(GenericEvent $e) use ($path, $date) { + /** @var IShare $share */ + $share = $e->getSubject(); + + return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && + $share->getNode() === $path && + $share->getSharedBy() === 'sharedBy' && + $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && + $share->getExpirationDate() === $date && + $share->getPassword() === 'hashed' && + $share->getToken() === 'token'; + }) + ); - $hookListner->expects($this->once()) - ->method('pre') - ->with($this->equalTo($hookListnerExpectsPre)); - $hookListner->expects($this->once()) - ->method('post') - ->with($this->equalTo($hookListnerExpectsPost)); + // Post share + $this->eventDispatcher->expects($this->at(1)) + ->method('dispatch') + ->with( + $this->equalTo('OCP\Share::postShare'), + $this->callback(function(GenericEvent $e) use ($path, $date) { + /** @var IShare $share */ + $share = $e->getSubject(); + + return $share->getShareType() === \OCP\Share::SHARE_TYPE_LINK && + $share->getNode() === $path && + $share->getSharedBy() === 'sharedBy' && + $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && + $share->getExpirationDate() === $date && + $share->getPassword() === 'hashed' && + $share->getToken() === 'token' && + $share->getId() === '42' && + $share->getTarget() === '/target'; + }) + ); /** @var IShare $share */ $share = $manager->createShare($share); @@ -1817,46 +1816,45 @@ class ManagerTest extends \Test\TestCase { return $share->setId(42); })); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre', 'post'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre'); - \OCP\Util::connectHook('OCP\Share', 'post_shared', $hookListner, 'post'); - - $hookListnerExpectsPre = [ - 'itemType' => 'file', - 'itemSource' => 1, - 'shareType' => \OCP\Share::SHARE_TYPE_EMAIL, - 'uidOwner' => 'sharedBy', - 'permissions' => 31, - 'fileSource' => 1, - 'expiration' => null, - 'token' => 'token', - 'run' => true, - 'error' => '', - 'itemTarget' => '/target', - 'shareWith' => null, - ]; - - $hookListnerExpectsPost = [ - 'itemType' => 'file', - 'itemSource' => 1, - 'shareType' => \OCP\Share::SHARE_TYPE_EMAIL, - 'uidOwner' => 'sharedBy', - 'permissions' => 31, - 'fileSource' => 1, - 'expiration' => null, - 'token' => 'token', - 'id' => 42, - 'itemTarget' => '/target', - 'fileTarget' => '/target', - 'shareWith' => null, - ]; + // Pre share + $this->eventDispatcher->expects($this->at(0)) + ->method('dispatch') + ->with( + $this->equalTo('OCP\Share::preShare'), + $this->callback(function(GenericEvent $e) use ($path) { + /** @var IShare $share */ + $share = $e->getSubject(); + + return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL && + $share->getNode() === $path && + $share->getSharedBy() === 'sharedBy' && + $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && + $share->getExpirationDate() === null && + $share->getPassword() === null && + $share->getToken() === 'token'; + }) + ); - $hookListner->expects($this->once()) - ->method('pre') - ->with($this->equalTo($hookListnerExpectsPre)); - $hookListner->expects($this->once()) - ->method('post') - ->with($this->equalTo($hookListnerExpectsPost)); + // Post share + $this->eventDispatcher->expects($this->at(1)) + ->method('dispatch') + ->with( + $this->equalTo('OCP\Share::postShare'), + $this->callback(function(GenericEvent $e) use ($path) { + /** @var IShare $share */ + $share = $e->getSubject(); + + return $share->getShareType() === \OCP\Share::SHARE_TYPE_EMAIL && + $share->getNode() === $path && + $share->getSharedBy() === 'sharedBy' && + $share->getPermissions() === \OCP\Constants::PERMISSION_ALL && + $share->getExpirationDate() === null && + $share->getPassword() === null && + $share->getToken() === 'token' && + $share->getId() === '42' && + $share->getTarget() === '/target'; + }) + ); /** @var IShare $share */ $share = $manager->createShare($share); @@ -1919,14 +1917,17 @@ class ManagerTest extends \Test\TestCase { ->method('setTarget') ->with('/target'); - $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock(); - \OCP\Util::connectHook('OCP\Share', 'pre_shared', $hookListner, 'pre'); - $hookListner->expects($this->once()) - ->method('pre') - ->will($this->returnCallback(function (array $data) { - $data['run'] = false; - $data['error'] = 'I won\'t let you share!'; - })); + // Pre share + $this->eventDispatcher->expects($this->once()) + ->method('dispatch') + ->with( + $this->equalTo('OCP\Share::preShare'), + $this->isInstanceOf(GenericEvent::class) + )->will($this->returnCallback(function($name, GenericEvent $e) { + $e->setArgument('error', 'I won\'t let you share!'); + $e->stopPropagation(); + }) + ); $manager->createShare($share); } |