Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 1
<?php
2
/**
3
 * FileTest file
4
 *
5
 * CakePHP(tm) Tests <http://book.cakephp.org/2.0/en/development/testing.html>
6
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
7
 *
8
 * Licensed under The MIT License
9
 * For full copyright and license information, please see the LICENSE.txt
10
 * Redistributions of files must retain the above copyright notice
11
 *
12
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
13
 * @link          http://book.cakephp.org/2.0/en/development/testing.html CakePHP(tm) Tests
14
 * @package       Cake.Test.Case.Utility
15
 * @since         CakePHP(tm) v 1.2.0.4206
16
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
17
 */
18
 
19
App::uses('File', 'Utility');
20
App::uses('Folder', 'Utility');
21
 
22
/**
23
 * FileTest class
24
 *
25
 * @package       Cake.Test.Case.Utility
26
 */
27
class FileTest extends CakeTestCase {
28
 
29
/**
30
 * File property
31
 *
32
 * @var mixed null
33
 */
34
	public $File = null;
35
 
36
/**
37
 * setup the test case
38
 *
39
 * @return void
40
 */
41
	public function setUp() {
42
		parent::setUp();
43
		$file = __FILE__;
44
		$this->File = new File($file);
45
	}
46
 
47
/**
48
 * tearDown method
49
 *
50
 * @return void
51
 */
52
	public function tearDown() {
53
		parent::tearDown();
54
		$this->File->close();
55
		unset($this->File);
56
 
57
		$Folder = new Folder();
58
		$Folder->delete(TMP . 'tests' . DS . 'permissions');
59
	}
60
 
61
/**
62
 * testBasic method
63
 *
64
 * @return void
65
 */
66
	public function testBasic() {
67
		$file = CAKE . DS . 'LICENSE.txt';
68
 
69
		$this->File = new File($file, false);
70
 
71
		$result = $this->File->name;
72
		$expecting = basename($file);
73
		$this->assertEquals($expecting, $result);
74
 
75
		$result = $this->File->info();
76
		$expecting = array(
77
			'dirname' => dirname($file),
78
			'basename' => basename($file),
79
			'extension' => 'txt',
80
			'filename' => 'LICENSE',
81
			'filesize' => filesize($file),
82
			'mime' => 'text/plain'
83
		);
84
		if (
85
			!function_exists('finfo_open') &&
86
			(!function_exists('mime_content_type') ||
87
			function_exists('mime_content_type') &&
88
			mime_content_type($this->File->pwd()) === false)
89
		) {
90
			$expecting['mime'] = false;
91
		}
92
		$this->assertEquals($expecting, $result);
93
 
94
		$result = $this->File->ext();
95
		$expecting = 'txt';
96
		$this->assertEquals($expecting, $result);
97
 
98
		$result = $this->File->name();
99
		$expecting = 'LICENSE';
100
		$this->assertEquals($expecting, $result);
101
 
102
		$result = $this->File->md5();
103
		$expecting = md5_file($file);
104
		$this->assertEquals($expecting, $result);
105
 
106
		$result = $this->File->md5(true);
107
		$expecting = md5_file($file);
108
		$this->assertEquals($expecting, $result);
109
 
110
		$result = $this->File->size();
111
		$expecting = filesize($file);
112
		$this->assertEquals($expecting, $result);
113
 
114
		$result = $this->File->owner();
115
		$expecting = fileowner($file);
116
		$this->assertEquals($expecting, $result);
117
 
118
		$result = $this->File->group();
119
		$expecting = filegroup($file);
120
		$this->assertEquals($expecting, $result);
121
 
122
		$result = $this->File->Folder();
123
		$this->assertInstanceOf('Folder', $result);
124
	}
125
 
126
/**
127
 * testPermission method
128
 */
129
	public function testPermission() {
130
		$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
131
 
132
		$dir = TMP . 'tests' . DS . 'permissions' . DS;
133
		$old = umask();
134
 
135
		umask(0002);
136
		$file = $dir . 'permission_' . uniqid();
137
		$expecting = decoct(0664 & ~umask());
138
		$File = new File($file, true);
139
		$result = $File->perms();
140
		$this->assertEquals($expecting, $result);
141
		$File->delete();
142
 
143
		umask(0022);
144
		$file = $dir . 'permission_' . uniqid();
145
		$expecting = decoct(0644 & ~umask());
146
		$File = new File($file, true);
147
		$result = $File->perms();
148
		$this->assertEquals($expecting, $result);
149
		$File->delete();
150
 
151
		umask(0422);
152
		$file = $dir . 'permission_' . uniqid();
153
		$expecting = decoct(0244 & ~umask());
154
		$File = new File($file, true);
155
		$result = $File->perms();
156
		$this->assertEquals($expecting, $result);
157
		$File->delete();
158
 
159
		umask(0444);
160
		$file = $dir . 'permission_' . uniqid();
161
		$expecting = decoct(0222 & ~umask());
162
		$File = new File($file, true);
163
		$result = $File->perms();
164
		$this->assertEquals($expecting, $result);
165
		$File->delete();
166
 
167
		umask($old);
168
	}
169
 
170
/**
171
 * testRead method
172
 *
173
 * @return void
174
 */
175
	public function testRead() {
176
		$file = __FILE__;
177
		$this->File = new File($file);
178
 
179
		$result = $this->File->read();
180
		$expecting = file_get_contents(__FILE__);
181
		$this->assertEquals($expecting, $result);
182
		$this->assertTrue(!is_resource($this->File->handle));
183
 
184
		$this->File->lock = true;
185
		$result = $this->File->read();
186
		$expecting = file_get_contents(__FILE__);
187
		$this->assertEquals(trim($expecting), $result);
188
		$this->File->lock = null;
189
 
190
		$data = $expecting;
191
		$expecting = substr($data, 0, 3);
192
		$result = $this->File->read(3);
193
		$this->assertEquals($expecting, $result);
194
		$this->assertTrue(is_resource($this->File->handle));
195
 
196
		$expecting = substr($data, 3, 3);
197
		$result = $this->File->read(3);
198
		$this->assertEquals($expecting, $result);
199
	}
200
 
201
/**
202
 * testOffset method
203
 *
204
 * @return void
205
 */
206
	public function testOffset() {
207
		$this->File->close();
208
 
209
		$result = $this->File->offset();
210
		$this->assertFalse($result);
211
 
212
		$this->assertFalse(is_resource($this->File->handle));
213
		$success = $this->File->offset(0);
214
		$this->assertTrue($success);
215
		$this->assertTrue(is_resource($this->File->handle));
216
 
217
		$result = $this->File->offset();
218
		$expected = 0;
219
		$this->assertSame($expected, $result);
220
 
221
		$data = file_get_contents(__FILE__);
222
		$success = $this->File->offset(5);
223
		$expected = substr($data, 5, 3);
224
		$result = $this->File->read(3);
225
		$this->assertTrue($success);
226
		$this->assertEquals($expected, $result);
227
 
228
		$result = $this->File->offset();
229
		$expected = 5 + 3;
230
		$this->assertSame($expected, $result);
231
	}
232
 
233
/**
234
 * testOpen method
235
 *
236
 * @return void
237
 */
238
	public function testOpen() {
239
		$this->File->handle = null;
240
 
241
		$r = $this->File->open();
242
		$this->assertTrue(is_resource($this->File->handle));
243
		$this->assertTrue($r);
244
 
245
		$handle = $this->File->handle;
246
		$r = $this->File->open();
247
		$this->assertTrue($r);
248
		$this->assertTrue($handle === $this->File->handle);
249
		$this->assertTrue(is_resource($this->File->handle));
250
 
251
		$r = $this->File->open('r', true);
252
		$this->assertTrue($r);
253
		$this->assertFalse($handle === $this->File->handle);
254
		$this->assertTrue(is_resource($this->File->handle));
255
	}
256
 
257
/**
258
 * testClose method
259
 *
260
 * @return void
261
 */
262
	public function testClose() {
263
		$this->File->handle = null;
264
		$this->assertFalse(is_resource($this->File->handle));
265
		$this->assertTrue($this->File->close());
266
		$this->assertFalse(is_resource($this->File->handle));
267
 
268
		$this->File->handle = fopen(__FILE__, 'r');
269
		$this->assertTrue(is_resource($this->File->handle));
270
		$this->assertTrue($this->File->close());
271
		$this->assertFalse(is_resource($this->File->handle));
272
	}
273
 
274
/**
275
 * testCreate method
276
 *
277
 * @return void
278
 */
279
	public function testCreate() {
280
		$tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
281
		$File = new File($tmpFile, true, 0777);
282
		$this->assertTrue($File->exists());
283
	}
284
 
285
/**
286
 * testOpeningNonExistentFileCreatesIt method
287
 *
288
 * @return void
289
 */
290
	public function testOpeningNonExistentFileCreatesIt() {
291
		$someFile = new File(TMP . 'some_file.txt', false);
292
		$this->assertTrue($someFile->open());
293
		$this->assertEquals('', $someFile->read());
294
		$someFile->close();
295
		$someFile->delete();
296
	}
297
 
298
/**
299
 * testPrepare method
300
 *
301
 * @return void
302
 */
303
	public function testPrepare() {
304
		$string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
305
		if (DS === '\\') {
306
			$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
307
			$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
308
		} else {
309
			$expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
310
		}
311
		$this->assertSame($expected, File::prepare($string));
312
 
313
		$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
314
		$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
315
		$this->assertSame($expected, File::prepare($string, true));
316
	}
317
 
318
/**
319
 * testReadable method
320
 *
321
 * @return void
322
 */
323
	public function testReadable() {
324
		$someFile = new File(TMP . 'some_file.txt', false);
325
		$this->assertTrue($someFile->open());
326
		$this->assertTrue($someFile->readable());
327
		$someFile->close();
328
		$someFile->delete();
329
	}
330
 
331
/**
332
 * testWritable method
333
 *
334
 * @return void
335
 */
336
	public function testWritable() {
337
		$someFile = new File(TMP . 'some_file.txt', false);
338
		$this->assertTrue($someFile->open());
339
		$this->assertTrue($someFile->writable());
340
		$someFile->close();
341
		$someFile->delete();
342
	}
343
 
344
/**
345
 * testExecutable method
346
 *
347
 * @return void
348
 */
349
	public function testExecutable() {
350
		$someFile = new File(TMP . 'some_file.txt', false);
351
		$this->assertTrue($someFile->open());
352
		$this->assertFalse($someFile->executable());
353
		$someFile->close();
354
		$someFile->delete();
355
	}
356
 
357
/**
358
 * testLastAccess method
359
 *
360
 * @return void
361
 */
362
	public function testLastAccess() {
363
		$someFile = new File(TMP . 'some_file.txt', false);
364
		$this->assertFalse($someFile->lastAccess());
365
		$this->assertTrue($someFile->open());
366
		$this->assertWithinMargin($someFile->lastAccess(), time(), 2);
367
		$someFile->close();
368
		$someFile->delete();
369
	}
370
 
371
/**
372
 * testLastChange method
373
 *
374
 * @return void
375
 */
376
	public function testLastChange() {
377
		$someFile = new File(TMP . 'some_file.txt', false);
378
		$this->assertFalse($someFile->lastChange());
379
		$this->assertTrue($someFile->open('r+'));
380
		$this->assertWithinMargin($someFile->lastChange(), time(), 2);
381
 
382
		$someFile->write('something');
383
		$this->assertWithinMargin($someFile->lastChange(), time(), 2);
384
 
385
		$someFile->close();
386
		$someFile->delete();
387
	}
388
 
389
/**
390
 * testWrite method
391
 *
392
 * @return void
393
 */
394
	public function testWrite() {
395
		if (!$tmpFile = $this->_getTmpFile()) {
396
			return false;
397
		}
398
		if (file_exists($tmpFile)) {
399
			unlink($tmpFile);
400
		}
401
 
402
		$TmpFile = new File($tmpFile);
403
		$this->assertFalse(file_exists($tmpFile));
404
		$this->assertFalse(is_resource($TmpFile->handle));
405
 
406
		$testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
407
		foreach ($testData as $data) {
408
			$r = $TmpFile->write($data);
409
			$this->assertTrue($r);
410
			$this->assertTrue(file_exists($tmpFile));
411
			$this->assertEquals($data, file_get_contents($tmpFile));
412
			$this->assertTrue(is_resource($TmpFile->handle));
413
			$TmpFile->close();
414
 
415
		}
416
		unlink($tmpFile);
417
	}
418
 
419
/**
420
 * testAppend method
421
 *
422
 * @return void
423
 */
424
	public function testAppend() {
425
		if (!$tmpFile = $this->_getTmpFile()) {
426
			return false;
427
		}
428
		if (file_exists($tmpFile)) {
429
			unlink($tmpFile);
430
		}
431
 
432
		$TmpFile = new File($tmpFile);
433
		$this->assertFalse(file_exists($tmpFile));
434
 
435
		$fragments = array('CakePHP\'s', ' test suite', ' was here ...');
436
		$data = null;
437
		$size = 0;
438
		foreach ($fragments as $fragment) {
439
			$r = $TmpFile->append($fragment);
440
			$this->assertTrue($r);
441
			$this->assertTrue(file_exists($tmpFile));
442
			$data = $data . $fragment;
443
			$this->assertEquals($data, file_get_contents($tmpFile));
444
			$newSize = $TmpFile->size();
445
			$this->assertTrue($newSize > $size);
446
			$size = $newSize;
447
			$TmpFile->close();
448
		}
449
 
450
		$TmpFile->append('');
451
		$this->assertEquals($data, file_get_contents($tmpFile));
452
		$TmpFile->close();
453
	}
454
 
455
/**
456
 * testDelete method
457
 *
458
 * @return void
459
 */
460
	public function testDelete() {
461
		if (!$tmpFile = $this->_getTmpFile()) {
462
			return false;
463
		}
464
 
465
		if (!file_exists($tmpFile)) {
466
			touch($tmpFile);
467
		}
468
		$TmpFile = new File($tmpFile);
469
		$this->assertTrue(file_exists($tmpFile));
470
		$result = $TmpFile->delete();
471
		$this->assertTrue($result);
472
		$this->assertFalse(file_exists($tmpFile));
473
 
474
		$TmpFile = new File('/this/does/not/exist');
475
		$result = $TmpFile->delete();
476
		$this->assertFalse($result);
477
	}
478
 
479
/**
480
 * Windows has issues unlinking files if there are
481
 * active filehandles open.
482
 *
483
 * @return void
484
 */
485
	public function testDeleteAfterRead() {
486
		if (!$tmpFile = $this->_getTmpFile()) {
487
			return false;
488
		}
489
		if (!file_exists($tmpFile)) {
490
			touch($tmpFile);
491
		}
492
		$File = new File($tmpFile);
493
		$File->read();
494
		$this->assertTrue($File->delete());
495
	}
496
 
497
/**
498
 * testCopy method
499
 *
500
 * @return void
501
 */
502
	public function testCopy() {
503
		$dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
504
		$file = __FILE__;
505
		$this->File = new File($file);
506
		$result = $this->File->copy($dest);
507
		$this->assertTrue($result);
508
 
509
		$result = $this->File->copy($dest, true);
510
		$this->assertTrue($result);
511
 
512
		$result = $this->File->copy($dest, false);
513
		$this->assertFalse($result);
514
 
515
		$this->File->close();
516
		unlink($dest);
517
 
518
		$TmpFile = new File('/this/does/not/exist');
519
		$result = $TmpFile->copy($dest);
520
		$this->assertFalse($result);
521
 
522
		$TmpFile->close();
523
	}
524
 
525
/**
526
 * Test mime()
527
 *
528
 * @return void
529
 */
530
	public function testMime() {
531
		$this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
532
		$path = CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
533
		$file = new File($path);
534
		$expected = 'image/gif';
535
		if (function_exists('mime_content_type') && false === mime_content_type($file->pwd())) {
536
			$expected = false;
537
		}
538
		$this->assertEquals($expected, $file->mime());
539
	}
540
 
541
/**
542
 * getTmpFile method
543
 *
544
 * @param boolean $paintSkip
545
 * @return void
546
 */
547
	protected function _getTmpFile($paintSkip = true) {
548
		$tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
549
		if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
550
			return $tmpFile;
551
		};
552
 
553
		if ($paintSkip) {
554
			$trace = debug_backtrace();
555
			$caller = $trace[0]['function'];
556
			$shortPath = dirname($tmpFile);
557
 
558
			$message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
559
			$this->markTestSkipped($message);
560
		}
561
		return false;
562
	}
563
}