From 12ccea5a243feb6b5ebc428a4ca0a0ed368934ee Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 28 Feb 2021 11:39:24 +0100 Subject: [PATCH 1/5] Initial Formula Translation tests --- samples/Basic/43_Merge_workbooks.php | 4 ++ .../Calculation/TranslationTest.php | 50 +++++++++++++++++++ tests/data/Calculation/Translations.php | 24 +++++++++ 3 files changed, 78 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Calculation/TranslationTest.php create mode 100644 tests/data/Calculation/Translations.php diff --git a/samples/Basic/43_Merge_workbooks.php b/samples/Basic/43_Merge_workbooks.php index 86314b3ba3..28353cc60b 100644 --- a/samples/Basic/43_Merge_workbooks.php +++ b/samples/Basic/43_Merge_workbooks.php @@ -18,6 +18,10 @@ foreach ($spreadsheet2->getSheetNames() as $sheetName) { $sheet = $spreadsheet2->getSheetByName($sheetName); + if ($sheet === null) { + continue; + } + $sheet->setTitle($sheet->getTitle() . ' copied'); $spreadsheet1->addExternalSheet($sheet); } diff --git a/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php b/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php new file mode 100644 index 0000000000..091af66c22 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php @@ -0,0 +1,50 @@ +compatibilityMode = Functions::getCompatibilityMode(); + $this->returnDate = Functions::getReturnDateType(); + Functions::setCompatibilityMode(Functions::COMPATIBILITY_EXCEL); + Functions::setReturnDateType(Functions::RETURNDATE_EXCEL); + } + + protected function tearDown(): void + { + Functions::setCompatibilityMode($this->compatibilityMode); + Functions::setReturnDateType($this->returnDate); + } + /** + * @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($formula, $restoredFormula); + } + + public function providerTranslations() + { + return require 'tests/data/Calculation/Translations.php'; + } +} diff --git a/tests/data/Calculation/Translations.php b/tests/data/Calculation/Translations.php new file mode 100644 index 0000000000..f26de667f2 --- /dev/null +++ b/tests/data/Calculation/Translations.php @@ -0,0 +1,24 @@ + Date: Sun, 28 Feb 2021 12:06:11 +0100 Subject: [PATCH 2/5] Another set of Formula Translation tests --- .../Calculation/TranslationTest.php | 1 + tests/data/Calculation/Translations.php | 15 ++++++++++----- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php b/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php index 091af66c22..1eb66a0a1c 100644 --- a/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php +++ b/tests/PhpSpreadsheetTests/Calculation/TranslationTest.php @@ -26,6 +26,7 @@ protected function tearDown(): void Functions::setCompatibilityMode($this->compatibilityMode); Functions::setReturnDateType($this->returnDate); } + /** * @dataProvider providerTranslations */ diff --git a/tests/data/Calculation/Translations.php b/tests/data/Calculation/Translations.php index f26de667f2..1f7b99be6d 100644 --- a/tests/data/Calculation/Translations.php +++ b/tests/data/Calculation/Translations.php @@ -1,11 +1,6 @@ Date: Sun, 28 Feb 2021 12:45:39 +0100 Subject: [PATCH 3/5] Another Formula Translation test --- tests/data/Calculation/Translations.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/data/Calculation/Translations.php b/tests/data/Calculation/Translations.php index 1f7b99be6d..5770e75a11 100644 --- a/tests/data/Calculation/Translations.php +++ b/tests/data/Calculation/Translations.php @@ -26,4 +26,9 @@ 'nl', '=CONCATENATE("""Hello ", B1, """,", " I said.")', ], + [ + '=TEKST.SAMENVOEGEN(JAAR(VANDAAG()); " is "; ALS(DAYS(DATUM(JAAR(VANDAAG())+1; 1; 1);DATUM(JAAR(VANDAAG()); 1; 1)) = 365;"NOT a Leap Year"; "a Leap Year"))', + 'nl', + '=CONCATENATE(YEAR(TODAY()), " is ", IF(DAYS(DATE(YEAR(TODAY())+1, 1, 1),DATE(YEAR(TODAY()), 1, 1)) = 365,"NOT a Leap Year", "a Leap Year"))', + ] ]; From 7c79a2c4ea4a39378051392e04f02b74c1853fd7 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 28 Feb 2021 12:58:22 +0100 Subject: [PATCH 4/5] Test that returns in formulae are maintained --- tests/data/Calculation/Translations.php | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/tests/data/Calculation/Translations.php b/tests/data/Calculation/Translations.php index 5770e75a11..6083c9e242 100644 --- a/tests/data/Calculation/Translations.php +++ b/tests/data/Calculation/Translations.php @@ -27,8 +27,22 @@ '=CONCATENATE("""Hello ", B1, """,", " I said.")', ], [ - '=TEKST.SAMENVOEGEN(JAAR(VANDAAG()); " is "; ALS(DAYS(DATUM(JAAR(VANDAAG())+1; 1; 1);DATUM(JAAR(VANDAAG()); 1; 1)) = 365;"NOT a Leap Year"; "a Leap Year"))', + '=TEKST.SAMENVOEGEN(JAAR(VANDAAG()); + " is "; + ALS( + DAYS(DATUM(JAAR(VANDAAG())+1; 1; 1); DATUM(JAAR(VANDAAG()); 1; 1)) = 365; + "NOT a Leap Year"; + "a Leap Year" + ) + )', 'nl', - '=CONCATENATE(YEAR(TODAY()), " is ", IF(DAYS(DATE(YEAR(TODAY())+1, 1, 1),DATE(YEAR(TODAY()), 1, 1)) = 365,"NOT a Leap Year", "a Leap Year"))', + '=CONCATENATE(YEAR(TODAY()), + " is ", + IF( + DAYS(DATE(YEAR(TODAY())+1, 1, 1), DATE(YEAR(TODAY()), 1, 1)) = 365, + "NOT a Leap Year", + "a Leap Year" + ) + )', ] ]; From fffd6f6ac36debdc95cf52641eef8d34a3952a8e Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Sun, 28 Feb 2021 13:01:26 +0100 Subject: [PATCH 5/5] PHPCS --- tests/data/Calculation/Translations.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/data/Calculation/Translations.php b/tests/data/Calculation/Translations.php index 6083c9e242..d470a05cb2 100644 --- a/tests/data/Calculation/Translations.php +++ b/tests/data/Calculation/Translations.php @@ -44,5 +44,5 @@ "a Leap Year" ) )', - ] + ], ];