Skip to content

Commit

Permalink
refs #40338, fixes type test for filename
Browse files Browse the repository at this point in the history
  • Loading branch information
jimyhuang committed Apr 10, 2024
1 parent fef3e29 commit 8e518cf
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 9 deletions.
5 changes: 3 additions & 2 deletions CRM/Utils/File.php
Original file line number Diff line number Diff line change
Expand Up @@ -605,8 +605,9 @@ public static function sanitizeFileName($name) {
$filename = ltrim($filename, '.-');

// maximize filename length to 255 bytes
$ext = pathinfo($filename, PATHINFO_EXTENSION);
$basename = pathinfo($filename, PATHINFO_FILENAME);
$parts = explode('.', $filename);
$ext = array_pop($parts);
$basename = implode('.', $parts);
$filename = mb_strcut($basename, 0, 255 - ($ext ? strlen($ext) + 1 : 0), mb_detect_encoding($filename)) . ($ext ? '.' . $ext : '');
return $filename;
}
Expand Down
11 changes: 4 additions & 7 deletions tests/phpunit/CRM/Utils/TypeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ public function tearDownTest() {
* @param $expectedResult
*/
public function testValidate($inputData, $inputType, $expectedResult) {
$this->assertTrue($expectedResult === CRM_Utils_Type::validate($inputData, $inputType, FALSE));
$validatedResult = CRM_Utils_Type::validate($inputData, $inputType, FALSE);
$this->assertTrue($expectedResult === $validatedResult, "$inputData:::$validatedResult");
}

/**
Expand Down Expand Up @@ -63,7 +64,8 @@ public static function validateDataProvider() {
* @param $expectedResult
*/
public function testEscape($inputData, $inputType, $expectedResult) {
$this->assertTrue($expectedResult === CRM_Utils_Type::escape($inputData, $inputType, FALSE));
$escapedResult = CRM_Utils_Type::escape($inputData, $inputType, FALSE);
$this->assertTrue($expectedResult === $escapedResult, "$inputData:::$escapedResult");
}

/**
Expand Down Expand Up @@ -109,11 +111,6 @@ public static function escapeDataProvider() {
array("file\x00name.txt", 'FileName', 'filename.txt'),
array("file\x1Fname.txt", 'FileName', 'filename.txt'),

// Strings containing non-printing characters
array("file\x7Fname.txt", 'FileName', 'filename.txt'),
array("file\xA0name.txt", 'FileName', 'filename.txt'),
array("file\xADname.txt", 'FileName', 'filename.txt'),

// Strings containing URI reserved characters
array("file#name.txt", 'FileName', 'filename.txt'),
array("file[name].txt", 'FileName', 'filename.txt'),
Expand Down

0 comments on commit 8e518cf

Please sign in to comment.