From 815dabae897292f5d3c223669905c0e1d224d46f Mon Sep 17 00:00:00 2001 From: xandros15 Date: Fri, 30 Apr 2021 15:22:07 +0200 Subject: [PATCH 1/7] #984 add support notContainsText for conditional styles in xlsx --- src/PhpSpreadsheet/Style/Conditional.php | 1 + src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 12 +++- .../Writer/Xlsx/ConditionalTest.php | 60 +++++++++++++++++++ 3 files changed, 70 insertions(+), 3 deletions(-) create mode 100644 tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php diff --git a/src/PhpSpreadsheet/Style/Conditional.php b/src/PhpSpreadsheet/Style/Conditional.php index b008c9f28b..2fd415af17 100644 --- a/src/PhpSpreadsheet/Style/Conditional.php +++ b/src/PhpSpreadsheet/Style/Conditional.php @@ -15,6 +15,7 @@ class Conditional implements IComparable const CONDITION_CONTAINSBLANKS = 'containsBlanks'; const CONDITION_NOTCONTAINSBLANKS = 'notContainsBlanks'; const CONDITION_DATABAR = 'dataBar'; + const CONDITION_NOTCONTAINSTEXT = 'notContainsText'; // Operator types const OPERATOR_NONE = ''; diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 3978eb6f04..4c2aeb394a 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -634,15 +634,21 @@ private function writeConditionalFormatting(XMLWriter $objWriter, Phpspreadsheet self::writeAttributeif( $objWriter, - ($conditional->getConditionType() == Conditional::CONDITION_CELLIS || $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT) - && $conditional->getOperatorType() != Conditional::OPERATOR_NONE, + in_array($conditional->getConditionType(), [ + Conditional::CONDITION_CELLIS, + Conditional::CONDITION_CONTAINSTEXT, + Conditional::CONDITION_NOTCONTAINSTEXT, + ]) && $conditional->getOperatorType() != Conditional::OPERATOR_NONE, 'operator', $conditional->getOperatorType() ); self::writeAttributeIf($objWriter, $conditional->getStopIfTrue(), 'stopIfTrue', '1'); - if ($conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT) { + if (in_array($conditional->getConditionType(), [ + Conditional::CONDITION_CONTAINSTEXT, + Conditional::CONDITION_NOTCONTAINSTEXT, + ])) { self::writeTextCondElements($objWriter, $conditional, $cellCoordinate); } else { self::writeOtherCondElements($objWriter, $conditional, $cellCoordinate); diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php new file mode 100644 index 0000000000..1bcad0d1e6 --- /dev/null +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php @@ -0,0 +1,60 @@ +prevValue = Settings::getLibXmlLoaderOptions(); + + // Disable validating XML with the DTD + Settings::setLibXmlLoaderOptions($this->prevValue & ~LIBXML_DTDVALID & ~LIBXML_DTDATTR & ~LIBXML_DTDLOAD); + } + + protected function tearDown(): void + { + Settings::setLibXmlLoaderOptions($this->prevValue); + } + + /** + * Test check if conditional style with type 'notContainsText' works on xlsx + */ + public function testConditionalNotContainsText(): void + { + $spreadsheet = new Spreadsheet(); + $worksheet = $spreadsheet->getActiveSheet(); + + $condition = new Conditional(); + $condition->setConditionType(Conditional::CONDITION_NOTCONTAINSTEXT); + $condition->setOperatorType(Conditional::OPERATOR_NOTCONTAINS); + $condition->setText("C"); + $condition->getStyle()->applyFromArray([ + 'fill' => [ + 'color' => ['argb' => 'FFFFC000'], + 'fillType' => Fill::FILL_SOLID, + ], + ]); + $worksheet->setConditionalStyles('A1:A5', [$condition]); + + $writer = new Xlsx($spreadsheet); + $writerWorksheet = new Xlsx\Worksheet($writer); + $data = $writerWorksheet->writeWorksheet($worksheet, []); + $needle = <<ISERROR(SEARCH("C",A1:A5)) +xml; + $this->assertStringContainsString($needle, $data); + } +} From 0b0215514ab0942c836dc23241b570856eaf117e Mon Sep 17 00:00:00 2001 From: xandros15 Date: Fri, 30 Apr 2021 15:30:43 +0200 Subject: [PATCH 2/7] #984 update changelog --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index 21762f893b..5fcace6fd5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ and this project adheres to [Semantic Versioning](https://semver.org). - Improved support for Row and Column ranges in formulae [Issue #1755](https://github.com/PHPOffice/PhpSpreadsheet/issues/1755) [PR #2028](https://github.com/PHPOffice/PhpSpreadsheet/pull/2028) - Implemented the CHITEST(), CHISQ.DIST() and CHISQ.INV() and equivalent Statistical functions, for both left- and right-tailed distributions. - Support for ActiveSheet and SelectedCells in the ODS Reader and Writer. [PR #1908](https://github.com/PHPOffice/PhpSpreadsheet/pull/1908) +- Support for notContainsText Conditional Style in xlsx [Issue #984](https://github.com/PHPOffice/PhpSpreadsheet/issues/984) ### Changed From 759c192a447b56f296ead23464c65eb9edfe2018 Mon Sep 17 00:00:00 2001 From: xandros15 Date: Sun, 2 May 2021 20:42:28 +0200 Subject: [PATCH 3/7] #984 fix phpcs warnings --- src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 4c2aeb394a..83238d7a3c 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -634,21 +634,21 @@ private function writeConditionalFormatting(XMLWriter $objWriter, Phpspreadsheet self::writeAttributeif( $objWriter, - in_array($conditional->getConditionType(), [ - Conditional::CONDITION_CELLIS, - Conditional::CONDITION_CONTAINSTEXT, - Conditional::CONDITION_NOTCONTAINSTEXT, - ]) && $conditional->getOperatorType() != Conditional::OPERATOR_NONE, + ( + $conditional->getConditionType() == Conditional::CONDITION_CELLIS + || $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT + || $conditional->getConditionType() == Conditional::CONDITION_NOTCONTAINSTEXT + ) && $conditional->getOperatorType() != Conditional::OPERATOR_NONE, 'operator', $conditional->getOperatorType() ); self::writeAttributeIf($objWriter, $conditional->getStopIfTrue(), 'stopIfTrue', '1'); - if (in_array($conditional->getConditionType(), [ - Conditional::CONDITION_CONTAINSTEXT, - Conditional::CONDITION_NOTCONTAINSTEXT, - ])) { + if ( + $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT + || $conditional->getConditionType() == Conditional::CONDITION_NOTCONTAINSTEXT + ) { self::writeTextCondElements($objWriter, $conditional, $cellCoordinate); } else { self::writeOtherCondElements($objWriter, $conditional, $cellCoordinate); From a75769299206f15389e1701132bcc1e9bb3a42f6 Mon Sep 17 00:00:00 2001 From: xandros15 Date: Sun, 2 May 2021 22:09:38 +0200 Subject: [PATCH 4/7] #984 add support notContainsText for conditional styles in xlsx reader --- .../Reader/Xlsx/ConditionalStyles.php | 1 + .../Reader/Xlsx/ConditionalTest.php | 31 ++++++++++++++++++ .../XLSX/conditionalFormatting3Test.xlsx | Bin 0 -> 9486 bytes 3 files changed, 32 insertions(+) create mode 100644 tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php create mode 100644 tests/data/Reader/XLSX/conditionalFormatting3Test.xlsx diff --git a/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php b/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php index 5f6cb4dc35..8036137187 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php +++ b/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php @@ -45,6 +45,7 @@ private function readConditionalStyles($xmlSheet) || (string) $cfRule['type'] == Conditional::CONDITION_CONTAINSTEXT || (string) $cfRule['type'] == Conditional::CONDITION_CONTAINSBLANKS || (string) $cfRule['type'] == Conditional::CONDITION_NOTCONTAINSBLANKS + || (string) $cfRule['type'] == Conditional::CONDITION_NOTCONTAINSTEXT || (string) $cfRule['type'] == Conditional::CONDITION_EXPRESSION) && isset($this->dxfs[(int) ($cfRule['dxfId'])]) ) { diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php new file mode 100644 index 0000000000..6ee7bc37cb --- /dev/null +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php @@ -0,0 +1,31 @@ +load($filename); + $worksheet = $spreadsheet->getActiveSheet(); + $styles = $worksheet->getConditionalStyles('A1:A5'); + + $this->assertCount(1, $styles); + + /** @var $notContainsTextStyle Conditional */ + $notContainsTextStyle = $styles[0]; + $this->assertEquals('A', $notContainsTextStyle->getText()); + $this->assertEquals(Conditional::CONDITION_NOTCONTAINSTEXT, $notContainsTextStyle->getConditionType()); + $this->assertEquals(Conditional::OPERATOR_NOTCONTAINS, $notContainsTextStyle->getOperatorType()); + } +} diff --git a/tests/data/Reader/XLSX/conditionalFormatting3Test.xlsx b/tests/data/Reader/XLSX/conditionalFormatting3Test.xlsx new file mode 100644 index 0000000000000000000000000000000000000000..ee23e21c8c5d9a6c1cf5b819de6a30954d0bad5c GIT binary patch literal 9486 zcmeHtby!qu*Z$BQ;?N?EpfpH#4k=QCbT>nHcS?6DARUs@NJ|f0(y4T}e53DqKhNR# z{;v1$?|1fGdp7&pvu^gY_Pw5UuV-xq88~<#01x6=ax<+{lB!jUaE zsw?`Hn})UB&M8C`Rg2rzL0y;%5t37{dM!6$D7CFvaJgf=qj9PpNe5OO>t6P^2P!@g z40?a^RHKmkvtFzB6E+hR`qP4T?3^_eJaa9Dlcx14cWQeB@sql{@@(nAqZZ~A3cRPB zs&`~x>$}x;I*X6|R+YbOW!hT?zC|u;<@~tOd03c+V>{|G+l$V(J)E`c_Lo&78X-FAM zHik+WGo&Yi>Xf@5J8&lWJYz>0%2{i7!~>}0Jm1PR0U;YMC96N=#Q$)lJ%wlFN$fOf z^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zwzKLz;^Q%B3ncQuk(y=GlW{<9V~uF$@>vlG$0X^>unh}#ad5Y+ciD9R#7&M?LT1wo z*F(ZwL-`;hMlk|(y`1`(3?T>+xV6MGNc;T|(WdXt_1!BPd6nR}$c!P|d`mh+TL)54 zexkYB!C7@u%8mRB$K!+XS8^8fyV8InRJvrFPyV(OEM>!lFE$Y}Wg9U#z68r@o|PfG zx~CE`b%A!!(AA=)B07U$7yJ5m_PiP;ROIbaV_l_>7@GB}5GeaU?(OQFH->ZwWjwrz zN`5GToEC?2NUVT+v7+%GdI4#cl3#+L%wva&JnTO+PtV5YAL9L>+`qQ;xIy`CX6(T8 zA8jLk$Jf{*2?hZd55mK(-`DO$vY9bx@*c_NB<8oqiM^-8ulQ7)!kv>dyjsyPZ{;y5 zB!VwW-kqaqh;2!)rG~3t@6u5BfG`&2{UM7G`I*m(YF1XEdf0O+J0C&~c>2!Nf=ua9 zCk$eL%0#$lDze0)>3IneD(m!oVvX}cWZb>S7AU-a3L>sAy@|pWY-b^0xqed(0S~Pm z3&hE9Wo6A(K^KWvT{A!y)p-6rdKc>8Z2boAX)C+xsruLs<-yWJo8j&T*2luV2EzRQ zX2Be6jPF!2Da6`qDzr=gPx3p+(jN|F=&fU37;5JRZ3j=qGW;vk!oV^>)#LBqqxko;`uFiSZ&fJB{1xD@ZQp+z{y4@$1L99j z;QNMu?V7DTuX}k9tv`N) z^0S(~k8;0k`GsuBMa+GK`vt-;gaN1;fZm7w|CS5)P48>TU#4FOf12J?lD}1! z_d)+EHhw`u6Tl;A8FOEH+&BMg#`v>&*yBH$|B*iwWDub7003Y@Kb6psk|6(a_CGC9 BGW`Gm literal 0 HcmV?d00001 From 2b72c7e91255f1c17ea2c9137de99750c7d80497 Mon Sep 17 00:00:00 2001 From: xandros15 Date: Mon, 3 May 2021 18:52:15 +0200 Subject: [PATCH 5/7] #984 fix Scrutinizer warnings --- .../Reader/Xlsx/ConditionalStyles.php | 12 +--------- src/PhpSpreadsheet/Style/Conditional.php | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 11 deletions(-) diff --git a/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php b/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php index 8036137187..dcd7ad12cb 100644 --- a/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php +++ b/src/PhpSpreadsheet/Reader/Xlsx/ConditionalStyles.php @@ -2,7 +2,6 @@ namespace PhpOffice\PhpSpreadsheet\Reader\Xlsx; -use PhpOffice\PhpSpreadsheet\Style\Color; use PhpOffice\PhpSpreadsheet\Style\Conditional; use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalDataBar; use PhpOffice\PhpSpreadsheet\Style\ConditionalFormatting\ConditionalFormattingRuleExtension; @@ -39,16 +38,7 @@ private function readConditionalStyles($xmlSheet) $conditionals = []; foreach ($xmlSheet->conditionalFormatting as $conditional) { foreach ($conditional->cfRule as $cfRule) { - if ( - ((string) $cfRule['type'] == Conditional::CONDITION_NONE - || (string) $cfRule['type'] == Conditional::CONDITION_CELLIS - || (string) $cfRule['type'] == Conditional::CONDITION_CONTAINSTEXT - || (string) $cfRule['type'] == Conditional::CONDITION_CONTAINSBLANKS - || (string) $cfRule['type'] == Conditional::CONDITION_NOTCONTAINSBLANKS - || (string) $cfRule['type'] == Conditional::CONDITION_NOTCONTAINSTEXT - || (string) $cfRule['type'] == Conditional::CONDITION_EXPRESSION) - && isset($this->dxfs[(int) ($cfRule['dxfId'])]) - ) { + if (Conditional::isValidConditionType((string) $cfRule['type']) && isset($this->dxfs[(int) ($cfRule['dxfId'])])) { $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule; } elseif ((string) $cfRule['type'] == Conditional::CONDITION_DATABAR) { $conditionals[(string) $conditional['sqref']][(int) ($cfRule['priority'])] = $cfRule; diff --git a/src/PhpSpreadsheet/Style/Conditional.php b/src/PhpSpreadsheet/Style/Conditional.php index 2fd415af17..8352072215 100644 --- a/src/PhpSpreadsheet/Style/Conditional.php +++ b/src/PhpSpreadsheet/Style/Conditional.php @@ -17,6 +17,17 @@ class Conditional implements IComparable const CONDITION_DATABAR = 'dataBar'; const CONDITION_NOTCONTAINSTEXT = 'notContainsText'; + private const CONDITION_TYPES = [ + self::CONDITION_CELLIS, + self::CONDITION_CONTAINSBLANKS, + self::CONDITION_CONTAINSTEXT, + self::CONDITION_DATABAR, + self::CONDITION_EXPRESSION, + self::CONDITION_NONE, + self::CONDITION_NOTCONTAINSBLANKS, + self::CONDITION_NOTCONTAINSTEXT, + ]; + // Operator types const OPERATOR_NONE = ''; const OPERATOR_BEGINSWITH = 'beginsWith'; @@ -301,4 +312,16 @@ public function __clone() } } } + + /** + * Verify if param is valid condition type + * + * @param string $type + * + * @return bool + */ + public static function isValidConditionType(string $type): bool + { + return in_array($type, self::CONDITION_TYPES); + } } From d0f76fd3ba5095ec1971741fb4f43a5034731ec4 Mon Sep 17 00:00:00 2001 From: xandros15 Date: Tue, 4 May 2021 11:41:55 +0200 Subject: [PATCH 6/7] #984 change to force typing in condition --- src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php index 83238d7a3c..2c8e6f58c3 100644 --- a/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php +++ b/src/PhpSpreadsheet/Writer/Xlsx/Worksheet.php @@ -635,10 +635,10 @@ private function writeConditionalFormatting(XMLWriter $objWriter, Phpspreadsheet self::writeAttributeif( $objWriter, ( - $conditional->getConditionType() == Conditional::CONDITION_CELLIS - || $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT - || $conditional->getConditionType() == Conditional::CONDITION_NOTCONTAINSTEXT - ) && $conditional->getOperatorType() != Conditional::OPERATOR_NONE, + $conditional->getConditionType() === Conditional::CONDITION_CELLIS + || $conditional->getConditionType() === Conditional::CONDITION_CONTAINSTEXT + || $conditional->getConditionType() === Conditional::CONDITION_NOTCONTAINSTEXT + ) && $conditional->getOperatorType() !== Conditional::OPERATOR_NONE, 'operator', $conditional->getOperatorType() ); @@ -646,8 +646,8 @@ private function writeConditionalFormatting(XMLWriter $objWriter, Phpspreadsheet self::writeAttributeIf($objWriter, $conditional->getStopIfTrue(), 'stopIfTrue', '1'); if ( - $conditional->getConditionType() == Conditional::CONDITION_CONTAINSTEXT - || $conditional->getConditionType() == Conditional::CONDITION_NOTCONTAINSTEXT + $conditional->getConditionType() === Conditional::CONDITION_CONTAINSTEXT + || $conditional->getConditionType() === Conditional::CONDITION_NOTCONTAINSTEXT ) { self::writeTextCondElements($objWriter, $conditional, $cellCoordinate); } else { From bb11378fca2e8026670ab35b8c6bb53e2c6aef9c Mon Sep 17 00:00:00 2001 From: xandros15 Date: Tue, 11 May 2021 12:44:40 +0200 Subject: [PATCH 7/7] #984 fix php-cs-fixer warnings --- src/PhpSpreadsheet/Style/Conditional.php | 6 +----- .../Reader/Xlsx/ConditionalTest.php | 13 ++++++------- .../Writer/Xlsx/ConditionalTest.php | 6 +++--- 3 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/PhpSpreadsheet/Style/Conditional.php b/src/PhpSpreadsheet/Style/Conditional.php index 8352072215..4cbc2746af 100644 --- a/src/PhpSpreadsheet/Style/Conditional.php +++ b/src/PhpSpreadsheet/Style/Conditional.php @@ -314,11 +314,7 @@ public function __clone() } /** - * Verify if param is valid condition type - * - * @param string $type - * - * @return bool + * Verify if param is valid condition type. */ public static function isValidConditionType(string $type): bool { diff --git a/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php b/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php index 6ee7bc37cb..8108d988ad 100644 --- a/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php +++ b/tests/PhpSpreadsheetTests/Reader/Xlsx/ConditionalTest.php @@ -8,9 +8,8 @@ class ConditionalTest extends AbstractFunctional { - /** - * Test check if conditional style with type 'notContainsText' works on xlsx + * Test check if conditional style with type 'notContainsText' works on xlsx. */ public function testConditionalNotContainsText(): void { @@ -20,12 +19,12 @@ public function testConditionalNotContainsText(): void $worksheet = $spreadsheet->getActiveSheet(); $styles = $worksheet->getConditionalStyles('A1:A5'); - $this->assertCount(1, $styles); + self::assertCount(1, $styles); - /** @var $notContainsTextStyle Conditional */ + /** @var Conditional $notContainsTextStyle */ $notContainsTextStyle = $styles[0]; - $this->assertEquals('A', $notContainsTextStyle->getText()); - $this->assertEquals(Conditional::CONDITION_NOTCONTAINSTEXT, $notContainsTextStyle->getConditionType()); - $this->assertEquals(Conditional::OPERATOR_NOTCONTAINS, $notContainsTextStyle->getOperatorType()); + self::assertEquals('A', $notContainsTextStyle->getText()); + self::assertEquals(Conditional::CONDITION_NOTCONTAINSTEXT, $notContainsTextStyle->getConditionType()); + self::assertEquals(Conditional::OPERATOR_NOTCONTAINS, $notContainsTextStyle->getOperatorType()); } } diff --git a/tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php b/tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php index 1bcad0d1e6..21df6ed6ee 100644 --- a/tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php +++ b/tests/PhpSpreadsheetTests/Writer/Xlsx/ConditionalTest.php @@ -30,7 +30,7 @@ protected function tearDown(): void } /** - * Test check if conditional style with type 'notContainsText' works on xlsx + * Test check if conditional style with type 'notContainsText' works on xlsx. */ public function testConditionalNotContainsText(): void { @@ -40,7 +40,7 @@ public function testConditionalNotContainsText(): void $condition = new Conditional(); $condition->setConditionType(Conditional::CONDITION_NOTCONTAINSTEXT); $condition->setOperatorType(Conditional::OPERATOR_NOTCONTAINS); - $condition->setText("C"); + $condition->setText('C'); $condition->getStyle()->applyFromArray([ 'fill' => [ 'color' => ['argb' => 'FFFFC000'], @@ -55,6 +55,6 @@ public function testConditionalNotContainsText(): void $needle = <<ISERROR(SEARCH("C",A1:A5)) xml; - $this->assertStringContainsString($needle, $data); + self::assertStringContainsString($needle, $data); } }