View source
<?php
namespace Drupal\Tests\path_alias\Functional;
use Drupal\Component\Render\FormattableMarkup;
use Drupal\Core\Database\Database;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\Traits\Core\PathAliasTestTrait;
class UrlAlterFunctionalTest extends BrowserTestBase {
use PathAliasTestTrait;
protected static $modules = [
'path',
'forum',
'url_alter_test',
];
protected $defaultTheme = 'stark';
public function testUrlAlter() {
$this
->assertTrue(Database::getConnection()
->schema()
->tableExists('path_alias'), 'The path_alias table exists after Drupal installation.');
$account = $this
->drupalCreateUser([
'administer url aliases',
], "a'foo+bar");
$this
->drupalLogin($account);
$uid = $account
->id();
$name = $account
->getAccountName();
$this
->drupalGet("user/{$name}");
$this
->assertSession()
->statusCodeEquals(200);
$this
->assertUrlOutboundAlter("/user/{$uid}", "/user/{$name}");
$this
->createPathAlias("/user/{$uid}/test1", '/alias/test1');
$this
->rebuildContainer();
$this
->assertUrlInboundAlter('/alias/test1', "/user/{$uid}/test1");
$this
->assertUrlOutboundAlter("/user/{$uid}/test1", '/alias/test1');
$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');
$uid++;
$this
->assertUrlOutboundAlter("/user/{$uid}", "/user/{$uid}");
$this
->drupalGet('community');
$this
->assertSession()
->pageTextContains('General discussion');
$this
->assertUrlOutboundAlter('/forum', '/community');
$forum_vid = $this
->config('forum.settings')
->get('vocabulary');
$term_name = $this
->randomMachineName();
$term = Term::create([
'name' => $term_name,
'vid' => $forum_vid,
]);
$term
->save();
$this
->drupalGet("community/" . $term
->id());
$this
->assertSession()
->pageTextContains($term_name);
$this
->assertUrlOutboundAlter("/forum/" . $term
->id(), "/community/" . $term
->id());
$url = Url::fromRoute('user.login');
$this
->assertSame(\Drupal::request()
->getBaseUrl() . '/user/login?foo=bar', $url
->toString());
}
protected function assertUrlOutboundAlter($original, $final) {
$result = $this->container
->get('path_processor_manager')
->processOutbound($original);
$this
->assertSame($final, $result, new FormattableMarkup('Altered outbound URL %original, expected %final, and got %result.', [
'%original' => $original,
'%final' => $final,
'%result' => $result,
]));
}
protected function assertUrlInboundAlter($original, $final) {
$result = $this->container
->get('path_alias.manager')
->getPathByAlias($original);
$this
->assertSame($final, $result, new FormattableMarkup('Altered inbound URL %original, expected %final, and got %result.', [
'%original' => $original,
'%final' => $final,
'%result' => $result,
]));
}
}