Files
oleibman e1fb68efc1 Handling of User-supplied Decimal and Thousands Separators
Fix #3900. The code to adjust Decimal and Thousands separators to user-specified choices expects to convert periods to commas and vice versa - hard-coded without taking the user specification into account. This is likely to be the only significant use case, however, as the issue demonstrates, it is not the only possibility. In particular, it will mess up Csv output if decimal separator is kept as period, but thousands separator is set to null string. The code is altered to replace period with the user-selected decimal separator, and comma with the user-selected thousands separator. No existing tests broke as a result of this change in behavior.
2024-02-14 21:20:45 -08:00

124 lines
3.8 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Shared;
use PhpOffice\PhpSpreadsheet\Shared\StringHelper;
use PhpOffice\PhpSpreadsheet\Spreadsheet;
use PhpOffice\PhpSpreadsheet\Writer\Csv;
use PHPUnit\Framework\TestCase;
class StringHelperTest extends TestCase
{
protected function tearDown(): void
{
StringHelper::setCurrencyCode(null);
StringHelper::setDecimalSeparator(null);
StringHelper::setThousandsSeparator(null);
}
public function testGetIsIconvEnabled(): void
{
$result = StringHelper::getIsIconvEnabled();
self::assertTrue($result);
}
public function testGetDecimalSeparator(): void
{
$localeconv = localeconv();
$expectedResult = (!empty($localeconv['decimal_point'])) ? $localeconv['decimal_point'] : ',';
$result = StringHelper::getDecimalSeparator();
self::assertEquals($expectedResult, $result);
}
public function testSetDecimalSeparator(): void
{
$expectedResult = ',';
StringHelper::setDecimalSeparator($expectedResult);
$result = StringHelper::getDecimalSeparator();
self::assertEquals($expectedResult, $result);
}
public function testGetThousandsSeparator(): void
{
$localeconv = localeconv();
$expectedResult = (!empty($localeconv['thousands_sep'])) ? $localeconv['thousands_sep'] : ',';
$result = StringHelper::getThousandsSeparator();
self::assertEquals($expectedResult, $result);
}
public function testSetThousandsSeparator(): void
{
$expectedResult = ' ';
StringHelper::setThousandsSeparator($expectedResult);
$result = StringHelper::getThousandsSeparator();
self::assertEquals($expectedResult, $result);
}
public function testGetCurrencyCode(): void
{
$localeconv = localeconv();
$expectedResult = (!empty($localeconv['currency_symbol']) ? $localeconv['currency_symbol'] : (!empty($localeconv['int_curr_symbol']) ? $localeconv['int_curr_symbol'] : '$'));
$result = StringHelper::getCurrencyCode();
self::assertEquals($expectedResult, $result);
}
public function testSetCurrencyCode(): void
{
$expectedResult = '£';
StringHelper::setCurrencyCode($expectedResult);
$result = StringHelper::getCurrencyCode();
self::assertEquals($expectedResult, $result);
}
public function testControlCharacterPHP2OOXML(): void
{
$expectedResult = 'foo_x000B_bar';
$result = StringHelper::controlCharacterPHP2OOXML('foo' . chr(11) . 'bar');
self::assertEquals($expectedResult, $result);
}
public function testControlCharacterOOXML2PHP(): void
{
$expectedResult = 'foo' . chr(11) . 'bar';
$result = StringHelper::controlCharacterOOXML2PHP('foo_x000B_bar');
self::assertEquals($expectedResult, $result);
}
public function testSYLKtoUTF8(): void
{
$expectedResult = 'foo' . chr(11) . 'bar';
$result = StringHelper::SYLKtoUTF8("foo\x1B ;bar");
self::assertEquals($expectedResult, $result);
}
public function testIssue3900(): void
{
StringHelper::setDecimalSeparator('.');
StringHelper::setThousandsSeparator('');
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet();
$sheet->setCellValue('A1', 1.4);
$sheet->setCellValue('B1', 1004.5);
$sheet->setCellValue('C1', 1000000.5);
ob_start();
$ioWriter = new Csv($spreadsheet);
$ioWriter->setDelimiter(';');
$ioWriter->save('php://output');
$output = ob_get_clean();
$spreadsheet->disconnectWorksheets();
self::assertSame('"1.4";"1004.5";"1000000.5"' . PHP_EOL, $output);
}
}