Chris@0: drupalCreateContentType(['type' => 'page', 'name' => 'Basic page']); Chris@0: $this->config('system.site')->set('page.front', '/node')->save(); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test if a language can be associated with a path alias. Chris@0: */ Chris@0: public function testPathLanguageConfiguration() { Chris@0: // User to add and remove language. Chris@0: $admin_user = $this->drupalCreateUser(['administer languages', 'create page content', 'administer url aliases', 'create url aliases', 'access administration pages', 'access content overview']); Chris@0: Chris@0: // Add custom language. Chris@0: $this->drupalLogin($admin_user); Chris@0: // Code for the language. Chris@0: $langcode = 'xx'; Chris@0: // The English name for the language. Chris@0: $name = $this->randomMachineName(16); Chris@0: // The domain prefix. Chris@0: $prefix = $langcode; Chris@0: $edit = [ Chris@0: 'predefined_langcode' => 'custom', Chris@0: 'langcode' => $langcode, Chris@0: 'label' => $name, Chris@0: 'direction' => LanguageInterface::DIRECTION_LTR, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add custom language')); Chris@0: Chris@0: // Set path prefix. Chris@0: $edit = ["prefix[$langcode]" => $prefix]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection/url', $edit, t('Save configuration')); Chris@0: Chris@0: // Check that the "xx" front page is readily available because path prefix Chris@0: // negotiation is pre-configured. Chris@0: $this->drupalGet($prefix); Chris@0: $this->assertText(t('Welcome to Drupal'), 'The "xx" front page is readibly available.'); Chris@0: Chris@0: // Create a node. Chris@0: $node = $this->drupalCreateNode(['type' => 'page']); Chris@0: Chris@0: // Create a path alias in default language (English). Chris@0: $path = 'admin/config/search/path/add'; Chris@0: $english_path = $this->randomMachineName(8); Chris@0: $edit = [ Chris@0: 'source' => '/node/' . $node->id(), Chris@0: 'alias' => '/' . $english_path, Chris@0: 'langcode' => 'en', Chris@0: ]; Chris@0: $this->drupalPostForm($path, $edit, t('Save')); Chris@0: Chris@0: // Create a path alias in new custom language. Chris@0: $custom_language_path = $this->randomMachineName(8); Chris@0: $edit = [ Chris@0: 'source' => '/node/' . $node->id(), Chris@0: 'alias' => '/' . $custom_language_path, Chris@0: 'langcode' => $langcode, Chris@0: ]; Chris@0: $this->drupalPostForm($path, $edit, t('Save')); Chris@0: Chris@0: // Confirm English language path alias works. Chris@0: $this->drupalGet($english_path); Chris@0: $this->assertText($node->label(), 'English alias works.'); Chris@0: Chris@0: // Confirm custom language path alias works. Chris@0: $this->drupalGet($prefix . '/' . $custom_language_path); Chris@0: $this->assertText($node->label(), 'Custom language alias works.'); Chris@0: Chris@0: // Create a custom path. Chris@0: $custom_path = $this->randomMachineName(8); Chris@0: Chris@0: // Check priority of language for alias by source path. Chris@0: $edit = [ Chris@0: 'source' => '/node/' . $node->id(), Chris@0: 'alias' => '/' . $custom_path, Chris@0: 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED, Chris@0: ]; Chris@0: $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']); Chris@0: $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $node->id(), 'en'); Chris@0: $this->assertEqual('/' . $english_path, $lookup_path, 'English language alias has priority.'); Chris@0: // Same check for language 'xx'. Chris@0: $lookup_path = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $node->id(), $prefix); Chris@0: $this->assertEqual('/' . $custom_language_path, $lookup_path, 'Custom language alias has priority.'); Chris@0: $this->container->get('path.alias_storage')->delete($edit); Chris@0: Chris@0: // Create language nodes to check priority of aliases. Chris@0: $first_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => 'en']); Chris@0: $second_node = $this->drupalCreateNode(['type' => 'page', 'promote' => 1, 'langcode' => LanguageInterface::LANGCODE_NOT_SPECIFIED]); Chris@0: Chris@0: // Assign a custom path alias to the first node with the English language. Chris@0: $edit = [ Chris@0: 'source' => '/node/' . $first_node->id(), Chris@0: 'alias' => '/' . $custom_path, Chris@0: 'langcode' => $first_node->language()->getId(), Chris@0: ]; Chris@0: $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']); Chris@0: Chris@0: // Assign a custom path alias to second node with Chris@0: // LanguageInterface::LANGCODE_NOT_SPECIFIED. Chris@0: $edit = [ Chris@0: 'source' => '/node/' . $second_node->id(), Chris@0: 'alias' => '/' . $custom_path, Chris@0: 'langcode' => $second_node->language()->getId(), Chris@0: ]; Chris@0: $this->container->get('path.alias_storage')->save($edit['source'], $edit['alias'], $edit['langcode']); Chris@0: Chris@0: // Test that both node titles link to our path alias. Chris@0: $this->drupalGet('admin/content'); Chris@0: $custom_path_url = Url::fromUserInput('/' . $custom_path)->toString(); Chris@0: $elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $first_node->label()]); Chris@0: $this->assertTrue(!empty($elements), 'First node links to the path alias.'); Chris@0: $elements = $this->xpath('//a[@href=:href and normalize-space(text())=:title]', [':href' => $custom_path_url, ':title' => $second_node->label()]); Chris@0: $this->assertTrue(!empty($elements), 'Second node links to the path alias.'); Chris@0: Chris@0: // Confirm that the custom path leads to the first node. Chris@0: $this->drupalGet($custom_path); Chris@0: $this->assertText($first_node->label(), 'Custom alias returns first node.'); Chris@0: Chris@0: // Confirm that the custom path with prefix leads to the second node. Chris@0: $this->drupalGet($prefix . '/' . $custom_path); Chris@0: $this->assertText($second_node->label(), 'Custom alias with prefix returns second node.'); Chris@0: Chris@0: } Chris@0: Chris@0: }