Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[stable10] Add accept and reject symfony events for share #31702

Merged
merged 1 commit into from
Jun 8, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion apps/files_sharing/lib/API/OCSShareWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,8 @@ private function getShare20OCS() {
\OC::$server->getUserSession()->getUser(),
\OC::$server->getL10N('files_sharing'),
\OC::$server->getConfig(),
$this->application->getContainer()->query(NotificationPublisher::class)
$this->application->getContainer()->query(NotificationPublisher::class),
\OC::$server->getEventDispatcher()
);
}

Expand Down
27 changes: 26 additions & 1 deletion apps/files_sharing/lib/API/Share20OCS.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@
use OCP\Share\IShare;
use OCA\Files_Sharing\Service\NotificationPublisher;
use OCA\Files_Sharing\Helper;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;

/**
* Class Share20OCS
Expand Down Expand Up @@ -67,6 +69,8 @@ class Share20OCS {
private $config;
/** @var NotificationPublisher */
private $notificationPublisher;
/** @var EventDispatcher */
private $eventDispatcher;

/**
* @var string
Expand Down Expand Up @@ -97,7 +101,8 @@ public function __construct(
IUser $currentUser,
IL10N $l10n,
IConfig $config,
NotificationPublisher $notificationPublisher
NotificationPublisher $notificationPublisher,
EventDispatcher $eventDispatcher
) {
$this->shareManager = $shareManager;
$this->userManager = $userManager;
Expand All @@ -109,6 +114,7 @@ public function __construct(
$this->l = $l10n;
$this->config = $config;
$this->notificationPublisher = $notificationPublisher;
$this->eventDispatcher = $eventDispatcher;
$this->additionalInfoField = $this->config->getAppValue('core', 'user_additional_info_field', '');
}

Expand Down Expand Up @@ -857,18 +863,34 @@ public function declineShare($id) {
return $this->updateShareState($id, \OCP\Share::STATE_REJECTED);
}

/**
* @param $id
* @param $state
* @return \OC\OCS\Result
*/
private function updateShareState($id, $state) {
$eventName = '';
if ($state === \OCP\Share::STATE_ACCEPTED) {
$eventName = 'accept';
} elseif ($state === \OCP\Share::STATE_REJECTED) {
$eventName = 'reject';
}

if (!$this->shareManager->shareApiEnabled()) {
return new \OC\OCS\Result(null, 404, $this->l->t('Share API is disabled'));
}

try {
$share = $this->getShareById($id, $this->currentUser->getUID());
$this->eventDispatcher->dispatch('share.before' . $eventName, new GenericEvent(null, ['share' => $share]));
} catch (ShareNotFound $e) {
return new \OC\OCS\Result(null, 404, $this->l->t('Wrong share ID, share doesn\'t exist'));
}

if ($share->getState() === $state) {
if ($eventName !== '') {
$this->eventDispatcher->dispatch('share.after' . $eventName, new GenericEvent(null, ['share' => $share]));
}
// if there are no changes in the state, just return the share as if the change was successful
return new \OC\OCS\Result([$this->formatShare($share, true)]);
}
Expand Down Expand Up @@ -922,6 +944,9 @@ private function updateShareState($id, $state) {

$this->notificationPublisher->discardNotificationForUser($share, $this->currentUser->getUID());

if ($eventName !== '') {
$this->eventDispatcher->dispatch('share.after' . $eventName, new GenericEvent(null, ['share' => $share]));
}
return new \OC\OCS\Result([$this->formatShare($share, true)]);
}

Expand Down
92 changes: 87 additions & 5 deletions apps/files_sharing/tests/API/Share20OCSTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
*/
namespace OCA\Files_Sharing\Tests\API;

use OC\OCS\Result;
use OCA\Files_Sharing\API\Share20OCS;
use OCA\Files_Sharing\Service\NotificationPublisher;
use OCP\Files\IRootFolder;
Expand All @@ -37,6 +38,8 @@
use OCP\IUserManager;
use OCP\Lock\LockedException;
use OCP\Share;
use Symfony\Component\EventDispatcher\EventDispatcher;
use Symfony\Component\EventDispatcher\GenericEvent;
use Test\TestCase;
use OCP\Files\Folder;
use OCP\Files\Node;
Expand Down Expand Up @@ -80,6 +83,8 @@ class Share20OCSTest extends TestCase {
/** @var NotificationPublisher */
private $notificationPublisher;

private $eventDispatcher;

protected function setUp() {
$this->shareManager = $this->getMockBuilder('OCP\Share\IManager')
->disableOriginalConstructor()
Expand Down Expand Up @@ -112,6 +117,7 @@ protected function setUp() {
]));

$this->notificationPublisher = $this->createMock(NotificationPublisher::class);
$this->eventDispatcher = $this->createMock(EventDispatcher::class);

$this->ocs = new Share20OCS(
$this->shareManager,
Expand All @@ -123,7 +129,8 @@ protected function setUp() {
$this->currentUser,
$this->l,
$this->config,
$this->notificationPublisher
$this->notificationPublisher,
$this->eventDispatcher
);
}

Expand All @@ -145,6 +152,7 @@ private function mockFormatShare() {
$this->l,
$this->config,
$this->notificationPublisher,
$this->eventDispatcher
])->setMethods(['formatShare'])
->getMock();
}
Expand Down Expand Up @@ -454,6 +462,7 @@ public function testGetShare(\OCP\Share\IShare $share, array $result) {
$this->l,
$this->config,
$this->notificationPublisher,
$this->eventDispatcher
])->setMethods(['canAccessShare'])
->getMock();

Expand Down Expand Up @@ -2699,7 +2708,8 @@ public function getOcsDisabledAPI() {
$this->currentUser,
$this->l,
$this->config,
$this->notificationPublisher
$this->notificationPublisher,
$this->eventDispatcher
);
}

Expand Down Expand Up @@ -2791,7 +2801,8 @@ public function testGetShareAdditionalInfo($configValue, $expectedInfo) {
$this->currentUser,
$this->l,
$config,
$this->notificationPublisher
$this->notificationPublisher,
$this->eventDispatcher
);

list($file,) = $this->getMockFileFolder();
Expand Down Expand Up @@ -3047,7 +3058,7 @@ public function providesAcceptRejectShare() {
public function testAcceptRejectShare($method, $target, $targetExists, $expectedState) {
$userShare = $this->makeReceivedUserShareForOperation($target);

$this->shareManager->expects($this->once())
$this->shareManager->expects($this->exactly(1))
->method('getShareById')
->with('ocinternal:123', 'currentUser')
->willReturn($userShare);
Expand Down Expand Up @@ -3087,9 +3098,21 @@ public function testAcceptRejectShare($method, $target, $targetExists, $expected
->with($target)
->willReturn(false);
}
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->equalTo('share.beforeaccept'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))],
[$this->equalTo('share.afteraccept'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))]
);
} else {
$userFolder->expects($this->never())
->method('nodeExists');
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->equalTo('share.beforereject'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))],
[$this->equalTo('share.afterreject'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))]
);
}

$this->rootFolder->method('getUserFolder')
Expand All @@ -3110,6 +3133,53 @@ public function testAcceptRejectShare($method, $target, $targetExists, $expected
$this->assertSame($expectedState, $userShare->getState());
}

public function providesAcceptRejectShareSameState() {
return [
['acceptShare', '/target', true, \OCP\Share::STATE_ACCEPTED],
['declineShare', '/sfoo/target', true, \OCP\Share::STATE_REJECTED],
];
}

/**
* @dataProvider providesAcceptRejectShareSameState
*/
public function testAcceptRejectShareSameState($method, $target) {
$node = $this->createMock(Node::class);

$userShare = $this->newShare();
$userShare->setId(123);
$userShare->setNode($node);

$this->shareManager->expects($this->exactly(1))
->method('getShareById')
->with('ocinternal:123', 'currentUser')
->willReturn($userShare);

if ($method === 'acceptShare') {
$userShare->setState(\OCP\Share::STATE_ACCEPTED);
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->equalTo('share.beforeaccept'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))],
[$this->equalTo('share.afteraccept'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))]
);
} else {
$userShare->setState(\OCP\Share::STATE_REJECTED);
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->equalTo('share.beforereject'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))],
[$this->equalTo('share.afterreject'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))]
);
}

$ocs = $this->mockFormatShare();
$result = $ocs->$method(123);
$this->assertInstanceOf(Result::class, $result);
$this->assertNull($result->getData()[0]);
$this->assertEquals(100, $result->getStatusCode());
}

/**
* @dataProvider providesAcceptRejectShare
*/
Expand All @@ -3122,7 +3192,7 @@ public function testAcceptRejectShareMultiple($method, $target, $targetExists, $
$groupShare->setNode($userShare->getNode());
$groupShare->setTarget($target);

$this->shareManager->expects($this->once())
$this->shareManager->expects($this->exactly(1))
->method('getShareById')
->with('ocinternal:123', 'currentUser')
->willReturn($userShare);
Expand Down Expand Up @@ -3162,9 +3232,21 @@ public function testAcceptRejectShareMultiple($method, $target, $targetExists, $
->with($target)
->willReturn(false);
}
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->equalTo('share.beforeaccept'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))],
[$this->equalTo('share.afteraccept'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))]
);
} else {
$userFolder->expects($this->never())
->method('nodeExists');
$this->eventDispatcher->expects($this->exactly(2))
->method('dispatch')
->withConsecutive(
[$this->equalTo('share.beforereject'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))],
[$this->equalTo('share.afterreject'), $this->equalTo(new GenericEvent(null, ['share' => $userShare]))]
);
}

$this->rootFolder->method('getUserFolder')
Expand Down
3 changes: 2 additions & 1 deletion apps/files_sharing/tests/ApiTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ private function createOCS($request, $userId) {
$currentUser,
$l,
\OC::$server->getConfig(),
\OC::$server->getAppContainer('files_sharing')->query(NotificationPublisher::class)
\OC::$server->getAppContainer('files_sharing')->query(NotificationPublisher::class),
\OC::$server->getEventDispatcher()
);
}

Expand Down