1
0

CacheTest.php 3.8 KB

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