Skip to content

Commit

Permalink
Add support for rename and copy events (#283)
Browse files Browse the repository at this point in the history
Fix #257
  • Loading branch information
R0Wi authored Dec 11, 2024
1 parent 796ca03 commit ea112f1
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
9 changes: 9 additions & 0 deletions lib/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
42 changes: 42 additions & 0 deletions tests/Unit/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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'],
Expand All @@ -455,4 +490,11 @@ public function dataProvider_EmptyOperationSettings() {
['{}']
];
}

public function dataProvider_EventsWithTwoNodes() {
return [
['\OCP\Files::postRename'],
['\OCP\Files::postCopy']
];
}
}

0 comments on commit ea112f1

Please sign in to comment.