1
0

CacheTest.php 3.1 KB

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