123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361 |
- <?php
- /**
- * @copyright 2017, Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @author Roeland Jago Douma <roeland@famdouma.nl>
- *
- * @license GNU AGPL version 3 or any later version
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <http://www.gnu.org/licenses/>.
- *
- */
- namespace Test\Share20;
- use OC\EventDispatcher\SymfonyAdapter;
- use OC\Share20\LegacyHooks;
- use OC\Share20\Manager;
- use OCP\Constants;
- use OCP\Files\Cache\ICacheEntry;
- use OCP\Files\File;
- use OCP\IServerContainer;
- use OCP\Share\IShare;
- use Psr\Log\LoggerInterface;
- use Symfony\Component\EventDispatcher\EventDispatcher;
- use Symfony\Component\EventDispatcher\GenericEvent;
- use Test\TestCase;
- class LegacyHooksTest extends TestCase {
- /** @var LegacyHooks */
- private $hooks;
- /** @var EventDispatcher */
- private $eventDispatcher;
- /** @var Manager */
- private $manager;
- protected function setUp(): void {
- parent::setUp();
- $symfonyDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher();
- $logger = $this->createMock(LoggerInterface::class);
- $eventDispatcher = new \OC\EventDispatcher\EventDispatcher($symfonyDispatcher, \OC::$server->get(IServerContainer::class), $logger);
- $this->eventDispatcher = new SymfonyAdapter($eventDispatcher, $logger);
- $this->hooks = new LegacyHooks($this->eventDispatcher);
- $this->manager = \OC::$server->getShareManager();
- }
- public function testPreUnshare() {
- $path = $this->createMock(File::class);
- $path->method('getId')->willReturn(1);
- $info = $this->createMock(ICacheEntry::class);
- $info->method('getMimeType')->willReturn('text/plain');
- $share = $this->manager->newShare();
- $share->setId(42)
- ->setProviderId('prov')
- ->setShareType(IShare::TYPE_USER)
- ->setSharedWith('awesomeUser')
- ->setSharedBy('sharedBy')
- ->setNode($path)
- ->setTarget('myTarget')
- ->setNodeCacheEntry($info);
- $hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
- \OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
- $hookListnerExpectsPre = [
- 'id' => 42,
- 'itemType' => 'file',
- 'itemSource' => 1,
- 'shareType' => IShare::TYPE_USER,
- 'shareWith' => 'awesomeUser',
- 'itemparent' => null,
- 'uidOwner' => 'sharedBy',
- 'fileSource' => 1,
- 'fileTarget' => 'myTarget',
- ];
- $hookListner
- ->expects($this->exactly(1))
- ->method('pre')
- ->with($hookListnerExpectsPre);
- $event = new GenericEvent($share);
- $this->eventDispatcher->dispatch('OCP\Share::preUnshare', $event);
- }
- public function testPostUnshare() {
- $path = $this->createMock(File::class);
- $path->method('getId')->willReturn(1);
- $info = $this->createMock(ICacheEntry::class);
- $info->method('getMimeType')->willReturn('text/plain');
- $share = $this->manager->newShare();
- $share->setId(42)
- ->setProviderId('prov')
- ->setShareType(IShare::TYPE_USER)
- ->setSharedWith('awesomeUser')
- ->setSharedBy('sharedBy')
- ->setNode($path)
- ->setTarget('myTarget')
- ->setNodeCacheEntry($info);
- $hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
- \OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
- $hookListnerExpectsPost = [
- 'id' => 42,
- 'itemType' => 'file',
- 'itemSource' => 1,
- 'shareType' => IShare::TYPE_USER,
- 'shareWith' => 'awesomeUser',
- 'itemparent' => null,
- 'uidOwner' => 'sharedBy',
- 'fileSource' => 1,
- 'fileTarget' => 'myTarget',
- 'deletedShares' => [
- [
- 'id' => 42,
- 'itemType' => 'file',
- 'itemSource' => 1,
- 'shareType' => IShare::TYPE_USER,
- 'shareWith' => 'awesomeUser',
- 'itemparent' => null,
- 'uidOwner' => 'sharedBy',
- 'fileSource' => 1,
- 'fileTarget' => 'myTarget',
- ],
- ],
- ];
- $hookListner
- ->expects($this->exactly(1))
- ->method('post')
- ->with($hookListnerExpectsPost);
- $event = new GenericEvent($share);
- $event->setArgument('deletedShares', [$share]);
- $this->eventDispatcher->dispatch('OCP\Share::postUnshare', $event);
- }
- public function testPostUnshareFromSelf() {
- $path = $this->createMock(File::class);
- $path->method('getId')->willReturn(1);
- $info = $this->createMock(ICacheEntry::class);
- $info->method('getMimeType')->willReturn('text/plain');
- $share = $this->manager->newShare();
- $share->setId(42)
- ->setProviderId('prov')
- ->setShareType(IShare::TYPE_USER)
- ->setSharedWith('awesomeUser')
- ->setSharedBy('sharedBy')
- ->setNode($path)
- ->setTarget('myTarget')
- ->setNodeCacheEntry($info);
- $hookListner = $this->getMockBuilder('Dummy')->setMethods(['postFromSelf'])->getMock();
- \OCP\Util::connectHook('OCP\Share', 'post_unshareFromSelf', $hookListner, 'postFromSelf');
- $hookListnerExpectsPostFromSelf = [
- 'id' => 42,
- 'itemType' => 'file',
- 'itemSource' => 1,
- 'shareType' => IShare::TYPE_USER,
- 'shareWith' => 'awesomeUser',
- 'itemparent' => null,
- 'uidOwner' => 'sharedBy',
- 'fileSource' => 1,
- 'fileTarget' => 'myTarget',
- 'itemTarget' => 'myTarget',
- 'unsharedItems' => [
- [
- 'id' => 42,
- 'itemType' => 'file',
- 'itemSource' => 1,
- 'shareType' => IShare::TYPE_USER,
- 'shareWith' => 'awesomeUser',
- 'itemparent' => null,
- 'uidOwner' => 'sharedBy',
- 'fileSource' => 1,
- 'fileTarget' => 'myTarget',
- 'itemTarget' => 'myTarget',
- ],
- ],
- ];
- $hookListner
- ->expects($this->exactly(1))
- ->method('postFromSelf')
- ->with($hookListnerExpectsPostFromSelf);
- $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(IShare::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' => IShare::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(IShare::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' => IShare::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)
- ->willReturnCallback(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(IShare::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' => IShare::TYPE_LINK,
- 'shareWith' => 'awesomeUser',
- 'uidOwner' => 'sharedBy',
- 'fileSource' => 1,
- 'itemTarget' => 'myTarget',
- 'fileTarget' => 'myTarget',
- 'permissions' => Constants::PERMISSION_ALL,
- 'expiration' => $date,
- 'token' => 'token',
- 'path' => null,
- ];
- $hookListner
- ->expects($this->exactly(1))
- ->method('postShare')
- ->with($expected);
- $event = new GenericEvent($share);
- $this->eventDispatcher->dispatch('OCP\Share::postShare', $event);
- }
- }
|