use OCP\DB\QueryBuilder\IQueryBuilder;
use OCP\EventDispatcher\IEventDispatcher;
use OCP\Files\Storage\IStorage;
+use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
use Symfony\Component\EventDispatcher\GenericEvent;
class Manager implements IManager {
-
/** @var IStorage */
protected $storage;
/** @var IConfig */
private $config;
+ private ICacheFactory $cacheFactory;
public function __construct(
IDBConnection $connection,
ILogger $logger,
IUserSession $session,
IEventDispatcher $dispatcher,
- IConfig $config
+ IConfig $config,
+ ICacheFactory $cacheFactory,
) {
$this->connection = $connection;
$this->container = $container;
$this->session = $session;
$this->dispatcher = $dispatcher;
$this->config = $config;
+ $this->cacheFactory = $cacheFactory;
}
public function getRuleMatcher(): IRuleMatcher {
}
public function getAllConfiguredEvents() {
+ $cache = $this->cacheFactory->createDistributed('flow');
+ $cached = $cache->get('events');
+ if ($cached !== null) {
+ return $cached;
+ }
+
$query = $this->connection->getQueryBuilder();
$query->select('class', 'entity')
}
$result->closeCursor();
+ $cache->set('events', $operations, 3600);
+
return $operations;
}
]);
$query->execute();
+ $this->cacheFactory->createDistributed('flow')->remove('events');
+
return $query->getLastInsertId();
}
throw $e;
}
unset($this->operations[$scopeContext->getHash()]);
+ $this->cacheFactory->createDistributed('flow')->remove('events');
return $this->getOperation($id);
}
unset($this->operations[$scopeContext->getHash()]);
}
+ $this->cacheFactory->createDistributed('flow')->remove('events');
+
return $result;
}
use OCA\WorkflowEngine\Helper\ScopeContext;
use OCA\WorkflowEngine\Manager;
use OCP\EventDispatcher\IEventDispatcher;
+use OCP\Files\Events\Node\NodeCreatedEvent;
use OCP\Files\IRootFolder;
+use OCP\ICache;
+use OCP\ICacheFactory;
use OCP\IConfig;
use OCP\IDBConnection;
use OCP\IL10N;
* @group DB
*/
class ManagerTest extends TestCase {
-
/** @var Manager */
protected $manager;
/** @var MockObject|IDBConnection */
protected $dispatcher;
/** @var MockObject|IConfig */
protected $config;
+ /** @var MockObject|ICacheFactory */
+ protected $cacheFactory;
protected function setUp(): void {
parent::setUp();
$this->session = $this->createMock(IUserSession::class);
$this->dispatcher = $this->createMock(IEventDispatcher::class);
$this->config = $this->createMock(IConfig::class);
+ $this->cacheFactory = $this->createMock(ICacheFactory::class);
$this->manager = new Manager(
\OC::$server->getDatabaseConnection(),
$this->logger,
$this->session,
$this->dispatcher,
- $this->config
+ $this->config,
+ $this->cacheFactory
);
$this->clearTables();
}
});
}
+ public function testGetAllConfiguredEvents() {
+ $adminScope = $this->buildScope();
+ $userScope = $this->buildScope('jackie');
+ $entity = File::class;
+
+ $opId5 = $this->invokePrivate(
+ $this->manager,
+ 'insertOperation',
+ ['OCA\WFE\OtherTestOp', 'Test04', [], 'foo', $entity, [NodeCreatedEvent::class]]
+ );
+ $this->invokePrivate($this->manager, 'addScope', [$opId5, $userScope]);
+
+ $allOperations = null;
+
+ $cache = $this->createMock(ICache::class);
+ $cache
+ ->method('get')
+ ->willReturnCallback(function () use (&$allOperations) {
+ if ($allOperations) {
+ return $allOperations;
+ }
+
+ return null;
+ });
+
+ $this->cacheFactory->method('createDistributed')->willReturn($cache);
+ $allOperations = $this->manager->getAllConfiguredEvents();
+ $this->assertCount(1, $allOperations);
+
+ $allOperationsCached = $this->manager->getAllConfiguredEvents();
+ $this->assertCount(1, $allOperationsCached);
+ $this->assertEquals($allOperationsCached, $allOperations);
+ }
+
public function testUpdateOperation() {
$adminScope = $this->buildScope();
$userScope = $this->buildScope('jackie');
$entity = File::class;
+ $cache = $this->createMock(ICache::class);
+ $cache->expects($this->exactly(4))
+ ->method('remove')
+ ->with('events');
+ $this->cacheFactory->method('createDistributed')->willReturn($cache);
+
$this->container->expects($this->any())
->method('query')
->willReturnCallback(function ($class) {
$userScope = $this->buildScope('jackie');
$entity = File::class;
+ $cache = $this->createMock(ICache::class);
+ $cache->expects($this->exactly(4))
+ ->method('remove')
+ ->with('events');
+ $this->cacheFactory->method('createDistributed')->willReturn($cache);
+
$opId1 = $this->invokePrivate(
$this->manager,
'insertOperation',