Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
13532 anikendra 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
				self::$_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(self::$_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 recursive directory create failure.
175
 *
176
 * @return void
177
 */
178
	public function testRecursiveCreateFailure() {
179
		$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Cant perform operations using permissions on windows.');
180
 
181
		$path = TMP . 'tests' . DS . 'one';
182
		mkdir($path);
183
		chmod($path, '0444');
184
 
185
		try {
186
			$Folder = new Folder($path);
187
			$result = $Folder->create($path . DS . 'two' . DS . 'three');
188
			$this->assertFalse($result);
189
		} catch (PHPUnit_Framework_Error $e) {
190
			$this->assertTrue(true);
191
		}
192
 
193
		chmod($path, '0777');
194
		rmdir($path);
195
	}
196
 
197
/**
198
 * testOperations method
199
 *
200
 * @return void
201
 */
202
	public function testOperations() {
203
		$path = CAKE . 'Console' . DS . 'Templates' . DS . 'skel';
204
		$Folder = new Folder($path);
205
 
206
		$result = is_dir($Folder->pwd());
207
		$this->assertTrue($result);
208
 
209
		$new = TMP . 'test_folder_new';
210
		$result = $Folder->create($new);
211
		$this->assertTrue($result);
212
 
213
		$copy = TMP . 'test_folder_copy';
214
		$result = $Folder->copy($copy);
215
		$this->assertTrue($result);
216
 
217
		$copy = TMP . 'test_folder_copy';
218
		$result = $Folder->copy($copy);
219
		$this->assertTrue($result);
220
 
221
		$copy = TMP . 'test_folder_copy';
222
		$result = $Folder->chmod($copy, 0755, false);
223
		$this->assertTrue($result);
224
 
225
		$result = $Folder->cd($copy);
226
		$this->assertTrue((bool)$result);
227
 
228
		$mv = TMP . 'test_folder_mv';
229
		$result = $Folder->move($mv);
230
		$this->assertTrue($result);
231
 
232
		$mv = TMP . 'test_folder_mv_2';
233
		$result = $Folder->move($mv);
234
		$this->assertTrue($result);
235
 
236
		$result = $Folder->delete($new);
237
		$this->assertTrue($result);
238
 
239
		$result = $Folder->delete($mv);
240
		$this->assertTrue($result);
241
 
242
		$result = $Folder->delete($mv);
243
		$this->assertTrue($result);
244
 
245
		$new = APP . 'index.php';
246
		$result = $Folder->create($new);
247
		$this->assertFalse($result);
248
 
249
		$expected = $new . ' is a file';
250
		$result = $Folder->errors();
251
		$this->assertEquals($expected, $result[0]);
252
 
253
		$new = TMP . 'test_folder_new';
254
		$result = $Folder->create($new);
255
		$this->assertTrue($result);
256
 
257
		$result = $Folder->cd($new);
258
		$this->assertTrue((bool)$result);
259
 
260
		$result = $Folder->delete();
261
		$this->assertTrue($result);
262
 
263
		$Folder = new Folder('non-existent');
264
		$result = $Folder->pwd();
265
		$this->assertNull($result);
266
	}
267
 
268
/**
269
 * testChmod method
270
 *
271
 * @return void
272
 */
273
	public function testChmod() {
274
		$this->skipIf(DIRECTORY_SEPARATOR === '\\', 'Folder permissions tests not supported on Windows.');
275
 
276
		$path = TMP;
277
		$Folder = new Folder($path);
278
 
279
		$subdir = 'test_folder_new';
280
		$new = TMP . $subdir;
281
 
282
		$this->assertTrue($Folder->create($new));
283
		$this->assertTrue($Folder->create($new . DS . 'test1'));
284
		$this->assertTrue($Folder->create($new . DS . 'test2'));
285
 
286
		$filePath = $new . DS . 'test1.php';
287
		$File = new File($filePath);
288
		$this->assertTrue($File->create());
289
 
290
		$filePath = $new . DS . 'skip_me.php';
291
		$File = new File($filePath);
292
		$this->assertTrue($File->create());
293
 
294
		$this->assertTrue($Folder->chmod($new, 0755, true));
295
		$perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
296
		$this->assertEquals('0755', $perms);
297
 
298
		$this->assertTrue($Folder->chmod($new, 0744, true, array('skip_me.php', 'test2')));
299
 
300
		$perms = substr(sprintf('%o', fileperms($new . DS . 'test2')), -4);
301
		$this->assertEquals('0755', $perms);
302
 
303
		$perms = substr(sprintf('%o', fileperms($new . DS . 'test1')), -4);
304
		$this->assertEquals('0744', $perms);
305
 
306
		$Folder->delete($new);
307
	}
308
 
309
/**
310
 * testRealPathForWebroot method
311
 *
312
 * @return void
313
 */
314
	public function testRealPathForWebroot() {
315
		$Folder = new Folder('files/');
316
		$this->assertEquals(realpath('files/'), $Folder->path);
317
	}
318
 
319
/**
320
 * testZeroAsDirectory method
321
 *
322
 * @return void
323
 */
324
	public function testZeroAsDirectory() {
325
		$Folder = new Folder(TMP);
326
		$new = TMP . '0';
327
		$this->assertTrue($Folder->create($new));
328
 
329
		$result = $Folder->read(true, true);
330
		$expected = array('0', 'cache', 'logs', 'sessions', 'tests');
331
		$this->assertEquals($expected, $result[0]);
332
 
333
		$result = $Folder->read(true, array('logs'));
334
		$expected = array('0', 'cache', 'sessions', 'tests');
335
		$this->assertEquals($expected, $result[0]);
336
 
337
		$result = $Folder->delete($new);
338
		$this->assertTrue($result);
339
	}
340
 
341
/**
342
 * test Adding path elements to a path
343
 *
344
 * @return void
345
 */
346
	public function testAddPathElement() {
347
		$result = Folder::addPathElement(DS . 'some' . DS . 'dir', 'another_path');
348
		$this->assertEquals(DS . 'some' . DS . 'dir' . DS . 'another_path', $result);
349
 
350
		$result = Folder::addPathElement(DS . 'some' . DS . 'dir' . DS, 'another_path');
351
		$this->assertEquals(DS . 'some' . DS . 'dir' . DS . 'another_path', $result);
352
	}
353
 
354
/**
355
 * testFolderRead method
356
 *
357
 * @return void
358
 */
359
	public function testFolderRead() {
360
		$Folder = new Folder(TMP);
361
 
362
		$expected = array('cache', 'logs', 'sessions', 'tests');
363
		$result = $Folder->read(true, true);
364
		$this->assertEquals($expected, $result[0]);
365
 
366
		$Folder->path = TMP . 'non-existent';
367
		$expected = array(array(), array());
368
		$result = $Folder->read(true, true);
369
		$this->assertEquals($expected, $result);
370
	}
371
 
372
/**
373
 * testFolderReadWithHiddenFiles method
374
 *
375
 * @return void
376
 */
377
	public function testFolderReadWithHiddenFiles() {
378
		$this->skipIf(!is_writable(TMP), 'Cant test Folder::read with hidden files unless the tmp folder is writable.');
379
 
380
		$Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
381
		mkdir($Folder->path . DS . '.svn');
382
		mkdir($Folder->path . DS . 'some_folder');
383
		touch($Folder->path . DS . 'not_hidden.txt');
384
		touch($Folder->path . DS . '.hidden.txt');
385
 
386
		$expected = array(
387
			array('some_folder'),
388
			array('not_hidden.txt'),
389
		);
390
		$result = $Folder->read(true, true);
391
		$this->assertEquals($expected, $result);
392
 
393
		$expected = array(
394
			array(
395
				'.svn',
396
				'some_folder'
397
			),
398
			array(
399
				'.hidden.txt',
400
				'not_hidden.txt'
401
			),
402
		);
403
		$result = $Folder->read(true);
404
		$this->assertEquals($expected, $result);
405
	}
406
 
407
/**
408
 * testFolderTree method
409
 *
410
 * @return void
411
 */
412
	public function testFolderTree() {
413
		$Folder = new Folder();
414
		$expected = array(
415
			array(
416
				CAKE . 'Config',
417
				CAKE . 'Config' . DS . 'unicode',
418
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding'
419
			),
420
			array(
421
				CAKE . 'Config' . DS . 'config.php',
422
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0080_00ff.php',
423
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0100_017f.php',
424
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0180_024F.php',
425
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0250_02af.php',
426
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0370_03ff.php',
427
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0400_04ff.php',
428
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0500_052f.php',
429
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '0530_058f.php',
430
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '1e00_1eff.php',
431
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '1f00_1fff.php',
432
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2100_214f.php',
433
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2150_218f.php',
434
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2460_24ff.php',
435
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c00_2c5f.php',
436
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c60_2c7f.php',
437
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . '2c80_2cff.php',
438
				CAKE . 'Config' . DS . 'unicode' . DS . 'casefolding' . DS . 'ff00_ffef.php'
439
			)
440
		);
441
 
442
		$result = $Folder->tree(CAKE . 'Config', false);
443
		$this->assertSame(array(), array_diff($expected[0], $result[0]));
444
		$this->assertSame(array(), array_diff($result[0], $expected[0]));
445
 
446
		$result = $Folder->tree(CAKE . 'Config', false, 'dir');
447
		$this->assertSame(array(), array_diff($expected[0], $result));
448
		$this->assertSame(array(), array_diff($expected[0], $result));
449
 
450
		$result = $Folder->tree(CAKE . 'Config', false, 'files');
451
		$this->assertSame(array(), array_diff($expected[1], $result));
452
		$this->assertSame(array(), array_diff($expected[1], $result));
453
	}
454
 
455
/**
456
 * testFolderTreeWithHiddenFiles method
457
 *
458
 * @return void
459
 */
460
	public function testFolderTreeWithHiddenFiles() {
461
		$this->skipIf(!is_writable(TMP), 'Can\'t test Folder::tree with hidden files unless the tmp folder is writable.');
462
 
463
		$Folder = new Folder(TMP . 'folder_tree_hidden', true, 0777);
464
		mkdir($Folder->path . DS . '.svn', 0777, true);
465
		touch($Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php');
466
		mkdir($Folder->path . DS . '.svn' . DS . 'inhiddenfolder');
467
		touch($Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php');
468
		touch($Folder->path . DS . 'not_hidden.txt');
469
		touch($Folder->path . DS . '.hidden.txt');
470
		mkdir($Folder->path . DS . 'visible_folder' . DS . '.git', 0777, true);
471
 
472
		$expected = array(
473
			array(
474
				$Folder->path,
475
				$Folder->path . DS . 'visible_folder',
476
			),
477
			array(
478
				$Folder->path . DS . 'not_hidden.txt',
479
			),
480
		);
481
 
482
		$result = $Folder->tree(null, true);
483
		$this->assertEquals($expected, $result);
484
 
485
		$result = $Folder->tree(null, array('.'));
486
		$this->assertEquals($expected, $result);
487
 
488
		$expected = array(
489
			array(
490
				$Folder->path,
491
				$Folder->path . DS . 'visible_folder',
492
				$Folder->path . DS . 'visible_folder' . DS . '.git',
493
				$Folder->path . DS . '.svn',
494
				$Folder->path . DS . '.svn' . DS . 'inhiddenfolder',
495
			),
496
			array(
497
				$Folder->path . DS . 'not_hidden.txt',
498
				$Folder->path . DS . '.hidden.txt',
499
				$Folder->path . DS . '.svn' . DS . 'inhiddenfolder' . DS . 'NestedInHiddenFolder.php',
500
				$Folder->path . DS . '.svn' . DS . 'InHiddenFolder.php',
501
			),
502
		);
503
 
504
		$result = $Folder->tree(null, false);
505
		sort($result[0]);
506
		sort($expected[0]);
507
		sort($result[1]);
508
		sort($expected[1]);
509
		$this->assertEquals($expected, $result);
510
 
511
		$Folder->delete();
512
	}
513
 
514
/**
515
 * testWindowsPath method
516
 *
517
 * @return void
518
 */
519
	public function testWindowsPath() {
520
		$this->assertFalse(Folder::isWindowsPath('0:\\cake\\is\\awesome'));
521
		$this->assertTrue(Folder::isWindowsPath('C:\\cake\\is\\awesome'));
522
		$this->assertTrue(Folder::isWindowsPath('d:\\cake\\is\\awesome'));
523
		$this->assertTrue(Folder::isWindowsPath('\\\\vmware-host\\Shared Folders\\file'));
524
	}
525
 
526
/**
527
 * testIsAbsolute method
528
 *
529
 * @return void
530
 */
531
	public function testIsAbsolute() {
532
		$this->assertFalse(Folder::isAbsolute('path/to/file'));
533
		$this->assertFalse(Folder::isAbsolute('cake/'));
534
		$this->assertFalse(Folder::isAbsolute('path\\to\\file'));
535
		$this->assertFalse(Folder::isAbsolute('0:\\path\\to\\file'));
536
		$this->assertFalse(Folder::isAbsolute('\\path/to/file'));
537
		$this->assertFalse(Folder::isAbsolute('\\path\\to\\file'));
538
 
539
		$this->assertTrue(Folder::isAbsolute('/usr/local'));
540
		$this->assertTrue(Folder::isAbsolute('//path/to/file'));
541
		$this->assertTrue(Folder::isAbsolute('C:\\cake'));
542
		$this->assertTrue(Folder::isAbsolute('C:\\path\\to\\file'));
543
		$this->assertTrue(Folder::isAbsolute('d:\\path\\to\\file'));
544
		$this->assertTrue(Folder::isAbsolute('\\\\vmware-host\\Shared Folders\\file'));
545
	}
546
 
547
/**
548
 * testIsSlashTerm method
549
 *
550
 * @return void
551
 */
552
	public function testIsSlashTerm() {
553
		$this->assertFalse(Folder::isSlashTerm('cake'));
554
 
555
		$this->assertTrue(Folder::isSlashTerm('C:\\cake\\'));
556
		$this->assertTrue(Folder::isSlashTerm('/usr/local/'));
557
	}
558
 
559
/**
560
 * testStatic method
561
 *
562
 * @return void
563
 */
564
	public function testSlashTerm() {
565
		$result = Folder::slashTerm('/path/to/file');
566
		$this->assertEquals('/path/to/file/', $result);
567
	}
568
 
569
/**
570
 * testNormalizePath method
571
 *
572
 * @return void
573
 */
574
	public function testNormalizePath() {
575
		$path = '/path/to/file';
576
		$result = Folder::normalizePath($path);
577
		$this->assertEquals('/', $result);
578
 
579
		$path = '\\path\\\to\\\file';
580
		$result = Folder::normalizePath($path);
581
		$this->assertEquals('/', $result);
582
 
583
		$path = 'C:\\path\\to\\file';
584
		$result = Folder::normalizePath($path);
585
		$this->assertEquals('\\', $result);
586
	}
587
 
588
/**
589
 * correctSlashFor method
590
 *
591
 * @return void
592
 */
593
	public function testCorrectSlashFor() {
594
		$path = '/path/to/file';
595
		$result = Folder::correctSlashFor($path);
596
		$this->assertEquals('/', $result);
597
 
598
		$path = '\\path\\to\\file';
599
		$result = Folder::correctSlashFor($path);
600
		$this->assertEquals('/', $result);
601
 
602
		$path = 'C:\\path\to\\file';
603
		$result = Folder::correctSlashFor($path);
604
		$this->assertEquals('\\', $result);
605
	}
606
 
607
/**
608
 * testInCakePath method
609
 *
610
 * @return void
611
 */
612
	public function testInCakePath() {
613
		$Folder = new Folder();
614
		$Folder->cd(ROOT);
615
		$path = 'C:\\path\\to\\file';
616
		$result = $Folder->inCakePath($path);
617
		$this->assertFalse($result);
618
 
619
		$path = ROOT;
620
		$Folder->cd(ROOT);
621
		$result = $Folder->inCakePath($path);
622
		$this->assertFalse($result);
623
 
624
		$path = DS . 'lib' . DS . 'Cake' . DS . 'Config';
625
		$Folder->cd(ROOT . DS . 'lib' . DS . 'Cake' . DS . 'Config');
626
		$result = $Folder->inCakePath($path);
627
		$this->assertTrue($result);
628
	}
629
 
630
/**
631
 * testFind method
632
 *
633
 * @return void
634
 */
635
	public function testFind() {
636
		$Folder = new Folder();
637
		$Folder->cd(CAKE . 'Config');
638
		$result = $Folder->find();
639
		$expected = array('config.php');
640
		$this->assertSame(array_diff($expected, $result), array());
641
		$this->assertSame(array_diff($expected, $result), array());
642
 
643
		$result = $Folder->find('.*', true);
644
		$expected = array('cacert.pem', 'config.php', 'routes.php');
645
		$this->assertSame($expected, $result);
646
 
647
		$result = $Folder->find('.*\.php');
648
		$expected = array('config.php');
649
		$this->assertSame(array_diff($expected, $result), array());
650
		$this->assertSame(array_diff($expected, $result), array());
651
 
652
		$result = $Folder->find('.*\.php', true);
653
		$expected = array('config.php', 'routes.php');
654
		$this->assertSame($expected, $result);
655
 
656
		$result = $Folder->find('.*ig\.php');
657
		$expected = array('config.php');
658
		$this->assertSame($expected, $result);
659
 
660
		$result = $Folder->find('config\.php');
661
		$expected = array('config.php');
662
		$this->assertSame($expected, $result);
663
 
664
		$Folder->cd(TMP);
665
		$File = new File($Folder->pwd() . DS . 'paths.php', true);
666
		$Folder->create($Folder->pwd() . DS . 'testme');
667
		$Folder->cd('testme');
668
		$result = $Folder->find('paths\.php');
669
		$expected = array();
670
		$this->assertSame($expected, $result);
671
 
672
		$Folder->cd($Folder->pwd() . '/..');
673
		$result = $Folder->find('paths\.php');
674
		$expected = array('paths.php');
675
		$this->assertSame($expected, $result);
676
 
677
		$Folder->cd(TMP);
678
		$Folder->delete($Folder->pwd() . DS . 'testme');
679
		$File->delete();
680
	}
681
 
682
/**
683
 * testFindRecursive method
684
 *
685
 * @return void
686
 */
687
	public function testFindRecursive() {
688
		$Folder = new Folder();
689
		$Folder->cd(CAKE);
690
		$result = $Folder->findRecursive('(config|paths)\.php');
691
		$expected = array(
692
			CAKE . 'Config' . DS . 'config.php'
693
		);
694
		$this->assertSame(array_diff($expected, $result), array());
695
		$this->assertSame(array_diff($expected, $result), array());
696
 
697
		$result = $Folder->findRecursive('(config|paths)\.php', true);
698
		$expected = array(
699
			CAKE . 'Config' . DS . 'config.php'
700
		);
701
		$this->assertSame($expected, $result);
702
 
703
		$Folder->cd(TMP);
704
		$Folder->create($Folder->pwd() . DS . 'testme');
705
		$Folder->cd('testme');
706
		$File = new File($Folder->pwd() . DS . 'paths.php');
707
		$File->create();
708
		$Folder->cd(TMP . 'sessions');
709
		$result = $Folder->findRecursive('paths\.php');
710
		$expected = array();
711
		$this->assertSame($expected, $result);
712
 
713
		$Folder->cd(TMP . 'testme');
714
		$File = new File($Folder->pwd() . DS . 'my.php');
715
		$File->create();
716
		$Folder->cd($Folder->pwd() . '/../..');
717
 
718
		$result = $Folder->findRecursive('(paths|my)\.php');
719
		$expected = array(
720
			TMP . 'testme' . DS . 'my.php',
721
			TMP . 'testme' . DS . 'paths.php'
722
		);
723
		$this->assertSame(array_diff($expected, $result), array());
724
		$this->assertSame(array_diff($expected, $result), array());
725
 
726
		$result = $Folder->findRecursive('(paths|my)\.php', true);
727
		$expected = array(
728
			TMP . 'testme' . DS . 'my.php',
729
			TMP . 'testme' . DS . 'paths.php'
730
		);
731
		$this->assertSame($expected, $result);
732
 
733
		$Folder->cd(CAKE . 'Config');
734
		$Folder->cd(TMP);
735
		$Folder->delete($Folder->pwd() . DS . 'testme');
736
		$File->delete();
737
	}
738
 
739
/**
740
 * testConstructWithNonExistentPath method
741
 *
742
 * @return void
743
 */
744
	public function testConstructWithNonExistentPath() {
745
		$Folder = new Folder(TMP . 'config_non_existent', true);
746
		$this->assertTrue(is_dir(TMP . 'config_non_existent'));
747
		$Folder->cd(TMP);
748
		$Folder->delete($Folder->pwd() . 'config_non_existent');
749
	}
750
 
751
/**
752
 * testDirSize method
753
 *
754
 * @return void
755
 */
756
	public function testDirSize() {
757
		$Folder = new Folder(TMP . 'config_non_existent', true);
758
		$this->assertEquals(0, $Folder->dirSize());
759
 
760
		$File = new File($Folder->pwd() . DS . 'my.php', true, 0777);
761
		$File->create();
762
		$File->write('something here');
763
		$File->close();
764
		$this->assertEquals(14, $Folder->dirSize());
765
 
766
		$Folder->cd(TMP);
767
		$Folder->delete($Folder->pwd() . 'config_non_existent');
768
	}
769
 
770
/**
771
 * test that errors and messages can be resetted
772
 *
773
 * @return void
774
 */
775
	public function testReset() {
776
		$path = TMP . 'folder_delete_test';
777
		mkdir($path);
778
		$folder = $path . DS . 'sub';
779
		mkdir($folder);
780
		$file = $folder . DS . 'file';
781
		touch($file);
782
 
783
		chmod($folder, 0555);
784
		chmod($file, 0444);
785
 
786
		$Folder = new Folder($folder);
787
		$return = $Folder->delete();
788
		$this->assertFalse($return);
789
 
790
		$messages = $Folder->messages();
791
		$errors = $Folder->errors();
792
		$expected = array(
793
			$file . ' NOT removed',
794
			$folder . ' NOT removed',
795
		);
796
		sort($expected);
797
		sort($errors);
798
		$this->assertEmpty($messages);
799
		$this->assertEquals($expected, $errors);
800
 
801
		chmod($file, 0644);
802
		chmod($folder, 0755);
803
 
804
		$return = $Folder->delete();
805
		$this->assertTrue($return);
806
 
807
		$messages = $Folder->messages();
808
		$errors = $Folder->errors();
809
		$expected = array(
810
			$file . ' removed',
811
			$folder . ' removed',
812
		);
813
		sort($expected);
814
		sort($messages);
815
		$this->assertEmpty($errors);
816
		$this->assertEquals($expected, $messages);
817
	}
818
 
819
/**
820
 * testDelete method
821
 *
822
 * @return void
823
 */
824
	public function testDelete() {
825
		$path = TMP . 'folder_delete_test';
826
		mkdir($path);
827
		touch($path . DS . 'file_1');
828
		mkdir($path . DS . 'level_1_1');
829
		touch($path . DS . 'level_1_1' . DS . 'file_1_1');
830
		mkdir($path . DS . 'level_1_1' . DS . 'level_2_1');
831
		touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1');
832
		touch($path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2');
833
		mkdir($path . DS . 'level_1_1' . DS . 'level_2_2');
834
 
835
		$Folder = new Folder($path, true);
836
		$return = $Folder->delete();
837
		$this->assertTrue($return);
838
 
839
		$messages = $Folder->messages();
840
		$errors = $Folder->errors();
841
		$this->assertEquals(array(), $errors);
842
 
843
		$expected = array(
844
			$path . DS . 'file_1 removed',
845
			$path . DS . 'level_1_1' . DS . 'file_1_1 removed',
846
			$path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_1 removed',
847
			$path . DS . 'level_1_1' . DS . 'level_2_1' . DS . 'file_2_2 removed',
848
			$path . DS . 'level_1_1' . DS . 'level_2_1 removed',
849
			$path . DS . 'level_1_1' . DS . 'level_2_2 removed',
850
			$path . DS . 'level_1_1 removed',
851
			$path . ' removed'
852
		);
853
		sort($expected);
854
		sort($messages);
855
		$this->assertEquals($expected, $messages);
856
	}
857
 
858
/**
859
 * testCopy method
860
 *
861
 * Verify that subdirectories existing in both destination and source directory
862
 * are merged recursively.
863
 *
864
 * @return void
865
 */
866
	public function testCopy() {
867
		extract($this->_setupFilesystem());
868
 
869
		$Folder = new Folder($folderOne);
870
		$result = $Folder->copy($folderThree);
871
		$this->assertTrue($result);
872
		$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
873
		$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
874
 
875
		$Folder = new Folder($folderTwo);
876
		$result = $Folder->copy($folderThree);
877
		$this->assertTrue($result);
878
		$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
879
		$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
880
		$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
881
		$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
882
 
883
		$Folder = new Folder($path);
884
		$Folder->delete();
885
	}
886
 
887
/**
888
 * testCopyWithMerge method
889
 *
890
 * Verify that subdirectories existing in both destination and source directory
891
 * are merged recursively.
892
 *
893
 * @return void
894
 */
895
	public function testCopyWithMerge() {
896
		extract($this->_setupFilesystem());
897
 
898
		$Folder = new Folder($folderOne);
899
		$result = $Folder->copy($folderThree);
900
		$this->assertTrue($result);
901
		$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
902
		$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
903
 
904
		$Folder = new Folder($folderTwo);
905
		$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::MERGE));
906
		$this->assertTrue($result);
907
		$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
908
		$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
909
		$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
910
		$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
911
 
912
		$Folder = new Folder($path);
913
		$Folder->delete();
914
	}
915
 
916
/**
917
 * testCopyWithSkip method
918
 *
919
 * Verify that directories and files are copied recursively
920
 * even if the destination directory already exists.
921
 * Subdirectories existing in both destination and source directory
922
 * are skipped and not merged or overwritten.
923
 *
924
 * @return void
925
 */
926
	public function testCopyWithSkip() {
927
		extract($this->_setupFilesystem());
928
 
929
		$Folder = new Folder($folderOne);
930
		$result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
931
		$this->assertTrue($result);
932
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
933
		$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
934
 
935
		$Folder = new Folder($folderTwo);
936
		$Folder->delete();
937
 
938
		$Folder = new Folder($folderOne);
939
		$result = $Folder->copy(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
940
		$this->assertTrue($result);
941
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
942
		$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
943
 
944
		$Folder = new Folder($folderTwo);
945
		$Folder->delete();
946
 
947
		new Folder($folderTwo, true);
948
		new Folder($folderTwo . DS . 'folderB', true);
949
		file_put_contents($folderTwo . DS . 'file2.php', 'touched');
950
		file_put_contents($folderTwo . DS . 'folderB' . DS . 'fileB.php', 'untouched');
951
 
952
		$Folder = new Folder($folderTwo);
953
		$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::SKIP));
954
		$this->assertTrue($result);
955
		$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
956
		$this->assertEquals('touched', file_get_contents($folderThree . DS . 'file2.php'));
957
		$this->assertEquals('untouched', file_get_contents($folderThree . DS . 'folderB' . DS . 'fileB.php'));
958
 
959
		$Folder = new Folder($path);
960
		$Folder->delete();
961
	}
962
 
963
/**
964
 * testCopyWithOverwrite
965
 *
966
 * Verify that subdirectories existing in both destination and source directory
967
 * are overwritten/replaced recursively.
968
 *
969
 * @return void
970
 */
971
	public function testCopyWithOverwrite() {
972
		extract($this->_setupFilesystem());
973
 
974
		$Folder = new Folder($folderOne);
975
		$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
976
 
977
		$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
978
		$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
979
 
980
		$Folder = new Folder($folderTwo);
981
		$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
982
		$this->assertTrue($result);
983
 
984
		$this->assertTrue(file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
985
 
986
		$Folder = new Folder($folderOne);
987
		unlink($fileOneA);
988
		$result = $Folder->copy(array('to' => $folderThree, 'scheme' => Folder::OVERWRITE));
989
		$this->assertTrue($result);
990
 
991
		$this->assertTrue(file_exists($folderThree . DS . 'file1.php'));
992
		$this->assertTrue(file_exists($folderThree . DS . 'file2.php'));
993
		$this->assertTrue(!file_exists($folderThree . DS . 'folderA' . DS . 'fileA.php'));
994
		$this->assertTrue(file_exists($folderThree . DS . 'folderB' . DS . 'fileB.php'));
995
 
996
		$Folder = new Folder($path);
997
		$Folder->delete();
998
	}
999
 
1000
/**
1001
 * Setup filesystem for copy tests
1002
 * $path: folder_test/
1003
 * - folder1/file1.php
1004
 * - folder1/folderA/fileA.php
1005
 * - folder2/file2.php
1006
 * - folder2/folderB/fileB.php
1007
 * - folder3/
1008
 *
1009
 * @return array Filenames to extract in the test methods
1010
 */
1011
	protected function _setupFilesystem() {
1012
		$path = TMP . 'folder_test';
1013
 
1014
		$folderOne = $path . DS . 'folder1';
1015
		$folderOneA = $folderOne . DS . 'folderA';
1016
		$folderTwo = $path . DS . 'folder2';
1017
		$folderTwoB = $folderTwo . DS . 'folderB';
1018
		$folderThree = $path . DS . 'folder3';
1019
 
1020
		$fileOne = $folderOne . DS . 'file1.php';
1021
		$fileTwo = $folderTwo . DS . 'file2.php';
1022
		$fileOneA = $folderOneA . DS . 'fileA.php';
1023
		$fileTwoB = $folderTwoB . DS . 'fileB.php';
1024
 
1025
		new Folder($path, true);
1026
		new Folder($folderOne, true);
1027
		new Folder($folderOneA, true);
1028
		new Folder($folderTwo, true);
1029
		new Folder($folderTwoB, true);
1030
		new Folder($folderThree, true);
1031
		touch($fileOne);
1032
		touch($fileTwo);
1033
		touch($fileOneA);
1034
		touch($fileTwoB);
1035
 
1036
		return compact(
1037
			'path',
1038
			'folderOne', 'folderOneA', 'folderTwo', 'folderTwoB', 'folderThree',
1039
			'fileOne', 'fileOneA', 'fileTwo', 'fileTwoB');
1040
	}
1041
 
1042
/**
1043
 * testMove method
1044
 *
1045
 * Verify that directories and files are moved recursively
1046
 * even if the destination directory already exists.
1047
 * Subdirectories existing in both destination and source directory
1048
 * are merged recursively.
1049
 *
1050
 * @return void
1051
 */
1052
	public function testMove() {
1053
		extract($this->_setupFilesystem());
1054
 
1055
		$Folder = new Folder($folderOne);
1056
		$result = $Folder->move($folderTwo);
1057
		$this->assertTrue($result);
1058
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1059
		$this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
1060
		$this->assertTrue(file_exists($folderTwo . DS . 'folderB' . DS . 'fileB.php'));
1061
		$this->assertFalse(file_exists($fileOne));
1062
		$this->assertTrue(file_exists($folderTwo . DS . 'folderA'));
1063
		$this->assertFalse(file_exists($folderOneA));
1064
		$this->assertFalse(file_exists($fileOneA));
1065
 
1066
		$Folder = new Folder($folderTwo);
1067
		$Folder->delete();
1068
 
1069
		new Folder($folderOne, true);
1070
		new Folder($folderOneA, true);
1071
		touch($fileOne);
1072
		touch($fileOneA);
1073
 
1074
		$Folder = new Folder($folderOne);
1075
		$result = $Folder->move($folderTwo);
1076
		$this->assertTrue($result);
1077
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1078
		$this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
1079
		$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
1080
		$this->assertFalse(file_exists($fileOne));
1081
		$this->assertFalse(file_exists($folderOneA));
1082
		$this->assertFalse(file_exists($fileOneA));
1083
 
1084
		$Folder = new Folder($folderTwo);
1085
		$Folder->delete();
1086
 
1087
		new Folder($folderOne, true);
1088
		new Folder($folderOneA, true);
1089
		new Folder($folderTwo, true);
1090
		new Folder($folderTwoB, true);
1091
		touch($fileOne);
1092
		touch($fileOneA);
1093
		new Folder($folderOne . DS . 'folderB', true);
1094
		touch($folderOne . DS . 'folderB' . DS . 'fileB.php');
1095
		file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
1096
 
1097
		$Folder = new Folder($folderOne);
1098
		$result = $Folder->move($folderTwo);
1099
		$this->assertTrue($result);
1100
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1101
		$this->assertEquals('', file_get_contents($folderTwoB . DS . 'fileB.php'));
1102
		$this->assertFalse(file_exists($fileOne));
1103
		$this->assertFalse(file_exists($folderOneA));
1104
		$this->assertFalse(file_exists($fileOneA));
1105
 
1106
		$Folder = new Folder($path);
1107
		$Folder->delete();
1108
	}
1109
 
1110
/**
1111
 * testMoveWithSkip method
1112
 *
1113
 * Verify that directories and files are moved recursively
1114
 * even if the destination directory already exists.
1115
 * Subdirectories existing in both destination and source directory
1116
 * are skipped and not merged or overwritten.
1117
 *
1118
 * @return void
1119
 */
1120
	public function testMoveWithSkip() {
1121
		extract($this->_setupFilesystem());
1122
 
1123
		$Folder = new Folder($folderOne);
1124
		$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
1125
		$this->assertTrue($result);
1126
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1127
		$this->assertTrue(is_dir($folderTwo . DS . 'folderB'));
1128
		$this->assertTrue(file_exists($folderTwoB . DS . 'fileB.php'));
1129
		$this->assertFalse(file_exists($fileOne));
1130
		$this->assertFalse(file_exists($folderOneA));
1131
		$this->assertFalse(file_exists($fileOneA));
1132
 
1133
		$Folder = new Folder($folderTwo);
1134
		$Folder->delete();
1135
 
1136
		new Folder($folderOne, true);
1137
		new Folder($folderOneA, true);
1138
		new Folder($folderTwo, true);
1139
		touch($fileOne);
1140
		touch($fileOneA);
1141
 
1142
		$Folder = new Folder($folderOne);
1143
		$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
1144
		$this->assertTrue($result);
1145
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1146
		$this->assertTrue(is_dir($folderTwo . DS . 'folderA'));
1147
		$this->assertTrue(file_exists($folderTwo . DS . 'folderA' . DS . 'fileA.php'));
1148
		$this->assertFalse(file_exists($fileOne));
1149
		$this->assertFalse(file_exists($folderOneA));
1150
		$this->assertFalse(file_exists($fileOneA));
1151
 
1152
		$Folder = new Folder($folderTwo);
1153
		$Folder->delete();
1154
 
1155
		new Folder($folderOne, true);
1156
		new Folder($folderOneA, true);
1157
		new Folder($folderTwo, true);
1158
		new Folder($folderTwoB, true);
1159
		touch($fileOne);
1160
		touch($fileOneA);
1161
		file_put_contents($folderTwoB . DS . 'fileB.php', 'untouched');
1162
 
1163
		$Folder = new Folder($folderOne);
1164
		$result = $Folder->move(array('to' => $folderTwo, 'scheme' => Folder::SKIP));
1165
		$this->assertTrue($result);
1166
		$this->assertTrue(file_exists($folderTwo . DS . 'file1.php'));
1167
		$this->assertEquals('untouched', file_get_contents($folderTwoB . DS . 'fileB.php'));
1168
		$this->assertFalse(file_exists($fileOne));
1169
		$this->assertFalse(file_exists($folderOneA));
1170
		$this->assertFalse(file_exists($fileOneA));
1171
 
1172
		$Folder = new Folder($path);
1173
		$Folder->delete();
1174
	}
1175
 
1176
}