Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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