IntegrationTest.php 3.9 KB

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