Chris@0: webUser = $this->drupalCreateUser($permissions); Chris@0: $this->drupalLogin($this->webUser); Chris@0: Chris@0: // Enable French language. Chris@0: $edit = []; Chris@0: $edit['predefined_langcode'] = 'fr'; Chris@0: Chris@0: $this->drupalPostForm('admin/config/regional/language/add', $edit, t('Add language')); Chris@0: Chris@0: // Enable URL language detection and selection. Chris@0: $edit = ['language_interface[enabled][language-url]' => 1]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Enable translation for page node. Chris@0: $edit = [ Chris@0: 'entity_types[node]' => 1, Chris@0: 'settings[node][page][translatable]' => 1, Chris@0: 'settings[node][page][fields][path]' => 1, Chris@0: 'settings[node][page][fields][body]' => 1, Chris@0: 'settings[node][page][settings][language][language_alterable]' => 1, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/content-language', $edit, t('Save configuration')); Chris@0: Chris@0: $definitions = \Drupal::entityManager()->getFieldDefinitions('node', 'page'); Chris@0: $this->assertTrue($definitions['path']->isTranslatable(), 'Node path is translatable.'); Chris@0: $this->assertTrue($definitions['body']->isTranslatable(), 'Node body is translatable.'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * Test alias functionality through the admin interfaces. Chris@0: */ Chris@0: public function testAliasTranslation() { Chris@0: $node_storage = $this->container->get('entity.manager')->getStorage('node'); Chris@0: $english_node = $this->drupalCreateNode(['type' => 'page', 'langcode' => 'en']); Chris@0: $english_alias = $this->randomMachineName(); Chris@0: Chris@0: // Edit the node to set language and path. Chris@0: $edit = []; Chris@0: $edit['path[0][alias]'] = '/' . $english_alias; Chris@0: $this->drupalPostForm('node/' . $english_node->id() . '/edit', $edit, t('Save')); Chris@0: Chris@0: // Confirm that the alias works. Chris@0: $this->drupalGet($english_alias); Chris@0: $this->assertText($english_node->body->value, 'Alias works.'); Chris@0: Chris@0: // Translate the node into French. Chris@0: $this->drupalGet('node/' . $english_node->id() . '/translations'); Chris@0: $this->clickLink(t('Add')); Chris@0: Chris@0: $edit = []; Chris@0: $edit['title[0][value]'] = $this->randomMachineName(); Chris@0: $edit['body[0][value]'] = $this->randomMachineName(); Chris@0: $french_alias = $this->randomMachineName(); Chris@0: $edit['path[0][alias]'] = '/' . $french_alias; Chris@0: $this->drupalPostForm(NULL, $edit, t('Save (this translation)')); Chris@0: Chris@0: // Clear the path lookup cache. Chris@0: $this->container->get('path.alias_manager')->cacheClear(); Chris@0: Chris@0: // Languages are cached on many levels, and we need to clear those caches. Chris@0: $this->container->get('language_manager')->reset(); Chris@0: $this->rebuildContainer(); Chris@0: $languages = $this->container->get('language_manager')->getLanguages(); Chris@0: Chris@0: // Ensure the node was created. Chris@0: $node_storage->resetCache([$english_node->id()]); Chris@0: $english_node = $node_storage->load($english_node->id()); Chris@0: $english_node_french_translation = $english_node->getTranslation('fr'); Chris@0: $this->assertTrue($english_node->hasTranslation('fr'), 'Node found in database.'); Chris@0: Chris@0: // Confirm that the alias works. Chris@0: $this->drupalGet('fr' . $edit['path[0][alias]']); Chris@0: $this->assertText($english_node_french_translation->body->value, 'Alias for French translation works.'); Chris@0: Chris@0: // Confirm that the alias is returned for the URL. Languages are cached on Chris@0: // many levels, and we need to clear those caches. Chris@0: $this->container->get('language_manager')->reset(); Chris@0: $languages = $this->container->get('language_manager')->getLanguages(); Chris@18: $url = $english_node_french_translation->toUrl('canonical', ['language' => $languages['fr']])->toString(); Chris@0: Chris@0: $this->assertTrue(strpos($url, $edit['path[0][alias]']), 'URL contains the path alias.'); Chris@0: Chris@0: // Confirm that the alias works even when changing language negotiation Chris@0: // options. Enable User language detection and selection over URL one. Chris@0: $edit = [ Chris@0: 'language_interface[enabled][language-user]' => 1, Chris@0: 'language_interface[weight][language-user]' => -9, Chris@0: 'language_interface[enabled][language-url]' => 1, Chris@0: 'language_interface[weight][language-url]' => -8, Chris@0: ]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Change user language preference. Chris@0: $edit = ['preferred_langcode' => 'fr']; Chris@0: $this->drupalPostForm("user/" . $this->webUser->id() . "/edit", $edit, t('Save')); Chris@0: Chris@0: // Check that the English alias works. In this situation French is the Chris@0: // current UI and content language, while URL language is English (since we Chris@0: // do not have a path prefix we fall back to the site's default language). Chris@0: // We need to ensure that the user language preference is not taken into Chris@0: // account while determining the path alias language, because if this Chris@0: // happens we have no way to check that the path alias is valid: there is no Chris@0: // path alias for French matching the english alias. So the alias manager Chris@0: // needs to use the URL language to check whether the alias is valid. Chris@0: $this->drupalGet($english_alias); Chris@0: $this->assertText($english_node_french_translation->body->value, 'English alias, but French preferred by the user: French translation.'); Chris@0: Chris@0: // Check that the French alias works. Chris@0: $this->drupalGet("fr/$french_alias"); Chris@0: $this->assertText($english_node_french_translation->body->value, 'Alias for French translation works.'); Chris@0: Chris@0: // Disable URL language negotiation. Chris@0: $edit = ['language_interface[enabled][language-url]' => FALSE]; Chris@0: $this->drupalPostForm('admin/config/regional/language/detection', $edit, t('Save settings')); Chris@0: Chris@0: // Check that the English alias still works. Chris@0: $this->drupalGet($english_alias); Chris@0: $this->assertText($english_node_french_translation->body->value, 'English alias, but French preferred by the user: French translation.'); Chris@0: Chris@0: // Check that the French alias is not available. We check the unprefixed Chris@0: // alias because we disabled URL language negotiation above. In this Chris@0: // situation only aliases in the default language and language neutral ones Chris@0: // should keep working. Chris@0: $this->drupalGet($french_alias); Chris@0: $this->assertResponse(404, 'Alias for French translation is unavailable when URL language negotiation is disabled.'); Chris@0: Chris@0: // The alias manager has an internal path lookup cache. Check to see that Chris@0: // it has the appropriate contents at this point. Chris@0: $this->container->get('path.alias_manager')->cacheClear(); Chris@0: $french_node_path = $this->container->get('path.alias_manager')->getPathByAlias('/' . $french_alias, 'fr'); Chris@0: $this->assertEqual($french_node_path, '/node/' . $english_node_french_translation->id(), 'Normal path works.'); Chris@0: // Second call should return the same path. Chris@0: $french_node_path = $this->container->get('path.alias_manager')->getPathByAlias('/' . $french_alias, 'fr'); Chris@0: $this->assertEqual($french_node_path, '/node/' . $english_node_french_translation->id(), 'Normal path is the same.'); Chris@0: Chris@0: // Confirm that the alias works. Chris@0: $french_node_alias = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $english_node_french_translation->id(), 'fr'); Chris@0: $this->assertEqual($french_node_alias, '/' . $french_alias, 'Alias works.'); Chris@0: // Second call should return the same alias. Chris@0: $french_node_alias = $this->container->get('path.alias_manager')->getAliasByPath('/node/' . $english_node_french_translation->id(), 'fr'); Chris@0: $this->assertEqual($french_node_alias, '/' . $french_alias, 'Alias is the same.'); Chris@0: Chris@0: // Confirm that the alias is removed if the translation is deleted. Chris@0: $english_node->removeTranslation('fr'); Chris@0: $english_node->save(); Chris@0: $this->assertFalse($this->container->get('path.alias_storage')->aliasExists('/' . $french_alias, 'fr'), 'Alias for French translation is removed when translation is deleted.'); Chris@0: Chris@0: // Check that the English alias still works. Chris@0: $this->drupalGet($english_alias); Chris@0: $this->assertTrue($this->container->get('path.alias_storage')->aliasExists('/' . $english_alias, 'en'), 'English alias is not deleted when French translation is removed.'); Chris@0: $this->assertText($english_node->body->value, 'English alias still works'); Chris@0: } Chris@0: Chris@0: }