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

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

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

1
<?php
2
/**
3
 * FolderTest 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('Folder', 'Utility');
20
App::uses('File', 'Utility');
21

    
22
/**
23
 * FolderTest class
24
 *
25
 * @package       Cake.Test.Case.Utility
26
 */
27
class FolderTest extends CakeTestCase {
28

    
29
        protected static $_tmp = array();
30

    
31
/**
32
 * Save the directory names in TMP and make sure default directories exist
33
 *
34
 * @return void
35
 */
36
        public static function setUpBeforeClass() {
37
                $dirs = array('cache', 'logs', 'sessions', 'tests');
38
                foreach ($dirs as $dir) {
39
                        new Folder(TMP . $dir, true);
40
                }
41

    
42
                foreach (scandir(TMP) as $file) {
43
                        if (is_dir(TMP . $file) && !in_array($file, array('.', '..'))) {
44
                                static::$_tmp[] = $file;
45
                        }
46
                }
47
        }
48

    
49
/**
50
 * setUp clearstatcache() to flush file descriptors.
51
 *
52
 * @return void
53
 */
54
        public function setUp() {
55
                parent::setUp();
56
                clearstatcache();
57
        }
58

    
59
/**
60
 * Restore the TMP directory to its original state.
61
 *
62
 * @return void
63
 */
64
        public function tearDown() {
65
                $exclude = array_merge(static::$_tmp, array('.', '..'));
66
                foreach (scandir(TMP) as $dir) {
67
                        if (is_dir(TMP . $dir) && !in_array($dir, $exclude)) {
68
                                $iterator = new RecursiveDirectoryIterator(TMP . $dir);
69
                                foreach (new RecursiveIteratorIterator($iterator, RecursiveIteratorIterator::CHILD_FIRST) as $file) {
70
                                        if ($file->isFile() || $file->isLink()) {
71
                                                unlink($file->getPathname());
72
                                        } elseif ($file->isDir() && !in_array($file->getFilename(), array('.', '..'))) {
73
                                                rmdir($file->getPathname());
74
                                        }
75
                                }
76
                                rmdir(TMP . $dir);
77
                        }
78
                }
79
                parent::tearDown();
80
        }
81

    
82
/**
83
 * testBasic method
84
 *
85
 * @return void
86
 */
87
        public function testBasic() {
88
                $path = dirname(__FILE__);
89
                $Folder = new Folder($path);
90

    
91
                $result = $Folder->pwd();
92
                $this->assertEquals($path, $result);
93

    
94
                $result = Folder::addPathElement($path, 'test');
95
                $expected = $path . DS . 'test';
96
                $this->assertEquals($expected, $result);
97

    
98
                $result = $Folder->cd(ROOT);
99
                $expected = ROOT;
100
                $this->assertEquals($expected, $result);
101

    
102
                $result = $Folder->cd(ROOT . DS . 'non-existent');
103
                $this->assertFalse($result);
104
        }
105

    
106
/**
107
 * testInPath method
108
 *
109
 * @return void
110
 */
111
        public function testInPath() {
112
                $path = dirname(dirname(__FILE__));
113
                $inside = dirname($path) . DS;
114

    
115
                $Folder = new Folder($path);
116

    
117
                $result = $Folder->pwd();
118
                $this->assertEquals($path, $result);
119

    
120
                $result = Folder::isSlashTerm($inside);
121
                $this->assertTrue($result);
122

    
123
                $result = $Folder->realpath('Test/');
124
                $this->assertEquals($path . DS . 'Test' . DS, $result);
125

    
126
                $result = $Folder->inPath('Test' . DS);
127
                $this->assertTrue($result);
128

    
129
                $result = $Folder->inPath(DS . 'non-existing' . $inside);
130
                $this->assertFalse($result);
131

    
132
                $result = $Folder->inPath($path . DS . 'Model', true);
133
                $this->assertTrue($result);
134
        }
135

    
136
/**
137
 * test creation of single and multiple paths.
138
 *
139
 * @return void
140
 */
141
        public function testCreation() {
142
                $Folder = new Folder(TMP . 'tests');
143
                $result = $Folder->create(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
144
                $this->assertTrue($result);
145

    
146
                rmdir(TMP . 'tests' . DS . 'first' . DS . 'second' . DS . 'third');
147
                rmdir(TMP . 'tests' . DS . 'first' . DS . 'second');
148
                rmdir(TMP . 'tests' . DS . 'first');
149

    
150
                $Folder = new Folder(TMP . 'tests');
151
                $result = $Folder->create(TMP . 'tests' . DS . 'first');
152
                $this->assertTrue($result);
153
                rmdir(TMP . 'tests' . DS . 'first');
154
        }
155

    
156
/**
157
 * test that creation of folders with trailing ds works
158
 *
159
 * @return void
160
 */
161
        public function testCreateWithTrailingDs() {
162
                $Folder = new Folder(TMP);
163
                $path = TMP . 'tests' . DS . 'trailing' . DS . 'dir' . DS;
164
                $result = $Folder->create($path);
165
                $this->assertTrue($result);
166

    
167
                $this->assertTrue(is_dir($path), 'Folder was not made');
168

    
169
                $Folder = new Folder(TMP . 'tests' . DS . 'trailing');
170
                $this->assertTrue($Folder->delete());
171
        }
172

    
173
/**
174
 * Test that relative paths to create() are added to cwd.
175
 *
176
 * @return void
177
 */
178
        public function testCreateRelative() {
179
                $folder = new Folder(TMP);
180
                $path = TMP . 'tests' . DS . 'relative-test';
181
                $result = $folder->create('tests' . DS . 'relative-test');
182
                $this->assertTrue($result, 'should create');
183

    
184
                $this->assertTrue(is_dir($path), 'Folder was not made');
185
                $folder = new Folder($path);
186
                $folder->delete();
187
        }
188

    
189
/**
190
 * test recursive directory create failure.
191
 *
192
 * @return void
193
 */
194
        public function testRecursiveCreateFailure() {
195
                $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Cant perform operations using permissions on Windows.');
196

    
197
                $path = TMP . 'tests' . DS . 'one';
198
                mkdir($path);
199
                chmod($path, '0444');
200

    
201
                try {
202
                        $Folder = new Folder($path);
203
                        $result = $Folder->create($path . DS . 'two' . DS . 'three');
204
                        $this->assertFalse($result);
205
                } catch (PHPUnit_Framework_Error $e) {
206
                        $this->assertTrue(true);
207
                }
208

    
209
                chmod($path, '0777');
210
                rmdir($path);
211
        }
212

    
213
/**
214
 * testOperations method
215
 *
216
 * @return void
217
 */
218
        public function testOperations() {
219
                $path = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
220
                $Folder = new Folder($path);
221

    
222
                $result = is_dir($Folder->pwd());
223
                $this->assertTrue($result);
224

    
225
                $new = TMP . 'test_folder_new';
226
                $result = $Folder->create($new);
227
                $this->assertTrue($result);
228

    
229
                $copy = TMP . 'test_folder_copy';
230
                $result = $Folder->copy($copy);
231
                $this->assertTrue($result);
232

    
233
                $copy = TMP . 'test_folder_copy';
234
                $result = $Folder->copy($copy);
235
                $this->assertTrue($result);
236

    
237
                $copy = TMP . 'test_folder_copy';
238
                $result = $Folder->chmod($copy, 0755, false);
239
                $this->assertTrue($result);
240

    
241
                $result = $Folder->cd($copy);
242
                $this->assertTrue((bool)$result);
243

    
244
                $mv = TMP . 'test_folder_mv';
245
                $result = $Folder->move($mv);
246
                $this->assertTrue($result);
247

    
248
                $mv = TMP . 'test_folder_mv_2';
249
                $result = $Folder->move($mv);
250
                $this->assertTrue($result);
251

    
252
                $result = $Folder->delete($new);
253
                $this->assertTrue($result);
254

    
255
                $result = $Folder->delete($mv);
256
                $this->assertTrue($result);
257

    
258
                $result = $Folder->delete($mv);
259
                $this->assertTrue($result);
260

    
261
                $new = APP . 'index.php';
262
                $result = $Folder->create($new);
263
                $this->assertFalse($result);
264

    
265
                $expected = $new . ' is a file';
266
                $result = $Folder->errors();
267
                $this->assertEquals($expected, $result[0]);
268

    
269
                $new = TMP . 'test_folder_new';
270
                $result = $Folder->create($new);
271
                $this->assertTrue($result);
272

    
273
                $result = $Folder->cd($new);
274
                $this->assertTrue((bool)$result);
275

    
276
                $result = $Folder->delete();
277
                $this->assertTrue($result);
278

    
279
                $Folder = new Folder('non-existent');
280
                $result = $Folder->pwd();
281
                $this->assertNull($result);
282
        }
283

    
284
/**
285
 * testChmod method
286
 *
287
 * @return void
288
 */
289
        public function testChmod() {
290
                $this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Folder permissions tests not supported on Windows.');
291

    
292
                $path = TMP;
293
                $Folder = new Folder($path);
294

    
295
                $subdir = 'test_folder_new';
296
                $new = TMP . $subdir;
297

    
298
                $this->assertTrue($Folder->create($new));
299
                $this->assertTrue($Folder->create($new . DS . 'test1'));
300
                $this->assertTrue($Folder->create($new . DS . 'test2'));
301

    
302
                $filePath = $new . DS . 'test1.php';
303
                $File = new File($filePath);
304
                $this->assertTrue($File->create());
305

    
306
                $filePath = $new . DS . 'skip_me.php';
307
                $File = new File($filePath);
308
                $this->assertTrue($File->create());
309

    
310
                $this->assertTrue($Folder->chmod($new, 0755, true));
311
                $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
312
                $this->assertEquals('0755', $perms);
313

    
314
                $this->assertTrue($Folder->chmod($new, 0744, true, array('skip_me.php', 'test2')));
315

    
316
                $perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
317
                $this->assertEquals('0755', $perms);
318

    
319
                $perms = substr(sprintf('%o', fileperms($new . DS . 'test1')), -4);
320
                $this->assertEquals('0744', $perms);
321

    
322
                $Folder->delete($new);
323
        }
324

    
325
/**
326
 * testRealPathForWebroot method
327
 *
328
 * @return void
329
 */
330
        public function testRealPathForWebroot() {
331
                $Folder = new Folder('files/');
332
                $this->assertEquals(realpath('files/'), $Folder->path);
333
        }
334

    
335
/**
336
 * testZeroAsDirectory method
337
 *
338
 * @return void
339
 */
340
        public function testZeroAsDirectory() {
341
                $Folder = new Folder(TMP);
342
                $new = TMP . '0';
343
                $this->assertTrue($Folder->create($new));
344

    
345
                $result = $Folder->read(true, true);
346
                $expected = array('0', 'cache', 'logs', 'sessions', 'tests');
347
                $this->assertEquals($expected, $result[0]);
348

    
349
                $result = $Folder->read(true, array('logs'));
350
                $expected = array('0', 'cache', 'sessions', 'tests');
351
                $this->assertEquals($expected, $result[0]);
352

    
353
                $result = $Folder->delete($new);
354
                $this->assertTrue($result);
355
        }
356

    
357
/**
358
 * test Adding path elements to a path
359
 *
360
 * @return void
361
 */
362
        public function testAddPathElement() {
363
                $expected = DS . 'some' . DS . 'dir' . DS . 'another_path';
364

    
365
                $result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
366
                $this->assertEquals($expected, $result);
367

    
368
                $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
369
                $this->assertEquals($expected, $result);
370

    
371
                $result = Folder::addPathElement(DS . 'some' . DS . 'dir', array('another_path'));
372
                $this->assertEquals($expected, $result);
373

    
374
                $result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, array('another_path'));
375
                $this->assertEquals($expected, $result);
376

    
377
                $expected = DS . 'some' . DS . 'dir' . DS . 'another_path' . DS . 'and' . DS . 'another';
378

    
379
                $result = Folder::addPathElement(DS . 'some' . DS . 'dir', array('another_path', 'and', 'another'));
380
                $this->assertEquals($expected, $result);
381
        }
382

    
383
/**
384
 * testFolderRead method
385
 *
386
 * @return void
387
 */
388
        public function testFolderRead() {
389
                $Folder = new Folder(TMP);
390

    
391
                $expected = array('cache', 'logs', 'sessions', 'tests');
392
                $result = $Folder->read(true, true);
393
                $this->assertEquals($expected, $result[0]);
394

    
395
                $Folder->path = TMP . 'non-existent';
396
                $expected = array(array(), array());
397
                $result = $Folder->read(true, true);
398
                $this->assertEquals($expected, $result);
399
        }
400

    
401
/**
402
 * testFolderReadWithHiddenFiles method
403
 *
404
 * @return void
405
 */
406
        public function testFolderReadWithHiddenFiles() {
407
                $this->skipIf(!is_writable(TMP), 'Cant test Folder::read with hidden files unless the tmp folder is writable.');
408

    
409
                $Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
410
                mkdir($Folder->path . DS . '.svn');
411
                mkdir($Folder->path . DS . 'some_folder');
412
                touch($Folder->path . DS . 'not_hidden.txt');
413
                touch($Folder->path . DS . '.hidden.txt');
414

    
415
                $expected = array(
416
                        array('some_folder'),
417
                        array('not_hidden.txt'),
418
                );
419
                $result = $Folder->read(true, true);
420
                $this->assertEquals($expected, $result);
421

    
422
                $expected = array(
423
                        array(
424
                                '.svn',
425
                                'some_folder'
426
                        ),
427
                        array(
428
                                '.hidden.txt',
429
                                'not_hidden.txt'
430
                        ),
431
                );
432
                $result = $Folder->read(true);
433
                $this->assertEquals($expected, $result);
434
        }
435

    
436
/**
437
 * testFolderTree method
438
 *
439
 * @return void
440
 */
441
        public function testFolderTree() {
442
                $Folder = new Folder();
443
                $expected = array(
444
                        array(
445
                                CAKE . 'Config',
446
                                CAKE . 'Config' . DS . 'unicode',
447
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding'
448
                        ),
449
                        array(
450
                                CAKE . 'Config' . DS . 'config.php',
451
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0080_00ff.php',
452
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0100_017f.php',
453
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0180_024F.php',
454
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0250_02af.php',
455
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0370_03ff.php',
456
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0400_04ff.php',
457
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0500_052f.php',
458
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0530_058f.php',
459
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '1e00_1eff.php',
460
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '1f00_1fff.php',
461
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2100_214f.php',
462
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2150_218f.php',
463
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2460_24ff.php',
464
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c00_2c5f.php',
465
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c60_2c7f.php',
466
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c80_2cff.php',
467
                                CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . 'ff00_ffef.php'
468
                        )
469
                );
470

    
471
                $result = $Folder->tree(CAKE . 'Config', false);
472
                $this->assertSame(array(), array_diff($expected[0], $result[0]));
473
                $this->assertSame(array(), array_diff($result[0], $expected[0]));
474

    
475
                $result = $Folder->tree(CAKE . 'Config', false, 'dir');
476
                $this->assertSame(array(), array_diff($expected[0], $result));
477
                $this->assertSame(array(), array_diff($expected[0], $result));
478

    
479
                $result = $Folder->tree(CAKE . 'Config', false, 'files');
480
                $this->assertSame(array(), array_diff($expected[1], $result));
481
                $this->assertSame(array(), array_diff($expected[1], $result));
482
        }
483

    
484
/**
485
 * testFolderTreeWithHiddenFiles method
486
 *
487
 * @return void
488
 */
489
        public function testFolderTreeWithHiddenFiles() {
490
                $this->skipIf(!is_writable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
491

    
492
                $Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
493
                mkdir($Folder->path . DS . '.svn', 0777, true);
494
                touch($Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php');
495
                mkdir($Folder->path . DS . '.svn' . DS . 'inhiddenfolder');
496
                touch($Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php');
497
                touch($Folder->path . DS . 'not_hidden.txt');
498
                touch($Folder->path . DS . '.hidden.txt');
499
                mkdir($Folder->path . DS . 'visible_folder' . DS . '.git', 0777, true);
500

    
501
                $expected = array(
502
                        array(
503
                                $Folder->path,
504
                                $Folder->path . DS . 'visible_folder',
505
                        ),
506
                        array(
507
                                $Folder->path . DS . 'not_hidden.txt',
508
                        ),
509
                );
510

    
511
                $result = $Folder->tree(null, true);
512
                $this->assertEquals($expected, $result);
513

    
514
                $result = $Folder->tree(null, array('.'));
515
                $this->assertEquals($expected, $result);
516

    
517
                $expected = array(
518
                        array(
519
                                $Folder->path,
520
                                $Folder->path . DS . 'visible_folder',
521
                                $Folder->path . DS . 'visible_folder' . DS . '.git',
522
                                $Folder->path . DS . '.svn',
523
                                $Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
524
                        ),
525
                        array(
526
                                $Folder->path . DS . 'not_hidden.txt',
527
                                $Folder->path . DS . '.hidden.txt',
528
                                $Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
529
                                $Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
530
                        ),
531
                );
532

    
533
                $result = $Folder->tree(null, false);
534
                sort($result[0]);
535
                sort($expected[0]);
536
                sort($result[1]);
537
                sort($expected[1]);
538
                $this->assertEquals($expected, $result);
539

    
540
                $Folder->delete();
541
        }
542

    
543
/**
544
 * testWindowsPath method
545
 *
546
 * @return void
547
 */
548
        public function testWindowsPath() {
549
                $this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
550
                $this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
551
                $this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
552
                $this->assertTrue(Folder::isWindowsPath('\\\\vmware-host\\Shared Folders\\file'));
553
        }
554

    
555
/**
556
 * testIsAbsolute method
557
 *
558
 * @return void
559
 */
560
        public function testIsAbsolute() {
561
                $this->assertFalse(Folder::isAbsolute('path/to/file'));
562
                $this->assertFalse(Folder::isAbsolute('cake/'));
563
                $this->assertFalse(Folder::isAbsolute('path\\to\\file'));
564
                $this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
565
                $this->assertFalse(Folder::isAbsolute('\\path/to/file'));
566
                $this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
567
                $this->assertFalse(Folder::isAbsolute('notRegisteredStreamWrapper://example'));
568
                $this->assertFalse(Folder::isAbsolute('://example'));
569

    
570
                $this->assertTrue(Folder::isAbsolute('/usr/local'));
571
                $this->assertTrue(Folder::isAbsolute('//path/to/file'));
572
                $this->assertTrue(Folder::isAbsolute('C:\\cake'));
573
                $this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
574
                $this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
575
                $this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
576
                $this->assertTrue(Folder::isAbsolute('http://www.example.com'));
577
        }
578

    
579
/**
580
 * testIsSlashTerm method
581
 *
582
 * @return void
583
 */
584
        public function testIsSlashTerm() {
585
                $this->assertFalse(Folder::isSlashTerm('cake'));
586

    
587
                $this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
588
                $this->assertTrue(Folder::isSlashTerm('/usr/local/'));
589
        }
590

    
591
/**
592
 * testStatic method
593
 *
594
 * @return void
595
 */
596
        public function testSlashTerm() {
597
                $result = Folder::slashTerm('/path/to/file');
598
                $this->assertEquals('/path/to/file/', $result);
599
        }
600

    
601
/**
602
 * testNormalizePath method
603
 *
604
 * @return void
605
 */
606
        public function testNormalizePath() {
607
                $path = '/path/to/file';
608
                $result = Folder::normalizePath($path);
609
                $this->assertEquals('/', $result);
610

    
611
                $path = '\\path\\\to\\\file';
612
                $result = Folder::normalizePath($path);
613
                $this->assertEquals('/', $result);
614

    
615
                $path = 'C:\\path\\to\\file';
616
                $result = Folder::normalizePath($path);
617
                $this->assertEquals('\\', $result);
618
        }
619

    
620
/**
621
 * correctSlashFor method
622
 *
623
 * @return void
624
 */
625
        public function testCorrectSlashFor() {
626
                $path = '/path/to/file';
627
                $result = Folder::correctSlashFor($path);
628
                $this->assertEquals('/', $result);
629

    
630
                $path = '\\path\\to\\file';
631
                $result = Folder::correctSlashFor($path);
632
                $this->assertEquals('/', $result);
633

    
634
                $path = 'C:\\path\to\\file';
635
                $result = Folder::correctSlashFor($path);
636
                $this->assertEquals('\\', $result);
637
        }
638

    
639
/**
640
 * testInCakePath method
641
 *
642
 * @return void
643
 */
644
        public function testInCakePath() {
645
                $Folder = new Folder();
646
                $Folder->cd(ROOT);
647
                $path = 'C:\\path\\to\\file';
648
                $result = $Folder->inCakePath($path);
649
                $this->assertFalse($result);
650

    
651
                $path = ROOT;
652
                $Folder->cd(ROOT);
653
                $result = $Folder->inCakePath($path);
654
                $this->assertFalse($result);
655

    
656
                $path = DS . 'lib' . DS . 'Cake' . DS . 'Config';
657
                $Folder->cd(ROOT . DS . 'lib' . DS . 'Cake' . DS . 'Config');
658
                $result = $Folder->inCakePath($path);
659
                $this->assertTrue($result);
660
        }
661

    
662
/**
663
 * testFind method
664
 *
665
 * @return void
666
 */
667
        public function testFind() {
668
                $Folder = new Folder();
669
                $Folder->cd(CAKE . 'Config');
670
                $result = $Folder->find();
671
                $expected = array('config.php');
672
                $this->assertSame(array_diff($expected, $result), array());
673
                $this->assertSame(array_diff($expected, $result), array());
674

    
675
                $result = $Folder->find('.*', true);
676
                $expected = array('cacert.pem', 'config.php', 'routes.php');
677
                $this->assertSame($expected, $result);
678

    
679
                $result = $Folder->find('.*\.php');
680
                $expected = array('config.php');
681
                $this->assertSame(array_diff($expected, $result), array());
682
                $this->assertSame(array_diff($expected, $result), array());
683

    
684
                $result = $Folder->find('.*\.php', true);
685
                $expected = array('config.php', 'routes.php');
686
                $this->assertSame($expected, $result);
687

    
688
                $result = $Folder->find('.*ig\.php');
689
                $expected = array('config.php');
690
                $this->assertSame($expected, $result);
691

    
692
                $result = $Folder->find('config\.php');
693
                $expected = array('config.php');
694
                $this->assertSame($expected, $result);
695

    
696
                $Folder->cd(TMP);
697
                $File = new File($Folder->pwd() . DS . 'paths.php', true);
698
                $Folder->create($Folder->pwd() . DS . 'testme');
699
                $Folder->cd('testme');
700
                $result = $Folder->find('paths\.php');
701
                $expected = array();
702
                $this->assertSame($expected, $result);
703

    
704
                $Folder->cd($Folder->pwd() . '/..');
705
                $result = $Folder->find('paths\.php');
706
                $expected = array('paths.php');
707
                $this->assertSame($expected, $result);
708

    
709
                $Folder->cd(TMP);
710
                $Folder->delete($Folder->pwd() . DS . 'testme');
711
                $File->delete();
712
        }
713

    
714
/**
715
 * testFindRecursive method
716
 *
717
 * @return void
718
 */
719
        public function testFindRecursive() {
720
                $Folder = new Folder();
721
                $Folder->cd(CAKE);
722
                $result = $Folder->findRecursive('(config|paths)\.php');
723
                $expected = array(
724
                        CAKE . 'Config' . DS . 'config.php'
725
                );
726
                $this->assertSame(array_diff($expected, $result), array());
727
                $this->assertSame(array_diff($expected, $result), array());
728

    
729
                $result = $Folder->findRecursive('(config|paths)\.php', true);
730
                $expected = array(
731
                        CAKE . 'Config' . DS . 'config.php'
732
                );
733
                $this->assertSame($expected, $result);
734

    
735
                $Folder->cd(TMP);
736
                $Folder->create($Folder->pwd() . DS . 'testme');
737
                $Folder->cd('testme');
738
                $File = new File($Folder->pwd() . DS . 'paths.php');
739
                $File->create();
740
                $Folder->cd(TMP . 'sessions');
741
                $result = $Folder->findRecursive('paths\.php');
742
                $expected = array();
743
                $this->assertSame($expected, $result);
744

    
745
                $Folder->cd(TMP . 'testme');
746
                $File = new File($Folder->pwd() . DS . 'my.php');
747
                $File->create();
748
                $Folder->cd($Folder->pwd() . '/../..');
749

    
750
                $result = $Folder->findRecursive('(paths|my)\.php');
751
                $expected = array(
752
                        TMP . 'testme' . DS . 'my.php',
753
                        TMP . 'testme' . DS . 'paths.php'
754
                );
755
                $this->assertSame(array_diff($expected, $result), array());
756
                $this->assertSame(array_diff($expected, $result), array());
757

    
758
                $result = $Folder->findRecursive('(paths|my)\.php', true);
759
                $expected = array(
760
                        TMP . 'testme' . DS . 'my.php',
761
                        TMP . 'testme' . DS . 'paths.php'
762
                );
763
                $this->assertSame($expected, $result);
764

    
765
                $Folder->cd(CAKE . 'Config');
766
                $Folder->cd(TMP);
767
                $Folder->delete($Folder->pwd() . DS . 'testme');
768
                $File->delete();
769
        }
770

    
771
/**
772
 * testConstructWithNonExistentPath method
773
 *
774
 * @return void
775
 */
776
        public function testConstructWithNonExistentPath() {
777
                $Folder = new Folder(TMP . 'config_non_existent', true);
778
                $this->assertTrue(is_dir(TMP . 'config_non_existent'));
779
                $Folder->cd(TMP);
780
                $Folder->delete($Folder->pwd() . 'config_non_existent');
781
        }
782

    
783
/**
784
 * testDirSize method
785
 *
786
 * @return void
787
 */
788
        public function testDirSize() {
789
                $Folder = new Folder(TMP . 'config_non_existent', true);
790
                $this->assertEquals(0, $Folder->dirSize());
791

    
792
                $File = new File($Folder->pwd() . DS . 'my.php', true, 0777);
793
                $File->create();
794
                $File->write('something here');
795
                $File->close();
796
                $this->assertEquals(14, $Folder->dirSize());
797

    
798
                $Folder->cd(TMP);
799
                $Folder->delete($Folder->pwd() . 'config_non_existent');
800
        }
801

    
802
/**
803
 * test that errors and messages can be resetted
804
 *
805
 * @return void
806
 */
807
        public function testReset() {
808
                $path = TMP . 'folder_delete_test';
809
                mkdir($path);
810
                $folder = $path . DS . 'sub';
811
                mkdir($folder);
812
                $file = $folder . DS . 'file';
813
                touch($file);
814

    
815
                chmod($folder, 0555);
816
                chmod($file, 0444);
817

    
818
                $Folder = new Folder($folder);
819
                $return = $Folder->delete();
820
                $this->assertFalse($return);
821

    
822
                $messages = $Folder->messages();
823
                $errors = $Folder->errors();
824
                $expected = array(
825
                        $file . ' NOT removed',
826
                        $folder . ' NOT removed',
827
                );
828
                sort($expected);
829
                sort($errors);
830
                $this->assertEmpty($messages);
831
                $this->assertEquals($expected, $errors);
832

    
833
                chmod($file, 0644);
834
                chmod($folder, 0755);
835

    
836
                $return = $Folder->delete();
837
                $this->assertTrue($return);
838

    
839
                $messages = $Folder->messages();
840
                $errors = $Folder->errors();
841
                $expected = array(
842
                        $file . ' removed',
843
                        $folder . ' removed',
844
                );
845
                sort($expected);
846
                sort($messages);
847
                $this->assertEmpty($errors);
848
                $this->assertEquals($expected, $messages);
849
        }
850

    
851
/**
852
 * testDelete method
853
 *
854
 * @return void
855
 */
856
        public function testDelete() {
857
                $path = TMP . 'folder_delete_test';
858
                mkdir($path);
859
                touch($path . DS . 'file_1');
860
                mkdir($path . DS . 'level_1_1');
861
                touch($path . DS . 'level_1_1' . DS . 'file_1_1');
862
                mkdir($path . DS . 'level_1_1' . DS . 'level_2_1');
863
                touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1');
864
                touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2');
865
                mkdir($path . DS . 'level_1_1' . DS . 'level_2_2');
866

    
867
                $Folder = new Folder($path, true);
868
                $return = $Folder->delete();
869
                $this->assertTrue($return);
870

    
871
                $messages = $Folder->messages();
872
                $errors = $Folder->errors();
873
                $this->assertEquals(array(), $errors);
874

    
875
                $expected = array(
876
                        $path . DS . 'file_1 removed',
877
                        $path . DS . 'level_1_1' . DS . 'file_1_1 removed',
878
                        $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
879
                        $path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
880
                        $path . DS . 'level_1_1' . DS . 'level_2_1 removed',
881
                        $path . DS . 'level_1_1' . DS . 'level_2_2 removed',
882
                        $path . DS . 'level_1_1 removed',
883
                        $path . ' removed'
884
                );
885
                sort($expected);
886
                sort($messages);
887
                $this->assertEquals($expected, $messages);
888
        }
889

    
890
/**
891
 * testCopy method
892
 *
893
 * Verify that subdirectories existing in both destination and source directory
894
 * are merged recursively.
895
 *
896
 * @return void
897
 */
898
        public function testCopy() {
899
                extract($this->_setupFilesystem());
900

    
901
                $Folder = new Folder($folderOne);
902
                $result = $Folder->copy($folderThree);
903
                $this->assertTrue($result);
904
                $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
905
                $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
906

    
907
                $Folder = new Folder($folderTwo);
908
                $result = $Folder->copy($folderThree);
909
                $this->assertTrue($result);
910
                $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
911
                $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
912
                $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
913
                $this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
914

    
915
                $Folder = new Folder($path);
916
                $Folder->delete();
917
        }
918

    
919
/**
920
 * testCopyWithMerge method
921
 *
922
 * Verify that subdirectories existing in both destination and source directory
923
 * are merged recursively.
924
 *
925
 * @return void
926
 */
927
        public function testCopyWithMerge() {
928
                extract($this->_setupFilesystem());
929

    
930
                $Folder = new Folder($folderOne);
931
                $result = $Folder->copy($folderThree);
932
                $this->assertTrue($result);
933
                $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
934
                $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
935

    
936
                $Folder = new Folder($folderTwo);
937
                $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::MERGE));
938
                $this->assertTrue($result);
939
                $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
940
                $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
941
                $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
942
                $this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
943

    
944
                $Folder = new Folder($path);
945
                $Folder->delete();
946
        }
947

    
948
/**
949
 * testCopyWithSkip method
950
 *
951
 * Verify that directories and files are copied recursively
952
 * even if the destination directory already exists.
953
 * Subdirectories existing in both destination and source directory
954
 * are skipped and not merged or overwritten.
955
 *
956
 * @return void
957
 */
958
        public function testCopyWithSkip() {
959
                extract($this->_setupFilesystem());
960

    
961
                $Folder = new Folder($folderOne);
962
                $result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
963
                $this->assertTrue($result);
964
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
965
                $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
966

    
967
                $Folder = new Folder($folderTwo);
968
                $Folder->delete();
969

    
970
                $Folder = new Folder($folderOne);
971
                $result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
972
                $this->assertTrue($result);
973
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
974
                $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
975

    
976
                $Folder = new Folder($folderTwo);
977
                $Folder->delete();
978

    
979
                new Folder($folderTwo, true);
980
                new Folder($folderTwo . DS . 'folderB', true);
981
                file_put_contents($folderTwo . DS . 'file2.php', 'touched');
982
                file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
983

    
984
                $Folder = new Folder($folderTwo);
985
                $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::SKIP));
986
                $this->assertTrue($result);
987
                $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
988
                $this->assertEquals('touched', file_get_contents($folderThree . DS . 'file2.php'));
989
                $this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folderB' . DS . 'fileB.php'));
990

    
991
                $Folder = new Folder($path);
992
                $Folder->delete();
993
        }
994

    
995
/**
996
 * Test that SKIP mode skips files too.
997
 *
998
 * @return void
999
 */
1000
        public function testCopyWithSkipFileSkipped() {
1001
                $path = TMP . 'folder_test';
1002
                $folderOne = $path . DS . 'folder1';
1003
                $folderTwo = $path . DS . 'folder2';
1004

    
1005
                new Folder($path, true);
1006
                new Folder($folderOne, true);
1007
                new Folder($folderTwo, true);
1008
                file_put_contents($folderOne . DS . 'fileA.txt', 'Folder One File');
1009
                file_put_contents($folderTwo . DS . 'fileA.txt', 'Folder Two File');
1010

    
1011
                $Folder = new Folder($folderOne);
1012
                $result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
1013
                $this->assertTrue($result);
1014
                $this->assertEquals('Folder Two File', file_get_contents($folderTwo . DS . 'fileA.txt'));
1015
        }
1016

    
1017
/**
1018
 * testCopyWithOverwrite
1019
 *
1020
 * Verify that subdirectories existing in both destination and source directory
1021
 * are overwritten/replaced recursively.
1022
 *
1023
 * @return void
1024
 */
1025
        public function testCopyWithOverwrite() {
1026
                extract($this->_setupFilesystem());
1027

    
1028
                $Folder = new Folder($folderOne);
1029
                $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
1030

    
1031
                $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
1032
                $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
1033

    
1034
                $Folder = new Folder($folderTwo);
1035
                $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
1036
                $this->assertTrue($result);
1037

    
1038
                $this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
1039

    
1040
                $Folder = new Folder($folderOne);
1041
                unlink($fileOneA);
1042
                $result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
1043
                $this->assertTrue($result);
1044

    
1045
                $this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
1046
                $this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
1047
                $this->assertTrue(!file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
1048
                $this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
1049

    
1050
                $Folder = new Folder($path);
1051
                $Folder->delete();
1052
        }
1053

    
1054
/**
1055
 * Setup filesystem for copy tests
1056
 * $path: folder_test/
1057
 * - folder1/file1.php
1058
 * - folder1/folderA/fileA.php
1059
 * - folder2/file2.php
1060
 * - folder2/folderB/fileB.php
1061
 * - folder3/
1062
 *
1063
 * @return array Filenames to extract in the test methods
1064
 */
1065
        protected function _setupFilesystem() {
1066
                $path = TMP . 'folder_test';
1067

    
1068
                $folderOne = $path . DS . 'folder1';
1069
                $folderOneA = $folderOne . DS . 'folderA';
1070
                $folderTwo = $path . DS . 'folder2';
1071
                $folderTwoB = $folderTwo . DS . 'folderB';
1072
                $folderThree = $path . DS . 'folder3';
1073

    
1074
                $fileOne = $folderOne . DS . 'file1.php';
1075
                $fileTwo = $folderTwo . DS . 'file2.php';
1076
                $fileOneA = $folderOneA . DS . 'fileA.php';
1077
                $fileTwoB = $folderTwoB . DS . 'fileB.php';
1078

    
1079
                new Folder($path, true);
1080
                new Folder($folderOne, true);
1081
                new Folder($folderOneA, true);
1082
                new Folder($folderTwo, true);
1083
                new Folder($folderTwoB, true);
1084
                new Folder($folderThree, true);
1085
                touch($fileOne);
1086
                touch($fileTwo);
1087
                touch($fileOneA);
1088
                touch($fileTwoB);
1089

    
1090
                return compact(
1091
                        'path',
1092
                        'folderOne', 'folderOneA', 'folderTwo', 'folderTwoB', 'folderThree',
1093
                        'fileOne', 'fileOneA', 'fileTwo', 'fileTwoB');
1094
        }
1095

    
1096
/**
1097
 * testMove method
1098
 *
1099
 * Verify that directories and files are moved recursively
1100
 * even if the destination directory already exists.
1101
 * Subdirectories existing in both destination and source directory
1102
 * are merged recursively.
1103
 *
1104
 * @return void
1105
 */
1106
        public function testMove() {
1107
                extract($this->_setupFilesystem());
1108

    
1109
                $Folder = new Folder($folderOne);
1110
                $result = $Folder->move($folderTwo);
1111
                $this->assertTrue($result);
1112
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1113
                $this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
1114
                $this->assertTrue(file_exists($folderTwo . DS . 'folderB' . DS . 'fileB.php'));
1115
                $this->assertFalse(file_exists($fileOne));
1116
                $this->assertTrue(file_exists($folderTwo . DS . 'folderA'));
1117
                $this->assertFalse(file_exists($folderOneA));
1118
                $this->assertFalse(file_exists($fileOneA));
1119

    
1120
                $Folder = new Folder($folderTwo);
1121
                $Folder->delete();
1122

    
1123
                new Folder($folderOne, true);
1124
                new Folder($folderOneA, true);
1125
                touch($fileOne);
1126
                touch($fileOneA);
1127

    
1128
                $Folder = new Folder($folderOne);
1129
                $result = $Folder->move($folderTwo);
1130
                $this->assertTrue($result);
1131
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1132
                $this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
1133
                $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
1134
                $this->assertFalse(file_exists($fileOne));
1135
                $this->assertFalse(file_exists($folderOneA));
1136
                $this->assertFalse(file_exists($fileOneA));
1137

    
1138
                $Folder = new Folder($folderTwo);
1139
                $Folder->delete();
1140

    
1141
                new Folder($folderOne, true);
1142
                new Folder($folderOneA, true);
1143
                new Folder($folderTwo, true);
1144
                new Folder($folderTwoB, true);
1145
                touch($fileOne);
1146
                touch($fileOneA);
1147
                new Folder($folderOne . DS . 'folderB', true);
1148
                touch($folderOne . DS . 'folderB' . DS . 'fileB.php');
1149
                file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
1150

    
1151
                $Folder = new Folder($folderOne);
1152
                $result = $Folder->move($folderTwo);
1153
                $this->assertTrue($result);
1154
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1155
                $this->assertEquals('', file_get_contents($folderTwoB . DS . 'fileB.php'));
1156
                $this->assertFalse(file_exists($fileOne));
1157
                $this->assertFalse(file_exists($folderOneA));
1158
                $this->assertFalse(file_exists($fileOneA));
1159

    
1160
                $Folder = new Folder($path);
1161
                $Folder->delete();
1162
        }
1163

    
1164
/**
1165
 * testMoveWithSkip method
1166
 *
1167
 * Verify that directories and files are moved recursively
1168
 * even if the destination directory already exists.
1169
 * Subdirectories existing in both destination and source directory
1170
 * are skipped and not merged or overwritten.
1171
 *
1172
 * @return void
1173
 */
1174
        public function testMoveWithSkip() {
1175
                extract($this->_setupFilesystem());
1176

    
1177
                $Folder = new Folder($folderOne);
1178
                $result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
1179
                $this->assertTrue($result);
1180
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1181
                $this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
1182
                $this->assertTrue(file_exists($folderTwoB . DS . 'fileB.php'));
1183
                $this->assertFalse(file_exists($fileOne));
1184
                $this->assertFalse(file_exists($folderOneA));
1185
                $this->assertFalse(file_exists($fileOneA));
1186

    
1187
                $Folder = new Folder($folderTwo);
1188
                $Folder->delete();
1189

    
1190
                new Folder($folderOne, true);
1191
                new Folder($folderOneA, true);
1192
                new Folder($folderTwo, true);
1193
                touch($fileOne);
1194
                touch($fileOneA);
1195

    
1196
                $Folder = new Folder($folderOne);
1197
                $result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
1198
                $this->assertTrue($result);
1199
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1200
                $this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
1201
                $this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
1202
                $this->assertFalse(file_exists($fileOne));
1203
                $this->assertFalse(file_exists($folderOneA));
1204
                $this->assertFalse(file_exists($fileOneA));
1205

    
1206
                $Folder = new Folder($folderTwo);
1207
                $Folder->delete();
1208

    
1209
                new Folder($folderOne, true);
1210
                new Folder($folderOneA, true);
1211
                new Folder($folderTwo, true);
1212
                new Folder($folderTwoB, true);
1213
                touch($fileOne);
1214
                touch($fileOneA);
1215
                file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
1216

    
1217
                $Folder = new Folder($folderOne);
1218
                $result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
1219
                $this->assertTrue($result);
1220
                $this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1221
                $this->assertEquals('untouched', file_get_contents($folderTwoB . DS . 'fileB.php'));
1222
                $this->assertFalse(file_exists($fileOne));
1223
                $this->assertFalse(file_exists($folderOneA));
1224
                $this->assertFalse(file_exists($fileOneA));
1225

    
1226
                $Folder = new Folder($path);
1227
                $Folder->delete();
1228
        }
1229

    
1230
}