Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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