CacheTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2017-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\Files_Sharing\Tests\External;
  8. use OC\Federation\CloudIdManager;
  9. use OC\Files\Storage\Storage;
  10. use OCA\Files_Sharing\External\Cache;
  11. use OCA\Files_Sharing\Tests\TestCase;
  12. use OCP\Contacts\IManager;
  13. use OCP\EventDispatcher\IEventDispatcher;
  14. use OCP\Federation\ICloudIdManager;
  15. use OCP\Files\Cache\ICacheEntry;
  16. use OCP\ICacheFactory;
  17. use OCP\IURLGenerator;
  18. use OCP\IUserManager;
  19. /**
  20. * Class Cache
  21. *
  22. * @group DB
  23. *
  24. * @package OCA\Files_Sharing\Tests\External
  25. */
  26. class CacheTest extends TestCase {
  27. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  28. protected $contactsManager;
  29. /**
  30. * @var Storage
  31. **/
  32. private $storage;
  33. /**
  34. * @var Cache
  35. */
  36. private $cache;
  37. /**
  38. * @var string
  39. */
  40. private $remoteUser;
  41. /** @var ICloudIdManager */
  42. private $cloudIdManager;
  43. protected function setUp(): void {
  44. parent::setUp();
  45. $this->contactsManager = $this->createMock(IManager::class);
  46. $this->cloudIdManager = new CloudIdManager(
  47. $this->contactsManager,
  48. $this->createMock(IURLGenerator::class),
  49. $this->createMock(IUserManager::class),
  50. $this->createMock(ICacheFactory::class),
  51. $this->createMock(IEventDispatcher::class)
  52. );
  53. $this->remoteUser = $this->getUniqueID('remoteuser');
  54. $this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage')
  55. ->disableOriginalConstructor()
  56. ->getMock();
  57. $this->storage
  58. ->expects($this->any())
  59. ->method('getId')
  60. ->willReturn('dummystorage::');
  61. $this->contactsManager->expects($this->any())
  62. ->method('search')
  63. ->willReturn([]);
  64. $this->cache = new Cache(
  65. $this->storage,
  66. $this->cloudIdManager->getCloudId($this->remoteUser, 'http://example.com/owncloud')
  67. );
  68. $this->cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
  69. $this->cache->put(
  70. 'test.txt',
  71. [
  72. 'mimetype' => 'text/plain',
  73. 'size' => 5,
  74. 'mtime' => 123,
  75. ]
  76. );
  77. }
  78. protected function tearDown(): void {
  79. if ($this->cache) {
  80. $this->cache->clear();
  81. }
  82. parent::tearDown();
  83. }
  84. public function testGetInjectsOwnerDisplayName(): void {
  85. $info = $this->cache->get('test.txt');
  86. $this->assertEquals(
  87. $this->remoteUser . '@example.com/owncloud',
  88. $info['displayname_owner']
  89. );
  90. }
  91. public function testGetReturnsFalseIfNotFound(): void {
  92. $info = $this->cache->get('unexisting-entry.txt');
  93. $this->assertFalse($info);
  94. }
  95. public function testGetFolderPopulatesOwner(): void {
  96. $dirId = $this->cache->put(
  97. 'subdir',
  98. [
  99. 'mimetype' => 'httpd/unix-directory',
  100. 'size' => 5,
  101. 'mtime' => 123,
  102. ]
  103. );
  104. $this->cache->put(
  105. 'subdir/contents.txt',
  106. [
  107. 'mimetype' => 'text/plain',
  108. 'size' => 5,
  109. 'mtime' => 123,
  110. ]
  111. );
  112. $results = $this->cache->getFolderContentsById($dirId);
  113. $this->assertEquals(1, count($results));
  114. $this->assertEquals(
  115. $this->remoteUser . '@example.com/owncloud',
  116. $results[0]['displayname_owner']
  117. );
  118. }
  119. }