-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixup! IBX-7987: Added extension point to skip nodes while extracting…
… text
- Loading branch information
Showing
5 changed files
with
120 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
src/contracts/RichText/TextExtractor/NodeFilterFactoryInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor; | ||
|
||
interface NodeFilterFactoryInterface | ||
{ | ||
public function createPathFilter(string ...$path): NodeFilterInterface; | ||
} |
20 changes: 20 additions & 0 deletions
20
src/lib/RichText/TextExtractor/NodeFilter/NodeFilterFactory.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter; | ||
|
||
use Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterFactoryInterface; | ||
use Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface; | ||
|
||
final class NodeFilterFactory implements NodeFilterFactoryInterface | ||
{ | ||
public function createPathFilter(string ...$path): NodeFilterInterface | ||
{ | ||
return new NodePathFilter(...$path); | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
tests/lib/RichText/TextExtractor/NodeFilter/AggregateFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\FieldTypeRichText\RichText\TextExtractor\NodeFilter; | ||
|
||
use DOMNode; | ||
use Ibexa\Contracts\FieldTypeRichText\RichText\TextExtractor\NodeFilterInterface; | ||
use Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\AggregateFilter; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
final class AggregateFilterTest extends TestCase | ||
{ | ||
public function testFilter(): void | ||
{ | ||
$node = $this->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)); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
<?php | ||
|
||
/** | ||
* @copyright Copyright (C) Ibexa AS. All rights reserved. | ||
* @license For full copyright and license information view LICENSE file distributed with this source code. | ||
*/ | ||
declare(strict_types=1); | ||
|
||
namespace Ibexa\Tests\FieldTypeRichText\RichText\TextExtractor\NodeFilter; | ||
|
||
use DOMDocument; | ||
use DOMNode; | ||
use DOMNodeList; | ||
use DOMXPath; | ||
use Ibexa\FieldTypeRichText\RichText\TextExtractor\NodeFilter\NodePathFilter; | ||
use PHPUnit\Framework\TestCase; | ||
use RuntimeException; | ||
|
||
final class NodePathFilterTest extends TestCase | ||
{ | ||
public function testFilter(): void | ||
{ | ||
$document = new DOMDocument(); | ||
$document->loadXML('<a><b><c></c></b></a>'); | ||
|
||
$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."); | ||
} | ||
} |