Subversion Repositories SmartDukaan

Rev

Details | Last modification | View Log | RSS feed

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