function UrlAlterFunctionalTest::testUrlAlter

Same name in this branch
  1. 11.x core/modules/forum/tests/src/Functional/UrlAlterFunctionalTest.php \Drupal\Tests\forum\Functional\UrlAlterFunctionalTest::testUrlAlter()
Same name in other branches
  1. 7.x modules/simpletest/tests/path.test \UrlAlterFunctionalTest::testUrlAlter()
  2. 9 core/modules/path_alias/tests/src/Functional/UrlAlterFunctionalTest.php \Drupal\Tests\path_alias\Functional\UrlAlterFunctionalTest::testUrlAlter()
  3. 8.9.x core/modules/path_alias/tests/src/Functional/UrlAlterFunctionalTest.php \Drupal\Tests\path_alias\Functional\UrlAlterFunctionalTest::testUrlAlter()
  4. 10 core/modules/path_alias/tests/src/Functional/UrlAlterFunctionalTest.php \Drupal\Tests\path_alias\Functional\UrlAlterFunctionalTest::testUrlAlter()
  5. 10 core/modules/forum/tests/src/Functional/UrlAlterFunctionalTest.php \Drupal\Tests\forum\Functional\UrlAlterFunctionalTest::testUrlAlter()

Tests that URL altering works and that it occurs in the correct order.

File

core/modules/path_alias/tests/src/Functional/UrlAlterFunctionalTest.php, line 34

Class

UrlAlterFunctionalTest
Tests altering the inbound path and the outbound path.

Namespace

Drupal\Tests\path_alias\Functional

Code

public function testUrlAlter() : void {
    // Ensure that the path_alias table exists after Drupal installation.
    $this->assertTrue(Database::getConnection()->schema()
        ->tableExists('path_alias'), 'The path_alias table exists after Drupal installation.');
    // User names can have quotes and plus signs so we should ensure that URL
    // altering works with this.
    $account = $this->drupalCreateUser([
        'administer url aliases',
    ], "it's+bar");
    $this->drupalLogin($account);
    $uid = $account->id();
    $name = $account->getAccountName();
    // Test a single altered path.
    $this->drupalGet("user/{$name}");
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertUrlOutboundAlter("/user/{$uid}", "/user/{$name}");
    // Test that a path always uses its alias.
    $this->createPathAlias("/user/{$uid}/test1", '/alias/test1');
    $this->rebuildContainer();
    $this->assertUrlInboundAlter('/alias/test1', "/user/{$uid}/test1");
    $this->assertUrlOutboundAlter("/user/{$uid}/test1", '/alias/test1');
    // Test adding an alias via the UI.
    $edit = [
        'path[0][value]' => "/user/{$uid}/edit",
        'alias[0][value]' => '/alias/test2',
    ];
    $this->drupalGet('admin/config/search/path/add');
    $this->submitForm($edit, 'Save');
    $this->assertSession()
        ->pageTextContains('The alias has been saved.');
    $this->drupalGet('alias/test2');
    $this->assertSession()
        ->statusCodeEquals(200);
    $this->assertUrlOutboundAlter("/user/{$uid}/edit", '/alias/test2');
    // Test a non-existent user is not altered.
    $uid++;
    $this->assertUrlOutboundAlter("/user/{$uid}", "/user/{$uid}");
    // Test outbound query string altering.
    $url = Url::fromRoute('user.login');
    $this->assertSame(\Drupal::request()->getBaseUrl() . '/user/login?foo=bar', $url->toString());
}

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