3 namespace Drupal\Tests\system\Functional\Path;
5 use Drupal\Core\Database\Database;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\taxonomy\Entity\Term;
11 * Tests altering the inbound path and the outbound path.
15 class UrlAlterFunctionalTest extends BrowserTestBase {
22 public static $modules = ['path', 'forum', 'url_alter_test'];
25 * Test that URL altering works and that it occurs in the correct order.
27 public function testUrlAlter() {
28 // Ensure that the url_alias table exists after Drupal installation.
29 $this->assertTrue(Database::getConnection()->schema()->tableExists('url_alias'), 'The url_alias table exists after Drupal installation.');
31 // User names can have quotes and plus signs so we should ensure that URL
32 // altering works with this.
33 $account = $this->drupalCreateUser(['administer url aliases'], "a'foo+bar");
34 $this->drupalLogin($account);
36 $uid = $account->id();
37 $name = $account->getUsername();
39 // Test a single altered path.
40 $this->drupalGet("user/$name");
41 $this->assertResponse('200', 'The user/username path gets resolved correctly');
42 $this->assertUrlOutboundAlter("/user/$uid", "/user/$name");
44 // Test that a path always uses its alias.
45 $path = ['source' => "/user/$uid/test1", 'alias' => '/alias/test1'];
46 $this->container->get('path.alias_storage')->save($path['source'], $path['alias']);
47 $this->rebuildContainer();
48 $this->assertUrlInboundAlter('/alias/test1', "/user/$uid/test1");
49 $this->assertUrlOutboundAlter("/user/$uid/test1", '/alias/test1');
51 // Test adding an alias via the UI.
52 $edit = ['source' => "/user/$uid/edit", 'alias' => '/alias/test2'];
53 $this->drupalPostForm('admin/config/search/path/add', $edit, t('Save'));
54 $this->assertText(t('The alias has been saved.'));
55 $this->drupalGet('alias/test2');
56 $this->assertResponse('200', 'The path alias gets resolved correctly');
57 $this->assertUrlOutboundAlter("/user/$uid/edit", '/alias/test2');
59 // Test a non-existent user is not altered.
61 $this->assertUrlOutboundAlter("/user/$uid", "/user/$uid");
63 // Test that 'forum' is altered to 'community' correctly, both at the root
64 // level and for a specific existing forum.
65 $this->drupalGet('community');
66 $this->assertText('General discussion', 'The community path gets resolved correctly');
67 $this->assertUrlOutboundAlter('/forum', '/community');
68 $forum_vid = $this->config('forum.settings')->get('vocabulary');
69 $term_name = $this->randomMachineName();
70 $term = Term::create([
75 $this->drupalGet("community/" . $term->id());
76 $this->assertText($term_name, 'The community/{tid} path gets resolved correctly');
77 $this->assertUrlOutboundAlter("/forum/" . $term->id(), "/community/" . $term->id());
79 // Test outbound query string altering.
80 $url = Url::fromRoute('user.login');
81 $this->assertIdentical(\Drupal::request()->getBaseUrl() . '/user/login?foo=bar', $url->toString());
85 * Assert that an outbound path is altered to an expected value.
88 * A string with the original path that is run through generateFrommPath().
90 * A string with the expected result after generateFrommPath().
93 * TRUE if $original was correctly altered to $final, FALSE otherwise.
95 protected function assertUrlOutboundAlter($original, $final) {
96 // Test outbound altering.
97 $result = $this->container->get('path_processor_manager')->processOutbound($original);
98 return $this->assertIdentical($result, $final, format_string('Altered outbound URL %original, expected %final, and got %result.', ['%original' => $original, '%final' => $final, '%result' => $result]));
102 * Assert that a inbound path is altered to an expected value.
105 * The original path before it has been altered by inbound URL processing.
107 * A string with the expected result.
110 * TRUE if $original was correctly altered to $final, FALSE otherwise.
112 protected function assertUrlInboundAlter($original, $final) {
113 // Test inbound altering.
114 $result = $this->container->get('path.alias_manager')->getPathByAlias($original);
115 return $this->assertIdentical($result, $final, format_string('Altered inbound URL %original, expected %final, and got %result.', ['%original' => $original, '%final' => $final, '%result' => $result]));