Skip to content

Commit

Permalink
Draft #72
Browse files Browse the repository at this point in the history
  • Loading branch information
R0Wi committed Nov 21, 2021
1 parent 2bac187 commit 7a2cddb
Show file tree
Hide file tree
Showing 2 changed files with 94 additions and 67 deletions.
97 changes: 77 additions & 20 deletions lib/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@

namespace OCA\WorkflowOcr;

use OCA\WorkflowEngine\Entity\File;
use OCA\WorkflowOcr\AppInfo\Application;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\Event;
Expand All @@ -38,9 +37,12 @@
use OCA\WorkflowOcr\BackgroundJobs\ProcessFileJob;
use OCA\WorkflowOcr\Helper\IProcessingFileAccessor;
use OCA\WorkflowOcr\Helper\SynchronizationHelper;
use OCP\Files\File;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IURLGenerator;
use OCP\SystemTag\MapperEvent;
use Psr\Log\LoggerInterface;

class Operation implements ISpecificOperation {
Expand All @@ -55,13 +57,22 @@ class Operation implements ISpecificOperation {
private $urlGenerator;
/** @var SynchronizationHelper */
private $processingFileAccessor;

public function __construct(IJobList $jobList, IL10N $l, LoggerInterface $logger, IURLGenerator $urlGenerator, IProcessingFileAccessor $processingFileAccessor) {
/** @var IRootFolder */
private $rootFolder;

public function __construct(
IJobList $jobList,
IL10N $l,
LoggerInterface $logger,
IURLGenerator $urlGenerator,
IProcessingFileAccessor $processingFileAccessor,
IRootFolder $rootFolder) {
$this->jobList = $jobList;
$this->l = $l;
$this->logger = $logger;
$this->urlGenerator = $urlGenerator;
$this->processingFileAccessor = $processingFileAccessor;
$this->rootFolder = $rootFolder;
}

/**
Expand Down Expand Up @@ -89,19 +100,14 @@ public function isAvailableForScope(int $scope): bool {
}

public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatcher): void {
if (!$this->checkEvent($eventName, $event) ||
!$this->checkRuleMatcher($ruleMatcher)) {
$this->logger->debug('onEvent: ' . $eventName);

if (!$this->checkRuleMatcher($ruleMatcher)) {
return;
}

/** @var GenericEvent */
$genericEvent = $event;
/** @var Node*/
$node = $genericEvent->getSubject();

if (!$node instanceof Node || $node->getType() !== FileInfo::TYPE_FILE) {
$this->logger->debug('Not processing event {eventname} because node is not a file.',
['eventname' => $eventName]);
// $node will be passed by reference
if (!$this->tryGetFile($eventName, $event, $node)) {
return;
}

Expand All @@ -115,21 +121,72 @@ public function onEvent(string $eventName, Event $event, IRuleMatcher $ruleMatch
'filePath' => $node->getPath(),
'uid' => $node->getOwner()->getUID()
];

$this->logger->debug('Adding file to jobqueue: ' . json_encode($args));

$this->jobList->add(ProcessFileJob::class, $args);
}

public function getEntityId(): string {
return File::class;
}

private function checkEvent(string $eventName, Event $event) : bool {
if ($eventName !== '\OCP\Files::postCreate' && $eventName !== '\OCP\Files::postWrite' ||
!$event instanceof GenericEvent) {
$this->logger->debug('Not processing event {eventname} with argument {event}.',
['eventname' => $eventName, 'event' => $event]);
return false;
private function tryGetFile(string $eventName, Event $event, ?Node & $node) : bool {
$node = null;

// Handle file creation/ file change events
if ($event instanceof GenericEvent) {
/** @var GenericEvent */
$genericEvent = $event;
$node = $genericEvent->getSubject();

if (!$node instanceof Node || $node->getType() !== FileInfo::TYPE_FILE) {
$this->logger->debug(
'Not processing event {eventname} because node is not a file.',
['eventname' => $eventName]
);
return false;
}

return true;
}
return true;

// Handle file tag assigned events
if ($event instanceof MapperEvent) {
if ($event->getObjectType() !== 'files') {
$this->logger->debug('Do not process MapperEvent of type {type}',
['type' => $event->getObjectType()]);
return false;
}

$fileId = intval($event->getObjectId());
if ($fileId === 0) {
$this->logger->warning(
'Not processing event {eventname} because file id \'{fileid}\' could not be casted to integer.',
['eventname' => $eventName],
['fileid' => $event->getObjectId()]
);
return false;
}

$files = $this->rootFolder->getById($fileId);
if (count($files) <= 0 || !($files[0] instanceof File)) {
$this->logger->warning(
'Not processing event {eventname} because node with id \'{fileid}\' could not be found or is not a file.',
['eventname' => $eventName],
['fileid' => $fileId]);
return false;
}

$node = $files[0];
return true;
}

$this->logger->warning('Not processing event {eventname} because the event type {eventtype} is not supported.',
['eventname' => $eventName],
['eventtype' => get_class($event)]);

return false;
}

private function checkRuleMatcher(IRuleMatcher $ruleMatcher) : bool {
Expand Down
64 changes: 17 additions & 47 deletions tests/Unit/OperationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,9 @@
use OCA\WorkflowOcr\Helper\IProcessingFileAccessor;
use OCA\WorkflowOcr\Operation;
use OCP\BackgroundJob\IJobList;
use OCP\EventDispatcher\Event;
use OCP\EventDispatcher\GenericEvent;
use OCP\Files\FileInfo;
use OCP\Files\IRootFolder;
use OCP\Files\Node;
use OCP\IL10N;
use OCP\IURLGenerator;
Expand All @@ -55,6 +55,8 @@ class OperationTest extends TestCase {
private $processingFileAccessor;
/** @var IRuleMatcher|MockObject */
private $ruleMatcher;
/** @var IRootFolder|MockObject */
private $rootFolder;

protected function setUp(): void {
parent::setUp();
Expand All @@ -66,21 +68,7 @@ protected function setUp(): void {
$this->ruleMatcher = $this->createMock(IRuleMatcher::class);
$this->ruleMatcher->method('getFlows')
->willReturn([$this->createMock(Operation::class)]); // simulate single matching operation
}

/**
* @dataProvider dataProvider_InvalidEvents
*/
public function testDoesNothingOnInvalidEvent(string $eventName, Event $event) {
$this->jobList->expects($this->never())
->method('add')
->withAnyParameters();
$this->logger->expects($this->once())
->method('debug')
->withAnyParameters();

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation->onEvent($eventName, $event, $this->ruleMatcher);
$this->rootFolder = $this->createMock(IRootFolder::class);
}

/**
Expand All @@ -90,11 +78,11 @@ public function testDoesNothingIfRuleMatcherDoesNotMatch($ruleMatcherResult) {
$this->jobList->expects($this->never())
->method('add')
->withAnyParameters();
$this->logger->expects($this->once())
$this->logger->expects($this->atLeastOnce())
->method('debug')
->withAnyParameters();

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

/** @var IRuleMatcher|MockObject */
$ruleMatcher = $this->createMock(IRuleMatcher::class);
Expand All @@ -114,7 +102,7 @@ public function testDoesNothingOnFolderEvent() {
->method('debug')
->withAnyParameters();

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$fileMock = $this->createMock(Node::class);
$fileMock->method('getType')
Expand All @@ -139,7 +127,7 @@ public function testDoesNothingOnPostWriteTriggeredByCurrentOcrProcess() {
->method('getCurrentlyProcessedFileId')
->willReturn(42);

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $processingFileAccessorMock);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $processingFileAccessorMock, $this->rootFolder);

$userMock = $this->createMock(IUser::class);
$userMock->expects($this->never())
Expand Down Expand Up @@ -171,7 +159,7 @@ public function testDoesNothingOnInvalidFilePath(string $filePath) {
->method('debug')
->withAnyParameters();

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$fileMock = $this->createMock(Node::class);
$fileMock->method('getType')
Expand All @@ -192,7 +180,7 @@ public function testDoesNothingOnFileWithoutOwner() {
->method('debug')
->withAnyParameters();

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$fileMock = $this->createMock(Node::class);
$fileMock->method('getType')
Expand All @@ -215,7 +203,7 @@ public function testAddWithCorrectFilePathAndUser() {
->method('add')
->with(ProcessFileJob::class, ['filePath' => $filePath, 'uid' => $uid]);

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$userMock = $this->createMock(IUser::class);
$userMock->expects($this->once())
Expand All @@ -240,7 +228,7 @@ public function testAddWithCorrectFilePathAndUser() {
* @dataProvider dataProvider_ValidScopes
*/
public function testIsAvailableForScope(int $scope) {
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);
$result = $operation->isAvailableForScope($scope);

$this->assertTrue($result);
Expand All @@ -256,7 +244,7 @@ public function testDoesNothing_OnValidateOperation() {
$this->urlGenerator->expects($this->never())
->method($this->anything());

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$operation->validateOperation('aName', [], 'aOp');
}
Expand All @@ -265,7 +253,7 @@ public function testCallsLang_OnGetDisplayName() {
$this->l->expects($this->once())
->method('t');

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$operation->getDisplayName();
}
Expand All @@ -275,7 +263,7 @@ public function testCallsLang_OnGetDescription() {
$this->l->expects($this->once())
->method('t');

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$operation->getDescription();
}
Expand All @@ -284,35 +272,17 @@ public function testCallsUrlGenerator_OnGetIcon() {
$this->urlGenerator->expects($this->once())
->method('imagePath');

$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$operation->getIcon();
}

public function testEntityIdIsFile() {
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor);
$operation = new Operation($this->jobList, $this->l, $this->logger, $this->urlGenerator, $this->processingFileAccessor, $this->rootFolder);

$this->assertEquals(File::class, $operation->getEntityId());
}

public function dataProvider_InvalidEvents() {
$arr = [
["\OCP\Files::preWrite", new GenericEvent()],
["\OCP\Files::preCreate", new GenericEvent()],
["\OCP\Files::preDelete", new GenericEvent()],
["\OCP\Files::postDelete", new GenericEvent()],
["\OCP\Files::postTouch", new GenericEvent()],
["\OCP\Files::preTouch", new GenericEvent()],
["\OCP\Files::preCopy", new GenericEvent()],
["\OCP\Files::postCopy", new GenericEvent()],
["\OCP\Files::preRename", new GenericEvent()],
["\OCP\Files::postRename", new GenericEvent()],
["\OCP\Files::postWrite", new Event()],
["\OCP\Files::postCreate", new Event()],
];
return $arr;
}

public function dataProvider_InvalidRuleMatcherResults() {
return [
[ [] ]
Expand Down

0 comments on commit 7a2cddb

Please sign in to comment.