SimpleFolderTest.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
  4. *
  5. * @author Roeland Jago Douma <roeland@famdouma.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace Test\File\SimpleFS;
  24. use OC\Files\SimpleFS\SimpleFolder;
  25. use OC\Files\Storage\Temporary;
  26. use OCP\Files\Folder;
  27. use OCP\Files\NotFoundException;
  28. use OCP\Files\SimpleFS\ISimpleFile;
  29. use OCP\Files\SimpleFS\ISimpleFolder;
  30. use Test\Traits\MountProviderTrait;
  31. use Test\Traits\UserTrait;
  32. /**
  33. * @group DB
  34. */
  35. class SimpleFolderTest extends \Test\TestCase {
  36. use MountProviderTrait;
  37. use UserTrait;
  38. /** @var Folder */
  39. private $folder;
  40. /** @var Folder */
  41. private $parentFolder;
  42. /** @var SimpleFolder */
  43. private $simpleFolder;
  44. private $storage;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->storage = new Temporary([]);
  48. $this->createUser('simple', 'simple');
  49. $this->registerMount('simple', $this->storage, '/simple/files');
  50. $this->loginAsUser('simple');
  51. $this->parentFolder = \OC::$server->getUserFolder('simple');
  52. $this->folder = $this->parentFolder->newFolder('test');
  53. $this->simpleFolder = new SimpleFolder($this->folder);
  54. }
  55. public function testGetName() {
  56. $this->assertEquals('test', $this->simpleFolder->getName());
  57. }
  58. public function testDelete() {
  59. $this->assertTrue($this->parentFolder->nodeExists('test'));
  60. $this->simpleFolder->delete();
  61. $this->assertFalse($this->parentFolder->nodeExists('test'));
  62. }
  63. public function testFileExists() {
  64. $this->folder->newFile('exists');
  65. $this->assertFalse($this->simpleFolder->fileExists('not-exists'));
  66. $this->assertTrue($this->simpleFolder->fileExists('exists'));
  67. }
  68. public function testGetFile() {
  69. $this->folder->newFile('exists');
  70. $result = $this->simpleFolder->getFile('exists');
  71. $this->assertInstanceOf(ISimpleFile::class, $result);
  72. $this->expectException(NotFoundException::class);
  73. $this->simpleFolder->getFile('not-exists');
  74. }
  75. public function testNewFile() {
  76. $result = $this->simpleFolder->newFile('file');
  77. $this->assertInstanceOf(ISimpleFile::class, $result);
  78. $this->assertFalse($this->folder->nodeExists('file'));
  79. $result->putContent('bar');
  80. $this->assertTrue($this->folder->nodeExists('file'));
  81. $this->assertEquals('bar', $result->getContent());
  82. }
  83. public function testGetDirectoryListing() {
  84. $this->folder->newFile('file1');
  85. $this->folder->newFile('file2');
  86. $result = $this->simpleFolder->getDirectoryListing();
  87. $this->assertCount(2, $result);
  88. $this->assertInstanceOf(ISimpleFile::class, $result[0]);
  89. $this->assertInstanceOf(ISimpleFile::class, $result[1]);
  90. }
  91. public function testGetFolder() {
  92. $this->folder->newFolder('exists');
  93. $result = $this->simpleFolder->getFolder('exists');
  94. $this->assertInstanceOf(ISimpleFolder::class, $result);
  95. $this->expectException(NotFoundException::class);
  96. $this->simpleFolder->getFolder('not-exists');
  97. }
  98. public function testNewFolder() {
  99. $result = $this->simpleFolder->newFolder('folder');
  100. $this->assertInstanceOf(ISimpleFolder::class, $result);
  101. $result->newFile('file');
  102. $this->assertTrue($this->folder->nodeExists('folder'));
  103. }
  104. }