diff options
author | Marcel Klehr <mklehr@gmx.net> | 2024-07-19 12:38:30 +0200 |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-07-19 12:38:30 +0200 |
commit | a3c3eab09c50bc7dd16e95e8689f3a03b1d2ac98 (patch) | |
tree | 4b832bf2b18e9468c0781d5f912626655b24cdaf /tests | |
parent | 64ca4b832dc6b7a574fd3301b1c0e022b192ea5d (diff) | |
parent | ff99df07e7212a3c8bfdcc3cf9a571353c6f84ff (diff) | |
download | nextcloud-server-a3c3eab09c50bc7dd16e95e8689f3a03b1d2ac98.tar.gz nextcloud-server-a3c3eab09c50bc7dd16e95e8689f3a03b1d2ac98.zip |
Merge pull request #46368 from nextcloud/fix/task-processing
TaskProcessing follow-up
Diffstat (limited to 'tests')
-rw-r--r-- | tests/lib/TaskProcessing/TaskProcessingTest.php | 83 |
1 files changed, 73 insertions, 10 deletions
diff --git a/tests/lib/TaskProcessing/TaskProcessingTest.php b/tests/lib/TaskProcessing/TaskProcessingTest.php index a1857fad1b3..27f46bed17c 100644 --- a/tests/lib/TaskProcessing/TaskProcessingTest.php +++ b/tests/lib/TaskProcessing/TaskProcessingTest.php @@ -16,11 +16,13 @@ use OCP\AppFramework\Utility\ITimeFactory; use OCP\BackgroundJob\IJobList; use OCP\EventDispatcher\IEventDispatcher; use OCP\Files\AppData\IAppDataFactory; -use OCP\Files\IAppData; +use OCP\Files\Config\ICachedMountInfo; +use OCP\Files\Config\IUserMountCache; use OCP\Files\IRootFolder; use OCP\IConfig; use OCP\IDBConnection; use OCP\IServerContainer; +use OCP\IUser; use OCP\IUserManager; use OCP\SpeechToText\ISpeechToTextManager; use OCP\TaskProcessing\EShapeType; @@ -295,8 +297,7 @@ class TaskProcessingTest extends \Test\TestCase { private RegistrationContext $registrationContext; private TaskMapper $taskMapper; private IJobList $jobList; - private IAppData $appData; - private \OCP\Share\IManager $shareManager; + private IUserMountCache $userMountCache; private IRootFolder $rootFolder; public const TEST_USER = 'testuser'; @@ -370,7 +371,7 @@ class TaskProcessingTest extends \Test\TestCase { \OC::$server->get(IAppDataFactory::class), ); - $this->shareManager = $this->createMock(\OCP\Share\IManager::class); + $this->userMountCache = $this->createMock(IUserMountCache::class); $this->manager = new Manager( \OC::$server->get(IConfig::class), @@ -385,7 +386,7 @@ class TaskProcessingTest extends \Test\TestCase { $textProcessingManager, $text2imageManager, \OC::$server->get(ISpeechToTextManager::class), - $this->shareManager, + $this->userMountCache, ); } @@ -416,17 +417,21 @@ class TaskProcessingTest extends \Test\TestCase { } public function testProviderShouldBeRegisteredAndTaskWithFilesFailValidation() { - $this->shareManager->expects($this->any())->method('getAccessList')->willReturn(['users' => []]); $this->registrationContext->expects($this->any())->method('getTaskProcessingTaskTypes')->willReturn([ new ServiceRegistration('test', AudioToImage::class) ]); $this->registrationContext->expects($this->any())->method('getTaskProcessingProviders')->willReturn([ new ServiceRegistration('test', AsyncProvider::class) ]); - $this->shareManager->expects($this->any())->method('getAccessList')->willReturn(['users' => [null]]); - self::assertCount(1, $this->manager->getAvailableTaskTypes()); + $user = $this->createMock(IUser::class); + $user->expects($this->any())->method('getUID')->willReturn(null); + $mount = $this->createMock(ICachedMountInfo::class); + $mount->expects($this->any())->method('getUser')->willReturn($user); + $this->userMountCache->expects($this->any())->method('getMountsForFileId')->willReturn([$mount]); + self::assertCount(1, $this->manager->getAvailableTaskTypes()); self::assertTrue($this->manager->hasProviders()); + $audioId = $this->getFile('audioInput', 'Hello')->getId(); $task = new Task(AudioToImage::ID, ['audio' => $audioId], 'test', null); self::assertNull($task->getId()); @@ -537,14 +542,20 @@ class TaskProcessingTest extends \Test\TestCase { self::assertEquals(1, $task->getProgress()); } - public function testAsyncProviderWithFilesShouldBeRegisteredAndRun() { + public function testAsyncProviderWithFilesShouldBeRegisteredAndRunReturningRawFileData() { $this->registrationContext->expects($this->any())->method('getTaskProcessingTaskTypes')->willReturn([ new ServiceRegistration('test', AudioToImage::class) ]); $this->registrationContext->expects($this->any())->method('getTaskProcessingProviders')->willReturn([ new ServiceRegistration('test', AsyncProvider::class) ]); - $this->shareManager->expects($this->any())->method('getAccessList')->willReturn(['users' => ['testuser' => 1]]); + + $user = $this->createMock(IUser::class); + $user->expects($this->any())->method('getUID')->willReturn('testuser'); + $mount = $this->createMock(ICachedMountInfo::class); + $mount->expects($this->any())->method('getUser')->willReturn($user); + $this->userMountCache->expects($this->any())->method('getMountsForFileId')->willReturn([$mount]); + self::assertCount(1, $this->manager->getAvailableTaskTypes()); self::assertTrue($this->manager->hasProviders()); @@ -583,6 +594,58 @@ class TaskProcessingTest extends \Test\TestCase { self::assertEquals('World', $node->getContent()); } + public function testAsyncProviderWithFilesShouldBeRegisteredAndRunReturningFileIds() { + $this->registrationContext->expects($this->any())->method('getTaskProcessingTaskTypes')->willReturn([ + new ServiceRegistration('test', AudioToImage::class) + ]); + $this->registrationContext->expects($this->any())->method('getTaskProcessingProviders')->willReturn([ + new ServiceRegistration('test', AsyncProvider::class) + ]); + $user = $this->createMock(IUser::class); + $user->expects($this->any())->method('getUID')->willReturn('testuser'); + $mount = $this->createMock(ICachedMountInfo::class); + $mount->expects($this->any())->method('getUser')->willReturn($user); + $this->userMountCache->expects($this->any())->method('getMountsForFileId')->willReturn([$mount]); + self::assertCount(1, $this->manager->getAvailableTaskTypes()); + + self::assertTrue($this->manager->hasProviders()); + $audioId = $this->getFile('audioInput', 'Hello')->getId(); + $task = new Task(AudioToImage::ID, ['audio' => $audioId], 'test', 'testuser'); + self::assertNull($task->getId()); + self::assertEquals(Task::STATUS_UNKNOWN, $task->getStatus()); + $this->manager->scheduleTask($task); + self::assertNotNull($task->getId()); + self::assertEquals(Task::STATUS_SCHEDULED, $task->getStatus()); + + // Task object retrieved from db is up-to-date + $task2 = $this->manager->getTask($task->getId()); + self::assertEquals($task->getId(), $task2->getId()); + self::assertEquals(['audio' => $audioId], $task2->getInput()); + self::assertNull($task2->getOutput()); + self::assertEquals(Task::STATUS_SCHEDULED, $task2->getStatus()); + + $this->eventDispatcher->expects($this->once())->method('dispatchTyped')->with(new IsInstanceOf(TaskSuccessfulEvent::class)); + + $this->manager->setTaskProgress($task2->getId(), 0.1); + $input = $this->manager->prepareInputData($task2); + self::assertTrue(isset($input['audio'])); + self::assertInstanceOf(\OCP\Files\File::class, $input['audio']); + self::assertEquals($audioId, $input['audio']->getId()); + + $outputFileId = $this->getFile('audioOutput', 'World')->getId(); + + $this->manager->setTaskResult($task2->getId(), null, ['spectrogram' => $outputFileId], true); + + $task = $this->manager->getTask($task->getId()); + self::assertEquals(Task::STATUS_SUCCESSFUL, $task->getStatus()); + self::assertEquals(1, $task->getProgress()); + self::assertTrue(isset($task->getOutput()['spectrogram'])); + $node = $this->rootFolder->getFirstNodeById($task->getOutput()['spectrogram']); + self::assertNotNull($node, 'fileId:' . $task->getOutput()['spectrogram']); + self::assertInstanceOf(\OCP\Files\File::class, $node); + self::assertEquals('World', $node->getContent()); + } + public function testNonexistentTask() { $this->expectException(\OCP\TaskProcessing\Exception\NotFoundException::class); $this->manager->getTask(2147483646); |