diff --git a/src/PhpSpreadsheet/Shared/StringHelper.php b/src/PhpSpreadsheet/Shared/StringHelper.php index 030df66df8..5b057c4c8e 100644 --- a/src/PhpSpreadsheet/Shared/StringHelper.php +++ b/src/PhpSpreadsheet/Shared/StringHelper.php @@ -334,7 +334,7 @@ public static function controlCharacterPHP2OOXML($textValue) public static function sanitizeUTF8(string $textValue): string { $textValue = str_replace(["\xef\xbf\xbe", "\xef\xbf\xbf"], "\xef\xbf\xbd", $textValue); - if (class_exists(UConverter::class)) { + if (class_exists(UConverter::class, false)) { $returnValue = UConverter::transcode($textValue, 'UTF-8', 'UTF-8'); if ($returnValue !== false) { return $returnValue; diff --git a/tests/PhpSpreadsheetTests/Shared/StringHelperInvalidCharTest.php b/tests/PhpSpreadsheetTests/Shared/StringHelperInvalidCharTest.php index 5467949993..f44b28379d 100644 --- a/tests/PhpSpreadsheetTests/Shared/StringHelperInvalidCharTest.php +++ b/tests/PhpSpreadsheetTests/Shared/StringHelperInvalidCharTest.php @@ -2,6 +2,7 @@ namespace PhpOffice\PhpSpreadsheetTests\Shared; +use Exception as Except; use PhpOffice\PhpSpreadsheet\Shared\StringHelper; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PHPUnit\Framework\TestCase; @@ -12,13 +13,14 @@ public function testInvalidChar(): void { $spreadsheet = new Spreadsheet(); $sheet = $spreadsheet->getActiveSheet(); - $substitution = '�'; + $substitution = class_exists('UConverter', false) ? '�' : '?'; + $substitution2 = '�'; $array = [ ['Normal string', 'Hello', 'Hello'], ['integer', 2, 2], ['float', 2.1, 2.1], ['boolean true', true, true], - ['illegal FFFE/FFFF', "H\xef\xbf\xbe\xef\xbf\xbfello", "H{$substitution}{$substitution}ello"], + ['illegal FFFE/FFFF', "H\xef\xbf\xbe\xef\xbf\xbfello", "H{$substitution2}{$substitution2}ello"], ['illegal character', "H\xef\x00\x00ello", "H{$substitution}\x00\x00ello"], ['overlong character', "H\xc0\xa0ello", "H{$substitution}{$substitution}ello"], ['Osmanya as single character', "H\xf0\x90\x90\x80ello", 'H𐐀ello'], @@ -41,4 +43,38 @@ public function testInvalidChar(): void ); } } + + public function fakespl(string $name): void + { + if (strlen($name) > 0) { + throw new Except("$name not found"); + } + } + + public function testClassNotFound(): void + { + // see issue 2982. + // This test will work all the time, but it is valuable + // only if php-intl not available + // and a user-supplied autoloader can issue exception. + /** @var callable */ + $fakespl = [$this, 'fakespl']; + spl_autoload_register($fakespl); + + try { + self::assertFalse(class_exists('abc123xyz')); + self::fail('Expected exception here'); + } catch (Except $e) { + // Nothing to do here + } + + $spreadsheet = new Spreadsheet(); + $sheet = $spreadsheet->getActiveSheet(); + $sheet->setCellValue('A1', 'Hello World !'); + + $spreadsheet->disconnectWorksheets(); + + spl_autoload_unregister($fakespl); + self::assertFalse(class_exists('abc123wxyz')); + } }