Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / system / tests / src / Functional / Path / UrlAlterFunctionalTest.php
1 <?php
2
3 namespace Drupal\Tests\system\Functional\Path;
4
5 use Drupal\Core\Database\Database;
6 use Drupal\Core\Url;
7 use Drupal\Tests\BrowserTestBase;
8 use Drupal\taxonomy\Entity\Term;
9
10 /**
11  * Tests altering the inbound path and the outbound path.
12  *
13  * @group Path
14  */
15 class UrlAlterFunctionalTest extends BrowserTestBase {
16
17   /**
18    * Modules to enable.
19    *
20    * @var array
21    */
22   public static $modules = ['path', 'forum', 'url_alter_test'];
23
24   /**
25    * Test that URL altering works and that it occurs in the correct order.
26    */
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.');
30
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);
35
36     $uid = $account->id();
37     $name = $account->getUsername();
38
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");
43
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');
50
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');
58
59     // Test a non-existent user is not altered.
60     $uid++;
61     $this->assertUrlOutboundAlter("/user/$uid", "/user/$uid");
62
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([
71       'name' => $term_name,
72       'vid' => $forum_vid,
73     ]);
74     $term->save();
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());
78
79     // Test outbound query string altering.
80     $url = Url::fromRoute('user.login');
81     $this->assertIdentical(\Drupal::request()->getBaseUrl() . '/user/login?foo=bar', $url->toString());
82   }
83
84   /**
85    * Assert that an outbound path is altered to an expected value.
86    *
87    * @param $original
88    *   A string with the original path that is run through generateFrommPath().
89    * @param $final
90    *   A string with the expected result after generateFrommPath().
91    *
92    * @return
93    *   TRUE if $original was correctly altered to $final, FALSE otherwise.
94    */
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]));
99   }
100
101   /**
102    * Assert that a inbound path is altered to an expected value.
103    *
104    * @param $original
105    *   The original path before it has been altered by inbound URL processing.
106    * @param $final
107    *   A string with the expected result.
108    *
109    * @return
110    *   TRUE if $original was correctly altered to $final, FALSE otherwise.
111    */
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]));
116   }
117
118 }