FilesMetadataManagerTest.php 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. <?php
  2. declare(strict_types=1);
  3. /**
  4. * SPDX-FileCopyrightText: 2024 Robin Appelman <robin@icewind.nl>
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\FilesMetadata;
  8. use OC\BackgroundJob\JobList;
  9. use OC\Files\Storage\Temporary;
  10. use OC\FilesMetadata\FilesMetadataManager;
  11. use OC\FilesMetadata\Service\IndexRequestService;
  12. use OC\FilesMetadata\Service\MetadataRequestService;
  13. use OCP\EventDispatcher\Event;
  14. use OCP\EventDispatcher\IEventDispatcher;
  15. use OCP\Files\Folder;
  16. use OCP\Files\IRootFolder;
  17. use OCP\FilesMetadata\AMetadataEvent;
  18. use OCP\IAppConfig;
  19. use OCP\IDBConnection;
  20. use OCP\Server;
  21. use Psr\Log\LoggerInterface;
  22. use Test\TestCase;
  23. use Test\Traits\MountProviderTrait;
  24. use Test\Traits\UserTrait;
  25. /**
  26. * @group DB
  27. */
  28. class FilesMetadataManagerTest extends TestCase {
  29. use UserTrait;
  30. use MountProviderTrait;
  31. private IEventDispatcher $eventDispatcher;
  32. private JobList $jobList;
  33. private IAppConfig $appConfig;
  34. private LoggerInterface $logger;
  35. private MetadataRequestService $metadataRequestService;
  36. private IndexRequestService $indexRequestService;
  37. private FilesMetadataManager $manager;
  38. private IDBConnection $connection;
  39. private Folder $userFolder;
  40. private array $metadata = [];
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->jobList = $this->createMock(JobList::class);
  44. $this->eventDispatcher = $this->createMock(IEventDispatcher::class);
  45. $this->eventDispatcher->method('dispatchTyped')->willReturnCallback(function (Event $event) {
  46. if ($event instanceof AMetadataEvent) {
  47. $name = $event->getNode()->getName();
  48. if (isset($this->metadata[$name])) {
  49. $meta = $event->getMetadata();
  50. foreach ($this->metadata[$name] as $key => $value) {
  51. $meta->setString($key, $value);
  52. }
  53. }
  54. }
  55. });
  56. $this->appConfig = $this->createMock(IAppConfig::class);
  57. $this->logger = $this->createMock(LoggerInterface::class);
  58. $this->connection = Server::get(IDBConnection::class);
  59. $this->metadataRequestService = new MetadataRequestService($this->connection, $this->logger);
  60. $this->indexRequestService = new IndexRequestService($this->connection, $this->logger);
  61. $this->manager = new FilesMetadataManager(
  62. $this->eventDispatcher,
  63. $this->jobList,
  64. $this->appConfig,
  65. $this->logger,
  66. $this->metadataRequestService,
  67. $this->indexRequestService,
  68. );
  69. $this->createUser('metatest', '');
  70. $this->registerMount('metatest', new Temporary([]), '/metatest');
  71. $rootFolder = Server::get(IRootFolder::class);
  72. $this->userFolder = $rootFolder->getUserFolder('metatest');
  73. }
  74. public function testRefreshMetadata(): void {
  75. $this->metadata['test.txt'] = [
  76. 'istest' => 'yes'
  77. ];
  78. $file = $this->userFolder->newFile('test.txt', 'test');
  79. $stored = $this->manager->refreshMetadata($file);
  80. $this->assertEquals($file->getId(), $stored->getFileId());
  81. $this->assertEquals('yes', $stored->getString('istest'));
  82. $retrieved = $this->manager->getMetadata($file->getId());
  83. $this->assertEquals($file->getId(), $retrieved->getFileId());
  84. $this->assertEquals('yes', $retrieved->getString('istest'));
  85. }
  86. }