| 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 | |
| 27 | public function testStripComment() { |
| 28 | $strings = array( |
| 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", |
| 41 | ); |
| 42 | foreach ($strings as $string => $check) { |
| 43 | $test = CRM_Utils_File::stripComments($string); |
| 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 | } |
| 54 | |
| 55 | public function fileExtensions() { |
| 56 | return array( |
| 57 | array('txt'), |
| 58 | array('danger'), |
| 59 | ); |
| 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 | |
| 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 | |
| 98 | } |