From 81931623a4d1e61010d90fab323a875b2813cf7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Adam=20W=C3=B3js?= Date: Wed, 20 Mar 2024 07:09:53 +0100 Subject: [PATCH] fixup! IBX-7987: Added extension point to skip nodes while extracting text --- .../config/settings/fieldtype_services.yaml | 6 +++ .../NodeFilterFactoryInterface.php | 14 ++++++ .../NodeFilter/NodeFilterFactory.php | 20 ++++++++ .../NodeFilter/AggregateFilterTest.php | 33 +++++++++++++ .../NodeFilter/NodePathFilterTest.php | 47 +++++++++++++++++++ 5 files changed, 120 insertions(+) create mode 100644 src/contracts/RichText/TextExtractor/NodeFilterFactoryInterface.php create mode 100644 src/lib/RichText/TextExtractor/NodeFilter/NodeFilterFactory.php create mode 100644 tests/lib/RichText/TextExtractor/NodeFilter/AggregateFilterTest.php create mode 100644 tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php diff --git a/src/bundle/Resources/config/settings/fieldtype_services.yaml b/src/bundle/Resources/config/settings/fieldtype_services.yaml index 09bdb2e0..54858900 100644 --- a/src/bundle/Resources/config/settings/fieldtype_services.yaml +++ b/src/bundle/Resources/config/settings/fieldtype_services.yaml @@ -73,12 +73,18 @@ services: Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface: alias: Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\AggregateFilter + Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterFactoryInterface: + alias: Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\NodeFilterFactory + + Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\NodeFilterFactory: ~ + Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\AggregateFilter: arguments: $filters: !tagged ibexa.field_type.richtext.text_extractor.node_filter ibexa.field_type.richtext.text_extractor.node_filter.template: class: Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\NodePathFilter + factory: ['@Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterFactoryInterface', 'createPathFilter'] arguments: ['eztemplate', 'ezconfig'] tags: - { name: ibexa.field_type.richtext.text_extractor.node_filter } diff --git a/src/contracts/RichText/TextExtractor/NodeFilterFactoryInterface.php b/src/contracts/RichText/TextExtractor/NodeFilterFactoryInterface.php new file mode 100644 index 00000000..fae43431 --- /dev/null +++ b/src/contracts/RichText/TextExtractor/NodeFilterFactoryInterface.php @@ -0,0 +1,14 @@ +createMock(DOMNode::class); + + $filterA = $this->createMock(NodeFilterInterface::class); + $filterA->expects(self::once())->method('filter')->with($node)->willReturn(false); + $filterB = $this->createMock(NodeFilterInterface::class); + $filterB->expects(self::once())->method('filter')->with($node)->willReturn(true); + $filterC = $this->createMock(NodeFilterInterface::class); + $filterC->expects(self::never())->method('filter'); + + $aggregateFilter = new AggregateFilter([$filterA, $filterB, $filterC]); + + self::assertTrue($aggregateFilter->filter($node)); + } +} diff --git a/tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php b/tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php new file mode 100644 index 00000000..f48aaf77 --- /dev/null +++ b/tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php @@ -0,0 +1,47 @@ +loadXML(''); + + $nodeB = $this->getNode($document, '//b'); + $nodeC = $this->getNode($document, '//c'); + + $filter = new NodePathFilter('b', 'c'); + + self::assertTrue($filter->filter($nodeB)); + self::assertFalse($filter->filter($nodeC)); + } + + private function getNode(DOMDocument $document, string $expression): DOMNode + { + $xpath = new DOMXPath($document); + + $results = $xpath->query($expression); + if ($results instanceof DOMNodeList) { + /** @var \DOMNode */ + return $results->item(0); + } + + throw new RuntimeException("Expression '$expression' did not return a node."); + } +}