annotate core/modules/locale/tests/src/Functional/LocalePathTest.php @ 19:fa3358dc1485 tip

Add ndrum files
author Chris Cannam
date Wed, 28 Aug 2019 13:14:47 +0100
parents 4c8ae668cc8c
children
rev   line source
Chris@0 1 <?php
Chris@0 2
Chris@0 3 namespace Drupal\Tests\locale\Functional;
Chris@0 4
Chris@0 5 use Drupal\Core\Language\LanguageInterface;
Chris@0 6 use Drupal\Core\Url;
Chris@0 7 use Drupal\Tests\BrowserTestBase;
Chris@0 8
Chris@0 9 /**
Chris@0 10 * Tests you can configure a language for individual URL aliases.
Chris@0 11 *
Chris@0 12 * @group locale
Chris@0 13 */
Chris@0 14 class LocalePathTest extends BrowserTestBase {
Chris@0 15
Chris@0 16 /**
Chris@0 17 * Modules to enable.
Chris@0 18 *
Chris@0 19 * @var array
Chris@0 20 */
Chris@0 21 public static $modules = ['node', 'locale', 'path', 'views'];
Chris@0 22
Chris@0 23 /**
Chris@0 24 * {@inheritdoc}
Chris@0 25 */
Chris@0 26 protected function setUp() {
Chris@0 27 parent::setUp();
Chris@0 28
Chris@0 29 $this->drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']);
Chris@0 30 $this->config('system.site')->set('page.front', '/node')->save();
Chris@0 31 }
Chris@0 32
Chris@0 33 /**
Chris@0 34 * Test if a language can be associated with a path alias.
Chris@0 35 */
Chris@0 36 public function testPathLanguageConfiguration() {
Chris@0 37 // User to add and remove language.
Chris@0 38 $admin_user = $this->drupalCreateUser(['administer languages', 'create page content', 'administer url aliases', 'create url aliases', 'access administration pages', 'access content overview']);
Chris@0 39
Chris@0 40 // Add custom language.
Chris@0 41 $this->drupalLogin($admin_user);
Chris@0 42 // Code for the language.
Chris@0 43 $langcode = 'xx';
Chris@0 44 // The English name for the language.
Chris@0 45 $name = $this->randomMachineName(16);
Chris@0 46 // The domain prefix.
Chris@0 47 $prefix = $langcode;
Chris@0 48 $edit = [
Chris@0 49 'predefined_langcode' => 'custom',
Chris@0 50 'langcode' => $langcode,
Chris@0 51 'label' => $name,
Chris@0 52 'direction' => LanguageInterface::DIRECTION_LTR,
Chris@0 53 ];
Chris@0 54 $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language'));
Chris@0 55
Chris@0 56 // Set path prefix.
Chris@0 57 $edit = ["prefix[$langcode]" => $prefix];
Chris@0 58 $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration'));
Chris@0 59
Chris@0 60 // Check that the "xx" front page is readily available because path prefix
Chris@0 61 // negotiation is pre-configured.
Chris@0 62 $this->drupalGet($prefix);
Chris@0 63 $this->assertText(t('Welcome to Drupal'), 'The "xx" front page is readibly available.');
Chris@0 64
Chris@0 65 // Create a node.
Chris@0 66 $node = $this->drupalCreateNode(['type' => 'page']);
Chris@0 67
Chris@0 68 // Create a path alias in default language (English).
Chris@0 69 $path = 'admin/config/search/path/add';
Chris@0 70 $english_path = $this->randomMachineName(8);
Chris@0 71 $edit = [
Chris@0 72 'source' => '/node/' . $node->id(),
Chris@0 73 'alias' => '/' . $english_path,
Chris@0 74 'langcode' => 'en',
Chris@0 75 ];
Chris@0 76 $this->drupalPostForm($path, $edit, t('Save'));
Chris@0 77
Chris@0 78 // Create a path alias in new custom language.
Chris@0 79 $custom_language_path = $this->randomMachineName(8);
Chris@0 80 $edit = [
Chris@0 81 'source' => '/node/' . $node->id(),
Chris@0 82 'alias' => '/' . $custom_language_path,
Chris@0 83 'langcode' => $langcode,
Chris@0 84 ];
Chris@0 85 $this->drupalPostForm($path, $edit, t('Save'));
Chris@0 86
Chris@0 87 // Confirm English language path alias works.
Chris@0 88 $this->drupalGet($english_path);
Chris@0 89 $this->assertText($node->label(), 'English alias works.');
Chris@0 90
Chris@0 91 // Confirm custom language path alias works.
Chris@0 92 $this->drupalGet($prefix . '/' . $custom_language_path);
Chris@0 93 $this->assertText($node->label(), 'Custom language alias works.');
Chris@0 94
Chris@0 95 // Create a custom path.
Chris@0 96 $custom_path = $this->randomMachineName(8);
Chris@0 97
Chris@0 98 // Check priority of language for alias by source path.
Chris@0 99 $edit = [
Chris@0 100 'source' => '/node/' . $node->id(),
Chris@0 101 'alias' => '/' . $custom_path,
Chris@0 102 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED,
Chris@0 103 ];
Chris@0 104 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
Chris@0 105 $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $node->id(), 'en');
Chris@0 106 $this->assertEqual('/' . $english_path, $lookup_path, 'English language alias has priority.');
Chris@0 107 // Same check for language 'xx'.
Chris@0 108 $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $node->id(), $prefix);
Chris@0 109 $this->assertEqual('/' . $custom_language_path, $lookup_path, 'Custom language alias has priority.');
Chris@0 110 $this->container->get('path.alias_storage')->delete($edit);
Chris@0 111
Chris@0 112 // Create language nodes to check priority of aliases.
Chris@0 113 $first_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => 'en']);
Chris@0 114 $second_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]);
Chris@0 115
Chris@0 116 // Assign a custom path alias to the first node with the English language.
Chris@0 117 $edit = [
Chris@0 118 'source' => '/node/' . $first_node->id(),
Chris@0 119 'alias' => '/' . $custom_path,
Chris@0 120 'langcode' => $first_node->language()->getId(),
Chris@0 121 ];
Chris@0 122 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
Chris@0 123
Chris@0 124 // Assign a custom path alias to second node with
Chris@0 125 // LanguageInterface::LANGCODE_NOT_SPECIFIED.
Chris@0 126 $edit = [
Chris@0 127 'source' => '/node/' . $second_node->id(),
Chris@0 128 'alias' => '/' . $custom_path,
Chris@0 129 'langcode' => $second_node->language()->getId(),
Chris@0 130 ];
Chris@0 131 $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']);
Chris@0 132
Chris@0 133 // Test that both node titles link to our path alias.
Chris@0 134 $this->drupalGet('admin/content');
Chris@0 135 $custom_path_url = Url::fromUserInput('/' . $custom_path)->toString();
Chris@0 136 $elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $first_node->label()]);
Chris@0 137 $this->assertTrue(!empty($elements), 'First node links to the path alias.');
Chris@0 138 $elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $second_node->label()]);
Chris@0 139 $this->assertTrue(!empty($elements), 'Second node links to the path alias.');
Chris@0 140
Chris@0 141 // Confirm that the custom path leads to the first node.
Chris@0 142 $this->drupalGet($custom_path);
Chris@0 143 $this->assertText($first_node->label(), 'Custom alias returns first node.');
Chris@0 144
Chris@0 145 // Confirm that the custom path with prefix leads to the second node.
Chris@0 146 $this->drupalGet($prefix . '/' . $custom_path);
Chris@0 147 $this->assertText($second_node->label(), 'Custom alias with prefix returns second node.');
Chris@0 148
Chris@0 149 }
Chris@0 150
Chris@0 151 }