Commit | Line | Data |
---|---|---|
6a488035 | 1 | <?php |
aba1cd8b EM |
2 | |
3 | /** | |
4 | * Class CRM_Utils_FileTest | |
acb109b7 | 5 | * @group headless |
aba1cd8b | 6 | */ |
6a488035 | 7 | class CRM_Utils_FileTest extends CiviUnitTestCase { |
3d469574 | 8 | |
9 | /** | |
10 | * Test is child path. | |
11 | */ | |
00be9182 | 12 | public function testIsChildPath() { |
6a488035 TO |
13 | $testCases = array(); |
14 | $testCases[] = array('/ab/cd/ef', '/ab/cd', FALSE); | |
15 | $testCases[] = array('/ab/cd', '/ab/cd/ef', TRUE); | |
16 | $testCases[] = array('/ab/cde', '/ab/cd/ef', FALSE); | |
17 | $testCases[] = array('/ab/cde', '/ab/cd', FALSE); | |
18 | $testCases[] = array('/ab/cd', 'ab/cd/ef', FALSE); | |
19 | foreach ($testCases as $testCase) { | |
20 | $actual = CRM_Utils_File::isChildPath($testCase[0], $testCase[1], FALSE); | |
21 | $this->assertEquals($testCase[2], $actual, sprintf("parent=[%s] child=[%s] expected=[%s] actual=[%s]", | |
22 | $testCase[0], $testCase[1], $testCase[2], $actual | |
23 | )); | |
24 | } | |
25 | } | |
fc6159c2 | 26 | public function testStripComment() { |
27 | $strings = array( | |
599520f3 | 28 | "\nab\n-- cd\nef" => "\nab\nef", |
29 | "ab\n-- cd\nef" => "ab\nef", | |
30 | "ab\n-- cd\nef\ngh" => "ab\nef\ngh", | |
31 | "ab\n--cd\nef" => "ab\nef", | |
32 | "ab\n--cd\nef\n" => "ab\nef\n", | |
33 | "ab\n#cd\nef\n" => "ab\nef\n", | |
34 | "ab\n--cd\nef" => "ab\nef", | |
35 | "ab\n#cd\nef" => "ab\nef", | |
36 | "ab\nfoo#cd\nef" => "ab\nfoo#cd\nef", | |
37 | "ab\r\n--cd\r\nef" => "ab\r\nef", | |
38 | "ab\r\n#cd\r\nef" => "ab\r\nef", | |
39 | "ab\r\nfoo#cd\r\nef" => "ab\r\nfoo#cd\r\nef", | |
fc6159c2 | 40 | ); |
41 | foreach ($strings as $string => $check) { | |
599520f3 | 42 | $test = CRM_Utils_File::stripComments($string); |
fc6159c2 | 43 | $this->assertEquals($test, |
44 | $check, | |
45 | sprintf("original=[%s]\nstripped=[%s]\nexpected=[%s]", | |
46 | json_encode($string), | |
47 | json_encode($test), | |
48 | json_encode($check) | |
49 | ) | |
50 | ); | |
51 | } | |
52 | } | |
96025800 | 53 | |
33d245c8 CW |
54 | public function fileExtensions() { |
55 | return array( | |
56 | array('txt'), | |
57 | array('danger'), | |
58 | ); | |
59 | } | |
60 | ||
61 | /** | |
62 | * @dataProvider fileExtensions | |
63 | * @param string $ext | |
64 | */ | |
65 | public function testDuplicate($ext) { | |
66 | $fileName = CRM_Utils_File::makeFileName('test' . rand(100, 999) . ".$ext"); | |
67 | CRM_Utils_File::createFakeFile('/tmp', 'test file content', $fileName); | |
68 | $newFile = CRM_Utils_File::duplicate("/tmp/$fileName"); | |
69 | $this->assertNotEquals("/tmp/$fileName", $newFile); | |
70 | $contents = file_get_contents($newFile); | |
71 | $this->assertEquals('test file content', $contents); | |
72 | unlink("/tmp/$fileName"); | |
73 | unlink($newFile); | |
74 | } | |
75 | ||
6a488035 | 76 | } |