class ShortcutEntityLinksTest

Same name and namespace in other branches
  1. 11.x core/modules/shortcut/tests/src/Kernel/ShortcutEntityLinksTest.php \Drupal\Tests\shortcut\Kernel\ShortcutEntityLinksTest

Tests the entity_links filter with shortcut entities.

Attributes

#[Group('shortcut')] #[CoversClass(EntityLinks::class)] #[RunTestsInSeparateProcesses]

Hierarchy

Expanded class hierarchy of ShortcutEntityLinksTest

File

core/modules/shortcut/tests/src/Kernel/ShortcutEntityLinksTest.php, line 21

Namespace

Drupal\Tests\shortcut\Kernel
View source
class ShortcutEntityLinksTest extends KernelTestBase {
  
  /**
   * {@inheritdoc}
   */
  protected static $modules = [
    'filter',
    'shortcut',
    'link',
    'user',
    'system',
  ];
  
  /**
   * The entity_links filter.
   *
   * @var \Drupal\filter\Plugin\Filter\EntityLinks
   */
  protected EntityLinks $filter;
  
  /**
   * {@inheritdoc}
   */
  protected function setUp() : void {
    parent::setUp();
    $this->installConfig('system');
    $this->installEntitySchema('user');
    $this->installEntitySchema('shortcut');
    $this->installConfig([
      'shortcut',
    ]);
    /** @var \Drupal\Component\Plugin\PluginManagerInterface $manager */
    $manager = $this->container
      ->get('plugin.manager.filter');
    $bag = new FilterPluginCollection($manager, []);
    $this->filter = $bag->get('entity_links');
  }
  
  /**
   * @legacy-covers ::getUrl
   * @legacy-covers \Drupal\shortcut\Entity\ShortcutLinkTarget
   */
  public function testShortcutEntity() : void {
    // cspell:disable-next-line
    $path = '/user/logout?token=fzL0Ox4jS6qafdt6gzGzjWGb_hsR6kJ8L8E0D4hC5Mo';
    $shortcut = Shortcut::create([
      'shortcut_set' => 'default',
      'title' => 'Comments',
      'weight' => -20,
      'link' => [
        'uri' => "internal:{$path}",
      ],
    ]);
    $shortcut->save();
    $result = $this->filter
      ->process(sprintf('<a data-entity-type="shortcut" data-entity-uuid="%s" href="something?query=string#fragment">Link text</a>', $shortcut->uuid()), 'en');
    $expected_result = (new FilterProcessResult())->setProcessedText(sprintf('<a href="%s?query=string#fragment">Link text</a>', $path))
      ->setCacheTags([
      'config:shortcut.set.default',
    ])
      ->setCacheContexts([])
      ->setCacheMaxAge(Cache::PERMANENT);
    $this->assertSame($expected_result->getProcessedText(), $result->getProcessedText());
    $this->assertEquals(CacheableMetadata::createFromObject($expected_result), CacheableMetadata::createFromObject($result));
  }

}

Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.