diff --git a/tests/src/Kernel/Matchers/NodeMatcherTest.php b/tests/src/Kernel/Matchers/NodeMatcherTest.php index 0ab5550..9b0ed6e 100644 --- a/tests/src/Kernel/Matchers/NodeMatcherTest.php +++ b/tests/src/Kernel/Matchers/NodeMatcherTest.php @@ -2,6 +2,7 @@ namespace Drupal\Tests\linkit\Kernel\Matchers; +use Drupal\language\Entity\ConfigurableLanguage; use Drupal\node\Entity\Node; use Drupal\node\Entity\NodeType; use Drupal\Tests\linkit\Kernel\LinkitKernelTestBase; @@ -18,7 +19,13 @@ class NodeMatcherTest extends LinkitKernelTestBase { * * @var array */ - public static $modules = ['field', 'node', 'content_moderation', 'workflows']; + public static $modules = [ + 'field', + 'node', + 'content_moderation', + 'workflows', + 'language', + ]; /** * The matcher manager. @@ -35,7 +42,7 @@ class NodeMatcherTest extends LinkitKernelTestBase { $this->installEntitySchema('node'); $this->installSchema('node', ['node_access']); - $this->installConfig(['field', 'node']); + $this->installConfig(['field', 'node', 'language']); $this->manager = $this->container->get('plugin.manager.linkit.matcher'); @@ -180,4 +187,49 @@ class NodeMatcherTest extends LinkitKernelTestBase { } } + /** + * Test node matches generated from an absolute URL input. + */ + public function testNodeMatcherFromAbsoluteUrl() { + /** @var \Drupal\linkit\MatcherInterface $plugin */ + $plugin = $this->manager->createInstance('entity:node'); + + /** @var \Drupal\node\NodeInterface[] $nodes */ + $nodes = $this->container->get('entity_type.manager')->getStorage('node')->loadByProperties(['title' => 'Lorem Ipsum 1']); + $node = reset($nodes); + + $suggestions = $plugin->execute($node->toUrl()->setAbsolute()->toString()); + $this->assertEquals(1, count($suggestions->getSuggestions())); + } + + /** + * Test node matches generated from an absolute URL input. + */ + public function testNodeMatcherFromAbsoluteUrlWithLanguagePrefix() { + /** @var \Drupal\linkit\MatcherInterface $plugin */ + $plugin = $this->manager->createInstance('entity:node'); + + $langcode = 'nl'; + ConfigurableLanguage::createFromLangcode($langcode)->save(); + \Drupal::configFactory()->getEditable('language.negotiation') + ->set('url.prefixes.nl', $langcode) + ->save(); + + // In order to reflect the changes for a multilingual site in the container + // we have to rebuild it. + \Drupal::service('kernel')->rebuildContainer(); + + /** @var \Drupal\node\NodeInterface[] $nodes */ + $nodes = $this->container->get('entity_type.manager')->getStorage('node')->loadByProperties(['title' => 'Lorem Ipsum 1']); + $node = reset($nodes); + $translation = $node->addTranslation($langcode, $node->toArray()); + $translation->save(); + + $translated_url = $translation->toUrl()->setAbsolute()->toString(); + // Make sure the translated URL contains our prefix. + $this->assertContains('/' . $langcode . '/', $translated_url); + $suggestions = $plugin->execute($translated_url); + $this->assertEquals(1, count($suggestions->getSuggestions())); + } + }