ManagerTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Joas Schilling <coding@schilljs.com>
  4. *
  5. * @license GNU AGPL version 3 or any later version
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU Affero General Public License as
  9. * published by the Free Software Foundation, either version 3 of the
  10. * License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU Affero General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU Affero General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. namespace OCA\WorkflowEngine\Tests;
  22. use OC\L10N\L10N;
  23. use OCA\WorkflowEngine\Entity\File;
  24. use OCA\WorkflowEngine\Helper\ScopeContext;
  25. use OCA\WorkflowEngine\Manager;
  26. use OCP\Files\IRootFolder;
  27. use OCP\IDBConnection;
  28. use OCP\IL10N;
  29. use OCP\ILogger;
  30. use OCP\IServerContainer;
  31. use OCP\IURLGenerator;
  32. use OCP\IUserSession;
  33. use OCP\WorkflowEngine\ICheck;
  34. use OCP\WorkflowEngine\IEntity;
  35. use OCP\WorkflowEngine\IManager;
  36. use OCP\WorkflowEngine\IOperation;
  37. use PHPUnit\Framework\MockObject\MockObject;
  38. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  39. use Test\TestCase;
  40. /**
  41. * Class ManagerTest
  42. *
  43. * @package OCA\WorkflowEngine\Tests
  44. * @group DB
  45. */
  46. class ManagerTest extends TestCase {
  47. /** @var Manager */
  48. protected $manager;
  49. /** @var MockObject|IDBConnection */
  50. protected $db;
  51. /** @var \PHPUnit\Framework\MockObject\MockObject|ILogger */
  52. protected $logger;
  53. /** @var \PHPUnit\Framework\MockObject\MockObject|EventDispatcherInterface */
  54. protected $eventDispatcher;
  55. /** @var MockObject|IServerContainer */
  56. protected $container;
  57. /** @var MockObject|IUserSession */
  58. protected $session;
  59. /** @var MockObject|L10N */
  60. protected $l;
  61. protected function setUp() {
  62. parent::setUp();
  63. $this->db = \OC::$server->getDatabaseConnection();
  64. $this->container = $this->createMock(IServerContainer::class);
  65. /** @var IL10N|MockObject $l */
  66. $this->l = $this->createMock(IL10N::class);
  67. $this->l->method('t')
  68. ->will($this->returnCallback(function($text, $parameters = []) {
  69. return vsprintf($text, $parameters);
  70. }));
  71. $this->eventDispatcher = $this->createMock(EventDispatcherInterface::class);
  72. $this->logger = $this->createMock(ILogger::class);
  73. $this->session = $this->createMock(IUserSession::class);
  74. $this->manager = new Manager(
  75. \OC::$server->getDatabaseConnection(),
  76. $this->container,
  77. $this->l,
  78. $this->eventDispatcher,
  79. $this->logger,
  80. $this->session
  81. );
  82. $this->clearTables();
  83. }
  84. protected function tearDown() {
  85. $this->clearTables();
  86. parent::tearDown();
  87. }
  88. /**
  89. * @return MockObject|ScopeContext
  90. */
  91. protected function buildScope(string $scopeId = null): MockObject {
  92. $scopeContext = $this->createMock(ScopeContext::class);
  93. $scopeContext->expects($this->any())
  94. ->method('getScope')
  95. ->willReturn($scopeId ? IManager::SCOPE_USER : IManager::SCOPE_ADMIN);
  96. $scopeContext->expects($this->any())
  97. ->method('getScopeId')
  98. ->willReturn($scopeId ?? '');
  99. $scopeContext->expects($this->any())
  100. ->method('getHash')
  101. ->willReturn(md5($scopeId ?? ''));
  102. return $scopeContext;
  103. }
  104. public function clearTables() {
  105. $query = $this->db->getQueryBuilder();
  106. foreach(['flow_checks', 'flow_operations', 'flow_operations_scope'] as $table) {
  107. $query->delete($table)
  108. ->execute();
  109. }
  110. }
  111. public function testChecks() {
  112. $check1 = $this->invokePrivate($this->manager, 'addCheck', ['Test', 'equal', 1]);
  113. $check2 = $this->invokePrivate($this->manager, 'addCheck', ['Test', '!equal', 2]);
  114. $data = $this->manager->getChecks([$check1]);
  115. $this->assertArrayHasKey($check1, $data);
  116. $this->assertArrayNotHasKey($check2, $data);
  117. $data = $this->manager->getChecks([$check1, $check2]);
  118. $this->assertArrayHasKey($check1, $data);
  119. $this->assertArrayHasKey($check2, $data);
  120. $data = $this->manager->getChecks([$check2, $check1]);
  121. $this->assertArrayHasKey($check1, $data);
  122. $this->assertArrayHasKey($check2, $data);
  123. $data = $this->manager->getChecks([$check2]);
  124. $this->assertArrayNotHasKey($check1, $data);
  125. $this->assertArrayHasKey($check2, $data);
  126. }
  127. public function testScope() {
  128. $adminScope = $this->buildScope();
  129. $userScope = $this->buildScope('jackie');
  130. $entity = File::class;
  131. $opId1 = $this->invokePrivate(
  132. $this->manager,
  133. 'insertOperation',
  134. ['OCA\WFE\TestOp', 'Test01', [11, 22], 'foo', $entity, []]
  135. );
  136. $this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
  137. $opId2 = $this->invokePrivate(
  138. $this->manager,
  139. 'insertOperation',
  140. ['OCA\WFE\TestOp', 'Test02', [33, 22], 'bar', $entity, []]
  141. );
  142. $this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
  143. $opId3 = $this->invokePrivate(
  144. $this->manager,
  145. 'insertOperation',
  146. ['OCA\WFE\TestOp', 'Test03', [11, 44], 'foobar', $entity, []]
  147. );
  148. $this->invokePrivate($this->manager, 'addScope', [$opId3, $userScope]);
  149. $this->assertTrue($this->invokePrivate($this->manager, 'canModify', [$opId1, $adminScope]));
  150. $this->assertFalse($this->invokePrivate($this->manager, 'canModify', [$opId2, $adminScope]));
  151. $this->assertFalse($this->invokePrivate($this->manager, 'canModify', [$opId3, $adminScope]));
  152. $this->assertFalse($this->invokePrivate($this->manager, 'canModify', [$opId1, $userScope]));
  153. $this->assertTrue($this->invokePrivate($this->manager, 'canModify', [$opId2, $userScope]));
  154. $this->assertTrue($this->invokePrivate($this->manager, 'canModify', [$opId3, $userScope]));
  155. }
  156. public function testGetAllOperations() {
  157. $adminScope = $this->buildScope();
  158. $userScope = $this->buildScope('jackie');
  159. $entity = File::class;
  160. $opId1 = $this->invokePrivate(
  161. $this->manager,
  162. 'insertOperation',
  163. ['OCA\WFE\TestAdminOp', 'Test01', [11, 22], 'foo', $entity, []]
  164. );
  165. $this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
  166. $opId2 = $this->invokePrivate(
  167. $this->manager,
  168. 'insertOperation',
  169. ['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
  170. );
  171. $this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
  172. $opId3 = $this->invokePrivate(
  173. $this->manager,
  174. 'insertOperation',
  175. ['OCA\WFE\TestUserOp', 'Test03', [11, 44], 'foobar', $entity, []]
  176. );
  177. $this->invokePrivate($this->manager, 'addScope', [$opId3, $userScope]);
  178. $adminOps = $this->manager->getAllOperations($adminScope);
  179. $userOps = $this->manager->getAllOperations($userScope);
  180. $this->assertSame(1, count($adminOps));
  181. $this->assertTrue(array_key_exists('OCA\WFE\TestAdminOp', $adminOps));
  182. $this->assertFalse(array_key_exists('OCA\WFE\TestUserOp', $adminOps));
  183. $this->assertSame(1, count($userOps));
  184. $this->assertFalse(array_key_exists('OCA\WFE\TestAdminOp', $userOps));
  185. $this->assertTrue(array_key_exists('OCA\WFE\TestUserOp', $userOps));
  186. $this->assertSame(2, count($userOps['OCA\WFE\TestUserOp']));
  187. }
  188. public function testGetOperations() {
  189. $adminScope = $this->buildScope();
  190. $userScope = $this->buildScope('jackie');
  191. $entity = File::class;
  192. $opId1 = $this->invokePrivate(
  193. $this->manager,
  194. 'insertOperation',
  195. ['OCA\WFE\TestOp', 'Test01', [11, 22], 'foo', $entity, []]
  196. );
  197. $this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
  198. $opId4 = $this->invokePrivate(
  199. $this->manager,
  200. 'insertOperation',
  201. ['OCA\WFE\OtherTestOp', 'Test04', [5], 'foo', $entity, []]
  202. );
  203. $this->invokePrivate($this->manager, 'addScope', [$opId4, $adminScope]);
  204. $opId2 = $this->invokePrivate(
  205. $this->manager,
  206. 'insertOperation',
  207. ['OCA\WFE\TestOp', 'Test02', [33, 22], 'bar', $entity, []]
  208. );
  209. $this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
  210. $opId3 = $this->invokePrivate(
  211. $this->manager,
  212. 'insertOperation',
  213. ['OCA\WFE\TestOp', 'Test03', [11, 44], 'foobar', $entity, []]
  214. );
  215. $this->invokePrivate($this->manager, 'addScope', [$opId3, $userScope]);
  216. $opId5 = $this->invokePrivate(
  217. $this->manager,
  218. 'insertOperation',
  219. ['OCA\WFE\OtherTestOp', 'Test05', [5], 'foobar', $entity, []]
  220. );
  221. $this->invokePrivate($this->manager, 'addScope', [$opId5, $userScope]);
  222. $adminOps = $this->manager->getOperations('OCA\WFE\TestOp', $adminScope);
  223. $userOps = $this->manager->getOperations('OCA\WFE\TestOp', $userScope);
  224. $this->assertSame(1, count($adminOps));
  225. array_walk($adminOps, function ($op) {
  226. $this->assertTrue($op['class'] === 'OCA\WFE\TestOp');
  227. });
  228. $this->assertSame(2, count($userOps));
  229. array_walk($userOps, function ($op) {
  230. $this->assertTrue($op['class'] === 'OCA\WFE\TestOp');
  231. });
  232. }
  233. public function testUpdateOperation() {
  234. $adminScope = $this->buildScope();
  235. $userScope = $this->buildScope('jackie');
  236. $entity = File::class;
  237. $this->container->expects($this->any())
  238. ->method('query')
  239. ->willReturnCallback(function ($class) {
  240. if(substr($class, -2) === 'Op') {
  241. return $this->createMock(IOperation::class);
  242. } else if($class === File::class) {
  243. return $this->getMockBuilder(File::class)
  244. ->setConstructorArgs([$this->l, $this->createMock(IURLGenerator::class), $this->createMock(IRootFolder::class)])
  245. ->setMethodsExcept(['getEvents'])
  246. ->getMock();
  247. }
  248. return $this->createMock(ICheck::class);
  249. });
  250. $opId1 = $this->invokePrivate(
  251. $this->manager,
  252. 'insertOperation',
  253. ['OCA\WFE\TestAdminOp', 'Test01', [11, 22], 'foo', $entity, []]
  254. );
  255. $this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
  256. $opId2 = $this->invokePrivate(
  257. $this->manager,
  258. 'insertOperation',
  259. ['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
  260. );
  261. $this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
  262. $check1 = ['class' => 'OCA\WFE\C22', 'operator' => 'eq', 'value' => 'asdf'];
  263. $check2 = ['class' => 'OCA\WFE\C33', 'operator' => 'eq', 'value' => 23456];
  264. /** @noinspection PhpUnhandledExceptionInspection */
  265. $op = $this->manager->updateOperation($opId1, 'Test01a', [$check1, $check2], 'foohur', $adminScope, $entity, ['\OCP\Files::postDelete']);
  266. $this->assertSame('Test01a', $op['name']);
  267. $this->assertSame('foohur', $op['operation']);
  268. /** @noinspection PhpUnhandledExceptionInspection */
  269. $op = $this->manager->updateOperation($opId2, 'Test02a', [$check1], 'barfoo', $userScope, $entity, ['\OCP\Files::postDelete']);
  270. $this->assertSame('Test02a', $op['name']);
  271. $this->assertSame('barfoo', $op['operation']);
  272. foreach([[$adminScope, $opId2], [$userScope, $opId1]] as $run) {
  273. try {
  274. /** @noinspection PhpUnhandledExceptionInspection */
  275. $this->manager->updateOperation($run[1], 'Evil', [$check2], 'hackx0r', $run[0], $entity, []);
  276. $this->assertTrue(false, 'DomainException not thrown');
  277. } catch (\DomainException $e) {
  278. $this->assertTrue(true);
  279. }
  280. }
  281. }
  282. public function testDeleteOperation() {
  283. $adminScope = $this->buildScope();
  284. $userScope = $this->buildScope('jackie');
  285. $entity = File::class;
  286. $opId1 = $this->invokePrivate(
  287. $this->manager,
  288. 'insertOperation',
  289. ['OCA\WFE\TestAdminOp', 'Test01', [11, 22], 'foo', $entity, []]
  290. );
  291. $this->invokePrivate($this->manager, 'addScope', [$opId1, $adminScope]);
  292. $opId2 = $this->invokePrivate(
  293. $this->manager,
  294. 'insertOperation',
  295. ['OCA\WFE\TestUserOp', 'Test02', [33, 22], 'bar', $entity, []]
  296. );
  297. $this->invokePrivate($this->manager, 'addScope', [$opId2, $userScope]);
  298. foreach([[$adminScope, $opId2], [$userScope, $opId1]] as $run) {
  299. try {
  300. /** @noinspection PhpUnhandledExceptionInspection */
  301. $this->manager->deleteOperation($run[1], $run[0]);
  302. $this->assertTrue(false, 'DomainException not thrown');
  303. } catch (\Exception $e) {
  304. $this->assertInstanceOf(\DomainException::class, $e);
  305. }
  306. }
  307. /** @noinspection PhpUnhandledExceptionInspection */
  308. $this->manager->deleteOperation($opId1, $adminScope);
  309. /** @noinspection PhpUnhandledExceptionInspection */
  310. $this->manager->deleteOperation($opId2, $userScope);
  311. foreach([$opId1, $opId2] as $opId) {
  312. try {
  313. $this->invokePrivate($this->manager, 'getOperation', [$opId]);
  314. $this->assertTrue(false, 'UnexpectedValueException not thrown');
  315. } catch(\Exception $e) {
  316. $this->assertInstanceOf(\UnexpectedValueException::class, $e);
  317. }
  318. }
  319. }
  320. public function testGetEntitiesListBuildInOnly() {
  321. $fileEntityMock = $this->createMock(File::class);
  322. $this->container->expects($this->once())
  323. ->method('query')
  324. ->with(File::class)
  325. ->willReturn($fileEntityMock);
  326. $entities = $this->manager->getEntitiesList();
  327. $this->assertCount(1, $entities);
  328. $this->assertInstanceOf(IEntity::class, $entities[0]);
  329. }
  330. public function testGetEntitiesList() {
  331. $fileEntityMock = $this->createMock(File::class);
  332. $this->container->expects($this->once())
  333. ->method('query')
  334. ->with(File::class)
  335. ->willReturn($fileEntityMock);
  336. /** @var MockObject|IEntity $extraEntity */
  337. $extraEntity = $this->createMock(IEntity::class);
  338. $this->eventDispatcher->expects($this->once())
  339. ->method('dispatch')
  340. ->with('OCP\WorkflowEngine::registerEntities', $this->anything())
  341. ->willReturnCallback(function() use ($extraEntity) {
  342. $this->manager->registerEntity($extraEntity);
  343. });
  344. $entities = $this->manager->getEntitiesList();
  345. $this->assertCount(2, $entities);
  346. $entityTypeCounts = array_reduce($entities, function (array $carry, IEntity $entity) {
  347. if($entity instanceof File) $carry[0]++;
  348. else if($entity instanceof IEntity) $carry[1]++;
  349. return $carry;
  350. }, [0, 0]);
  351. $this->assertSame(1, $entityTypeCounts[0]);
  352. $this->assertSame(1, $entityTypeCounts[1]);
  353. }
  354. }