統計
| ブランチ: | リビジョン:

pictcode / lib / Cake / Test / Case / Utility / FileTest.php @ 635eef61

履歴 | 表示 | アノテート | ダウンロード (15.834 KB)

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
 * Tests the exists() method.
306
 *
307
 * @return void
308
 */
309
        public function testExists() {
310
                $tmpFile = TMP . 'tests/cakephp.file.test.tmp';
311
                $file = new File($tmpFile, true, 0777);
312
                $this->assertTrue($file->exists(), 'absolute path should exist');
313

    
314
                $file = new File('file://' . $tmpFile, false);
315
                $this->assertTrue($file->exists(), 'file:// should exist.');
316

    
317
                $file = new File('/something/bad', false);
318
                $this->assertFalse($file->exists(), 'missing file should not exist.');
319
        }
320

    
321
/**
322
 * testOpeningNonExistentFileCreatesIt method
323
 *
324
 * @return void
325
 * @covers ::open
326
 * @covers ::create
327
 */
328
        public function testOpeningNonExistentFileCreatesIt() {
329
                $someFile = new File(TMP . 'some_file.txt', false);
330
                $this->assertTrue($someFile->open());
331
                $this->assertEquals('', $someFile->read());
332
                $someFile->close();
333
                $someFile->delete();
334
        }
335

    
336
/**
337
 * testPrepare method
338
 *
339
 * @return void
340
 * @covers ::prepare
341
 */
342
        public function testPrepare() {
343
                $string = "some\nvery\ncool\r\nteststring here\n\n\nfor\r\r\n\n\r\n\nhere";
344
                if (DS === '\\') {
345
                        $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
346
                        $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
347
                } else {
348
                        $expected = "some\nvery\ncool\nteststring here\n\n\nfor\n\n\n\n\nhere";
349
                }
350
                $this->assertSame($expected, File::prepare($string));
351

    
352
                $expected = "some\r\nvery\r\ncool\r\nteststring here\r\n\r\n\r\n";
353
                $expected .= "for\r\n\r\n\r\n\r\n\r\nhere";
354
                $this->assertSame($expected, File::prepare($string, true));
355
        }
356

    
357
/**
358
 * testReadable method
359
 *
360
 * @return void
361
 * @covers ::readable
362
 */
363
        public function testReadable() {
364
                $someFile = new File(TMP . 'some_file.txt', false);
365
                $this->assertTrue($someFile->open());
366
                $this->assertTrue($someFile->readable());
367
                $someFile->close();
368
                $someFile->delete();
369
        }
370

    
371
/**
372
 * testWritable method
373
 *
374
 * @return void
375
 * @covers ::writable
376
 */
377
        public function testWritable() {
378
                $someFile = new File(TMP . 'some_file.txt', false);
379
                $this->assertTrue($someFile->open());
380
                $this->assertTrue($someFile->writable());
381
                $someFile->close();
382
                $someFile->delete();
383
        }
384

    
385
/**
386
 * testExecutable method
387
 *
388
 * @return void
389
 * @covers ::executable
390
 */
391
        public function testExecutable() {
392
                $someFile = new File(TMP . 'some_file.txt', false);
393
                $this->assertTrue($someFile->open());
394
                $this->assertFalse($someFile->executable());
395
                $someFile->close();
396
                $someFile->delete();
397
        }
398

    
399
/**
400
 * testLastAccess method
401
 *
402
 * @return void
403
 * @covers ::lastAccess
404
 */
405
        public function testLastAccess() {
406
                $someFile = new File(TMP . 'some_file.txt', false);
407
                $this->assertFalse($someFile->lastAccess());
408
                $this->assertTrue($someFile->open());
409
                $this->assertWithinMargin($someFile->lastAccess(), time(), 2);
410
                $someFile->close();
411
                $someFile->delete();
412
        }
413

    
414
/**
415
 * testLastChange method
416
 *
417
 * @return void
418
 * @covers ::lastChange
419
 */
420
        public function testLastChange() {
421
                $someFile = new File(TMP . 'some_file.txt', false);
422
                $this->assertFalse($someFile->lastChange());
423
                $this->assertTrue($someFile->open('r+'));
424
                $this->assertWithinMargin($someFile->lastChange(), time(), 2);
425

    
426
                $someFile->write('something');
427
                $this->assertWithinMargin($someFile->lastChange(), time(), 2);
428

    
429
                $someFile->close();
430
                $someFile->delete();
431
        }
432

    
433
/**
434
 * testWrite method
435
 *
436
 * @return void
437
 * @covers ::write
438
 */
439
        public function testWrite() {
440
                if (!$tmpFile = $this->_getTmpFile()) {
441
                        return false;
442
                }
443
                if (file_exists($tmpFile)) {
444
                        unlink($tmpFile);
445
                }
446

    
447
                $TmpFile = new File($tmpFile);
448
                $this->assertFalse(file_exists($tmpFile));
449
                $this->assertFalse(is_resource($TmpFile->handle));
450

    
451
                $testData = array('CakePHP\'s', ' test suite', ' was here ...', '');
452
                foreach ($testData as $data) {
453
                        $r = $TmpFile->write($data);
454
                        $this->assertTrue($r);
455
                        $this->assertTrue(file_exists($tmpFile));
456
                        $this->assertEquals($data, file_get_contents($tmpFile));
457
                        $this->assertTrue(is_resource($TmpFile->handle));
458
                        $TmpFile->close();
459

    
460
                }
461
                unlink($tmpFile);
462
        }
463

    
464
/**
465
 * testAppend method
466
 *
467
 * @return void
468
 * @covers ::append
469
 */
470
        public function testAppend() {
471
                if (!$tmpFile = $this->_getTmpFile()) {
472
                        return false;
473
                }
474
                if (file_exists($tmpFile)) {
475
                        unlink($tmpFile);
476
                }
477

    
478
                $TmpFile = new File($tmpFile);
479
                $this->assertFalse(file_exists($tmpFile));
480

    
481
                $fragments = array('CakePHP\'s', ' test suite', ' was here ...');
482
                $data = null;
483
                $size = 0;
484
                foreach ($fragments as $fragment) {
485
                        $r = $TmpFile->append($fragment);
486
                        $this->assertTrue($r);
487
                        $this->assertTrue(file_exists($tmpFile));
488
                        $data = $data . $fragment;
489
                        $this->assertEquals($data, file_get_contents($tmpFile));
490
                        $newSize = $TmpFile->size();
491
                        $this->assertTrue($newSize > $size);
492
                        $size = $newSize;
493
                        $TmpFile->close();
494
                }
495

    
496
                $TmpFile->append('');
497
                $this->assertEquals($data, file_get_contents($tmpFile));
498
                $TmpFile->close();
499
        }
500

    
501
/**
502
 * testDelete method
503
 *
504
 * @return void
505
 * @covers ::delete
506
 */
507
        public function testDelete() {
508
                if (!$tmpFile = $this->_getTmpFile()) {
509
                        return false;
510
                }
511

    
512
                if (!file_exists($tmpFile)) {
513
                        touch($tmpFile);
514
                }
515
                $TmpFile = new File($tmpFile);
516
                $this->assertTrue(file_exists($tmpFile));
517
                $result = $TmpFile->delete();
518
                $this->assertTrue($result);
519
                $this->assertFalse(file_exists($tmpFile));
520

    
521
                $TmpFile = new File('/this/does/not/exist');
522
                $result = $TmpFile->delete();
523
                $this->assertFalse($result);
524
        }
525

    
526
/**
527
 * Windows has issues unlinking files if there are
528
 * active filehandles open.
529
 *
530
 * @return void
531
 * @covers ::delete
532
 */
533
        public function testDeleteAfterRead() {
534
                if (!$tmpFile = $this->_getTmpFile()) {
535
                        return false;
536
                }
537
                if (!file_exists($tmpFile)) {
538
                        touch($tmpFile);
539
                }
540
                $File = new File($tmpFile);
541
                $File->read();
542
                $this->assertTrue($File->delete());
543
        }
544

    
545
/**
546
 * testCopy method
547
 *
548
 * @return void
549
 * @covers ::copy
550
 */
551
        public function testCopy() {
552
                $dest = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
553
                $file = __FILE__;
554
                $this->File = new File($file);
555
                $result = $this->File->copy($dest);
556
                $this->assertTrue($result);
557

    
558
                $result = $this->File->copy($dest, true);
559
                $this->assertTrue($result);
560

    
561
                $result = $this->File->copy($dest, false);
562
                $this->assertFalse($result);
563

    
564
                $this->File->close();
565
                unlink($dest);
566

    
567
                $TmpFile = new File('/this/does/not/exist');
568
                $result = $TmpFile->copy($dest);
569
                $this->assertFalse($result);
570

    
571
                $TmpFile->close();
572
        }
573

    
574
/**
575
 * Test mime()
576
 *
577
 * @return void
578
 * @covers ::mime
579
 */
580
        public function testMime() {
581
                $this->skipIf(!function_exists('finfo_open') && !function_exists('mime_content_type'), 'Not able to read mime type');
582
                $path = CAKE . 'Test' . DS . 'test_app' . DS . 'webroot' . DS . 'img' . DS . 'cake.power.gif';
583
                $file = new File($path);
584
                $expected = 'image/gif';
585
                if (function_exists('mime_content_type') && mime_content_type($file->pwd()) === false) {
586
                        $expected = false;
587
                }
588
                $this->assertEquals($expected, $file->mime());
589
        }
590

    
591
/**
592
 * getTmpFile method
593
 *
594
 * @param bool $paintSkip
595
 * @return void
596
 */
597
        protected function _getTmpFile($paintSkip = true) {
598
                $tmpFile = TMP . 'tests' . DS . 'cakephp.file.test.tmp';
599
                if (is_writable(dirname($tmpFile)) && (!file_exists($tmpFile) || is_writable($tmpFile))) {
600
                        return $tmpFile;
601
                }
602

    
603
                if ($paintSkip) {
604
                        $trace = debug_backtrace();
605
                        $caller = $trace[0]['function'];
606
                        $shortPath = dirname($tmpFile);
607

    
608
                        $message = __d('cake_dev', '[FileTest] Skipping %s because "%s" not writeable!', $caller, $shortPath);
609
                        $this->markTestSkipped($message);
610
                }
611
                return false;
612
        }
613

    
614
/**
615
 * testReplaceText method
616
 *
617
 * @return void
618
 * @covers ::replaceText
619
 */
620
        public function testReplaceText() {
621
                $TestFile = new File(dirname(__FILE__) . '/../../test_app/Vendor/welcome.php');
622
                $TmpFile = new File(TMP . 'tests' . DS . 'cakephp.file.test.tmp');
623

    
624
                // Copy the test file to the temporary location
625
                $TestFile->copy($TmpFile->path, true);
626

    
627
                // Replace the contents of the tempory file
628
                $result = $TmpFile->replaceText('welcome.php', 'welcome.tmp');
629
                $this->assertTrue($result);
630

    
631
                // Double check
632
                $expected = 'This is the welcome.tmp file in vendors directory';
633
                $contents = $TmpFile->read();
634
                $this->assertContains($expected, $contents);
635

    
636
                $search = array('This is the', 'welcome.php file', 'in tmp directory');
637
                $replace = array('This should be a', 'welcome.tmp file', 'in the Lib directory');
638

    
639
                // Replace the contents of the tempory file
640
                $result = $TmpFile->replaceText($search, $replace);
641
                $this->assertTrue($result);
642

    
643
                // Double check
644
                $expected = 'This should be a welcome.tmp file in vendors directory';
645
                $contents = $TmpFile->read();
646
                $this->assertContains($expected, $contents);
647

    
648
                $TmpFile->delete();
649
        }
650

    
651
/**
652
 * Tests that no path is being set for passed file paths that
653
 * do not exist.
654
 *
655
 * @return void
656
 * @covers ::pwd
657
 */
658
        public function testNoPartialPathBeingSetForNonExistentPath() {
659
                $tmpFile = new File('/non/existent/file');
660
                $this->assertNull($tmpFile->pwd());
661
                $this->assertNull($tmpFile->path);
662
        }
663
}