summaryrefslogtreecommitdiffstats
path: root/tests/lib/Files/SimpleFS/SimpleFolderTest.php
blob: 4cab1136d8223a0d728e22ddac2ced4ecf2367d7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
<?php
/**
 * @copyright 2016 Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @author Roeland Jago Douma <roeland@famdouma.nl>
 *
 * @license GNU AGPL version 3 or any later version
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

namespace Test\File\SimpleFS;

use OC\Files\SimpleFS\SimpleFolder;
use OC\Files\Storage\Temporary;
use OCP\Files\Folder;
use OCP\Files\NotFoundException;
use OCP\Files\SimpleFS\ISimpleFile;
use Test\Traits\MountProviderTrait;
use Test\Traits\UserTrait;

/**
 * @group DB
 */
class SimpleFolderTest extends \Test\TestCase {
	use MountProviderTrait;
	use UserTrait;

	/** @var Folder */
	private $folder;

	/** @var Folder */
	private $parentFolder;

	/** @var SimpleFolder */
	private $simpleFolder;

	private $storage;

	protected function setUp(): void {
		parent::setUp();

		$this->storage = new Temporary([]);
		$this->createUser('simple', 'simple');
		$this->registerMount('simple', $this->storage, '/simple/files');
		$this->loginAsUser('simple');

		$this->parentFolder = \OC::$server->getUserFolder('simple');

		$this->folder = $this->parentFolder->newFolder('test');
		$this->simpleFolder = new SimpleFolder($this->folder);
	}

	public function testGetName() {
		$this->assertEquals('test', $this->simpleFolder->getName());
	}

	public function testDelete() {
		$this->assertTrue($this->parentFolder->nodeExists('test'));
		$this->simpleFolder->delete();
		$this->assertFalse($this->parentFolder->nodeExists('test'));
	}

	public function testFileExists() {
		$this->folder->newFile('exists');

		$this->assertFalse($this->simpleFolder->fileExists('not-exists'));
		$this->assertTrue($this->simpleFolder->fileExists('exists'));
	}

	public function testGetFile() {
		$this->folder->newFile('exists');

		$result = $this->simpleFolder->getFile('exists');
		$this->assertInstanceOf(ISimpleFile::class, $result);

		$this->expectException(NotFoundException::class);
		$this->simpleFolder->getFile('not-exists');
	}

	public function testNewFile() {
		$result = $this->simpleFolder->newFile('file');
		$this->assertInstanceOf(ISimpleFile::class, $result);
		$this->assertFalse($this->folder->nodeExists('file'));
		$result->putContent('bar');

		$this->assertTrue($this->folder->nodeExists('file'));
		$this->assertEquals('bar', $result->getContent());
	}

	public function testGetDirectoryListing() {
		$this->folder->newFile('file1');
		$this->folder->newFile('file2');

		$result = $this->simpleFolder->getDirectoryListing();
		$this->assertCount(2, $result);
		$this->assertInstanceOf(ISimpleFile::class, $result[0]);
		$this->assertInstanceOf(ISimpleFile::class, $result[1]);
	}

}