storage.php 10.6 KB
Newer Older
1
2
<?php
/**
Robin Appelman's avatar
Robin Appelman committed
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
 * ownCloud
 *
 * @author Robin Appelman
 * @copyright 2012 Robin Appelman icewind@owncloud.com
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
 * License as published by the Free Software Foundation; either
 * version 3 of the License, or any later version.
 *
 * This library 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 library.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
22

23
24
namespace Test\Files\Storage;

Robin Appelman's avatar
Robin Appelman committed
25
abstract class Storage extends \PHPUnit_Framework_TestCase {
26
	/**
27
	 * @var \OC\Files\Storage\Storage instance
28
29
30
31
32
33
	 */
	protected $instance;

	/**
	 * the root folder of the storage should always exist, be readable and be recognized as a directory
	 */
Thomas Mueller's avatar
Thomas Mueller committed
34
	public function testRoot() {
Robin Appelman's avatar
Robin Appelman committed
35
36
37
38
		$this->assertTrue($this->instance->file_exists('/'), 'Root folder does not exist');
		$this->assertTrue($this->instance->isReadable('/'), 'Root folder is not readable');
		$this->assertTrue($this->instance->is_dir('/'), 'Root folder is not a directory');
		$this->assertFalse($this->instance->is_file('/'), 'Root folder is a file');
Thomas Mueller's avatar
Thomas Mueller committed
39
		$this->assertEquals('dir', $this->instance->filetype('/'));
Robin Appelman's avatar
Robin Appelman committed
40

Robin Appelman's avatar
Robin Appelman committed
41
		//without this, any further testing would be useless, not an actual requirement for filestorage though
Robin Appelman's avatar
Robin Appelman committed
42
		$this->assertTrue($this->instance->isUpdatable('/'), 'Root folder is not writable');
43
	}
Robin Appelman's avatar
Robin Appelman committed
44

Thomas Mueller's avatar
Thomas Mueller committed
45
	public function testDirectories() {
46
		$this->assertFalse($this->instance->file_exists('/folder'));
Robin Appelman's avatar
Robin Appelman committed
47

48
		$this->assertTrue($this->instance->mkdir('/folder'));
Robin Appelman's avatar
Robin Appelman committed
49

50
51
52
		$this->assertTrue($this->instance->file_exists('/folder'));
		$this->assertTrue($this->instance->is_dir('/folder'));
		$this->assertFalse($this->instance->is_file('/folder'));
Thomas Mueller's avatar
Thomas Mueller committed
53
54
		$this->assertEquals('dir', $this->instance->filetype('/folder'));
		$this->assertEquals(0, $this->instance->filesize('/folder'));
55
56
		$this->assertTrue($this->instance->isReadable('/folder'));
		$this->assertTrue($this->instance->isUpdatable('/folder'));
Robin Appelman's avatar
Robin Appelman committed
57
58
59
60
61
62

		$dh = $this->instance->opendir('/');
		$content = array();
		while ($file = readdir($dh)) {
			if ($file != '.' and $file != '..') {
				$content[] = $file;
63
64
			}
		}
Thomas Mueller's avatar
Thomas Mueller committed
65
		$this->assertEquals(array('folder'), $content);
Robin Appelman's avatar
Robin Appelman committed
66
67

		$this->assertFalse($this->instance->mkdir('/folder')); //cant create existing folders
68
		$this->assertTrue($this->instance->rmdir('/folder'));
Robin Appelman's avatar
Robin Appelman committed
69

70
		$this->assertFalse($this->instance->file_exists('/folder'));
Robin Appelman's avatar
Robin Appelman committed
71
72
73
74
75
76
77
78

		$this->assertFalse($this->instance->rmdir('/folder')); //cant remove non existing folders

		$dh = $this->instance->opendir('/');
		$content = array();
		while ($file = readdir($dh)) {
			if ($file != '.' and $file != '..') {
				$content[] = $file;
79
80
			}
		}
Thomas Mueller's avatar
Thomas Mueller committed
81
		$this->assertEquals(array(), $content);
82
	}
83
84
85
86

	/**
	 * test the various uses of file_get_contents and file_put_contents
	 */
Thomas Mueller's avatar
Thomas Mueller committed
87
	public function testGetPutContents() {
Robin Appelman's avatar
Robin Appelman committed
88
		$sourceFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
Robin Appelman's avatar
Robin Appelman committed
89
90
		$sourceText = file_get_contents($sourceFile);

91
		//fill a file with string data
Robin Appelman's avatar
Robin Appelman committed
92
		$this->instance->file_put_contents('/lorem.txt', $sourceText);
Robin Appelman's avatar
Robin Appelman committed
93
		$this->assertFalse($this->instance->is_dir('/lorem.txt'));
Thomas Mueller's avatar
Thomas Mueller committed
94
		$this->assertEquals($sourceText, $this->instance->file_get_contents('/lorem.txt'), 'data returned from file_get_contents is not equal to the source data');
95
96

		//empty the file
Robin Appelman's avatar
Robin Appelman committed
97
		$this->instance->file_put_contents('/lorem.txt', '');
Thomas Mueller's avatar
Thomas Mueller committed
98
		$this->assertEquals('', $this->instance->file_get_contents('/lorem.txt'), 'file not emptied');
99
	}
Robin Appelman's avatar
Robin Appelman committed
100

101
102
103
	/**
	 * test various known mimetypes
	 */
Thomas Mueller's avatar
Thomas Mueller committed
104
	public function testMimeType() {
Thomas Mueller's avatar
Thomas Mueller committed
105
106
		$this->assertEquals('httpd/unix-directory', $this->instance->getMimeType('/'));
		$this->assertEquals(false, $this->instance->getMimeType('/non/existing/file'));
Robin Appelman's avatar
Robin Appelman committed
107

Robin Appelman's avatar
Robin Appelman committed
108
		$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
Robin Appelman's avatar
Robin Appelman committed
109
		$this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
Thomas Mueller's avatar
Thomas Mueller committed
110
		$this->assertEquals('text/plain', $this->instance->getMimeType('/lorem.txt'));
Robin Appelman's avatar
Robin Appelman committed
111

Robin Appelman's avatar
Robin Appelman committed
112
		$pngFile = \OC::$SERVERROOT . '/tests/data/logo-wide.png';
Robin Appelman's avatar
Robin Appelman committed
113
		$this->instance->file_put_contents('/logo-wide.png', file_get_contents($pngFile, 'r'));
Thomas Mueller's avatar
Thomas Mueller committed
114
		$this->assertEquals('image/png', $this->instance->getMimeType('/logo-wide.png'));
Robin Appelman's avatar
Robin Appelman committed
115

Robin Appelman's avatar
Robin Appelman committed
116
		$svgFile = \OC::$SERVERROOT . '/tests/data/logo-wide.svg';
Robin Appelman's avatar
Robin Appelman committed
117
		$this->instance->file_put_contents('/logo-wide.svg', file_get_contents($svgFile, 'r'));
Thomas Mueller's avatar
Thomas Mueller committed
118
		$this->assertEquals('image/svg+xml', $this->instance->getMimeType('/logo-wide.svg'));
119
	}
Robin Appelman's avatar
Robin Appelman committed
120

Thomas Mueller's avatar
Thomas Mueller committed
121
	public function testCopyAndMove() {
Robin Appelman's avatar
Robin Appelman committed
122
		$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
Robin Appelman's avatar
Robin Appelman committed
123
124
		$this->instance->file_put_contents('/source.txt', file_get_contents($textFile));
		$this->instance->copy('/source.txt', '/target.txt');
Robin Appelman's avatar
Robin Appelman committed
125
		$this->assertTrue($this->instance->file_exists('/target.txt'));
Thomas Mueller's avatar
Thomas Mueller committed
126
		$this->assertEquals($this->instance->file_get_contents('/source.txt'), $this->instance->file_get_contents('/target.txt'));
Robin Appelman's avatar
Robin Appelman committed
127
128

		$this->instance->rename('/source.txt', '/target2.txt');
Robin Appelman's avatar
Robin Appelman committed
129
130
		$this->assertTrue($this->instance->file_exists('/target2.txt'));
		$this->assertFalse($this->instance->file_exists('/source.txt'));
Thomas Mueller's avatar
Thomas Mueller committed
131
		$this->assertEquals(file_get_contents($textFile), $this->instance->file_get_contents('/target.txt'));
Robin Appelman's avatar
Robin Appelman committed
132
	}
Robin Appelman's avatar
Robin Appelman committed
133

Thomas Mueller's avatar
Thomas Mueller committed
134
	public function testLocal() {
Robin Appelman's avatar
Robin Appelman committed
135
		$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
Robin Appelman's avatar
Robin Appelman committed
136
137
		$this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
		$localFile = $this->instance->getLocalFile('/lorem.txt');
138
		$this->assertTrue(file_exists($localFile));
Thomas Mueller's avatar
Thomas Mueller committed
139
		$this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));
Robin Appelman's avatar
Robin Appelman committed
140

141
		$this->instance->mkdir('/folder');
Robin Appelman's avatar
Robin Appelman committed
142
143
		$this->instance->file_put_contents('/folder/lorem.txt', file_get_contents($textFile));
		$this->instance->file_put_contents('/folder/bar.txt', 'asd');
144
		$this->instance->mkdir('/folder/recursive');
Robin Appelman's avatar
Robin Appelman committed
145
146
		$this->instance->file_put_contents('/folder/recursive/file.txt', 'foo');
		$localFolder = $this->instance->getLocalFolder('/folder');
147
148

		$this->assertTrue(is_dir($localFolder));
149
150
151
152
153
154
155
156
157
158
159
160
161

		// test below require to use instance->getLocalFile because the physical storage might be different
		$localFile = $this->instance->getLocalFile('/folder/lorem.txt');
		$this->assertTrue(file_exists($localFile));
		$this->assertEquals(file_get_contents($localFile), file_get_contents($textFile));

		$localFile = $this->instance->getLocalFile('/folder/bar.txt');
		$this->assertTrue(file_exists($localFile));
		$this->assertEquals(file_get_contents($localFile), 'asd');

		$localFile = $this->instance->getLocalFile('/folder/recursive/file.txt');
		$this->assertTrue(file_exists($localFile));
		$this->assertEquals(file_get_contents($localFile), 'foo');
162
	}
163

Thomas Mueller's avatar
Thomas Mueller committed
164
	public function testStat() {
Robin Appelman's avatar
Robin Appelman committed
165
		$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
Robin Appelman's avatar
Robin Appelman committed
166
167
		$ctimeStart = time();
		$this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile));
168
		$this->assertTrue($this->instance->isReadable('/lorem.txt'));
Robin Appelman's avatar
Robin Appelman committed
169
170
171
172
173
174
175
		$ctimeEnd = time();
		$mTime = $this->instance->filemtime('/lorem.txt');
		$this->assertTrue($this->instance->hasUpdated('/lorem.txt', $ctimeStart - 1));
		$this->assertTrue($this->instance->hasUpdated('/', $ctimeStart - 1));

		$this->assertTrue(($ctimeStart - 1) <= $mTime);
		$this->assertTrue($mTime <= ($ctimeEnd + 1));
Thomas Mueller's avatar
Thomas Mueller committed
176
		$this->assertEquals(filesize($textFile), $this->instance->filesize('/lorem.txt'));
Robin Appelman's avatar
Robin Appelman committed
177
178

		$stat = $this->instance->stat('/lorem.txt');
179
		//only size and mtime are requered in the result
Thomas Mueller's avatar
Thomas Mueller committed
180
181
		$this->assertEquals($stat['size'], $this->instance->filesize('/lorem.txt'));
		$this->assertEquals($stat['mtime'], $mTime);
Robin Appelman's avatar
Robin Appelman committed
182
183

		$mtimeStart = time();
184
		$supportsTouch = $this->instance->touch('/lorem.txt');
Robin Appelman's avatar
Robin Appelman committed
185
186
187
188
189
190
191
192
193
194
		$mtimeEnd = time();
		if ($supportsTouch !== false) {
			$mTime = $this->instance->filemtime('/lorem.txt');
			$this->assertTrue(($mtimeStart - 1) <= $mTime);
			$this->assertTrue($mTime <= ($mtimeEnd + 1));

			$this->assertTrue($this->instance->hasUpdated('/lorem.txt', $mtimeStart - 1));

			if ($this->instance->touch('/lorem.txt', 100) !== false) {
				$mTime = $this->instance->filemtime('/lorem.txt');
Thomas Mueller's avatar
Thomas Mueller committed
195
				$this->assertEquals($mTime, 100);
196
			}
197
		}
198

Robin Appelman's avatar
Robin Appelman committed
199
200
201
		$mtimeStart = time();
		$fh = $this->instance->fopen('/lorem.txt', 'a');
		fwrite($fh, ' ');
202
203
		fclose($fh);
		clearstatcache();
Robin Appelman's avatar
Robin Appelman committed
204
205
206
207
		$mtimeEnd = time();
		$mTime = $this->instance->filemtime('/lorem.txt');
		$this->assertTrue(($mtimeStart - 1) <= $mTime);
		$this->assertTrue($mTime <= ($mtimeEnd + 1));
208
209

		$this->instance->unlink('/lorem.txt');
Robin Appelman's avatar
Robin Appelman committed
210
		$this->assertTrue($this->instance->hasUpdated('/', $mtimeStart - 1));
211
	}
212

Thomas Mueller's avatar
Thomas Mueller committed
213
	public function testSearch() {
Robin Appelman's avatar
Robin Appelman committed
214
		$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
Robin Appelman's avatar
Robin Appelman committed
215
		$this->instance->file_put_contents('/lorem.txt', file_get_contents($textFile, 'r'));
Robin Appelman's avatar
Robin Appelman committed
216
		$pngFile = \OC::$SERVERROOT . '/tests/data/logo-wide.png';
Robin Appelman's avatar
Robin Appelman committed
217
		$this->instance->file_put_contents('/logo-wide.png', file_get_contents($pngFile, 'r'));
Robin Appelman's avatar
Robin Appelman committed
218
		$svgFile = \OC::$SERVERROOT . '/tests/data/logo-wide.svg';
Robin Appelman's avatar
Robin Appelman committed
219
220
		$this->instance->file_put_contents('/logo-wide.svg', file_get_contents($svgFile, 'r'));
		$result = $this->instance->search('logo');
Thomas Mueller's avatar
Thomas Mueller committed
221
		$this->assertEquals(2, count($result));
Robin Appelman's avatar
Robin Appelman committed
222
223
		$this->assertContains('/logo-wide.svg', $result);
		$this->assertContains('/logo-wide.png', $result);
224
	}
225

226
	public function testSearchInSubFolder() {
227
		$this->instance->mkdir('sub');
228
229
230
231
232
233
234
235
236
237
238
239
240
		$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
		$this->instance->file_put_contents('/sub/lorem.txt', file_get_contents($textFile, 'r'));
		$pngFile = \OC::$SERVERROOT . '/tests/data/logo-wide.png';
		$this->instance->file_put_contents('/sub/logo-wide.png', file_get_contents($pngFile, 'r'));
		$svgFile = \OC::$SERVERROOT . '/tests/data/logo-wide.svg';
		$this->instance->file_put_contents('/sub/logo-wide.svg', file_get_contents($svgFile, 'r'));

		$result = $this->instance->search('logo');
		$this->assertEquals(2, count($result));
		$this->assertContains('/sub/logo-wide.svg', $result);
		$this->assertContains('/sub/logo-wide.png', $result);
	}

241
	public function testFOpen() {
Robin Appelman's avatar
Robin Appelman committed
242
		$textFile = \OC::$SERVERROOT . '/tests/data/lorem.txt';
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

		$fh = @$this->instance->fopen('foo', 'r');
		if ($fh) {
			fclose($fh);
		}
		$this->assertFalse($fh);
		$this->assertFalse($this->instance->file_exists('foo'));

		$fh = $this->instance->fopen('foo', 'w');
		fwrite($fh, file_get_contents($textFile));
		fclose($fh);
		$this->assertTrue($this->instance->file_exists('foo'));

		$fh = $this->instance->fopen('foo', 'r');
		$content = stream_get_contents($fh);
Thomas Mueller's avatar
Thomas Mueller committed
258
		$this->assertEquals(file_get_contents($textFile), $content);
259
	}
260
261
262
263
264
265

	public function testTouchCreateFile(){
		$this->assertFalse($this->instance->file_exists('foo'));
		$this->instance->touch('foo');
		$this->assertTrue($this->instance->file_exists('foo'));
	}
266
}