You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

CacheTest.php 3.6KB

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