2017-03-14 21:18:20 +03:00
|
|
|
<?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/>.
|
|
|
|
*
|
|
|
|
*/
|
2019-11-22 22:52:10 +03:00
|
|
|
|
2017-03-14 21:18:20 +03:00
|
|
|
namespace Test\Share20;
|
|
|
|
|
|
|
|
use OC\Share20\LegacyHooks;
|
|
|
|
use OC\Share20\Manager;
|
2017-08-03 16:14:29 +03:00
|
|
|
use OCP\Constants;
|
2021-01-26 17:48:04 +03:00
|
|
|
use OCP\Files\Cache\ICacheEntry;
|
2017-03-14 21:18:20 +03:00
|
|
|
use OCP\Files\File;
|
2020-06-24 17:49:16 +03:00
|
|
|
use OCP\Share\IShare;
|
2017-03-14 21:18:20 +03:00
|
|
|
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;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2017-03-14 21:18:20 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->eventDispatcher = new EventDispatcher();
|
|
|
|
$this->hooks = new LegacyHooks($this->eventDispatcher);
|
|
|
|
$this->manager = \OC::$server->getShareManager();
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testPreUnshare() {
|
|
|
|
$path = $this->createMock(File::class);
|
|
|
|
$path->method('getId')->willReturn(1);
|
|
|
|
|
2021-01-26 17:48:04 +03:00
|
|
|
$info = $this->createMock(ICacheEntry::class);
|
|
|
|
$info->method('getMimeType')->willReturn('text/plain');
|
|
|
|
|
2017-03-14 21:18:20 +03:00
|
|
|
$share = $this->manager->newShare();
|
|
|
|
$share->setId(42)
|
|
|
|
->setProviderId('prov')
|
2020-06-24 17:49:16 +03:00
|
|
|
->setShareType(IShare::TYPE_USER)
|
2017-03-14 21:18:20 +03:00
|
|
|
->setSharedWith('awesomeUser')
|
|
|
|
->setSharedBy('sharedBy')
|
|
|
|
->setNode($path)
|
2021-01-26 17:48:04 +03:00
|
|
|
->setTarget('myTarget')
|
|
|
|
->setNodeCacheEntry($info);
|
2017-03-14 21:18:20 +03:00
|
|
|
|
|
|
|
$hookListner = $this->getMockBuilder('Dummy')->setMethods(['pre'])->getMock();
|
|
|
|
\OCP\Util::connectHook('OCP\Share', 'pre_unshare', $hookListner, 'pre');
|
|
|
|
|
|
|
|
$hookListnerExpectsPre = [
|
|
|
|
'id' => 42,
|
|
|
|
'itemType' => 'file',
|
|
|
|
'itemSource' => 1,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2017-03-14 21:18:20 +03:00
|
|
|
'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);
|
|
|
|
|
2021-01-26 17:48:04 +03:00
|
|
|
$info = $this->createMock(ICacheEntry::class);
|
|
|
|
$info->method('getMimeType')->willReturn('text/plain');
|
|
|
|
|
2017-03-14 21:18:20 +03:00
|
|
|
$share = $this->manager->newShare();
|
|
|
|
$share->setId(42)
|
|
|
|
->setProviderId('prov')
|
2020-06-24 17:49:16 +03:00
|
|
|
->setShareType(IShare::TYPE_USER)
|
2017-03-14 21:18:20 +03:00
|
|
|
->setSharedWith('awesomeUser')
|
|
|
|
->setSharedBy('sharedBy')
|
|
|
|
->setNode($path)
|
2021-01-26 17:48:04 +03:00
|
|
|
->setTarget('myTarget')
|
|
|
|
->setNodeCacheEntry($info);
|
2017-03-14 21:18:20 +03:00
|
|
|
|
|
|
|
$hookListner = $this->getMockBuilder('Dummy')->setMethods(['post'])->getMock();
|
2017-07-16 12:49:46 +03:00
|
|
|
\OCP\Util::connectHook('OCP\Share', 'post_unshare', $hookListner, 'post');
|
2017-03-14 21:18:20 +03:00
|
|
|
|
|
|
|
$hookListnerExpectsPost = [
|
|
|
|
'id' => 42,
|
|
|
|
'itemType' => 'file',
|
|
|
|
'itemSource' => 1,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2017-03-14 21:18:20 +03:00
|
|
|
'shareWith' => 'awesomeUser',
|
|
|
|
'itemparent' => null,
|
|
|
|
'uidOwner' => 'sharedBy',
|
|
|
|
'fileSource' => 1,
|
|
|
|
'fileTarget' => 'myTarget',
|
|
|
|
'deletedShares' => [
|
|
|
|
[
|
|
|
|
'id' => 42,
|
|
|
|
'itemType' => 'file',
|
|
|
|
'itemSource' => 1,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2017-03-14 21:18:20 +03:00
|
|
|
'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);
|
|
|
|
}
|
2017-07-16 17:26:11 +03:00
|
|
|
|
|
|
|
public function testPostUnshareFromSelf() {
|
|
|
|
$path = $this->createMock(File::class);
|
|
|
|
$path->method('getId')->willReturn(1);
|
|
|
|
|
2021-01-26 17:48:04 +03:00
|
|
|
$info = $this->createMock(ICacheEntry::class);
|
|
|
|
$info->method('getMimeType')->willReturn('text/plain');
|
|
|
|
|
2017-07-16 17:26:11 +03:00
|
|
|
$share = $this->manager->newShare();
|
|
|
|
$share->setId(42)
|
|
|
|
->setProviderId('prov')
|
2020-06-24 17:49:16 +03:00
|
|
|
->setShareType(IShare::TYPE_USER)
|
2017-07-16 17:26:11 +03:00
|
|
|
->setSharedWith('awesomeUser')
|
|
|
|
->setSharedBy('sharedBy')
|
|
|
|
->setNode($path)
|
2021-01-26 17:48:04 +03:00
|
|
|
->setTarget('myTarget')
|
|
|
|
->setNodeCacheEntry($info);
|
2017-07-16 17:26:11 +03:00
|
|
|
|
|
|
|
$hookListner = $this->getMockBuilder('Dummy')->setMethods(['postFromSelf'])->getMock();
|
|
|
|
\OCP\Util::connectHook('OCP\Share', 'post_unshareFromSelf', $hookListner, 'postFromSelf');
|
|
|
|
|
|
|
|
$hookListnerExpectsPostFromSelf = [
|
|
|
|
'id' => 42,
|
|
|
|
'itemType' => 'file',
|
|
|
|
'itemSource' => 1,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2017-07-16 17:26:11 +03:00
|
|
|
'shareWith' => 'awesomeUser',
|
|
|
|
'itemparent' => null,
|
|
|
|
'uidOwner' => 'sharedBy',
|
|
|
|
'fileSource' => 1,
|
|
|
|
'fileTarget' => 'myTarget',
|
|
|
|
'itemTarget' => 'myTarget',
|
|
|
|
'unsharedItems' => [
|
|
|
|
[
|
|
|
|
'id' => 42,
|
|
|
|
'itemType' => 'file',
|
|
|
|
'itemSource' => 1,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_USER,
|
2017-07-16 17:26:11 +03:00
|
|
|
'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);
|
|
|
|
}
|
2017-08-03 16:14:29 +03:00
|
|
|
|
|
|
|
public function testPreShare() {
|
|
|
|
$path = $this->createMock(File::class);
|
|
|
|
$path->method('getId')->willReturn(1);
|
|
|
|
|
|
|
|
$date = new \DateTime();
|
|
|
|
|
|
|
|
$share = $this->manager->newShare();
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->setShareType(IShare::TYPE_LINK)
|
2017-08-03 16:14:29 +03:00
|
|
|
->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,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_LINK,
|
2017-08-03 16:14:29 +03:00
|
|
|
'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();
|
2020-06-24 17:49:16 +03:00
|
|
|
$share->setShareType(IShare::TYPE_LINK)
|
2017-08-03 16:14:29 +03:00
|
|
|
->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,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_LINK,
|
2017-08-03 16:14:29 +03:00
|
|
|
'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)
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback(function ($data) {
|
2017-08-03 16:14:29 +03:00
|
|
|
$data['run'] = false;
|
|
|
|
$data['error'] = 'I error';
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2017-08-03 16:14:29 +03:00
|
|
|
|
|
|
|
$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)
|
2020-06-24 17:49:16 +03:00
|
|
|
->setShareType(IShare::TYPE_LINK)
|
2017-08-03 16:14:29 +03:00
|
|
|
->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,
|
2020-06-24 17:49:16 +03:00
|
|
|
'shareType' => IShare::TYPE_LINK,
|
2017-08-03 16:14:29 +03:00
|
|
|
'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);
|
|
|
|
}
|
2017-03-14 21:18:20 +03:00
|
|
|
}
|