Files
PhpSpreadsheet/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php
T
oleibman 5b7fce9286 Strip _xlfn. and _xlfs. In Formula Translations
Fix #3819. Excel can add these prefixes (basically invisible to end-user). Formula translation in PhpSpreadsheet fails when dealing with these unexpected prefixes, and, even if it handled it correctly, the unexpected prefixes confuse the users. I have changed to strip those prefixes when translating to a locale. This is probably not perfect, but is almost certainly good enough. I could easily add the same change when translating from a locale to English, but I don't think there's a good use case for that, so am opting not to do so for now.

The documentation mentions `translateFormulaToLocale` and `translateFormulaToEnglish`. Neither of these exist; both names are preceded by an underscore. I have changed the code to match the documentation rather than vice versa, retaining deprecated versions of the underscored routines which merely invoke the non-underscored routines.
2023-12-14 13:29:30 -08:00

58 lines
1.8 KiB
PHP

<?php
declare(strict_types=1);
namespace PhpOffice\PhpSpreadsheetTests\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Calculation;
use PhpOffice\PhpSpreadsheet\Calculation\Functions;
use PhpOffice\PhpSpreadsheet\Settings;
use PHPUnit\Framework\TestCase;
class TranslationTest extends TestCase
{
private string $compatibilityMode;
private string $returnDate;
private string $locale;
protected function setUp(): void
{
$this->compatibilityMode = Functions::getCompatibilityMode();
$this->returnDate = Functions::getReturnDateType();
Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL);
Functions::setReturnDateType(Functions::RETURNDATE_EXCEL);
$this->locale = Settings::getLocale();
}
protected function tearDown(): void
{
Functions::setCompatibilityMode($this->compatibilityMode);
Functions::setReturnDateType($this->returnDate);
Settings::setLocale($this->locale);
}
/**
* @dataProvider providerTranslations
*/
public function testTranslation(string $expectedResult, string $locale, string $formula): void
{
$validLocale = Settings::setLocale($locale);
if (!$validLocale) {
self::markTestSkipped("Unable to set locale to {$locale}");
}
$translatedFormula = Calculation::getInstance()->_translateFormulaToLocale($formula);
self::assertSame($expectedResult, $translatedFormula);
$restoredFormula = Calculation::getInstance()->_translateFormulaToEnglish($translatedFormula);
self::assertSame(preg_replace('/_(xlfn|xlws)[.]/', '', $formula), $restoredFormula);
}
public static function providerTranslations(): array
{
return require 'tests/data/Calculation/Translations.php';
}
}