1
0

SimpleFolderTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-License-Identifier: AGPL-3.0-or-later
  5. */
  6. namespace Test\File\SimpleFS;
  7. use OC\Files\SimpleFS\SimpleFolder;
  8. use OC\Files\Storage\Temporary;
  9. use OCP\Files\Folder;
  10. use OCP\Files\NotFoundException;
  11. use OCP\Files\SimpleFS\ISimpleFile;
  12. use OCP\Files\SimpleFS\ISimpleFolder;
  13. use Test\Traits\MountProviderTrait;
  14. use Test\Traits\UserTrait;
  15. /**
  16. * @group DB
  17. */
  18. class SimpleFolderTest extends \Test\TestCase {
  19. use MountProviderTrait;
  20. use UserTrait;
  21. /** @var Folder */
  22. private $folder;
  23. /** @var Folder */
  24. private $parentFolder;
  25. /** @var SimpleFolder */
  26. private $simpleFolder;
  27. private $storage;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->storage = new Temporary([]);
  31. $this->createUser('simple', 'simple');
  32. $this->registerMount('simple', $this->storage, '/simple/files');
  33. $this->loginAsUser('simple');
  34. $this->parentFolder = \OC::$server->getUserFolder('simple');
  35. $this->folder = $this->parentFolder->newFolder('test');
  36. $this->simpleFolder = new SimpleFolder($this->folder);
  37. }
  38. public function testGetName() {
  39. $this->assertEquals('test', $this->simpleFolder->getName());
  40. }
  41. public function testDelete() {
  42. $this->assertTrue($this->parentFolder->nodeExists('test'));
  43. $this->simpleFolder->delete();
  44. $this->assertFalse($this->parentFolder->nodeExists('test'));
  45. }
  46. public function testFileExists() {
  47. $this->folder->newFile('exists');
  48. $this->assertFalse($this->simpleFolder->fileExists('not-exists'));
  49. $this->assertTrue($this->simpleFolder->fileExists('exists'));
  50. }
  51. public function testGetFile() {
  52. $this->folder->newFile('exists');
  53. $result = $this->simpleFolder->getFile('exists');
  54. $this->assertInstanceOf(ISimpleFile::class, $result);
  55. $this->expectException(NotFoundException::class);
  56. $this->simpleFolder->getFile('not-exists');
  57. }
  58. public function testNewFile() {
  59. $result = $this->simpleFolder->newFile('file');
  60. $this->assertInstanceOf(ISimpleFile::class, $result);
  61. $this->assertFalse($this->folder->nodeExists('file'));
  62. $result->putContent('bar');
  63. $this->assertTrue($this->folder->nodeExists('file'));
  64. $this->assertEquals('bar', $result->getContent());
  65. }
  66. public function testGetDirectoryListing() {
  67. $this->folder->newFile('file1');
  68. $this->folder->newFile('file2');
  69. $result = $this->simpleFolder->getDirectoryListing();
  70. $this->assertCount(2, $result);
  71. $this->assertInstanceOf(ISimpleFile::class, $result[0]);
  72. $this->assertInstanceOf(ISimpleFile::class, $result[1]);
  73. }
  74. public function testGetFolder() {
  75. $this->folder->newFolder('exists');
  76. $result = $this->simpleFolder->getFolder('exists');
  77. $this->assertInstanceOf(ISimpleFolder::class, $result);
  78. $this->expectException(NotFoundException::class);
  79. $this->simpleFolder->getFolder('not-exists');
  80. }
  81. public function testNewFolder() {
  82. $result = $this->simpleFolder->newFolder('folder');
  83. $this->assertInstanceOf(ISimpleFolder::class, $result);
  84. $result->newFile('file');
  85. $this->assertTrue($this->folder->nodeExists('folder'));
  86. }
  87. }