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']];
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));