Verified Commit aac54ad5 authored by Andrei Mateescu's avatar Andrei Mateescu
Browse files

feat: #3582769 Add clickLink() to HttpKernelUiHelperTrait

By: joachim
By: smustgrave
By: dww
By: amateescu
(cherry picked from commit 53a72dc5)
parent eb04bd5f
Loading
Loading
Loading
Loading
Loading
+16 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ class KernelTestHttpRequestTest extends KernelTestBase {
  protected static $modules = [
    'system',
    'system_test',
    'test_page_test',
  ];

  /**
@@ -45,4 +46,19 @@ public function testRequest(): void {
    $this->assertSession()->pageTextContains('Content to test main content fallback');
  }

  /**
   * Tests clickLink() functionality.
   */
  public function testClickLink(): void {
    $this->drupalGet('test-page');
    $this->clickLink('Visually identical test links');
    $this->assertStringContainsString('user/login', $this->getSession()->getCurrentUrl());
    $this->drupalGet('test-page');
    $this->clickLink('Visually identical test links', 0);
    $this->assertStringContainsString('user/login', $this->getSession()->getCurrentUrl());
    $this->drupalGet('test-page');
    $this->clickLink('Visually identical test links', 1);
    $this->assertStringContainsString('user/register', $this->getSession()->getCurrentUrl());
  }

}
+24 −0
Original line number Diff line number Diff line
@@ -73,6 +73,30 @@ protected function drupalGet($path): string {
    return $out;
  }

  /**
   * Follows a link by complete name.
   *
   * Will click the first link found with this link text unless $index is
   * specified.
   *
   * If the link is not found, an assertion will fail, halting the test.
   *
   * @param string|\Stringable $label
   *   Text between the anchor tags.
   * @param int $index
   *   (optional) The index number for cases where multiple links have the same
   *   text. Defaults to 0.
   */
  protected function clickLink(string|\Stringable $label, int $index = 0): void {
    $label = (string) $label;
    $links = $this->getSession()->getPage()->findAll('named', ['link', $label]);
    $this->assertArrayHasKey($index, $links, 'The link ' . $label . ' was not found on the page.');

    // Use static::drupalGet() rather than the click() method on the element,
    // because that will not produce HTML debug output.
    $this->drupalGet($links[$index]->getAttribute('href'));
  }

  /**
   * Returns Mink session.
   *