X-Git-Url: https://vcs.fsf.org/?a=blobdiff_plain;f=tests%2Fphpunit%2FCRM%2FUtils%2FFileTest.php;h=692bbee80380ba4610019b0a5f48e0f54c2a2ae6;hb=a0741dc66a56012dca28350f8c1ddaad5ec03918;hp=5e1659d93b64eb6fac6f3ee61d46ed22b95c2fd1;hpb=a7813bf21d3f7978b013ed04aef1fbdb2d9e116b;p=civicrm-core.git diff --git a/tests/phpunit/CRM/Utils/FileTest.php b/tests/phpunit/CRM/Utils/FileTest.php index 5e1659d93b..692bbee803 100644 --- a/tests/phpunit/CRM/Utils/FileTest.php +++ b/tests/phpunit/CRM/Utils/FileTest.php @@ -23,6 +23,7 @@ class CRM_Utils_FileTest extends CiviUnitTestCase { )); } } + public function testStripComment() { $strings = array( "\nab\n-- cd\nef" => "\nab\nef", @@ -73,4 +74,60 @@ class CRM_Utils_FileTest extends CiviUnitTestCase { unlink($newFile); } + public function fileNames() { + $cases = []; + $cases[] = ['helloworld.txt', TRUE]; + $cases[] = ['../helloworld.txt', FALSE]; + // Test case seems to be failing for a strange reason + // $cases[] = ['\helloworld.txt', FALSE]; + $cases[] = ['.helloworld', FALSE]; + $cases[] = ['smartwatch_1736683_1280_9af3657015e8660cc234eb1601da871.jpg', TRUE]; + return $cases; + } + + /** + * Test if the fileName is valid or not + * @dataProvider fileNames + * @param string $fileName + * @param bool $expectedResult + */ + public function testFileNameValid($fileName, $expectedResult) { + $this->assertEquals($expectedResult, CRM_Utils_File::isValidFileName($fileName)); + } + + public function pathToFileExtension() { + $cases = []; + $cases[] = ['/evil.pdf', 'pdf']; + $cases[] = ['/helloworld.jpg', 'jpg']; + $cases[] = ['/smartwatch_1736683_1280_9af3657015e8660cc234eb1601da871.jpg', 'jpg']; + return $cases; + } + + /** + * Test returning appropriate file extension + * @dataProvider pathToFileExtension + * @param string $path + * @param string $expectedExtension + */ + public function testPathToExtension($path, $expectedExtension) { + $this->assertEquals($expectedExtension, CRM_Utils_File::getExtensionFromPath($path)); + } + + public function mimeTypeToExtension() { + $cases = []; + $cases[] = ['text/plain', ['txt', 'text', 'conf', 'def', 'list', 'log', 'in']]; + $cases[] = ['image/jpeg', ['jpeg', 'jpg', 'jpe']]; + $cases[] = ['image/png', ['png']]; + return $cases; + } + + /** + * @dataProvider mimeTypeToExtension + * @param stirng $mimeType + * @param array $expectedExtensions + */ + public function testMimeTypeToExtension($mimeType, $expectedExtensions) { + $this->assertEquals($expectedExtensions, CRM_Utils_File::getAcceptableExtensionsForMimeType($mimeType)); + } + }