1
0

CacheTest.php 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  7. * @author Joas Schilling <coding@schilljs.com>
  8. * @author Robin Appelman <robin@icewind.nl>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. * @author Vincent Petry <vincent@nextcloud.com>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\Files_Sharing\Tests\External;
  29. use OC\Federation\CloudIdManager;
  30. use OCA\Files_Sharing\Tests\TestCase;
  31. use OCP\Contacts\IManager;
  32. use OCP\EventDispatcher\IEventDispatcher;
  33. use OCP\Federation\ICloudIdManager;
  34. use OCP\Files\Cache\ICacheEntry;
  35. use OCP\ICacheFactory;
  36. use OCP\IURLGenerator;
  37. use OCP\IUserManager;
  38. /**
  39. * Class Cache
  40. *
  41. * @group DB
  42. *
  43. * @package OCA\Files_Sharing\Tests\External
  44. */
  45. class CacheTest extends TestCase {
  46. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  47. protected $contactsManager;
  48. /**
  49. * @var \OC\Files\Storage\Storage
  50. **/
  51. private $storage;
  52. /**
  53. * @var \OCA\Files_Sharing\External\Cache
  54. */
  55. private $cache;
  56. /**
  57. * @var string
  58. */
  59. private $remoteUser;
  60. /** @var ICloudIdManager */
  61. private $cloudIdManager;
  62. protected function setUp(): void {
  63. parent::setUp();
  64. $this->contactsManager = $this->createMock(IManager::class);
  65. $this->cloudIdManager = new CloudIdManager(
  66. $this->contactsManager,
  67. $this->createMock(IURLGenerator::class),
  68. $this->createMock(IUserManager::class),
  69. $this->createMock(ICacheFactory::class),
  70. $this->createMock(IEventDispatcher::class)
  71. );
  72. $this->remoteUser = $this->getUniqueID('remoteuser');
  73. $this->storage = $this->getMockBuilder('\OCA\Files_Sharing\External\Storage')
  74. ->disableOriginalConstructor()
  75. ->getMock();
  76. $this->storage
  77. ->expects($this->any())
  78. ->method('getId')
  79. ->willReturn('dummystorage::');
  80. $this->contactsManager->expects($this->any())
  81. ->method('search')
  82. ->willReturn([]);
  83. $this->cache = new \OCA\Files_Sharing\External\Cache(
  84. $this->storage,
  85. $this->cloudIdManager->getCloudId($this->remoteUser, 'http://example.com/owncloud')
  86. );
  87. $this->cache->insert('', ['size' => 0, 'mtime' => 0, 'mimetype' => ICacheEntry::DIRECTORY_MIMETYPE]);
  88. $this->cache->put(
  89. 'test.txt',
  90. [
  91. 'mimetype' => 'text/plain',
  92. 'size' => 5,
  93. 'mtime' => 123,
  94. ]
  95. );
  96. }
  97. protected function tearDown(): void {
  98. if ($this->cache) {
  99. $this->cache->clear();
  100. }
  101. parent::tearDown();
  102. }
  103. public function testGetInjectsOwnerDisplayName() {
  104. $info = $this->cache->get('test.txt');
  105. $this->assertEquals(
  106. $this->remoteUser . '@example.com/owncloud',
  107. $info['displayname_owner']
  108. );
  109. }
  110. public function testGetReturnsFalseIfNotFound() {
  111. $info = $this->cache->get('unexisting-entry.txt');
  112. $this->assertFalse($info);
  113. }
  114. public function testGetFolderPopulatesOwner() {
  115. $dirId = $this->cache->put(
  116. 'subdir',
  117. [
  118. 'mimetype' => 'httpd/unix-directory',
  119. 'size' => 5,
  120. 'mtime' => 123,
  121. ]
  122. );
  123. $this->cache->put(
  124. 'subdir/contents.txt',
  125. [
  126. 'mimetype' => 'text/plain',
  127. 'size' => 5,
  128. 'mtime' => 123,
  129. ]
  130. );
  131. $results = $this->cache->getFolderContentsById($dirId);
  132. $this->assertEquals(1, count($results));
  133. $this->assertEquals(
  134. $this->remoteUser . '@example.com/owncloud',
  135. $results[0]['displayname_owner']
  136. );
  137. }
  138. }