diff --git a/phpstan-baseline.neon b/phpstan-baseline.neon index 308b4d09677635cc3a8dc19eb3c851ff4fe73c64..002dc99d16a3c7ba1fd4932269bc5c23b821c2bb 100644 --- a/phpstan-baseline.neon +++ b/phpstan-baseline.neon @@ -1,196 +1,235 @@ parameters: ignoreErrors: - - message: "#^Method Drupal\\\\entity_usage_updater\\\\EntityUsageUpdater\\:\\:finish\\(\\) has no return type specified\\.$#" + message: '#^Method Drupal\\entity_usage_updater\\EntityUsageUpdater\:\:finish\(\) has no return type specified\.$#' + identifier: missingType.return count: 1 path: src/EntityUsageUpdater.php - - message: "#^Method Drupal\\\\entity_usage_updater\\\\EntityUsageUpdater\\:\\:updateReferences\\(\\) has no return type specified\\.$#" + message: '#^Method Drupal\\entity_usage_updater\\EntityUsageUpdater\:\:updateReferences\(\) has no return type specified\.$#' + identifier: missingType.return count: 1 path: src/EntityUsageUpdater.php - - message: "#^Method Drupal\\\\entity_usage_updater\\\\Form\\\\SettingsForm\\:\\:create\\(\\) has no return type specified\\.$#" + message: '#^Method Drupal\\entity_usage_updater\\Form\\SettingsForm\:\:create\(\) has no return type specified\.$#' + identifier: missingType.return count: 1 path: src/Form/SettingsForm.php - - message: "#^If condition is always true\\.$#" + message: '#^If condition is always true\.$#' + identifier: if.alwaysTrue count: 1 path: src/Plugin/EntityUsageUpdater/HtmlLink.php - - message: "#^Method Drupal\\\\entity_usage_updater\\\\Plugin\\\\EntityUsageUpdater\\\\HtmlLink\\:\\:getUuid\\(\\) has parameter \\$entity_id with no type specified\\.$#" + message: '#^Method Drupal\\entity_usage_updater\\Plugin\\EntityUsageUpdater\\HtmlLink\:\:getUuid\(\) has parameter \$entity_id with no type specified\.$#' + identifier: missingType.parameter count: 1 path: src/Plugin/EntityUsageUpdater/HtmlLink.php - - message: "#^Method Drupal\\\\entity_usage_updater\\\\Plugin\\\\EntityUsageUpdater\\\\HtmlLink\\:\\:parseEntitiesFromText\\(\\) has no return type specified\\.$#" + message: '#^Method Drupal\\entity_usage_updater\\Plugin\\EntityUsageUpdater\\HtmlLink\:\:parseEntitiesFromText\(\) has no return type specified\.$#' + identifier: missingType.return count: 1 path: src/Plugin/EntityUsageUpdater/HtmlLink.php - - message: "#^Method Drupal\\\\entity_usage_updater\\\\Plugin\\\\EntityUsageUpdater\\\\HtmlLink\\:\\:submitConfigurationForm\\(\\) has no return type specified\\.$#" + message: '#^Method Drupal\\entity_usage_updater\\Plugin\\EntityUsageUpdater\\HtmlLink\:\:submitConfigurationForm\(\) has no return type specified\.$#' + identifier: missingType.return count: 1 path: src/Plugin/EntityUsageUpdater/HtmlLink.php - - message: "#^Method Drupal\\\\entity_usage_updater\\\\Plugin\\\\EntityUsageUpdater\\\\HtmlLink\\:\\:validateConfigurationForm\\(\\) has no return type specified\\.$#" + message: '#^Method Drupal\\entity_usage_updater\\Plugin\\EntityUsageUpdater\\HtmlLink\:\:validateConfigurationForm\(\) has no return type specified\.$#' + identifier: missingType.return count: 1 path: src/Plugin/EntityUsageUpdater/HtmlLink.php - - message: "#^Parameter \\#4 \\$text of method Drupal\\\\entity_usage_updater\\\\Plugin\\\\EntityUsageUpdater\\\\LinkIt\\:\\:updateItemProperty\\(\\) expects Drupal\\\\Core\\\\TypedData\\\\Type\\\\StringInterface, Drupal\\\\Core\\\\TypedData\\\\TypedDataInterface given\\.$#" + message: '#^Parameter \#4 \$text of method Drupal\\entity_usage_updater\\Plugin\\EntityUsageUpdater\\LinkIt\:\:updateItemProperty\(\) expects Drupal\\Core\\TypedData\\Type\\StringInterface, Drupal\\Core\\TypedData\\TypedDataInterface given\.$#' + identifier: argument.type count: 2 path: src/Plugin/EntityUsageUpdater/LinkIt.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/EntityUsageUpdaterParagraphsTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\RevisionableInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\RevisionableInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 2 path: tests/src/Kernel/EntityUsageUpdaterParagraphsTest.php - - message: "#^Access to an undefined property Drupal\\\\Core\\\\Entity\\\\TranslatableInterface\\:\\:\\$field_reference\\.$#" + message: '#^Access to an undefined property Drupal\\Core\\Entity\\TranslatableInterface\:\:\$field_reference\.$#' + identifier: property.notFound count: 7 path: tests/src/Kernel/EntityUsageUpdaterSimpleMulRevTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\TranslatableRevisionableInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\TranslatableRevisionableInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/EntityUsageUpdaterSimpleMulRevTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:get\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/EntityUsageUpdaterSimpleRevTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\RevisionableInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\RevisionableInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/EntityUsageUpdaterSimpleRevTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/EntityUsageUpdaterSimpleTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/EntityUsageUpdaterViolationTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:get\(\)\.$#' + identifier: method.notFound count: 4 path: tests/src/Kernel/Plugin/EntityReferencePluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 2 path: tests/src/Kernel/Plugin/EntityReferencePluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:get\(\)\.$#' + identifier: method.notFound count: 2 path: tests/src/Kernel/Plugin/HtmlLinkPluginLinkItTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/Plugin/HtmlLinkPluginLinkItTest.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Kernel\\\\Plugin\\\\HtmlLinkPluginLinkItTest\\:\\:updateLinkTexts\\(\\) return type has no value type specified in iterable type iterable\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Kernel\\Plugin\\HtmlLinkPluginLinkItTest\:\:updateLinkTexts\(\) return type has no value type specified in iterable type iterable\.$#' + identifier: missingType.iterableValue count: 1 path: tests/src/Kernel/Plugin/HtmlLinkPluginLinkItTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:addTranslation\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:addTranslation\(\)\.$#' + identifier: method.notFound count: 1 path: tests/src/Kernel/Plugin/HtmlLinkPluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#" - count: 6 + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:get\(\)\.$#' + identifier: method.notFound + count: 8 path: tests/src/Kernel/Plugin/HtmlLinkPluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" - count: 3 + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound + count: 4 path: tests/src/Kernel/Plugin/HtmlLinkPluginTest.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Kernel\\\\Plugin\\\\HtmlLinkPluginTest\\:\\:removeLinkTexts\\(\\) return type has no value type specified in iterable type iterable\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Kernel\\Plugin\\HtmlLinkPluginTest\:\:removeLinkTexts\(\) return type has no value type specified in iterable type iterable\.$#' + identifier: missingType.iterableValue count: 1 path: tests/src/Kernel/Plugin/HtmlLinkPluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:get\(\)\.$#' + identifier: method.notFound count: 4 path: tests/src/Kernel/Plugin/LinkItPluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 2 path: tests/src/Kernel/Plugin/LinkItPluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:get\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:get\(\)\.$#' + identifier: method.notFound count: 4 path: tests/src/Kernel/Plugin/LinkPluginTest.php - - message: "#^Call to an undefined method Drupal\\\\Core\\\\Entity\\\\EntityInterface\\:\\:set\\(\\)\\.$#" + message: '#^Call to an undefined method Drupal\\Core\\Entity\\EntityInterface\:\:set\(\)\.$#' + identifier: method.notFound count: 2 path: tests/src/Kernel/Plugin/LinkPluginTest.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:alert\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:alert\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:critical\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:critical\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:debug\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:debug\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:emergency\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:emergency\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:error\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:error\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:info\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:info\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:log\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:log\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:notice\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:notice\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php - - message: "#^Method Drupal\\\\Tests\\\\entity_usage_updater\\\\Tools\\\\TestLogger\\:\\:warning\\(\\) has parameter \\$message with no type specified\\.$#" + message: '#^Method Drupal\\Tests\\entity_usage_updater\\Tools\\TestLogger\:\:warning\(\) has parameter \$message with no type specified\.$#' + identifier: missingType.parameter count: 1 path: tests/src/Tools/TestLogger.php diff --git a/src/LinkRemoverTrait.php b/src/LinkRemoverTrait.php index 628dc9309e125fea37de169332d0c9204bfb82da..a8b5d6518a11c08ed38dac59b1d49c73294db7fd 100644 --- a/src/LinkRemoverTrait.php +++ b/src/LinkRemoverTrait.php @@ -3,6 +3,7 @@ namespace Drupal\entity_usage_updater; use Drupal\Component\Utility\Html; +use Drupal\Component\Utility\UrlHelper; use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Entity\EntityTypeManagerInterface; use Drupal\Core\TypedData\Type\StringInterface; @@ -67,9 +68,14 @@ trait LinkRemoverTrait { ) { // Copy inner HTML to a new fragment. $fragment = $dom->createDocumentFragment(); - $fragment->appendXML( - array_reduce(iterator_to_array($element->childNodes), fn(string $carry, \DomNode $child) => $carry . $dom->saveXML($child), '') - ); + $parsed_url = UrlHelper::parse($href); + // If the text content contains the href then remove it completely. + if (!str_contains($element->textContent, $parsed_url['path'])) { + $fragment->appendXML( + array_reduce(iterator_to_array($element->childNodes), fn(string $carry, \DomNode $child) => $carry . $dom->saveXML($child), '') + ); + } + // Replace link with fragment. $element->parentNode->replaceChild($fragment, $element); $elements_replaced = TRUE; diff --git a/tests/src/Kernel/EntityUsageUpdaterKernelTestBase.php b/tests/src/Kernel/EntityUsageUpdaterKernelTestBase.php index af4a7a1dcb174371269017da1b817d7165449de0..50bd06bb3a9b84ad473aaa40a710e4b192cbd509 100644 --- a/tests/src/Kernel/EntityUsageUpdaterKernelTestBase.php +++ b/tests/src/Kernel/EntityUsageUpdaterKernelTestBase.php @@ -38,6 +38,7 @@ abstract class EntityUsageUpdaterKernelTestBase extends EntityKernelTestBase { protected function setUp(): void { parent::setUp(); $this->installConfig(['entity_usage']); + $this->config('entity_usage.settings')->set('site_domains', ['http://localhost'])->save(); $this->installSchema('entity_usage', ['entity_usage']); $this->installEntitySchema(static::$entityTypeId); $this->storage = $this->container->get('entity_type.manager') diff --git a/tests/src/Kernel/Plugin/HtmlLinkPluginTest.php b/tests/src/Kernel/Plugin/HtmlLinkPluginTest.php index 6bdcc6aa66930b986bc10058ca0f4b5fd60a89f2..35b8e65834f18ebb52d3039433b5122475cacfd2 100644 --- a/tests/src/Kernel/Plugin/HtmlLinkPluginTest.php +++ b/tests/src/Kernel/Plugin/HtmlLinkPluginTest.php @@ -105,6 +105,10 @@ class HtmlLinkPluginTest extends HtmlLinkPluginTestBase { '<p>Invalid markup (missing closing p tag).', ]; + yield 'Correct markup with link in the link text' => [ + '<p>Some text with <a href="%%ENTITY_URL%%?foo=bar#fragment">%%ENTITY_URL%%</a>.</p>', + '<p>Some text with .</p>', + ]; } /** @@ -125,4 +129,22 @@ class HtmlLinkPluginTest extends HtmlLinkPluginTestBase { $this->assertSame($expected, $this->entity->get('test_field')->value); } + /** + * Tests removing a link in a text field. + * + * @dataProvider removeLinkTexts + */ + public function testRemoveAbsolute(string $initial, string $expected): void { + $target = $this->storage->create(); + $target->save(); + $new_entity = $this->storage->create(); + $new_entity->save(); + $initial_text = str_replace('%%ENTITY_URL%%', $target->toUrl()->setAbsolute()->toString(), $initial); + $this->entity->set('test_field', $initial_text); + + $this->plugin->remove($target, $this->entity->get('test_field')[0]); + + $this->assertSame($expected, $this->entity->get('test_field')->value); + } + }