-
-
Notifications
You must be signed in to change notification settings - Fork 4.9k
test(workflowengine): add tests for RuleMatcher #60502
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
salmart-dev
wants to merge
1
commit into
master
Choose a base branch
from
fix/add-rulematcher-test
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+336
−0
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\WorkflowEngine\Tests\Service; | ||
|
|
||
| use OCP\Files\Storage\IStorage; | ||
| use OCP\WorkflowEngine\ICheck; | ||
| use OCP\WorkflowEngine\IEntity; | ||
| use OCP\WorkflowEngine\IFileCheck; | ||
|
|
||
| /** | ||
| * Concrete stub that satisfies both IFileCheck and ICheck, since IFileCheck does not extend ICheck | ||
| * but all real implementations are expected to implement both. | ||
| */ | ||
| class FileCheckStub implements IFileCheck, ICheck { | ||
| public function setFileInfo(IStorage $storage, string $path, bool $isDir = false): void { | ||
| } | ||
|
|
||
| public function setEntitySubject(IEntity $entity, $subject): void { | ||
| } | ||
|
|
||
| public function executeCheck($operator, $value): bool { | ||
| return true; | ||
| } | ||
|
|
||
| public function validateCheck($operator, $value): void { | ||
| } | ||
|
|
||
| public function supportedEntities(): array { | ||
| return []; | ||
| } | ||
|
|
||
| public function isAvailableForScope(int $scope): bool { | ||
| return true; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,295 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
| namespace OCA\WorkflowEngine\Tests\Service; | ||
|
|
||
| use LogicException; | ||
| use NCU\WorkflowEngine\RuntimeOperation; | ||
| use OCA\WorkflowEngine\Manager; | ||
| use OCA\WorkflowEngine\Service\Logger; | ||
| use OCA\WorkflowEngine\Service\RuleMatcher; | ||
| use OCP\Files\Storage\IStorage; | ||
| use OCP\IL10N; | ||
| use OCP\IUserSession; | ||
| use OCP\WorkflowEngine\ICheck; | ||
| use OCP\WorkflowEngine\IEntity; | ||
| use OCP\WorkflowEngine\IFileCheck; | ||
| use OCP\WorkflowEngine\IOperation; | ||
| use PHPUnit\Framework\MockObject\MockObject; | ||
| use Psr\Container\ContainerInterface; | ||
| use RuntimeException; | ||
| use Test\TestCase; | ||
| use UnexpectedValueException; | ||
|
|
||
| class RuleMatcherTest extends TestCase { | ||
| private IUserSession&MockObject $session; | ||
| private ContainerInterface&MockObject $container; | ||
| private IL10N&MockObject $l; | ||
| private Manager&MockObject $manager; | ||
| private Logger&MockObject $logger; | ||
| private RuleMatcher $ruleMatcher; | ||
| private IOperation&MockObject $operation; | ||
| private IEntity&MockObject $entity; | ||
|
|
||
| protected function setUp(): void { | ||
| parent::setUp(); | ||
|
|
||
| $this->session = $this->createMock(IUserSession::class); | ||
| $this->session->method('getUser')->willReturn(null); | ||
|
|
||
| $this->container = $this->createMock(ContainerInterface::class); | ||
| $this->l = $this->createMock(IL10N::class); | ||
| $this->l->method('t')->willReturnArgument(0); | ||
|
|
||
| $this->manager = $this->createMock(Manager::class); | ||
| $this->manager->method('isUserScopeEnabled')->willReturn(false); | ||
| $this->manager->method('getAllConfiguredScopesForOperation')->willReturn([]); | ||
| $this->manager->method('getAllConfiguredScopesForRuntimeOperation')->willReturn([]); | ||
|
|
||
| $this->logger = $this->createMock(Logger::class); | ||
|
|
||
| $this->operation = $this->createMock(IOperation::class); | ||
| $this->entity = $this->createMock(IEntity::class); | ||
|
|
||
| $this->ruleMatcher = new RuleMatcher( | ||
| $this->session, | ||
| $this->container, | ||
| $this->l, | ||
| $this->manager, | ||
| $this->logger, | ||
| ); | ||
| } | ||
|
|
||
| public function testSetOperationThrowsIfCalledTwice(): void { | ||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->expectException(RuntimeException::class); | ||
| $this->ruleMatcher->setOperation($this->operation); | ||
| } | ||
|
|
||
| public function testSetEntityThrowsIfCalledTwice(): void { | ||
| $this->ruleMatcher->setEntity($this->entity); | ||
| $this->expectException(RuntimeException::class); | ||
| $this->ruleMatcher->setEntity($this->entity); | ||
| } | ||
|
|
||
| public function testSetEventNameThrowsIfCalledTwice(): void { | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
| $this->expectException(RuntimeException::class); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
| } | ||
|
|
||
| public function testGetEntityThrowsIfNotSet(): void { | ||
| $this->expectException(LogicException::class); | ||
| $this->ruleMatcher->getEntity(); | ||
| } | ||
|
|
||
| public function testGetFlowsThrowsIfOperationNotSet(): void { | ||
| $this->expectException(RuntimeException::class); | ||
| $this->ruleMatcher->getFlows(); | ||
| } | ||
|
|
||
| private function buildDbOperation(string $name = 'DbOp', array $events = ['MyEvent']): array { | ||
| return [ | ||
| 'id' => 1, | ||
| 'class' => get_class($this->operation), | ||
| 'name' => $name, | ||
| 'checks' => '[]', | ||
| 'operation' => '', | ||
| 'entity' => get_class($this->entity), | ||
| 'events' => json_encode($events), | ||
| ]; | ||
| } | ||
|
|
||
| private function buildRuntimeOperation(string $name = 'RuntimeOp', array $events = ['MyEvent']): RuntimeOperation { | ||
| return new RuntimeOperation( | ||
| id: 'runtime-op-1', | ||
| class: get_class($this->operation), | ||
| name: $name, | ||
| checks: [], | ||
| operation: '', | ||
| entity: get_class($this->entity), | ||
| events: $events, | ||
| appId: 'testapp', | ||
| ); | ||
| } | ||
|
|
||
| public function testGetFlowsReturnsMatchingDbOperation(): void { | ||
| $this->manager->method('getOperations')->willReturn([$this->buildDbOperation()]); | ||
| $this->manager->method('getRuntimeOperations')->willReturn([]); | ||
| $this->manager->method('getChecks')->willReturn([]); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $result = $this->ruleMatcher->getFlows(true); | ||
|
|
||
| $this->assertIsArray($result); | ||
| $this->assertSame('DbOp', $result['name']); | ||
| } | ||
|
|
||
| public function testGetFlowsSkipsDbOperationWithNonMatchingEvent(): void { | ||
| $this->manager->method('getOperations')->willReturn([$this->buildDbOperation('DbOp', ['OtherEvent'])]); | ||
| $this->manager->method('getRuntimeOperations')->willReturn([]); | ||
| $this->manager->method('getChecks')->willReturn([]); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $this->assertSame([], $this->ruleMatcher->getFlows(false)); | ||
| } | ||
|
|
||
| public function testGetFlowsReturnsMatchingRuntimeOperation(): void { | ||
| $this->manager->method('getOperations')->willReturn([]); | ||
| $this->manager->method('getRuntimeOperations')->willReturn([$this->buildRuntimeOperation()]); | ||
| $this->manager->method('getRuntimeChecks')->willReturn([]); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $result = $this->ruleMatcher->getFlows(true); | ||
|
|
||
| $this->assertIsArray($result); | ||
| $this->assertSame('RuntimeOp', $result['name']); | ||
| $this->assertTrue($result['runtime']); | ||
| } | ||
|
|
||
| public function testGetFlowsSkipsRuntimeOperationWithNonMatchingEvent(): void { | ||
| $this->manager->method('getOperations')->willReturn([]); | ||
| $this->manager->method('getRuntimeOperations')->willReturn([$this->buildRuntimeOperation('RuntimeOp', ['OtherEvent'])]); | ||
| $this->manager->method('getRuntimeChecks')->willReturn([]); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $this->assertSame([], $this->ruleMatcher->getFlows(false)); | ||
| } | ||
|
|
||
| public function testGetFlowsMixedOperationsWithEventFilter(): void { | ||
| $this->manager->method('getOperations') | ||
| ->willReturn([$this->buildDbOperation('DbOp', ['MyEvent'])]); | ||
| $this->manager->method('getRuntimeOperations') | ||
| ->willReturn([$this->buildRuntimeOperation('RuntimeOp', ['OtherEvent'])]); | ||
| $this->manager->method('getChecks')->willReturn([]); | ||
| $this->manager->method('getRuntimeChecks')->willReturn([]); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $results = $this->ruleMatcher->getFlows(false); | ||
|
|
||
| $this->assertCount(1, $results); | ||
| $this->assertSame('DbOp', $results[0]['name']); | ||
| } | ||
|
|
||
| public function testGetFlowsReturnAllMatches(): void { | ||
| $this->manager->method('getOperations') | ||
| ->willReturn([$this->buildDbOperation('DbOp')]); | ||
| $this->manager->method('getRuntimeOperations') | ||
| ->willReturn([$this->buildRuntimeOperation('RuntimeOp')]); | ||
| $this->manager->method('getChecks')->willReturn([]); | ||
| $this->manager->method('getRuntimeChecks')->willReturn([]); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $results = $this->ruleMatcher->getFlows(false); | ||
|
|
||
| $this->assertCount(2, $results); | ||
| $names = array_column($results, 'name'); | ||
| $this->assertContains('DbOp', $names); | ||
| $this->assertContains('RuntimeOp', $names); | ||
| } | ||
|
|
||
| public function testGetFlowsReturnFirstMatchOnly(): void { | ||
| $this->manager->method('getOperations') | ||
| ->willReturn([$this->buildDbOperation('DbOp'), $this->buildDbOperation('DbOp2')]); | ||
| $this->manager->method('getRuntimeOperations')->willReturn([]); | ||
| $this->manager->method('getChecks')->willReturn([]); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $result = $this->ruleMatcher->getFlows(true); | ||
|
|
||
| $this->assertIsArray($result); | ||
| $this->assertArrayHasKey('name', $result); | ||
| $this->assertSame('DbOp', $result['name']); | ||
| } | ||
|
|
||
| public function testGetFlowsSkipsOperationWhenCheckFails(): void { | ||
| $checkData = ['class' => ICheck::class, 'operator' => 'is', 'value' => 'x']; | ||
| $this->manager->method('getOperations')->willReturn([$this->buildDbOperation()]); | ||
| $this->manager->method('getRuntimeOperations')->willReturn([]); | ||
| $this->manager->method('getChecks')->willReturn([$checkData]); | ||
|
|
||
| $checkInstance = $this->createMock(ICheck::class); | ||
| $checkInstance->method('executeCheck')->willReturn(false); | ||
| $this->container->method('get')->willReturn($checkInstance); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $this->assertSame([], $this->ruleMatcher->getFlows(false)); | ||
| } | ||
|
|
||
| public function testGetFlowsIncludesOperationWhenCheckPasses(): void { | ||
| $checkData = ['class' => ICheck::class, 'operator' => 'is', 'value' => 'x']; | ||
| $this->manager->method('getOperations')->willReturn([$this->buildDbOperation()]); | ||
| $this->manager->method('getRuntimeOperations')->willReturn([]); | ||
| $this->manager->method('getChecks')->willReturn([$checkData]); | ||
|
|
||
| $checkInstance = $this->createMock(ICheck::class); | ||
| $checkInstance->method('executeCheck')->willReturn(true); | ||
| $this->container->method('get')->willReturn($checkInstance); | ||
|
|
||
| $this->ruleMatcher->setOperation($this->operation); | ||
| $this->ruleMatcher->setEventName('MyEvent'); | ||
|
|
||
| $results = $this->ruleMatcher->getFlows(false); | ||
| $this->assertCount(1, $results); | ||
| } | ||
|
|
||
| public function testCheckWithPlainICheck(): void { | ||
| $checkInstance = $this->createMock(ICheck::class); | ||
| $checkInstance->expects($this->once()) | ||
| ->method('executeCheck') | ||
| ->with('is', 'foo') | ||
| ->willReturn(true); | ||
| $this->container->method('get')->willReturn($checkInstance); | ||
|
|
||
| $this->assertTrue($this->ruleMatcher->check(['class' => ICheck::class, 'operator' => 'is', 'value' => 'foo'])); | ||
| } | ||
|
|
||
| public function testCheckThrowsForInvalidCheckClass(): void { | ||
| $this->container->method('get')->willReturn(new \stdClass()); | ||
| $this->expectException(UnexpectedValueException::class); | ||
| $this->ruleMatcher->check(['class' => \stdClass::class, 'operator' => 'is', 'value' => 'x']); | ||
| } | ||
|
|
||
| public function testCheckWithFileCheckThrowsWithoutFileInfo(): void { | ||
| $checkInstance = $this->createMock(IFileCheck::class); | ||
| $this->container->method('get')->willReturn($checkInstance); | ||
|
|
||
| $this->expectException(RuntimeException::class); | ||
|
Comment on lines
+270
to
+278
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same here. |
||
| $this->ruleMatcher->check(['class' => IFileCheck::class, 'operator' => 'is', 'value' => 'x']); | ||
| } | ||
|
|
||
| public function testCheckWithFileCheckPassesFileInfo(): void { | ||
| $storage = $this->createMock(IStorage::class); | ||
| $this->ruleMatcher->setFileInfo($storage, '/foo/bar.txt'); | ||
|
|
||
| $checkInstance = $this->createMock(FileCheckStub::class); | ||
| $checkInstance->expects($this->once()) | ||
| ->method('setFileInfo') | ||
| ->with($storage, '/foo/bar.txt', false); | ||
| $checkInstance->method('executeCheck')->willReturn(true); | ||
| $this->container->method('get')->willReturn($checkInstance); | ||
|
|
||
| $this->assertTrue($this->ruleMatcher->check(['class' => IFileCheck::class, 'operator' => 'is', 'value' => 'x'])); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
IMO you should check for the error message itself. Otherwise something else could throw the exception and you wouldn't notice it in the tests.