4 * Class CRM_Utils_FileTest
7 class CRM_Utils_FileTest
extends CiviUnitTestCase
{
12 public function testIsChildPath() {
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];
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
27 public function testStripComment() {
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",
42 foreach ($strings as $string => $check) {
43 $test = CRM_Utils_File
::stripComments($string);
44 $this->assertEquals($test,
46 sprintf("original=[%s]\nstripped=[%s]\nexpected=[%s]",
55 public function fileExtensions() {
63 * @dataProvider fileExtensions
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");
77 public function fileNames() {
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];
89 * Test if the fileName is valid or not
90 * @dataProvider fileNames
91 * @param string $fileName
92 * @param bool $expectedResult
94 public function testFileNameValid($fileName, $expectedResult) {
95 $this->assertEquals($expectedResult, CRM_Utils_File
::isValidFileName($fileName));
98 public function pathToFileExtension() {
100 $cases[] = ['/evil.pdf', 'pdf'];
101 $cases[] = ['/helloworld.jpg', 'jpg'];
102 $cases[] = ['/smartwatch_1736683_1280_9af3657015e8660cc234eb1601da871.jpg', 'jpg'];
107 * Test returning appropriate file extension
108 * @dataProvider pathToFileExtension
109 * @param string $path
110 * @param string $expectedExtension
112 public function testPathToExtension($path, $expectedExtension) {
113 $this->assertEquals($expectedExtension, CRM_Utils_File
::getExtensionFromPath($path));
116 public function mimeTypeToExtension() {
118 $cases[] = ['text/plain', ['txt', 'text', 'conf', 'def', 'list', 'log', 'in', 'ini']];
119 $cases[] = ['image/jpeg', ['jpeg', 'jpg', 'jpe']];
120 $cases[] = ['image/png', ['png']];
125 * @dataProvider mimeTypeToExtension
126 * @param stirng $mimeType
127 * @param array $expectedExtensions
129 public function testMimeTypeToExtension($mimeType, $expectedExtensions) {
130 $this->assertEquals($expectedExtensions, CRM_Utils_File
::getAcceptableExtensionsForMimeType($mimeType));
134 * Check a few variations of isIncludable
136 public function testIsIncludable() {
137 $path = \Civi
::paths()->getPath('[civicrm.private]/');
138 $bare_filename = 'afile' . time() . '.php';
139 $file = "$path/$bare_filename";
140 file_put_contents($file, '<?php');
142 // A file that doesn't exist shouldn't be includable.
143 $this->assertFalse(CRM_Utils_File
::isIncludable('invisiblefile.php'));
145 // Shouldn't be includable by default in civicrm.private
146 $this->assertFalse(CRM_Utils_File
::isIncludable($bare_filename));
148 // Add civicrm.private to the include_path, then it should be includable.
149 $old_include_path = ini_get('include_path');
150 ini_set('include_path', $old_include_path . PATH_SEPARATOR
. $path);
151 $this->assertTrue(CRM_Utils_File
::isIncludable($bare_filename));
153 // Set permissions to 0, then it shouldn't be includable even if in path.
154 if (strtoupper(substr(PHP_OS
, 0, 3)) !== 'WIN') {
156 $this->assertFalse(CRM_Utils_File
::isIncludable($bare_filename));
160 ini_set('include_path', $old_include_path);
165 * dataprovider for testMakeFilenameWithUnicode
168 public function makeFilenameWithUnicodeProvider(): array {
170 // explicit indices to make it easier to see which one failed
173 'replacementCharacter' => NULL,
174 'cutoffLength' => NULL,
179 'replacementCharacter' => NULL,
180 'cutoffLength' => NULL,
185 'replacementCharacter' => NULL,
186 'cutoffLength' => NULL,
191 'replacementCharacter' => NULL,
192 'cutoffLength' => NULL,
196 'string' => '_a!@#$%^&*()[]+-=."\'{}<>?/\\|;:b',
197 'replacementCharacter' => NULL,
198 'cutoffLength' => NULL,
199 'expected' => '_a____________________________b',
202 'string' => '_a!@#$%^&*()[]+-=."\'{}<>?/\\|;:b',
203 'replacementCharacter' => '',
204 'cutoffLength' => NULL,
207 // emojis get replaced, but alphabetic letters in non-english are kept
209 'string' => 'açbяc😀d',
210 'replacementCharacter' => NULL,
211 'cutoffLength' => NULL,
212 'expected' => 'açbяc_d',
216 'replacementCharacter' => NULL,
217 'cutoffLength' => NULL,
220 // test default cutoff
222 'string' => 'abcdefghijklmnopqrstuvwxyz0123456789012345678901234567890123456789',
223 'replacementCharacter' => NULL,
224 'cutoffLength' => NULL,
225 'expected' => 'abcdefghijklmnopqrstuvwxyz0123456789012345678901234567890123456',
228 'string' => 'abcdefghijklmnopqrstuvwxyz0123456789012345678901234567890123456789',
229 'replacementCharacter' => '_',
230 'cutoffLength' => 30,
231 'expected' => 'abcdefghijklmnopqrstuvwxyz0123',
233 // test cutoff truncates multibyte properly
235 'string' => 'ДДДДДДДДДДДДДДД',
236 'replacementCharacter' => '',
237 'cutoffLength' => 10,
238 'expected' => 'ДДДДДДДДДД',
244 * test makeFilenameWithUnicode
245 * @dataProvider makeFilenameWithUnicodeProvider
246 * @param string $input
247 * @param ?string $replacementCharacter
248 * @param ?int $cutoffLength
249 * @param string $expected
251 public function testMakeFilenameWithUnicode(string $input, ?
string $replacementCharacter, ?
int $cutoffLength, string $expected) {
252 if (is_null($replacementCharacter) && is_null($cutoffLength)) {
253 $this->assertSame($expected, CRM_Utils_File
::makeFilenameWithUnicode($input));
255 elseif (is_null($cutoffLength)) {
256 $this->assertSame($expected, CRM_Utils_File
::makeFilenameWithUnicode($input, $replacementCharacter));
259 $this->assertSame($expected, CRM_Utils_File
::makeFilenameWithUnicode($input, $replacementCharacter, $cutoffLength));