diff --git a/core/modules/comment/src/Tests/CommentTranslationUITest.php b/core/modules/comment/src/Tests/CommentTranslationUITest.php index 5e1e8a6..a0d13a9 100644 --- a/core/modules/comment/src/Tests/CommentTranslationUITest.php +++ b/core/modules/comment/src/Tests/CommentTranslationUITest.php @@ -118,11 +118,10 @@ protected function assertPublishedStatus() { $languages = $this->container->get('language_manager')->getLanguages(); // Check that simple users cannot see unpublished field translations. - $path = $entity->url(); foreach ($this->langcodes as $index => $langcode) { $translation = $this->getTranslation($entity, $langcode); $value = $this->getValue($translation, 'comment_body', $langcode); - $this->drupalGet($path, array('language' => $languages[$langcode])); + $this->drupalGet($entity->urlInfo(), array('language' => $languages[$langcode])); if ($index > 0) { $this->assertNoRaw($value, 'Unpublished field translation is not shown.'); } diff --git a/core/modules/content_translation/src/Tests/ContentTranslationUITest.php b/core/modules/content_translation/src/Tests/ContentTranslationUITest.php index a1a8eab..775df7a 100644 --- a/core/modules/content_translation/src/Tests/ContentTranslationUITest.php +++ b/core/modules/content_translation/src/Tests/ContentTranslationUITest.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\Core\Language\Language; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Url; use Drupal\language\Entity\ConfigurableLanguage; /** @@ -53,9 +54,9 @@ protected function doTestBasicTranslation() { $this->entityId = $this->createEntity($values[$default_langcode], $default_langcode); $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); $this->assertTrue($entity, 'Entity found in the database.'); - $this->drupalGet($entity->url()); + $this->drupalGet($entity->urlInfo()); $this->assertResponse(200, 'Entity URL is valid.'); - $this->drupalGet($entity->url('drupal:content-translation-overview')); + $this->drupalGet($entity->urlInfo('drupal:content-translation-overview')); $this->assertNoText('Source language', 'Source language column correctly hidden.'); $translation = $this->getTranslation($entity, $default_langcode); @@ -71,14 +72,17 @@ protected function doTestBasicTranslation() { $language = ConfigurableLanguage::load($langcode); $values[$langcode] = $this->getNewEntityValues($langcode); - $content_translation_path = $entity->url('drupal:content-translation-overview', array('language' => $language)); - $path = $content_translation_path . '/add/' . $default_langcode . '/' . $langcode; - $this->drupalPostForm($path, $this->getEditValues($values, $langcode), $this->getFormSubmitActionForNewTranslation($entity, $langcode)); + $add_url = Url::fromRoute('content_translation.translation_add_' . $entity->getEntityTypeId(), [ + $entity->getEntityTypeId() => $entity->id(), + 'source' => $default_langcode, + 'target' => $langcode + ], array('language' => $language)); + $this->drupalPostForm($add_url, $this->getEditValues($values, $langcode), $this->getFormSubmitActionForNewTranslation($entity, $langcode)); if ($this->testLanguageSelector) { $this->assertNoFieldByXPath('//select[@id="edit-langcode-0-value"]', NULL, 'Language selector correctly disabled on translations.'); } $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); - $this->drupalGet($entity->url('drupal:content-translation-overview')); + $this->drupalGet($entity->urlInfo('drupal:content-translation-overview')); $this->assertNoText('Source language', 'Source language column correctly hidden.'); // Switch the source language. @@ -86,20 +90,27 @@ protected function doTestBasicTranslation() { $language = ConfigurableLanguage::load($langcode); $source_langcode = 'it'; $edit = array('source_langcode[source]' => $source_langcode); - $content_translation_path = $entity->url('drupal:content-translation-overview', array('language' => $language)); - $path = $content_translation_path . '/add/' . $default_langcode . '/' . $langcode; + $add_url = Url::fromRoute('content_translation.translation_add_' . $entity->getEntityTypeId(), [ + $entity->getEntityTypeId() => $entity->id(), + 'source' => $default_langcode, + 'target' => $langcode + ], array('language' => $language)); // This does not save anything, it merely reloads the form and fills in the // fields with the values from the different source language. - $this->drupalPostForm($path, $edit, t('Change')); + $this->drupalPostForm($add_url, $edit, t('Change')); $this->assertFieldByXPath("//input[@name=\"{$this->fieldName}[0][value]\"]", $values[$source_langcode][$this->fieldName][0]['value'], 'Source language correctly switched.'); // Add another translation and mark the other ones as outdated. $values[$langcode] = $this->getNewEntityValues($langcode); $edit = $this->getEditValues($values, $langcode) + array('content_translation[retranslate]' => TRUE); - $path = $content_translation_path . '/add/' . $source_langcode . '/' . $langcode; - $this->drupalPostForm($path, $edit, $this->getFormSubmitActionForNewTranslation($entity, $langcode)); + $add_url = Url::fromRoute('content_translation.translation_add_' . $entity->getEntityTypeId(), [ + $entity->getEntityTypeId() => $entity->id(), + 'source' => $source_langcode, + 'target' => $langcode + ], array('language' => $language)); + $this->drupalPostForm($add_url, $edit, $this->getFormSubmitActionForNewTranslation($entity, $langcode)); $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); - $this->drupalGet($entity->url('drupal:content-translation-overview')); + $this->drupalGet($entity->urlInfo('drupal:content-translation-overview')); $this->assertText('Source language', 'Source language column correctly shown.'); // Check that the entered values have been correctly stored. @@ -119,7 +130,7 @@ protected function doTestBasicTranslation() { */ protected function doTestTranslationOverview() { $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); - $this->drupalGet($entity->url('drupal:content-translation-overview')); + $this->drupalGet($entity->urlInfo('drupal:content-translation-overview')); foreach ($this->langcodes as $langcode) { if ($entity->hasTranslation($langcode)) { @@ -144,15 +155,15 @@ protected function doTestOutdatedStatus() { // Mark translations as outdated. $edit = array('content_translation[retranslate]' => TRUE); - $edit_path = $entity->url('edit-form', array('language' => $languages[$langcode])); + $edit_path = $entity->urlInfo('edit-form', array('language' => $languages[$langcode])); $this->drupalPostForm($edit_path, $edit, $this->getFormSubmitAction($entity, $langcode)); $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); // Check that every translation has the correct "outdated" status, and that // the Translation fieldset is open if the translation is "outdated". foreach ($this->langcodes as $added_langcode) { - $path = $entity->url('edit-form', array('language' => ConfigurableLanguage::load($added_langcode))); - $this->drupalGet($path); + $url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($added_langcode))); + $this->drupalGet($url); if ($added_langcode == $langcode) { $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is not checked by default.'); $this->assertFalse($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab should be collapsed by default.'); @@ -161,8 +172,8 @@ protected function doTestOutdatedStatus() { $this->assertFieldByXPath('//input[@name="content_translation[outdated]"]', TRUE, 'The translate flag is checked by default.'); $this->assertTrue($this->xpath('//details[@id="edit-content-translation" and @open="open"]'), 'The translation tab is correctly expanded when the translation is outdated.'); $edit = array('content_translation[outdated]' => FALSE); - $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $added_langcode)); - $this->drupalGet($path); + $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $added_langcode)); + $this->drupalGet($url); $this->assertFieldByXPath('//input[@name="content_translation[retranslate]"]', FALSE, 'The retranslate flag is now shown.'); $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); $this->assertFalse($entity->translation[$added_langcode]['outdated'], 'The "outdated" status has been correctly stored.'); @@ -179,16 +190,16 @@ protected function doTestPublishedStatus() { // Unpublish translations. foreach ($this->langcodes as $index => $langcode) { if ($index > 0) { - $path = $entity->url('edit-form', array('language' => ConfigurableLanguage::load($langcode))); + $url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode))); $edit = array('content_translation[status]' => FALSE); - $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $langcode)); + $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode)); $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); $this->assertFalse($entity->translation[$langcode]['status'], 'The translation has been correctly unpublished.'); } } // Check that the last published translation cannot be unpublished. - $this->drupalGet($entity->url('edit-form')); + $this->drupalGet($entity->urlInfo('edit-form')); $this->assertFieldByXPath('//input[@name="content_translation[status]" and @disabled="disabled"]', TRUE, 'The last translation is published and cannot be unpublished.'); } @@ -210,8 +221,8 @@ protected function doTestAuthoringInfo() { 'content_translation[name]' => $user->getUsername(), 'content_translation[created]' => format_date($values[$langcode]['created'], 'custom', 'Y-m-d H:i:s O'), ); - $path = $entity->url('edit-form', array('language' => ConfigurableLanguage::load($langcode))); - $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $langcode)); + $url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode))); + $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode)); } $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); @@ -227,7 +238,7 @@ protected function doTestAuthoringInfo() { 'content_translation[name]' => $this->randomMachineName(12), 'content_translation[created]' => '19/11/1978', ); - $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $langcode)); + $this->drupalPostForm($entity->urlInfo('edit-form'), $edit, $this->getFormSubmitAction($entity, $langcode)); $this->assertTrue($this->xpath('//div[contains(@class, "error")]//ul'), 'Invalid values generate a list of form errors.'); $this->assertEqual($entity->translation[$langcode]['uid'], $values[$langcode]['uid'], 'Translation author correctly kept.'); $this->assertEqual($entity->translation[$langcode]['created'], $values[$langcode]['created'], 'Translation date correctly kept.'); @@ -240,8 +251,8 @@ protected function doTestTranslationDeletion() { // Confirm and delete a translation. $langcode = 'fr'; $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); - $path = $entity->url('edit-form', array('language' => ConfigurableLanguage::load($langcode))); - $this->drupalPostForm($path, array(), t('Delete translation')); + $url = $entity->urlInfo('edit-form', array('language' => ConfigurableLanguage::load($langcode))); + $this->drupalPostForm($url, array(), t('Delete translation')); $this->drupalPostForm(NULL, array(), t('Delete')); $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); if ($this->assertTrue(is_object($entity), 'Entity found')) { diff --git a/core/modules/node/src/Tests/NodeTranslationUITest.php b/core/modules/node/src/Tests/NodeTranslationUITest.php index 01bd5e9..865c881 100644 --- a/core/modules/node/src/Tests/NodeTranslationUITest.php +++ b/core/modules/node/src/Tests/NodeTranslationUITest.php @@ -10,6 +10,7 @@ use Drupal\Core\Entity\EntityInterface; use Drupal\content_translation\Tests\ContentTranslationUITest; use Drupal\Core\Language\LanguageInterface; +use Drupal\Core\Url; /** * Tests the Node Translation UI. @@ -144,8 +145,8 @@ protected function doTestAuthoringInfo() { 'promote[value]' => $values[$langcode]['promote'], ); $options = array('language' => $languages[$langcode]); - $path = $entity->url('edit-form', $options); - $this->drupalPostForm($path, $edit, $this->getFormSubmitAction($entity, $langcode), $options); + $url = $entity->urlInfo('edit-form', $options); + $this->drupalPostForm($url, $edit, $this->getFormSubmitAction($entity, $langcode), $options); } $entity = entity_load($this->entityTypeId, $this->entityId, TRUE); @@ -293,7 +294,7 @@ function testTranslationRendering() { $this->doTestTranslations('node/' . $node->id(), $values); // Test that the node page has the correct alternate hreflang links. - $this->doTestAlternateHreflangLinks('node/' . $node->id()); + $this->doTestAlternateHreflangLinks($node->urlInfo()); } /** @@ -315,20 +316,23 @@ protected function doTestTranslations($path, array $values) { /** * Tests that the given path provides the correct alternate hreflang links. * - * @param string $path + * @param \Drupal\Core\Url $url * The path to be tested. */ - protected function doTestAlternateHreflangLinks($path) { + protected function doTestAlternateHreflangLinks(Url $url) { $languages = $this->container->get('language_manager')->getLanguages(); + $url->setOption('absolute', TRUE); + $urls = []; foreach ($this->langcodes as $langcode) { - $urls[$langcode] = _url($path, array('absolute' => TRUE, 'language' => $languages[$langcode])); + $language_url = clone $url; + $urls[$langcode] = $language_url->setOption('language', $languages[$langcode]); } foreach ($this->langcodes as $langcode) { - $this->drupalGet($path, array('language' => $languages[$langcode])); - foreach ($urls as $alternate_langcode => $url) { + $this->drupalGet($urls[$langcode]); + foreach ($urls as $alternate_langcode => $language_url) { // Retrieve desired link elements from the HTML head. $links = $this->xpath('head/link[@rel = "alternate" and @href = :href and @hreflang = :hreflang]', - array(':href' => $url, ':hreflang' => $alternate_langcode)); + array(':href' => $language_url->toString(), ':hreflang' => $alternate_langcode)); $this->assert(isset($links[0]), format_string('The %langcode node translation has the correct alternate hreflang link for %alternate_langcode: %link.', array('%langcode' => $langcode, '%alternate_langcode' => $alternate_langcode, '%link' => $url))); } } diff --git a/core/modules/simpletest/src/WebTestBase.php b/core/modules/simpletest/src/WebTestBase.php index 47eb889..812995a 100644 --- a/core/modules/simpletest/src/WebTestBase.php +++ b/core/modules/simpletest/src/WebTestBase.php @@ -27,6 +27,7 @@ use Drupal\Core\StreamWrapper\PublicStream; use Drupal\Core\Datetime\DrupalDateTime; use Drupal\block\Entity\Block; +use Drupal\Core\Url; use Symfony\Component\HttpFoundation\Request; use Drupal\user\Entity\Role; @@ -1474,9 +1475,13 @@ protected function isInChildSite() { * The retrieved HTML string, also available as $this->getRawContent() */ protected function drupalGet($path, array $options = array(), array $headers = array()) { + if ($path instanceof Url) { + $url = $path->setOption('absolute', TRUE)->toString(); + } // Assume that paths that start with a / are already fully generated // URL's that are just missing $base_root. - if (isset($path[0]) && $path[0] == '/') { + elseif (isset($path[0]) && $path[0] == '/') { + throw new \Exception($path); $url = $GLOBALS['base_root'] . $path; } // The URL generator service is not necessarily available yet; e.g., in