You are here

public function EntityTranslationIntegrationTestCase::testPathautoIntegration in Entity Translation 7

Tests Pathauto integration.

File

tests/entity_translation.test, line 810
Tests for Entity translation module.

Class

EntityTranslationIntegrationTestCase
Tests for integration of Entity Translation with other modules.

Code

public function testPathautoIntegration() {
  $languages = language_list();

  // Enable and configure the pathauto module.
  $this
    ->login($this
    ->getAdminUser());
  if (!module_exists('pathauto')) {
    module_enable(array(
      'pathauto',
    ));
  }

  // Configure pathauto patterns.
  variable_set('pathauto_node_page_en_pattern', 'content/[node:title]/en_pattern');
  variable_set('pathauto_node_page_fr_pattern', 'content/[node:title]/fr_pattern');
  variable_set('pathauto_node_page_es_pattern', 'content/[node:title]/es_pattern');

  // Disable pathauto lowercase conversion.
  variable_set('pathauto_case', 0);

  // Configure pathauto related permissions.
  $admin_rid = $this
    ->getUserRole($this
    ->getAdminUser());
  user_role_grant_permissions($admin_rid, array(
    'administer url aliases',
    'administer pathauto',
  ));
  $translator_rid = $this
    ->getUserRole($this
    ->getTranslatorUser());
  user_role_grant_permissions($translator_rid, array(
    'create url aliases',
  ));
  $this
    ->login($this
    ->getTranslatorUser());

  // Configure auto-generation of all aliases for new translations.
  variable_set('entity_translation_pathauto_state_active_new_translation', TRUE);
  variable_set('entity_translation_pathauto_state_mode_update', 'generate_all_aliases_all_languages');

  // Create Basic page in English and add a translation in Spanish.
  $node_title = $this
    ->randomName();
  $node_body = $this
    ->randomName();
  $node = $this
    ->createPage($node_title, $node_body, 'en');
  $this
    ->createTranslation($node, $node_title, $node_body, 'es');

  // Clear the static caches in case they interfere.
  drupal_lookup_path('wipe');

  // Check that a pathauto alias was created for the source and
  // matches the pathauto's pattern logic.
  $source_alias = drupal_get_path_alias('node/' . $node->nid);
  $this
    ->assertEqual($source_alias, 'content/' . $node_title . '/en_pattern');

  // Check that a pathauto alias was created for the translation and
  // matches the pathauto's pattern logic.
  $translation_alias = drupal_get_path_alias('node/' . $node->nid, 'es');
  $this
    ->assertEqual($translation_alias, 'content/' . $node_title . '/es_pattern');

  // Check that when adding a new translation, and the variable
  // 'entity_translation_pathauto_state_active_new_translation' is not set,
  // TRUE is used as its default value and the path[pathauto] checkbox is
  // checked.
  variable_del('entity_translation_pathauto_state_active_new_translation');
  $this
    ->drupalGet('node/' . $node->nid . '/edit/add/en/fr');
  $this
    ->assertFieldChecked('edit-path-pathauto');

  // Check that when adding a new translation, and the variable
  // 'entity_translation_pathauto_state_active_new_translation' is TRUE,
  // the path[pathauto] checkbox is checked.
  variable_set('entity_translation_pathauto_state_active_new_translation', TRUE);
  $this
    ->drupalGet('node/' . $node->nid . '/edit/add/en/fr');
  $this
    ->assertFieldChecked('edit-path-pathauto');

  // Check that when adding a new translation, and the variable
  // 'entity_translation_pathauto_state_active_new_translation' is FALSE,
  // the path[pathauto] checkbox is unchecked.
  variable_set('entity_translation_pathauto_state_active_new_translation', FALSE);
  $this
    ->drupalGet('node/' . $node->nid . '/edit/add/en/fr');
  $this
    ->assertNoFieldChecked('edit-path-pathauto');

  // Check that when adding a new translation, and the variables
  // 'entity_translation_pathauto_state_active_new_translation' is TRUE,
  // and 'entity_translation_pathauto_state_mode_update' is set to
  // 'generate_all_aliases_all_languages', aliases are added for all
  // languages, overwriting existing ones.
  variable_set('entity_translation_pathauto_state_active_new_translation', TRUE);
  variable_set('entity_translation_pathauto_state_mode_update', 'generate_all_aliases_all_languages');
  path_delete(array(
    'source' => 'node/' . $node->nid,
  ));
  $path_en = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_fr = drupal_get_path_alias('node/' . $node->nid, 'fr');
  $path_es = drupal_get_path_alias('node/' . $node->nid, 'es');
  $this
    ->createTranslation($node, $node_title, '', 'fr');

  // Clear the static caches in case they interfere.
  drupal_lookup_path('wipe');
  $path_en_new = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_fr_new = drupal_get_path_alias('node/' . $node->nid, 'fr');
  $path_es_new = drupal_get_path_alias('node/' . $node->nid, 'es');
  $this
    ->assertNotEqual($path_en, $path_en_new);
  $this
    ->assertNotEqual($path_fr, $path_fr_new);
  $this
    ->assertNotEqual($path_es, $path_es_new);

  // Check that when adding a new translation, and the variables
  // 'entity_translation_pathauto_state_active_new_translation' is TRUE,
  // and 'entity_translation_pathauto_state_mode_update' is set to
  // 'generate_alias_active_language', an alias is generated only for
  // the active language, maintaining the existing one(s).
  variable_set('entity_translation_pathauto_state_active_new_translation', TRUE);
  variable_set('entity_translation_pathauto_state_mode_update', 'generate_alias_active_language');

  // Create another Basic page in English.
  $node_title = $this
    ->randomName();
  $node_body = $this
    ->randomName();
  $node = $this
    ->createPage($node_title, $node_body, 'en');
  $this
    ->createTranslation($node, $node_title, '', 'es');
  $path_en = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_es = drupal_get_path_alias('node/' . $node->nid, 'es');
  $edit = array(
    'title' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit/add/en/fr', $edit, t('Save'));

  // Clear the static caches in case they interfere.
  drupal_lookup_path('wipe');
  $path_en_new = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_fr_new = drupal_get_path_alias('node/' . $node->nid, 'fr');
  $path_es_new = drupal_get_path_alias('node/' . $node->nid, 'es');
  $this
    ->assertEqual($path_en, $path_en_new);
  $this
    ->assertNotEqual($path_fr_new, 'node/' . $node->nid);
  $this
    ->assertEqual($path_es, $path_es_new);

  // Check that when adding a new translation, and the variable
  // 'entity_translation_pathauto_state_active_new_translation' is FALSE
  // no alias is generated for the active language or otherwise,
  // maintaining the existing one(s), irrespective of the value of the
  // 'entity_translation_pathauto_state_mode_update' variable.
  variable_set('entity_translation_pathauto_state_active_new_translation', FALSE);
  variable_set('entity_translation_pathauto_state_mode_update', 'generate_all_aliases_all_languages');

  // Create another Basic page in English.
  $node_title = $this
    ->randomName();
  $node_body = $this
    ->randomName();
  $node = $this
    ->createPage($node_title, $node_body, 'en');
  $this
    ->createTranslation($node, $node_title, '', 'es');
  $path_en = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_es = drupal_get_path_alias('node/' . $node->nid, 'es');
  $edit = array(
    'title' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit/add/en/fr', $edit, t('Save'));

  // Clear the static caches in case they interfere.
  drupal_lookup_path('wipe');
  $path_en_new = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_fr_new = drupal_get_path_alias('node/' . $node->nid, 'fr');
  $path_es_new = drupal_get_path_alias('node/' . $node->nid, 'es');
  $this
    ->assertEqual($path_en, $path_en_new);
  $this
    ->assertEqual($path_fr_new, 'node/' . $node->nid);
  $this
    ->assertEqual($path_es, $path_es_new);

  // Check that when adding a new translation, and the variables
  // 'entity_translation_pathauto_state_active_new_translation' is TRUE,
  // and 'entity_translation_pathauto_state_mode_update' is set to
  // 'generate_missing_aliases_all_languages', only missing aliases are
  // generated for all languages, maintaining the existing one(s).
  variable_set('entity_translation_pathauto_state_active_new_translation', TRUE);
  variable_set('entity_translation_pathauto_state_mode_update', 'generate_missing_aliases_all_languages');

  // Create another Basic page in English.
  $node_title = $this
    ->randomName();
  $node_body = $this
    ->randomName();
  $node = $this
    ->createPage($node_title, $node_body, 'en');
  $this
    ->createTranslation($node, $node_title, '', 'es');
  path_delete(array(
    'source' => 'node/' . $node->nid,
    'language' => 'es',
  ));
  $path_en = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_fr = drupal_get_path_alias('node/' . $node->nid, 'fr');
  $path_es = drupal_get_path_alias('node/' . $node->nid, 'es');
  $edit = array(
    'title' => $this
      ->randomName(),
  );
  $this
    ->drupalPost('node/' . $node->nid . '/edit/add/en/fr', $edit, t('Save'));

  // Clear the static caches in case they interfere.
  drupal_lookup_path('wipe');
  $path_en_new = drupal_get_path_alias('node/' . $node->nid, 'en');
  $path_fr_new = drupal_get_path_alias('node/' . $node->nid, 'fr');
  $path_es_new = drupal_get_path_alias('node/' . $node->nid, 'es');
  $this
    ->assertEqual($path_en, $path_en_new);
  $this
    ->assertNotEqual($path_fr_new, 'node/' . $node->nid);
  $this
    ->assertNotEqual($path_es, $path_es_new);
}