4 * Class CRM_Utils_FileTest
7 class CRM_Utils_FileTest
extends CiviUnitTestCase
{
12 public function testIsChildPath() {
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
26 public function testStripComment() {
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",
41 foreach ($strings as $string => $check) {
42 $test = CRM_Utils_File
::stripComments($string);
43 $this->assertEquals($test,
45 sprintf("original=[%s]\nstripped=[%s]\nexpected=[%s]",
54 public function fileExtensions() {
62 * @dataProvider fileExtensions
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");