Loading core/tests/Drupal/KernelTests/KernelTestHttpRequestTest.php +16 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ class KernelTestHttpRequestTest extends KernelTestBase { protected static $modules = [ 'system', 'system_test', 'test_page_test', ]; /** Loading @@ -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()); } } core/tests/Drupal/Tests/HttpKernelUiHelperTrait.php +24 −0 Original line number Diff line number Diff line Loading @@ -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. * Loading Loading
core/tests/Drupal/KernelTests/KernelTestHttpRequestTest.php +16 −0 Original line number Diff line number Diff line Loading @@ -27,6 +27,7 @@ class KernelTestHttpRequestTest extends KernelTestBase { protected static $modules = [ 'system', 'system_test', 'test_page_test', ]; /** Loading @@ -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()); } }
core/tests/Drupal/Tests/HttpKernelUiHelperTrait.php +24 −0 Original line number Diff line number Diff line Loading @@ -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. * Loading