CacheTest.php 2.9 KB

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