1.20.x path.test PathLanguageTestCase::testAliasTranslation()

Test alias functionality through the admin interfaces.

File

modules/path/tests/path.test, line 337
Tests for the Path module.

Class

PathLanguageTestCase
Tests URL aliases for translated nodes.

Code

function testAliasTranslation() {
  // Set 'page' content type to enable translation.
  $node_type = node_type_load('page');
  $node_type->settings['language'] = TRANSLATION_ENABLED;
  node_type_save($node_type);

  $english_node = $this->backdropCreateNode(array('type' => 'page'));
  $english_alias = $this->randomName();

  // Edit the node to set language and path.
  $edit = array();
  $edit['langcode'] = 'en';
  $edit['path[auto]'] = FALSE;
  $edit['path[alias]'] = $english_alias;
  $this->backdropPost('node/' . $english_node->nid . '/edit', $edit, t('Save'));

  // Confirm that the alias works.
  $this->backdropGet($english_alias);
  $this->assertText($english_node->title, 'Alias works.');

  // Translate the node into French.
  $this->backdropGet('node/' . $english_node->nid . '/translate');
  $this->clickLink(t('Add translation'));
  $edit = array();
  $langcode = LANGUAGE_NONE;
  $edit["title"] = $this->randomName();
  $edit["body[$langcode][0][value]"] = $this->randomName();
  $french_alias = $this->randomName();
  $edit['path[auto]'] = FALSE;
  $edit['path[alias]'] = $french_alias;
  $this->backdropPost(NULL, $edit, t('Save'));

  // Clear the path lookup cache.
  backdrop_lookup_path('wipe');

  // Ensure the node was created.
  $french_node = $this->backdropGetNodeByTitle($edit["title"]);
  $this->assertTrue(($french_node), 'Node found in database.');

  // Confirm that the alias works.
  $this->backdropGet('fr/' . $edit['path[alias]']);
  $this->assertText($french_node->title, 'Alias for French translation works.');

  // Confirm that the alias is returned by url(). Languages are cached on
  // many levels, and we need to clear those caches.
  backdrop_static_reset('language_list');
  backdrop_static_reset('locale_url_outbound_alter');
  backdrop_static_reset('locale_language_url_rewrite_url');
  $languages = language_list();
  $url = url('node/' . $french_node->nid, array('language' => $languages[$french_node->langcode]));
  $this->assertTrue(strpos($url, $edit['path[alias]']), 'URL contains the path alias.');

  // Confirm that the alias works even when changing language negotiation
  // options. Enable User language detection and selection over URL one.
  $edit = array(
    'language[locale-user][enabled]' => 1,
    'language[locale-user][weight]' => -9,
    'language[locale-url][enabled]' => 1,
    'language[locale-url][weight]' => -8,
  );
  $this->backdropPost('admin/config/regional/language/detection', $edit, t('Save settings'));

  // Change user language preference.
  $edit = array('language' => 'fr');
  $this->backdropPost("user/{$this->web_user->uid}/edit", $edit, t('Save'));

  // Check that the English alias works. In this situation French is the
  // current UI and content language, while URL language is English (since we
  // do not have a path prefix we fall back to the site's default language).
  // We need to ensure that the user language preference is not taken into
  // account while determining the path alias language, because if this
  // happens we have no way to check that the path alias is valid: there is no
  // path alias for French matching the english alias. So backdrop_lookup_path()
  // needs to use the URL language to check whether the alias is valid.
  $this->backdropGet($english_alias);
  $this->assertText($english_node->title, 'Alias for English translation works.');

  // Check that the French alias works.
  $this->backdropGet("fr/$french_alias");
  $this->assertText($french_node->title, 'Alias for French translation works.');

  // Disable URL language negotiation.
  $edit = array('language[locale-url][enabled]' => FALSE);
  $this->backdropPost('admin/config/regional/language/detection', $edit, t('Save settings'));

  // Check that the English alias still works.
  $this->backdropGet($english_alias);
  $this->assertText($english_node->title, 'Alias for English translation works.');

  // Check that the French alias is not available. We check the unprefixed
  // alias because we disabled URL language negotiation above. In this
  // situation only aliases in the default language and language neutral ones
  // should keep working.
  $this->backdropGet($french_alias);
  $this->assertResponse(404, 'Alias for French translation is unavailable when URL language negotiation is disabled.');

  // backdrop_lookup_path() has an internal static cache. Check to see that
  // it has the appropriate contents at this point.
  backdrop_lookup_path('wipe');
  $french_node_path = backdrop_lookup_path('source', $french_alias, $french_node->langcode);
  $this->assertEqual($french_node_path, 'node/' . $french_node->nid, 'Normal path works.');
  // Second call should return the same path.
  $french_node_path = backdrop_lookup_path('source', $french_alias, $french_node->langcode);
  $this->assertEqual($french_node_path, 'node/' . $french_node->nid, 'Normal path is the same.');

  // Confirm that the alias works.
  $french_node_alias = backdrop_lookup_path('alias', 'node/' . $french_node->nid, $french_node->langcode);
  $this->assertEqual($french_node_alias, $french_alias, 'Alias works.');
  // Second call should return the same alias.
  $french_node_alias = backdrop_lookup_path('alias', 'node/' . $french_node->nid, $french_node->langcode);
  $this->assertEqual($french_node_alias, $french_alias, 'Alias is the same.');
}