Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
15403 manish.sha 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
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 (!function_exists('finfo_open') &&
85
			(!function_exists('mime_content_type') ||
86
			function_exists('mime_content_type') &&
87
			mime_content_type($this->File->pwd()) === false)
88
		) {
89
			$expecting['mime'] = false;
90
		}
91
		$this->assertEquals($expecting, $result);
92
 
93
		$result = $this->File->ext();
94
		$expecting = 'txt';
95
		$this->assertEquals($expecting, $result);
96
 
97
		$result = $this->File->name();
98
		$expecting = 'LICENSE';
99
		$this->assertEquals($expecting, $result);
100
 
101
		$result = $this->File->md5();
102
		$expecting = md5_file($file);
103
		$this->assertEquals($expecting, $result);
104
 
105
		$result = $this->File->md5(true);
106
		$expecting = md5_file($file);
107
		$this->assertEquals($expecting, $result);
108
 
109
		$result = $this->File->size();
110
		$expecting = filesize($file);
111
		$this->assertEquals($expecting, $result);
112
 
113
		$result = $this->File->owner();
114
		$expecting = fileowner($file);
115
		$this->assertEquals($expecting, $result);
116
 
117
		$result = $this->File->group();
118
		$expecting = filegroup($file);
119
		$this->assertEquals($expecting, $result);
120
 
121
		$result = $this->File->Folder();
122
		$this->assertInstanceOf('Folder', $result);
123
	}
124
 
125
/**
126
 * testPermission method
127
 *
128
 * @return void
129
 */
130
	public function testPermission() {
131
		$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'File permissions tests not supported on Windows.');
132
 
133
		$dir = TMP . 'tests' . DS . 'permissions' . DS;
134
		$old = umask();
135
 
136
		umask(0002);
137
		$file = $dir . 'permission_' . uniqid();
138
		$expecting = decoct(0664 & ~umask());
139
		$File = new File($file, true);
140
		$result = $File->perms();
141
		$this->assertEquals($expecting, $result);
142
		$File->delete();
143
 
144
		umask(0022);
145
		$file = $dir . 'permission_' . uniqid();
146
		$expecting = decoct(0644 & ~umask());
147
		$File = new File($file, true);
148
		$result = $File->perms();
149
		$this->assertEquals($expecting, $result);
150
		$File->delete();
151
 
152
		umask(0422);
153
		$file = $dir . 'permission_' . uniqid();
154
		$expecting = decoct(0244 & ~umask());
155
		$File = new File($file, true);
156
		$result = $File->perms();
157
		$this->assertEquals($expecting, $result);
158
		$File->delete();
159
 
160
		umask(0444);
161
		$file = $dir . 'permission_' . uniqid();
162
		$expecting = decoct(0222 & ~umask());
163
		$File = new File($file, true);
164
		$result = $File->perms();
165
		$this->assertEquals($expecting, $result);
166
		$File->delete();
167
 
168
		umask($old);
169
	}
170
 
171
/**
172
 * testRead method
173
 *
174
 * @return void
175
 */
176
	public function testRead() {
177
		$file = __FILE__;
178
		$this->File = new File($file);
179
 
180
		$result = $this->File->read();
181
		$expecting = file_get_contents(__FILE__);
182
		$this->assertEquals($expecting, $result);
183
		$this->assertTrue(!is_resource($this->File->handle));
184
 
185
		$this->File->lock = true;
186
		$result = $this->File->read();
187
		$expecting = file_get_contents(__FILE__);
188
		$this->assertEquals(trim($expecting), $result);
189
		$this->File->lock = null;
190
 
191
		$data = $expecting;
192
		$expecting = substr($data, 0, 3);
193
		$result = $this->File->read(3);
194
		$this->assertEquals($expecting, $result);
195
		$this->assertTrue(is_resource($this->File->handle));
196
 
197
		$expecting = substr($data, 3, 3);
198
		$result = $this->File->read(3);
199
		$this->assertEquals($expecting, $result);
200
	}
201
 
202
/**
203
 * testOffset method
204
 *
205
 * @return void
206
 */
207
	public function testOffset() {
208
		$this->File->close();
209
 
210
		$result = $this->File->offset();
211
		$this->assertFalse($result);
212
 
213
		$this->assertFalse(is_resource($this->File->handle));
214
		$success = $this->File->offset(0);
215
		$this->assertTrue($success);
216
		$this->assertTrue(is_resource($this->File->handle));
217
 
218
		$result = $this->File->offset();
219
		$expected = 0;
220
		$this->assertSame($expected, $result);
221
 
222
		$data = file_get_contents(__FILE__);
223
		$success = $this->File->offset(5);
224
		$expected = substr($data, 5, 3);
225
		$result = $this->File->read(3);
226
		$this->assertTrue($success);
227
		$this->assertEquals($expected, $result);
228
 
229
		$result = $this->File->offset();
230
		$expected = 5 + 3;
231
		$this->assertSame($expected, $result);
232
	}
233
 
234
/**
235
 * testOpen method
236
 *
237
 * @return void
238
 */
239
	public function testOpen() {
240
		$this->File->handle = null;
241
 
242
		$r = $this->File->open();
243
		$this->assertTrue(is_resource($this->File->handle));
244
		$this->assertTrue($r);
245
 
246
		$handle = $this->File->handle;
247
		$r = $this->File->open();
248
		$this->assertTrue($r);
249
		$this->assertTrue($handle === $this->File->handle);
250
		$this->assertTrue(is_resource($this->File->handle));
251
 
252
		$r = $this->File->open('r', true);
253
		$this->assertTrue($r);
254
		$this->assertFalse($handle === $this->File->handle);
255
		$this->assertTrue(is_resource($this->File->handle));
256
	}
257
 
258
/**
259
 * testClose method
260
 *
261
 * @return void
262
 */
263
	public function testClose() {
264
		$this->File->handle = null;
265
		$this->assertFalse(is_resource($this->File->handle));
266
		$this->assertTrue($this->File->close());
267
		$this->assertFalse(is_resource($this->File->handle));
268
 
269
		$this->File->handle = fopen(__FILE__, 'r');
270
		$this->assertTrue(is_resource($this->File->handle));
271
		$this->assertTrue($this->File->close());
272
		$this->assertFalse(is_resource($this->File->handle));
273
	}
274
 
275
/**
276
 * testCreate method
277
 *
278
 * @return void
279
 */
280
	public function testCreate() {
281
		$tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
282
		$File = new File($tmpFile, true, 0777);
283
		$this->assertTrue($File->exists());
284
	}
285
 
286
/**
287
 * testOpeningNonExistentFileCreatesIt method
288
 *
289
 * @return void
290
 */
291
	public function testOpeningNonExistentFileCreatesIt() {
292
		$someFile = new File(TMP . 'some_file.txt', false);
293
		$this->assertTrue($someFile->open());
294
		$this->assertEquals('', $someFile->read());
295
		$someFile->close();
296
		$someFile->delete();
297
	}
298
 
299
/**
300
 * testPrepare method
301
 *
302
 * @return void
303
 */
304
	public function testPrepare() {
305
		$string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
306
		if (DS === '\\') {
307
			$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
308
			$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
309
		} else {
310
			$expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
311
		}
312
		$this->assertSame($expected, File::prepare($string));
313
 
314
		$expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
315
		$expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
316
		$this->assertSame($expected, File::prepare($string, true));
317
	}
318
 
319
/**
320
 * testReadable method
321
 *
322
 * @return void
323
 */
324
	public function testReadable() {
325
		$someFile = new File(TMP . 'some_file.txt', false);
326
		$this->assertTrue($someFile->open());
327
		$this->assertTrue($someFile->readable());
328
		$someFile->close();
329
		$someFile->delete();
330
	}
331
 
332
/**
333
 * testWritable method
334
 *
335
 * @return void
336
 */
337
	public function testWritable() {
338
		$someFile = new File(TMP . 'some_file.txt', false);
339
		$this->assertTrue($someFile->open());
340
		$this->assertTrue($someFile->writable());
341
		$someFile->close();
342
		$someFile->delete();
343
	}
344
 
345
/**
346
 * testExecutable method
347
 *
348
 * @return void
349
 */
350
	public function testExecutable() {
351
		$someFile = new File(TMP . 'some_file.txt', false);
352
		$this->assertTrue($someFile->open());
353
		$this->assertFalse($someFile->executable());
354
		$someFile->close();
355
		$someFile->delete();
356
	}
357
 
358
/**
359
 * testLastAccess method
360
 *
361
 * @return void
362
 */
363
	public function testLastAccess() {
364
		$someFile = new File(TMP . 'some_file.txt', false);
365
		$this->assertFalse($someFile->lastAccess());
366
		$this->assertTrue($someFile->open());
367
		$this->assertWithinMargin($someFile->lastAccess(), time(), 2);
368
		$someFile->close();
369
		$someFile->delete();
370
	}
371
 
372
/**
373
 * testLastChange method
374
 *
375
 * @return void
376
 */
377
	public function testLastChange() {
378
		$someFile = new File(TMP . 'some_file.txt', false);
379
		$this->assertFalse($someFile->lastChange());
380
		$this->assertTrue($someFile->open('r+'));
381
		$this->assertWithinMargin($someFile->lastChange(), time(), 2);
382
 
383
		$someFile->write('something');
384
		$this->assertWithinMargin($someFile->lastChange(), time(), 2);
385
 
386
		$someFile->close();
387
		$someFile->delete();
388
	}
389
 
390
/**
391
 * testWrite method
392
 *
393
 * @return void
394
 */
395
	public function testWrite() {
396
		if (!$tmpFile = $this->_getTmpFile()) {
397
			return false;
398
		}
399
		if (file_exists($tmpFile)) {
400
			unlink($tmpFile);
401
		}
402
 
403
		$TmpFile = new File($tmpFile);
404
		$this->assertFalse(file_exists($tmpFile));
405
		$this->assertFalse(is_resource($TmpFile->handle));
406
 
407
		$testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
408
		foreach ($testData as $data) {
409
			$r = $TmpFile->write($data);
410
			$this->assertTrue($r);
411
			$this->assertTrue(file_exists($tmpFile));
412
			$this->assertEquals($data, file_get_contents($tmpFile));
413
			$this->assertTrue(is_resource($TmpFile->handle));
414
			$TmpFile->close();
415
 
416
		}
417
		unlink($tmpFile);
418
	}
419
 
420
/**
421
 * testAppend method
422
 *
423
 * @return void
424
 */
425
	public function testAppend() {
426
		if (!$tmpFile = $this->_getTmpFile()) {
427
			return false;
428
		}
429
		if (file_exists($tmpFile)) {
430
			unlink($tmpFile);
431
		}
432
 
433
		$TmpFile = new File($tmpFile);
434
		$this->assertFalse(file_exists($tmpFile));
435
 
436
		$fragments = array('CakePHP\'s', ' test suite', ' was here ...');
437
		$data = null;
438
		$size = 0;
439
		foreach ($fragments as $fragment) {
440
			$r = $TmpFile->append($fragment);
441
			$this->assertTrue($r);
442
			$this->assertTrue(file_exists($tmpFile));
443
			$data = $data . $fragment;
444
			$this->assertEquals($data, file_get_contents($tmpFile));
445
			$newSize = $TmpFile->size();
446
			$this->assertTrue($newSize > $size);
447
			$size = $newSize;
448
			$TmpFile->close();
449
		}
450
 
451
		$TmpFile->append('');
452
		$this->assertEquals($data, file_get_contents($tmpFile));
453
		$TmpFile->close();
454
	}
455
 
456
/**
457
 * testDelete method
458
 *
459
 * @return void
460
 */
461
	public function testDelete() {
462
		if (!$tmpFile = $this->_getTmpFile()) {
463
			return false;
464
		}
465
 
466
		if (!file_exists($tmpFile)) {
467
			touch($tmpFile);
468
		}
469
		$TmpFile = new File($tmpFile);
470
		$this->assertTrue(file_exists($tmpFile));
471
		$result = $TmpFile->delete();
472
		$this->assertTrue($result);
473
		$this->assertFalse(file_exists($tmpFile));
474
 
475
		$TmpFile = new File('/this/does/not/exist');
476
		$result = $TmpFile->delete();
477
		$this->assertFalse($result);
478
	}
479
 
480
/**
481
 * Windows has issues unlinking files if there are
482
 * active filehandles open.
483
 *
484
 * @return void
485
 */
486
	public function testDeleteAfterRead() {
487
		if (!$tmpFile = $this->_getTmpFile()) {
488
			return false;
489
		}
490
		if (!file_exists($tmpFile)) {
491
			touch($tmpFile);
492
		}
493
		$File = new File($tmpFile);
494
		$File->read();
495
		$this->assertTrue($File->delete());
496
	}
497
 
498
/**
499
 * testCopy method
500
 *
501
 * @return void
502
 */
503
	public function testCopy() {
504
		$dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
505
		$file = __FILE__;
506
		$this->File = new File($file);
507
		$result = $this->File->copy($dest);
508
		$this->assertTrue($result);
509
 
510
		$result = $this->File->copy($dest, true);
511
		$this->assertTrue($result);
512
 
513
		$result = $this->File->copy($dest, false);
514
		$this->assertFalse($result);
515
 
516
		$this->File->close();
517
		unlink($dest);
518
 
519
		$TmpFile = new File('/this/does/not/exist');
520
		$result = $TmpFile->copy($dest);
521
		$this->assertFalse($result);
522
 
523
		$TmpFile->close();
524
	}
525
 
526
/**
527
 * Test mime()
528
 *
529
 * @return void
530
 */
531
	public function testMime() {
532
		$this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
533
		$path = CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
534
		$file = new File($path);
535
		$expected = 'image/gif';
536
		if (function_exists('mime_content_type') && mime_content_type($file->pwd()) === false) {
537
			$expected = false;
538
		}
539
		$this->assertEquals($expected, $file->mime());
540
	}
541
 
542
/**
543
 * getTmpFile method
544
 *
545
 * @param bool $paintSkip
546
 * @return void
547
 */
548
	protected function _getTmpFile($paintSkip = true) {
549
		$tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
550
		if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
551
			return $tmpFile;
552
		}
553
 
554
		if ($paintSkip) {
555
			$trace = debug_backtrace();
556
			$caller = $trace[0]['function'];
557
			$shortPath = dirname($tmpFile);
558
 
559
			$message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
560
			$this->markTestSkipped($message);
561
		}
562
		return false;
563
	}
564
 
565
/**
566
 * testReplaceText method
567
 *
568
 * @return void
569
 */
570
	public function testReplaceText() {
571
		$TestFile = new File(dirname(__FILE__) . '/../../test_app/Vendor/welcome.php');
572
		$TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp');
573
 
574
		// Copy the test file to the temporary location
575
		$TestFile->copy($TmpFile->path, true);
576
 
577
		// Replace the contents of the tempory file
578
		$result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
579
		$this->assertTrue($result);
580
 
581
		// Double check
582
		$expected = 'This is the welcome.tmp file in vendors directory';
583
		$contents = $TmpFile->read();
584
		$this->assertContains($expected, $contents);
585
 
586
		$search = array('This is the', 'welcome.php file', 'in tmp directory');
587
		$replace = array('This should be a', 'welcome.tmp file', 'in the Lib directory');
588
 
589
		// Replace the contents of the tempory file
590
		$result = $TmpFile->replaceText($search, $replace);
591
		$this->assertTrue($result);
592
 
593
		// Double check
594
		$expected = 'This should be a welcome.tmp file in vendors directory';
595
		$contents = $TmpFile->read();
596
		$this->assertContains($expected, $contents);
597
 
598
		$TmpFile->delete();
599
	}
600
 
601
/**
602
 * Tests that no path is being set for passed file paths that
603
 * do not exist.
604
 *
605
 * @return void
606
 */
607
	public function testNoPartialPathBeingSetForNonExistentPath() {
608
		$tmpFile = new File('/non/existent/file');
609
		$this->assertNull($tmpFile->pwd());
610
		$this->assertNull($tmpFile->path);
611
	}
612
}