Skip to content

Commit

Permalink
fixup! IBX-7987: Added extension point to skip nodes while extracting…
Browse files Browse the repository at this point in the history
… text
  • Loading branch information
adamwojs committed Mar 20, 2024
1 parent b4ac7dc commit 8193162
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 0 deletions.
6 changes: 6 additions & 0 deletions src/bundle/Resources/config/settings/fieldtype_services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
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 src/lib/RichText/TextExtractor/NodeFilter/NodeFilterFactory.php
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);
}
}
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 tests/lib/RichText/TextExtractor/NodeFilter/NodePathFilterTest.php
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.");
}
}

0 comments on commit 8193162

Please sign in to comment.