IntegrationTest.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test\Files\Node;
  8. use OC\Files\Node\Root;
  9. use OC\Files\Storage\Temporary;
  10. use OC\Files\View;
  11. use OC\Memcache\ArrayCache;
  12. use OCP\EventDispatcher\IEventDispatcher;
  13. use OCP\Files\Config\IUserMountCache;
  14. use OCP\Files\Mount\IMountManager;
  15. use OCP\ICacheFactory;
  16. use OCP\IUserManager;
  17. use Psr\Log\LoggerInterface;
  18. use Test\Traits\UserTrait;
  19. /**
  20. * Class IntegrationTest
  21. *
  22. * @group DB
  23. *
  24. * @package Test\Files\Node
  25. */
  26. class IntegrationTest extends \Test\TestCase {
  27. use UserTrait;
  28. /**
  29. * @var \OC\Files\Node\Root $root
  30. */
  31. private $root;
  32. /**
  33. * @var \OC\Files\Storage\Storage[]
  34. */
  35. private $storages;
  36. /**
  37. * @var \OC\Files\View $view
  38. */
  39. private $view;
  40. protected function setUp(): void {
  41. parent::setUp();
  42. $manager = \OCP\Server::get(IMountManager::class);
  43. \OC_Hook::clear('OC_Filesystem');
  44. $user = $this->createUser($this->getUniqueID('user'), '');
  45. $this->loginAsUser($user->getUID());
  46. $cacheFactory = $this->createMock(ICacheFactory::class);
  47. $cacheFactory->method('createLocal')
  48. ->willReturnCallback(function () {
  49. return new ArrayCache();
  50. });
  51. $this->view = new View();
  52. $this->root = new Root(
  53. $manager,
  54. $this->view,
  55. $user,
  56. \OCP\Server::get(IUserMountCache::class),
  57. $this->createMock(LoggerInterface::class),
  58. $this->createMock(IUserManager::class),
  59. $this->createMock(IEventDispatcher::class),
  60. $cacheFactory,
  61. );
  62. $storage = new Temporary([]);
  63. $subStorage = new Temporary([]);
  64. $this->storages[] = $storage;
  65. $this->storages[] = $subStorage;
  66. $this->root->mount($storage, '/');
  67. $this->root->mount($subStorage, '/substorage/');
  68. $manager->removeMount('/' . $user->getUID());
  69. }
  70. protected function tearDown(): void {
  71. foreach ($this->storages as $storage) {
  72. $storage->getCache()->clear();
  73. }
  74. $this->logout();
  75. parent::tearDown();
  76. }
  77. public function testBasicFile(): void {
  78. $file = $this->root->newFile('/foo.txt');
  79. $this->assertCount(2, $this->root->getDirectoryListing());
  80. $this->assertTrue($this->root->nodeExists('/foo.txt'));
  81. $id = $file->getId();
  82. $this->assertInstanceOf('\OC\Files\Node\File', $file);
  83. $file->putContent('qwerty');
  84. $this->assertEquals('text/plain', $file->getMimeType());
  85. $this->assertEquals('qwerty', $file->getContent());
  86. $this->assertFalse($this->root->nodeExists('/bar.txt'));
  87. $target = $file->move('/bar.txt');
  88. $this->assertEquals($id, $target->getId());
  89. $this->assertEquals($id, $file->getId());
  90. $this->assertFalse($this->root->nodeExists('/foo.txt'));
  91. $this->assertTrue($this->root->nodeExists('/bar.txt'));
  92. $this->assertEquals('bar.txt', $file->getName());
  93. $this->assertEquals('bar.txt', $file->getInternalPath());
  94. $file->move('/substorage/bar.txt');
  95. $this->assertEquals($id, $file->getId());
  96. $this->assertEquals('qwerty', $file->getContent());
  97. }
  98. public function testBasicFolder(): void {
  99. $folder = $this->root->newFolder('/foo');
  100. $this->assertTrue($this->root->nodeExists('/foo'));
  101. $file = $folder->newFile('/bar');
  102. $this->assertTrue($this->root->nodeExists('/foo/bar'));
  103. $file->putContent('qwerty');
  104. $listing = $folder->getDirectoryListing();
  105. $this->assertEquals(1, count($listing));
  106. $this->assertEquals($file->getId(), $listing[0]->getId());
  107. $this->assertEquals($file->getStorage(), $listing[0]->getStorage());
  108. $rootListing = $this->root->getDirectoryListing();
  109. $this->assertEquals(2, count($rootListing));
  110. $folder->move('/asd');
  111. /**
  112. * @var \OC\Files\Node\File $file
  113. */
  114. $file = $folder->get('/bar');
  115. $this->assertInstanceOf('\OC\Files\Node\File', $file);
  116. $this->assertFalse($this->root->nodeExists('/foo/bar'));
  117. $this->assertTrue($this->root->nodeExists('/asd/bar'));
  118. $this->assertEquals('qwerty', $file->getContent());
  119. $folder->move('/substorage/foo');
  120. /**
  121. * @var \OC\Files\Node\File $file
  122. */
  123. $file = $folder->get('/bar');
  124. $this->assertInstanceOf('\OC\Files\Node\File', $file);
  125. $this->assertTrue($this->root->nodeExists('/substorage/foo/bar'));
  126. $this->assertEquals('qwerty', $file->getContent());
  127. }
  128. }