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() { |
9099cab3 CW |
13 | $testCases = []; |
14 | $testCases[] = ['/ab/cd/ef', '/ab/cd', FALSE]; | |
15 | $testCases[] = ['/ab/cd', '/ab/cd/ef', TRUE]; | |
16 | $testCases[] = ['/ab/cde', '/ab/cd/ef', FALSE]; | |
17 | $testCases[] = ['/ab/cde', '/ab/cd', FALSE]; | |
18 | $testCases[] = ['/ab/cd', 'ab/cd/ef', FALSE]; | |
6a488035 TO |
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 | } | |
39b959db | 26 | |
fc6159c2 | 27 | public function testStripComment() { |
9099cab3 | 28 | $strings = [ |
599520f3 | 29 | "\nab\n-- cd\nef" => "\nab\nef", |
30 | "ab\n-- cd\nef" => "ab\nef", | |
31 | "ab\n-- cd\nef\ngh" => "ab\nef\ngh", | |
32 | "ab\n--cd\nef" => "ab\nef", | |
33 | "ab\n--cd\nef\n" => "ab\nef\n", | |
34 | "ab\n#cd\nef\n" => "ab\nef\n", | |
35 | "ab\n--cd\nef" => "ab\nef", | |
36 | "ab\n#cd\nef" => "ab\nef", | |
37 | "ab\nfoo#cd\nef" => "ab\nfoo#cd\nef", | |
38 | "ab\r\n--cd\r\nef" => "ab\r\nef", | |
39 | "ab\r\n#cd\r\nef" => "ab\r\nef", | |
40 | "ab\r\nfoo#cd\r\nef" => "ab\r\nfoo#cd\r\nef", | |
9099cab3 | 41 | ]; |
fc6159c2 | 42 | foreach ($strings as $string => $check) { |
599520f3 | 43 | $test = CRM_Utils_File::stripComments($string); |
fc6159c2 | 44 | $this->assertEquals($test, |
45 | $check, | |
46 | sprintf("original=[%s]\nstripped=[%s]\nexpected=[%s]", | |
47 | json_encode($string), | |
48 | json_encode($test), | |
49 | json_encode($check) | |
50 | ) | |
51 | ); | |
52 | } | |
53 | } | |
96025800 | 54 | |
33d245c8 | 55 | public function fileExtensions() { |
9099cab3 CW |
56 | return [ |
57 | ['txt'], | |
58 | ['danger'], | |
59 | ]; | |
33d245c8 CW |
60 | } |
61 | ||
62 | /** | |
63 | * @dataProvider fileExtensions | |
64 | * @param string $ext | |
65 | */ | |
66 | public function testDuplicate($ext) { | |
67 | $fileName = CRM_Utils_File::makeFileName('test' . rand(100, 999) . ".$ext"); | |
68 | CRM_Utils_File::createFakeFile('/tmp', 'test file content', $fileName); | |
69 | $newFile = CRM_Utils_File::duplicate("/tmp/$fileName"); | |
70 | $this->assertNotEquals("/tmp/$fileName", $newFile); | |
71 | $contents = file_get_contents($newFile); | |
72 | $this->assertEquals('test file content', $contents); | |
73 | unlink("/tmp/$fileName"); | |
74 | unlink($newFile); | |
75 | } | |
76 | ||
a7762e79 SL |
77 | public function fileNames() { |
78 | $cases = []; | |
79 | $cases[] = ['helloworld.txt', TRUE]; | |
80 | $cases[] = ['../helloworld.txt', FALSE]; | |
81 | // Test case seems to be failing for a strange reason | |
82 | // $cases[] = ['\helloworld.txt', FALSE]; | |
83 | $cases[] = ['.helloworld', FALSE]; | |
84 | $cases[] = ['smartwatch_1736683_1280_9af3657015e8660cc234eb1601da871.jpg', TRUE]; | |
85 | return $cases; | |
86 | } | |
87 | ||
88 | /** | |
89 | * Test if the fileName is valid or not | |
90 | * @dataProvider fileNames | |
91 | * @param string $fileName | |
92 | * @param bool $expectedResult | |
93 | */ | |
94 | public function testFileNameValid($fileName, $expectedResult) { | |
95 | $this->assertEquals($expectedResult, CRM_Utils_File::isValidFileName($fileName)); | |
96 | } | |
97 | ||
6cb3fe2e SL |
98 | public function pathToFileExtension() { |
99 | $cases = []; | |
100 | $cases[] = ['/evil.pdf', 'pdf']; | |
101 | $cases[] = ['/helloworld.jpg', 'jpg']; | |
102 | $cases[] = ['/smartwatch_1736683_1280_9af3657015e8660cc234eb1601da871.jpg', 'jpg']; | |
103 | return $cases; | |
104 | } | |
105 | ||
106 | /** | |
107 | * Test returning appropriate file extension | |
108 | * @dataProvider pathToFileExtension | |
109 | * @param string $path | |
110 | * @param string $expectedExtension | |
111 | */ | |
112 | public function testPathToExtension($path, $expectedExtension) { | |
113 | $this->assertEquals($expectedExtension, CRM_Utils_File::getExtensionFromPath($path)); | |
114 | } | |
115 | ||
116 | public function mimeTypeToExtension() { | |
117 | $cases = []; | |
118 | $cases[] = ['text/plain', ['txt', 'text', 'conf', 'def', 'list', 'log', 'in']]; | |
10b23672 | 119 | $cases[] = ['image/jpeg', ['jpeg', 'jpg', 'jpe']]; |
6cb3fe2e SL |
120 | $cases[] = ['image/png', ['png']]; |
121 | return $cases; | |
122 | } | |
123 | ||
124 | /** | |
125 | * @dataProvider mimeTypeToExtension | |
126 | * @param stirng $mimeType | |
127 | * @param array $expectedExtensions | |
128 | */ | |
129 | public function testMimeTypeToExtension($mimeType, $expectedExtensions) { | |
130 | $this->assertEquals($expectedExtensions, CRM_Utils_File::getAcceptableExtensionsForMimeType($mimeType)); | |
131 | } | |
132 | ||
6a488035 | 133 | } |