Merge pull request #13657 from MegaphoneJon/deprecateBasicContactFields
[civicrm-core.git] / tests / phpunit / CRM / Utils / FileTest.php
1 <?php
2
3 /**
4 * Class CRM_Utils_FileTest
5 * @group headless
6 */
7 class CRM_Utils_FileTest extends CiviUnitTestCase {
8
9 /**
10 * Test is child path.
11 */
12 public function testIsChildPath() {
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 }
26 public function testStripComment() {
27 $strings = array(
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",
40 );
41 foreach ($strings as $string => $check) {
42 $test = CRM_Utils_File::stripComments($string);
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 }
53
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
76 public function fileNames() {
77 $cases = [];
78 $cases[] = ['helloworld.txt', TRUE];
79 $cases[] = ['../helloworld.txt', FALSE];
80 // Test case seems to be failing for a strange reason
81 // $cases[] = ['\helloworld.txt', FALSE];
82 $cases[] = ['.helloworld', FALSE];
83 $cases[] = ['smartwatch_1736683_1280_9af3657015e8660cc234eb1601da871.jpg', TRUE];
84 return $cases;
85 }
86
87 /**
88 * Test if the fileName is valid or not
89 * @dataProvider fileNames
90 * @param string $fileName
91 * @param bool $expectedResult
92 */
93 public function testFileNameValid($fileName, $expectedResult) {
94 $this->assertEquals($expectedResult, CRM_Utils_File::isValidFileName($fileName));
95 }
96
97 }