2016-10-17 16:20:13 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\WorkflowEngine\Tests;
|
|
|
|
|
2019-08-28 16:00:02 +03:00
|
|
|
use OC\L10N\L10N;
|
2019-08-09 14:24:48 +03:00
|
|
|
use OCA\WorkflowEngine\Entity\File;
|
2019-08-19 18:13:47 +03:00
|
|
|
use OCA\WorkflowEngine\Helper\ScopeContext;
|
2016-10-17 16:20:13 +03:00
|
|
|
use OCA\WorkflowEngine\Manager;
|
2019-12-22 17:04:39 +03:00
|
|
|
use OCP\EventDispatcher\IEventDispatcher;
|
2019-09-10 09:59:51 +03:00
|
|
|
use OCP\Files\IRootFolder;
|
2020-03-13 16:36:47 +03:00
|
|
|
use OCP\IConfig;
|
2016-10-17 16:20:13 +03:00
|
|
|
use OCP\IDBConnection;
|
|
|
|
use OCP\IL10N;
|
2019-08-09 14:24:48 +03:00
|
|
|
use OCP\ILogger;
|
2016-10-17 16:20:13 +03:00
|
|
|
use OCP\IServerContainer;
|
2019-08-28 16:00:02 +03:00
|
|
|
use OCP\IURLGenerator;
|
2019-08-27 18:47:44 +03:00
|
|
|
use OCP\IUserSession;
|
2019-11-28 16:25:50 +03:00
|
|
|
use OCP\SystemTag\ISystemTagManager;
|
2019-08-19 18:13:47 +03:00
|
|
|
use OCP\WorkflowEngine\ICheck;
|
2019-08-09 14:24:48 +03:00
|
|
|
use OCP\WorkflowEngine\IEntity;
|
2020-10-28 16:07:14 +03:00
|
|
|
use OCP\WorkflowEngine\IEntityEvent;
|
2019-08-19 18:13:47 +03:00
|
|
|
use OCP\WorkflowEngine\IManager;
|
|
|
|
use OCP\WorkflowEngine\IOperation;
|
2019-08-09 14:24:48 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2016-10-17 16:20:13 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Class ManagerTest
|
|
|
|
*
|
|
|
|
* @package OCA\WorkflowEngine\Tests
|
|
|
|
* @group DB
|
|
|
|
*/
|
|
|
|
class ManagerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var Manager */
|
|
|
|
protected $manager;
|
2019-08-09 14:24:48 +03:00
|
|
|
/** @var MockObject|IDBConnection */
|
2016-10-17 16:20:13 +03:00
|
|
|
protected $db;
|
2019-08-09 14:24:48 +03:00
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */
|
|
|
|
protected $logger;
|
|
|
|
/** @var \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface */
|
2019-12-22 17:04:39 +03:00
|
|
|
protected $legacyDispatcher;
|
2019-08-09 14:24:48 +03:00
|
|
|
/** @var MockObject|IServerContainer */
|
|
|
|
protected $container;
|
2019-08-27 18:47:44 +03:00
|
|
|
/** @var MockObject|IUserSession */
|
|
|
|
protected $session;
|
2019-09-10 13:01:12 +03:00
|
|
|
/** @var MockObject|L10N */
|
|
|
|
protected $l;
|
2019-12-22 17:04:39 +03:00
|
|
|
/** @var MockObject|IEventDispatcher */
|
|
|
|
protected $dispatcher;
|
2020-03-13 16:36:47 +03:00
|
|
|
/** @var MockObject|IConfig */
|
|
|
|
protected $config;
|
2016-10-17 16:20:13 +03:00
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2016-10-17 16:20:13 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->db = \OC::$server->getDatabaseConnection();
|
2019-08-09 14:24:48 +03:00
|
|
|
$this->container = $this->createMock(IServerContainer::class);
|
|
|
|
/** @var IL10N|MockObject $l */
|
2019-09-10 13:01:12 +03:00
|
|
|
$this->l = $this->createMock(IL10N::class);
|
|
|
|
$this->l->method('t')
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function ($text, $parameters = []) {
|
2016-10-17 16:20:13 +03:00
|
|
|
return vsprintf($text, $parameters);
|
2020-03-26 00:21:27 +03:00
|
|
|
});
|
2016-10-17 16:20:13 +03:00
|
|
|
|
2019-12-22 17:04:39 +03:00
|
|
|
$this->legacyDispatcher = $this->createMock(EventDispatcherInterface::class);
|
2019-08-09 14:24:48 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2019-08-27 18:47:44 +03:00
|
|
|
$this->session = $this->createMock(IUserSession::class);
|
2019-12-22 17:04:39 +03:00
|
|
|
$this->dispatcher = $this->createMock(IEventDispatcher::class);
|
2020-03-13 16:36:47 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2019-08-09 14:24:48 +03:00
|
|
|
|
2016-10-17 16:20:13 +03:00
|
|
|
$this->manager = new Manager(
|
|
|
|
\OC::$server->getDatabaseConnection(),
|
2019-08-09 14:24:48 +03:00
|
|
|
$this->container,
|
2019-09-10 13:01:12 +03:00
|
|
|
$this->l,
|
2019-12-22 17:04:39 +03:00
|
|
|
$this->legacyDispatcher,
|
2019-08-27 18:47:44 +03:00
|
|
|
$this->logger,
|
2019-12-22 17:04:39 +03:00
|
|
|
$this->session,
|
2020-03-13 16:36:47 +03:00
|
|
|
$this->dispatcher,
|
|
|
|
$this->config
|
2016-10-17 16:20:13 +03:00
|
|
|
);
|
2019-08-19 18:13:47 +03:00
|
|
|
$this->clearTables();
|
2016-10-17 16:20:13 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function tearDown(): void {
|
2019-08-19 18:13:47 +03:00
|
|
|
$this->clearTables();
|
2016-10-17 16:20:13 +03:00
|
|
|
parent::tearDown();
|
|
|
|
}
|
|
|
|
|
2019-08-19 18:13:47 +03:00
|
|
|
/**
|
|
|
|
* @return MockObject|ScopeContext
|
|
|
|
*/
|
|
|
|
protected function buildScope(string $scopeId = null): MockObject {
|
|
|
|
$scopeContext = $this->createMock(ScopeContext::class);
|
|
|
|
$scopeContext->expects($this->any())
|
|
|
|
->method('getScope')
|
|
|
|
->willReturn($scopeId ? IManager::SCOPE_USER : IManager::SCOPE_ADMIN);
|
|
|
|
$scopeContext->expects($this->any())
|
|
|
|
->method('getScopeId')
|
|
|
|
->willReturn($scopeId ?? '');
|
|
|
|
$scopeContext->expects($this->any())
|
|
|
|
->method('getHash')
|
|
|
|
->willReturn(md5($scopeId ?? ''));
|
|
|
|
|
|
|
|
return $scopeContext;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function clearTables() {
|
2016-10-17 16:20:13 +03:00
|
|
|
$query = $this->db->getQueryBuilder();
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach (['flow_checks', 'flow_operations', 'flow_operations_scope'] as $table) {
|
2019-08-19 18:13:47 +03:00
|
|
|
$query->delete($table)
|
|
|
|
->execute();
|
|
|
|
}
|
2016-10-17 16:20:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testChecks() {
|
|
|
|
$check1 = $this->invokePrivate($this->manager, 'addCheck', ['Test', 'equal', 1]);
|
|
|
|
$check2 = $this->invokePrivate($this->manager, 'addCheck', ['Test', '!equal', 2]);
|
|
|
|
|
|
|
|
$data = $this->manager->getChecks([$check1]);
|
|
|
|
$this->assertArrayHasKey($check1, $data);
|
|
|
|
$this->assertArrayNotHasKey($check2, $data);
|
|
|
|
|
|
|
|
$data = $this->manager->getChecks([$check1, $check2]);
|
|
|
|
$this->assertArrayHasKey($check1, $data);
|
|
|
|
$this->assertArrayHasKey($check2, $data);
|
|
|
|
|
|
|
|
$data = $this->manager->getChecks([$check2, $check1]);
|
|
|
|
$this->assertArrayHasKey($check1, $data);
|
|
|
|
$this->assertArrayHasKey($check2, $data);
|
|
|
|
|
|
|
|
$data = $this->manager->getChecks([$check2]);
|
|
|
|
$this->assertArrayNotHasKey($check1, $data);
|
|
|
|
$this->assertArrayHasKey($check2, $data);
|
|
|
|
}
|
2019-08-09 14:24:48 +03:00
|
|
|
|
2019-08-19 18:13:47 +03:00
|
|
|
public function testScope() {
|
|
|
|
$adminScope = $this->buildScope();
|
|
|
|
$userScope = $this->buildScope('jackie');
|
2019-08-28 16:00:02 +03:00
|
|
|
$entity = File::class;
|
2019-08-19 18:13:47 +03:00
|
|
|
|
|
|
|
$opId1 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestOp', 'Test01', [11, 22], 'foo', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
|
|
|
|
|
|
|
|
$opId2 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestOp', 'Test02', [33, 22], 'bar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
|
|
|
|
$opId3 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestOp', 'Test03', [11, 44], 'foobar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId3, $userScope]);
|
|
|
|
|
|
|
|
$this->assertTrue($this->invokePrivate($this->manager, 'canModify', [$opId1, $adminScope]));
|
|
|
|
$this->assertFalse($this->invokePrivate($this->manager, 'canModify', [$opId2, $adminScope]));
|
|
|
|
$this->assertFalse($this->invokePrivate($this->manager, 'canModify', [$opId3, $adminScope]));
|
|
|
|
|
|
|
|
$this->assertFalse($this->invokePrivate($this->manager, 'canModify', [$opId1, $userScope]));
|
|
|
|
$this->assertTrue($this->invokePrivate($this->manager, 'canModify', [$opId2, $userScope]));
|
|
|
|
$this->assertTrue($this->invokePrivate($this->manager, 'canModify', [$opId3, $userScope]));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetAllOperations() {
|
|
|
|
$adminScope = $this->buildScope();
|
|
|
|
$userScope = $this->buildScope('jackie');
|
2019-08-28 16:00:02 +03:00
|
|
|
$entity = File::class;
|
2019-08-19 18:13:47 +03:00
|
|
|
|
|
|
|
$opId1 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestAdminOp', 'Test01', [11, 22], 'foo', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
|
|
|
|
|
|
|
|
$opId2 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
|
|
|
|
$opId3 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestUserOp', 'Test03', [11, 44], 'foobar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId3, $userScope]);
|
|
|
|
|
|
|
|
$adminOps = $this->manager->getAllOperations($adminScope);
|
|
|
|
$userOps = $this->manager->getAllOperations($userScope);
|
|
|
|
|
|
|
|
$this->assertSame(1, count($adminOps));
|
|
|
|
$this->assertTrue(array_key_exists('OCA\WFE\TestAdminOp', $adminOps));
|
|
|
|
$this->assertFalse(array_key_exists('OCA\WFE\TestUserOp', $adminOps));
|
|
|
|
|
|
|
|
$this->assertSame(1, count($userOps));
|
|
|
|
$this->assertFalse(array_key_exists('OCA\WFE\TestAdminOp', $userOps));
|
|
|
|
$this->assertTrue(array_key_exists('OCA\WFE\TestUserOp', $userOps));
|
|
|
|
$this->assertSame(2, count($userOps['OCA\WFE\TestUserOp']));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetOperations() {
|
|
|
|
$adminScope = $this->buildScope();
|
|
|
|
$userScope = $this->buildScope('jackie');
|
2019-08-28 16:00:02 +03:00
|
|
|
$entity = File::class;
|
2019-08-19 18:13:47 +03:00
|
|
|
|
|
|
|
$opId1 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestOp', 'Test01', [11, 22], 'foo', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
|
|
|
|
$opId4 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\OtherTestOp', 'Test04', [5], 'foo', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId4, $adminScope]);
|
|
|
|
|
|
|
|
$opId2 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestOp', 'Test02', [33, 22], 'bar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
|
|
|
|
$opId3 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestOp', 'Test03', [11, 44], 'foobar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId3, $userScope]);
|
|
|
|
$opId5 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\OtherTestOp', 'Test05', [5], 'foobar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId5, $userScope]);
|
|
|
|
|
|
|
|
$adminOps = $this->manager->getOperations('OCA\WFE\TestOp', $adminScope);
|
|
|
|
$userOps = $this->manager->getOperations('OCA\WFE\TestOp', $userScope);
|
|
|
|
|
|
|
|
$this->assertSame(1, count($adminOps));
|
|
|
|
array_walk($adminOps, function ($op) {
|
|
|
|
$this->assertTrue($op['class'] === 'OCA\WFE\TestOp');
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertSame(2, count($userOps));
|
|
|
|
array_walk($userOps, function ($op) {
|
|
|
|
$this->assertTrue($op['class'] === 'OCA\WFE\TestOp');
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testUpdateOperation() {
|
|
|
|
$adminScope = $this->buildScope();
|
|
|
|
$userScope = $this->buildScope('jackie');
|
2019-08-28 16:00:02 +03:00
|
|
|
$entity = File::class;
|
2019-08-19 18:13:47 +03:00
|
|
|
|
|
|
|
$this->container->expects($this->any())
|
|
|
|
->method('query')
|
|
|
|
->willReturnCallback(function ($class) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if (substr($class, -2) === 'Op') {
|
2019-08-19 18:13:47 +03:00
|
|
|
return $this->createMock(IOperation::class);
|
2020-04-10 15:19:56 +03:00
|
|
|
} elseif ($class === File::class) {
|
2019-08-28 16:00:02 +03:00
|
|
|
return $this->getMockBuilder(File::class)
|
2019-11-06 12:54:37 +03:00
|
|
|
->setConstructorArgs([
|
|
|
|
$this->l,
|
|
|
|
$this->createMock(IURLGenerator::class),
|
|
|
|
$this->createMock(IRootFolder::class),
|
2019-11-28 16:25:50 +03:00
|
|
|
$this->createMock(ILogger::class),
|
|
|
|
$this->createMock(\OCP\Share\IManager::class),
|
|
|
|
$this->createMock(IUserSession::class),
|
|
|
|
$this->createMock(ISystemTagManager::class)
|
2019-11-06 12:54:37 +03:00
|
|
|
])
|
2019-08-28 16:00:02 +03:00
|
|
|
->setMethodsExcept(['getEvents'])
|
|
|
|
->getMock();
|
2019-08-19 18:13:47 +03:00
|
|
|
}
|
|
|
|
return $this->createMock(ICheck::class);
|
|
|
|
});
|
|
|
|
|
|
|
|
$opId1 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestAdminOp', 'Test01', [11, 22], 'foo', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
|
|
|
|
|
|
|
|
$opId2 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
|
|
|
|
|
|
|
|
$check1 = ['class' => 'OCA\WFE\C22', 'operator' => 'eq', 'value' => 'asdf'];
|
|
|
|
$check2 = ['class' => 'OCA\WFE\C33', 'operator' => 'eq', 'value' => 23456];
|
|
|
|
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
2019-09-10 13:01:12 +03:00
|
|
|
$op = $this->manager->updateOperation($opId1, 'Test01a', [$check1, $check2], 'foohur', $adminScope, $entity, ['\OCP\Files::postDelete']);
|
2019-08-19 18:13:47 +03:00
|
|
|
$this->assertSame('Test01a', $op['name']);
|
|
|
|
$this->assertSame('foohur', $op['operation']);
|
|
|
|
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
2019-09-10 13:01:12 +03:00
|
|
|
$op = $this->manager->updateOperation($opId2, 'Test02a', [$check1], 'barfoo', $userScope, $entity, ['\OCP\Files::postDelete']);
|
2019-08-19 18:13:47 +03:00
|
|
|
$this->assertSame('Test02a', $op['name']);
|
|
|
|
$this->assertSame('barfoo', $op['operation']);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ([[$adminScope, $opId2], [$userScope, $opId1]] as $run) {
|
2019-08-19 18:13:47 +03:00
|
|
|
try {
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
2019-08-28 16:00:02 +03:00
|
|
|
$this->manager->updateOperation($run[1], 'Evil', [$check2], 'hackx0r', $run[0], $entity, []);
|
2019-08-19 18:13:47 +03:00
|
|
|
$this->assertTrue(false, 'DomainException not thrown');
|
|
|
|
} catch (\DomainException $e) {
|
|
|
|
$this->assertTrue(true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testDeleteOperation() {
|
|
|
|
$adminScope = $this->buildScope();
|
|
|
|
$userScope = $this->buildScope('jackie');
|
2019-08-28 16:00:02 +03:00
|
|
|
$entity = File::class;
|
2019-08-19 18:13:47 +03:00
|
|
|
|
|
|
|
$opId1 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestAdminOp', 'Test01', [11, 22], 'foo', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
|
|
|
|
|
|
|
|
$opId2 = $this->invokePrivate(
|
|
|
|
$this->manager,
|
|
|
|
'insertOperation',
|
2019-08-28 16:00:02 +03:00
|
|
|
['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
|
2019-08-19 18:13:47 +03:00
|
|
|
);
|
|
|
|
$this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ([[$adminScope, $opId2], [$userScope, $opId1]] as $run) {
|
2019-08-19 18:13:47 +03:00
|
|
|
try {
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
$this->manager->deleteOperation($run[1], $run[0]);
|
|
|
|
$this->assertTrue(false, 'DomainException not thrown');
|
|
|
|
} catch (\Exception $e) {
|
|
|
|
$this->assertInstanceOf(\DomainException::class, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
$this->manager->deleteOperation($opId1, $adminScope);
|
|
|
|
/** @noinspection PhpUnhandledExceptionInspection */
|
|
|
|
$this->manager->deleteOperation($opId2, $userScope);
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
foreach ([$opId1, $opId2] as $opId) {
|
2019-08-19 18:13:47 +03:00
|
|
|
try {
|
|
|
|
$this->invokePrivate($this->manager, 'getOperation', [$opId]);
|
|
|
|
$this->assertTrue(false, 'UnexpectedValueException not thrown');
|
2020-04-10 15:19:56 +03:00
|
|
|
} catch (\Exception $e) {
|
2019-08-19 18:13:47 +03:00
|
|
|
$this->assertInstanceOf(\UnexpectedValueException::class, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-09 14:24:48 +03:00
|
|
|
public function testGetEntitiesListBuildInOnly() {
|
|
|
|
$fileEntityMock = $this->createMock(File::class);
|
|
|
|
|
|
|
|
$this->container->expects($this->once())
|
|
|
|
->method('query')
|
|
|
|
->with(File::class)
|
|
|
|
->willReturn($fileEntityMock);
|
|
|
|
|
|
|
|
$entities = $this->manager->getEntitiesList();
|
|
|
|
|
|
|
|
$this->assertCount(1, $entities);
|
|
|
|
$this->assertInstanceOf(IEntity::class, $entities[0]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetEntitiesList() {
|
|
|
|
$fileEntityMock = $this->createMock(File::class);
|
|
|
|
|
|
|
|
$this->container->expects($this->once())
|
|
|
|
->method('query')
|
|
|
|
->with(File::class)
|
|
|
|
->willReturn($fileEntityMock);
|
|
|
|
|
|
|
|
/** @var MockObject|IEntity $extraEntity */
|
|
|
|
$extraEntity = $this->createMock(IEntity::class);
|
|
|
|
|
2019-12-22 17:04:39 +03:00
|
|
|
$this->legacyDispatcher->expects($this->once())
|
2019-08-09 14:24:48 +03:00
|
|
|
->method('dispatch')
|
|
|
|
->with('OCP\WorkflowEngine::registerEntities', $this->anything())
|
2020-04-09 14:53:40 +03:00
|
|
|
->willReturnCallback(function () use ($extraEntity) {
|
2019-08-09 14:24:48 +03:00
|
|
|
$this->manager->registerEntity($extraEntity);
|
|
|
|
});
|
|
|
|
|
|
|
|
$entities = $this->manager->getEntitiesList();
|
|
|
|
|
|
|
|
$this->assertCount(2, $entities);
|
|
|
|
|
|
|
|
$entityTypeCounts = array_reduce($entities, function (array $carry, IEntity $entity) {
|
2020-04-10 15:19:56 +03:00
|
|
|
if ($entity instanceof File) {
|
|
|
|
$carry[0]++;
|
|
|
|
} elseif ($entity instanceof IEntity) {
|
|
|
|
$carry[1]++;
|
|
|
|
}
|
2019-08-09 14:24:48 +03:00
|
|
|
return $carry;
|
|
|
|
}, [0, 0]);
|
|
|
|
|
|
|
|
$this->assertSame(1, $entityTypeCounts[0]);
|
|
|
|
$this->assertSame(1, $entityTypeCounts[1]);
|
|
|
|
}
|
2020-10-28 16:07:14 +03:00
|
|
|
|
|
|
|
public function testValidateOperationOK() {
|
|
|
|
$check = [
|
|
|
|
'class' => ICheck::class,
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => 'barfoo',
|
|
|
|
];
|
|
|
|
|
|
|
|
$operationMock = $this->createMock(IOperation::class);
|
|
|
|
$entityMock = $this->createMock(IEntity::class);
|
|
|
|
$eventEntityMock = $this->createMock(IEntityEvent::class);
|
|
|
|
$checkMock = $this->createMock(ICheck::class);
|
|
|
|
|
|
|
|
$operationMock->expects($this->once())
|
|
|
|
->method('validateOperation')
|
|
|
|
->with('test', [$check], 'operationData');
|
|
|
|
|
|
|
|
$entityMock->expects($this->any())
|
|
|
|
->method('getEvents')
|
|
|
|
->willReturn([$eventEntityMock]);
|
|
|
|
|
|
|
|
$eventEntityMock->expects($this->any())
|
|
|
|
->method('getEventName')
|
|
|
|
->willReturn('MyEvent');
|
|
|
|
|
|
|
|
$checkMock->expects($this->any())
|
|
|
|
->method('supportedEntities')
|
|
|
|
->willReturn([IEntity::class]);
|
|
|
|
$checkMock->expects($this->atLeastOnce())
|
|
|
|
->method('validateCheck');
|
|
|
|
|
|
|
|
$this->container->expects($this->any())
|
|
|
|
->method('query')
|
|
|
|
->willReturnCallback(function ($className) use ($operationMock, $entityMock, $eventEntityMock, $checkMock) {
|
|
|
|
switch ($className) {
|
|
|
|
case IOperation::class:
|
|
|
|
return $operationMock;
|
|
|
|
case IEntity::class:
|
|
|
|
return $entityMock;
|
|
|
|
case IEntityEvent::class:
|
|
|
|
return $eventEntityMock;
|
|
|
|
case ICheck::class:
|
|
|
|
return $checkMock;
|
|
|
|
default:
|
|
|
|
return $this->createMock($className);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->manager->validateOperation(IOperation::class, 'test', [$check], 'operationData', IEntity::class, ['MyEvent']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateOperationCheckInputLengthError() {
|
|
|
|
$check = [
|
|
|
|
'class' => ICheck::class,
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => str_pad('', IManager::MAX_CHECK_VALUE_BYTES + 1, 'FooBar'),
|
|
|
|
];
|
|
|
|
|
|
|
|
$operationMock = $this->createMock(IOperation::class);
|
|
|
|
$entityMock = $this->createMock(IEntity::class);
|
|
|
|
$eventEntityMock = $this->createMock(IEntityEvent::class);
|
|
|
|
$checkMock = $this->createMock(ICheck::class);
|
|
|
|
|
|
|
|
$operationMock->expects($this->once())
|
|
|
|
->method('validateOperation')
|
|
|
|
->with('test', [$check], 'operationData');
|
|
|
|
|
|
|
|
$entityMock->expects($this->any())
|
|
|
|
->method('getEvents')
|
|
|
|
->willReturn([$eventEntityMock]);
|
|
|
|
|
|
|
|
$eventEntityMock->expects($this->any())
|
|
|
|
->method('getEventName')
|
|
|
|
->willReturn('MyEvent');
|
|
|
|
|
|
|
|
$checkMock->expects($this->any())
|
|
|
|
->method('supportedEntities')
|
|
|
|
->willReturn([IEntity::class]);
|
|
|
|
$checkMock->expects($this->never())
|
|
|
|
->method('validateCheck');
|
|
|
|
|
|
|
|
$this->container->expects($this->any())
|
|
|
|
->method('query')
|
|
|
|
->willReturnCallback(function ($className) use ($operationMock, $entityMock, $eventEntityMock, $checkMock) {
|
|
|
|
switch ($className) {
|
|
|
|
case IOperation::class:
|
|
|
|
return $operationMock;
|
|
|
|
case IEntity::class:
|
|
|
|
return $entityMock;
|
|
|
|
case IEntityEvent::class:
|
|
|
|
return $eventEntityMock;
|
|
|
|
case ICheck::class:
|
|
|
|
return $checkMock;
|
|
|
|
default:
|
|
|
|
return $this->createMock($className);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->manager->validateOperation(IOperation::class, 'test', [$check], 'operationData', IEntity::class, ['MyEvent']);
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
$this->assertSame('The provided check value is too long', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testValidateOperationDataLengthError() {
|
|
|
|
$check = [
|
|
|
|
'class' => ICheck::class,
|
|
|
|
'operator' => 'is',
|
|
|
|
'value' => 'barfoo',
|
|
|
|
];
|
|
|
|
$operationData = str_pad('', IManager::MAX_OPERATION_VALUE_BYTES + 1, 'FooBar');
|
|
|
|
|
|
|
|
$operationMock = $this->createMock(IOperation::class);
|
|
|
|
$entityMock = $this->createMock(IEntity::class);
|
|
|
|
$eventEntityMock = $this->createMock(IEntityEvent::class);
|
|
|
|
$checkMock = $this->createMock(ICheck::class);
|
|
|
|
|
|
|
|
$operationMock->expects($this->never())
|
|
|
|
->method('validateOperation');
|
|
|
|
|
|
|
|
$entityMock->expects($this->any())
|
|
|
|
->method('getEvents')
|
|
|
|
->willReturn([$eventEntityMock]);
|
|
|
|
|
|
|
|
$eventEntityMock->expects($this->any())
|
|
|
|
->method('getEventName')
|
|
|
|
->willReturn('MyEvent');
|
|
|
|
|
|
|
|
$checkMock->expects($this->any())
|
|
|
|
->method('supportedEntities')
|
|
|
|
->willReturn([IEntity::class]);
|
|
|
|
$checkMock->expects($this->never())
|
|
|
|
->method('validateCheck');
|
|
|
|
|
|
|
|
$this->container->expects($this->any())
|
|
|
|
->method('query')
|
|
|
|
->willReturnCallback(function ($className) use ($operationMock, $entityMock, $eventEntityMock, $checkMock) {
|
|
|
|
switch ($className) {
|
|
|
|
case IOperation::class:
|
|
|
|
return $operationMock;
|
|
|
|
case IEntity::class:
|
|
|
|
return $entityMock;
|
|
|
|
case IEntityEvent::class:
|
|
|
|
return $eventEntityMock;
|
|
|
|
case ICheck::class:
|
|
|
|
return $checkMock;
|
|
|
|
default:
|
|
|
|
return $this->createMock($className);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
|
|
|
$this->manager->validateOperation(IOperation::class, 'test', [$check], $operationData, IEntity::class, ['MyEvent']);
|
|
|
|
} catch (\UnexpectedValueException $e) {
|
|
|
|
$this->assertSame('The provided operation data is too long', $e->getMessage());
|
|
|
|
}
|
|
|
|
}
|
2016-10-17 16:20:13 +03:00
|
|
|
}
|