diff --git a/lib/Operation.php b/lib/Operation.php index eec7950..8f07aa1 100644 --- a/lib/Operation.php +++ b/lib/Operation.php @@ -140,6 +140,15 @@ private function tryGetFile(string $eventName, Event $event, ?Node & $node) : bo private function tryGetFileFromGenericEvent(string $eventName, GenericEvent $event, ?Node & $node) : bool { $node = $event->getSubject(); + + // Some events have two nodes involved + $arrayEvents = [ + '\OCP\Files::postRename', + '\OCP\Files::postCopy' + ]; + if (in_array($eventName, $arrayEvents) && is_array($node) && count($node) >= 2) { + $node = $node[1]; + } if (!$node instanceof Node || $node->getType() !== FileInfo::TYPE_FILE) { $this->logger->debug( diff --git a/tests/Unit/OperationTest.php b/tests/Unit/OperationTest.php index 3737132..4e400d6 100644 --- a/tests/Unit/OperationTest.php +++ b/tests/Unit/OperationTest.php @@ -433,6 +433,41 @@ public function testFileAddedToQueueOnTagAssignedEvent() { $operation->onEvent($eventName, $event, $this->ruleMatcher); } + /** + * @dataProvider dataProvider_EventsWithTwoNodes + */ + public function testAddsEventOnEventWhereSubjectIsArray(string $eventName) { + $filePath = '/admin/files/path/to/file.pdf'; + $fileId = 42; + $uid = 'admin'; + $this->jobList->expects($this->once()) + ->method('add') + ->with(ProcessFileJob::class, ['fileId' => $fileId, 'uid' => $uid, 'settings' => self::SETTINGS]); + + $operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder); + + /** @var MockObject|IUser */ + $userMock = $this->createMock(IUser::class); + $userMock->expects($this->never()) + ->method('getUID') + ->willReturn($uid); + /** @var MockObject|Node */ + $fileMockSecondFile = $this->createMock(Node::class); + $fileMockSecondFile->method('getType') + ->willReturn(FileInfo::TYPE_FILE); + $fileMockSecondFile->method('getPath') + ->willReturn($filePath); + $fileMockSecondFile->method('getOwner') + ->willReturn($userMock); + $fileMockSecondFile->method('getId') + ->willReturn($fileId); + /** @var MockObject|Node */ + $fileMockFirstFile = $this->createMock(Node::class); + $event = new GenericEvent([$fileMockFirstFile, $fileMockSecondFile]); + + $operation->onEvent($eventName, $event, $this->ruleMatcher); + } + public function dataProvider_InvalidFilePaths() { $arr = [ ['/user/nofiles/somefile.pdf'], @@ -455,4 +490,11 @@ public function dataProvider_EmptyOperationSettings() { ['{}'] ]; } + + public function dataProvider_EventsWithTwoNodes() { + return [ + ['\OCP\Files::postRename'], + ['\OCP\Files::postCopy'] + ]; + } }