You are here

entity_translation_hierarchy.test in Language Hierarchy 7

Tests for Entity Translation Hierarchy module.

File

modules/entity_translation_hierarchy/tests/entity_translation_hierarchy.test
View source
<?php

/**
 * @file
 * Tests for Entity Translation Hierarchy module.
 */

/**
 * Base class for Entity Translation Hierarchy module tests.
 */
abstract class EntityTranslationHierarchyBaseTestCase extends LanguageHierarchyBaseTestCase {
  protected $admin_user_permissions = array(
    'administer fields',
    'bypass node access',
    'administer nodes',
    'administer languages',
    'administer content types',
    'administer blocks',
    'access administration pages',
    'administer site configuration',
    'translate any entity',
  );
  protected $translator_user_permissions = array(
    'create page content',
    'edit own page content',
    'delete own page content',
    'translate any entity',
  );
  function setUp() {
    $args = func_get_args();
    call_user_func_array(array(
      'parent',
      'setUp',
    ), $args);
    $this
      ->loginAdminUser();
    $this
      ->addLanguages();
    $this
      ->enableUrlLanguageDetection();
    $this
      ->configureContentType();
    $this
      ->loginTranslatorUser();
  }

  /**
   * Configure the "Basic page" content type for entity translation tests.
   */
  function configureContentType() {

    // Configure the "Basic page" content type to use multilingual support with
    // translation.
    $edit = array();
    $edit['language_content_type'] = ENTITY_TRANSLATION_ENABLED;
    $this
      ->drupalPost('admin/structure/types/manage/page', $edit, t('Save content type'));
    $this
      ->assertRaw(t('The content type %type has been updated.', array(
      '%type' => 'Basic page',
    )), t('Basic page content type has been updated.'));

    // Set body field's cardinality to unlimited and toggle translatability.
    $edit = array();
    $edit['field[cardinality]'] = FIELD_CARDINALITY_UNLIMITED;
    $edit['field[translatable]'] = 1;
    $this
      ->drupalPost('admin/structure/types/manage/page/fields/body', $edit, t('Save settings'));
    $this
      ->assertRaw(t('Saved %field configuration.', array(
      '%field' => 'Body',
    )), t('Body field settings have been updated.'));

    // Check if the setting works.
    $this
      ->drupalGet('node/add/page');
    $this
      ->assertFieldById('edit-body-en-add-more', t('Add another item'), t('Add another item button found.'));
  }

  /**
   * Create a translation.
   *
   * @param $node
   *   Node of the basic page to create translation for.
   * @param $body
   *   Body of the basic page in the specified language.
   * @param $langcode
   *   The language code to be assigned to the specified values.
   */
  public function createTranslation($node, $body, $langcode, $source_langcode = 'en') {
    $this
      ->drupalGet('node/' . $node->nid . '/edit/add/' . $source_langcode . '/' . $langcode);
    $body_key = "body[{$langcode}][0][value]";
    $this
      ->assertFieldByXPath("//textarea[@name='{$body_key}']", $node->body[$node->language][0]['value'], 'Original body value correctly populated.');
    $this
      ->assertFieldById('edit-body-' . $langcode . '-add-more', t('Add another item'), t('Add another item button found.'));
    $edit = array();
    $edit[$body_key] = $body;
    $this
      ->drupalPost(NULL, $edit, t('Save'));
    $this
      ->drupalGet('node/' . $node->nid . '/translate');
    $this
      ->assertLinkByHref('node/' . $node->nid . '/edit/' . $langcode, 0, t('Translation edit link found. Translation created.'));
    return $node;
  }

}

/**
 * Functional tests for entity translation.
 */
class EntityTranslationHierarchyWebTestCase extends EntityTranslationHierarchyBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Entity Translation Hierarchy: Entity view',
      'description' => 'Test entity translations inheritance when viewing entity.',
      'group' => 'Language Hierarchy',
    );
  }
  public function setUp() {
    parent::setUp('entity_translation_hierarchy');
  }

  /**
   * Test if translations are properly displayed when viewing node page.
   */
  public function testFieldTranslationInheritance() {

    // Create Basic page in English.
    $node_title = $this
      ->randomName();
    $node_body = $this
      ->randomName();
    $node = $this
      ->createPageNode($node_title, $node_body, 'en');

    // Submit translation in Portuguese, Portugal.
    $node_translation_body_pt_pt = $this
      ->randomName();
    $this
      ->createTranslation($node, $node_translation_body_pt_pt, 'pt-pt');

    // Submit translation in Portuguese, International.
    $node_translation_body_pt = $this
      ->randomName();
    $this
      ->createTranslation($node, $node_translation_body_pt, 'pt');
    $this
      ->drupalLogout();
    $this
      ->get('pt', 'node/' . $node->nid);
    $this
      ->assertText($node_translation_body_pt, 'Portuguese, International content found.');
    $this
      ->get('pt-pt', 'node/' . $node->nid);
    $this
      ->assertText($node_translation_body_pt_pt, 'Portuguese, Portugal content found.');

    // Check if for language content is inherited from parent.
    $this
      ->get('pt-br', 'node/' . $node->nid);
    $this
      ->assertText($node_translation_body_pt, 'Portuguese, Brazil translation is inheriting content from parent.');
  }

  /**
   * Test if translation blocking mechanism is working as expected when viewing
   * node.
   */
  public function testBlockingOfTranslation() {

    // Create Basic page in English.
    $node_title = $this
      ->randomName();
    $node_body = $this
      ->randomName();
    $node = $this
      ->createPageNode($node_title, $node_body, 'en');

    // Submit translation in Portuguese, Portugal.
    $node_translation_body_pt_pt = $this
      ->randomName();
    $this
      ->createTranslation($node, $node_translation_body_pt_pt, 'pt-pt');

    // Submit translation in Portuguese, International.
    $node_translation_body_pt = $this
      ->randomName();
    $this
      ->createTranslation($node, $node_translation_body_pt, 'pt');

    // Edit translation in Portuguese, Portugal.
    $edit = array(
      'translation[blocking]' => 1,
    );
    $this
      ->drupalPost('node/' . $node->nid . '/edit/pt-pt', $edit, t('Save'));
    $this
      ->drupalLogout();

    // Check if access to blocked translation is denied.
    $this
      ->get('pt-pt', 'node/' . $node->nid);
    $this
      ->assertResponse(403);
  }

  /**
   * Test if translation blocking mechanism is removed on uninstall.
   */
  public function testBlockingColumnRemoved() {
    $this
      ->assertTrue(db_field_exists('entity_translation', 'blocking'), 'Ensure the blocking column exists on entity_translation table.');
    $this
      ->assertTrue(db_field_exists('entity_translation_revision', 'blocking'), 'Ensure the blocking column exists on entity_translation_revision table.');
    $modules = array(
      'entity_translation_hierarchy',
    );
    module_disable($modules);
    drupal_uninstall_modules($modules);
    $this
      ->assertFalse(db_field_exists('entity_translation', 'blocking'), 'Ensure the blocking column does not exist on entity_translation table after uninstall.');
    $this
      ->assertFalse(db_field_exists('entity_translation_revision', 'blocking'), 'Ensure the blocking column does not exist on entity_translation_revision table after uninstall.');
  }

}

/**
 * Functional tests for entity translation.
 */
class EntityTranslationHierarchyViewsWebTestCase extends EntityTranslationHierarchyBaseTestCase {
  public static function getInfo() {
    return array(
      'name' => 'Entity Translation Hierarchy: Views support',
      'description' => 'Test entity translations inheritance in views.',
      'group' => 'Language Hierarchy',
      'dependencies' => array(
        'views',
      ),
    );
  }
  public function setUp() {
    parent::setUp('views', 'entity_translation_hierarchy');

    // Save the test page view.
    $this
      ->getTestView()
      ->save();

    // Reset views static cache.
    views_get_view('test', TRUE);

    // Rebuild the menu.
    // views_invalidate_cache only sets the rebuild variable.
    menu_rebuild();
  }

  /**
   * Test support for inheritance in views.
   */
  public function testViewsSupport() {

    // Create Basic page in English.
    $node_title = $this
      ->randomName();
    $node_body = $this
      ->randomName();
    $node = $this
      ->createPageNode($node_title, $node_body, 'en');

    // Submit translation in Portuguese, Portugal.
    $node_translation_body_pt_pt = $this
      ->randomName();
    $this
      ->createTranslation($node, $node_translation_body_pt_pt, 'pt-pt');

    // Submit translation in Portuguese, International.
    $node_translation_body_pt = $this
      ->randomName();
    $this
      ->createTranslation($node, $node_translation_body_pt, 'pt');

    // Test if view is exported properly.
    $this
      ->get('en', 'test');
    $this
      ->assertText('Entity Translation Hierarchy test page');

    // Test if english content is displayed properly.
    $this
      ->assertText($node_body);

    // Test if Portuguese, International content is displayed properly.
    $this
      ->get('pt', 'test');
    $this
      ->assertText($node_translation_body_pt, 'Portuguese, International content is displayed properly.');

    // Test if there is only one views row.
    $this
      ->assertNoFieldByXPath('/div[@class="views-row-2"]', TRUE, 'There is only one row in view for Portuguese, International.');

    // Test if Portuguese, Portugal content is displayed properly.
    $this
      ->get('pt-pt', 'test');
    $this
      ->assertText($node_translation_body_pt_pt, 'Portuguese, Portugal content is displayed properly.');

    // Test if there is only one views row.
    $this
      ->assertNoFieldByXPath('/div[@class="views-row-2"]', TRUE, 'There is only one row in view for Portuguese, Portugal.');

    // Test if Portuguese, Brazil content is inherited from parent (Portuguese, International).
    $this
      ->get('pt-br', 'test');
    $this
      ->assertText($node_translation_body_pt, 'Portuguese, Brazil content is inherited from parent (Portuguese, International).');

    // Test if there is only one views row.
    $this
      ->assertNoFieldByXPath('/div[@class="views-row-2"]', TRUE, 'There is only one row in view for Portuguese, Brazil.');

    // Make Portuguese, Portugal translation as blocking.
    $edit = array(
      'translation[blocking]' => 1,
    );
    $this
      ->drupalPost('node/' . $node->nid . '/edit/pt-pt', $edit, t('Save'));

    // Test if blocking Portuguese, Portugal translation is no longer visible.
    $this
      ->get('pt-pt', 'test');
    $this
      ->assertNoText($node_translation_body_pt, 'Portuguese, Portugal blocking translation is no longer visible.');

    // Test if there are no rows.
    $this
      ->assertNoFieldByXPath('/div[@class="views-row-1"]', TRUE, 'There is no rows in view for blocked Portuguese, Portugal translation.');

    // Test if Portuguese, Brazil content is still inherited from parent (Portuguese, International).
    $this
      ->get('pt-br', 'test');
    $this
      ->assertText($node_translation_body_pt, 'Portuguese, Brazil content is still inherited from parent (Portuguese, International).');

    // Test if there is still only one views row.
    $this
      ->assertNoFieldByXPath('/div[@class="views-row-2"]', TRUE, 'There is still only one row in view for Portuguese, International.');
  }

  /**
   * Returns a test page view with a path under "test".
   */
  protected static function getTestView() {
    $view = new view();
    $view->name = 'test';
    $view->description = '';
    $view->tag = 'default';
    $view->base_table = 'node';
    $view->human_name = 'test';
    $view->core = 7;
    $view->api_version = '3.0';
    $view->disabled = FALSE;

    /* Edit this to true to make a default view disabled initially */

    /* Display: Master */
    $handler = $view
      ->new_display('default', 'Master', 'default');
    $handler->display->display_options['title'] = 'Entity Translation Hierarchy test page';
    $handler->display->display_options['use_more_always'] = FALSE;
    $handler->display->display_options['access']['type'] = 'perm';
    $handler->display->display_options['cache']['type'] = 'none';
    $handler->display->display_options['query']['type'] = 'views_query';
    $handler->display->display_options['query']['options']['distinct'] = TRUE;
    $handler->display->display_options['exposed_form']['type'] = 'basic';
    $handler->display->display_options['pager']['type'] = 'full';
    $handler->display->display_options['pager']['options']['items_per_page'] = '10';
    $handler->display->display_options['style_plugin'] = 'default';
    $handler->display->display_options['row_plugin'] = 'fields';

    /* Relationship: Content: Entity translation: translations */
    $handler->display->display_options['relationships']['entity_translations']['id'] = 'entity_translations';
    $handler->display->display_options['relationships']['entity_translations']['table'] = 'node';
    $handler->display->display_options['relationships']['entity_translations']['field'] = 'entity_translations';
    $handler->display->display_options['relationships']['entity_translations']['required'] = TRUE;

    /* Field: Content: Nid */
    $handler->display->display_options['fields']['nid']['id'] = 'nid';
    $handler->display->display_options['fields']['nid']['table'] = 'node';
    $handler->display->display_options['fields']['nid']['field'] = 'nid';

    /* Field: Content: Body */
    $handler->display->display_options['fields']['body']['id'] = 'body';
    $handler->display->display_options['fields']['body']['table'] = 'field_data_body';
    $handler->display->display_options['fields']['body']['field'] = 'body';
    $handler->display->display_options['fields']['body']['label'] = '';
    $handler->display->display_options['fields']['body']['element_label_colon'] = FALSE;

    /* Sort criterion: Content: Post date */
    $handler->display->display_options['sorts']['created']['id'] = 'created';
    $handler->display->display_options['sorts']['created']['table'] = 'node';
    $handler->display->display_options['sorts']['created']['field'] = 'created';
    $handler->display->display_options['sorts']['created']['order'] = 'DESC';

    /* Filter criterion: Content: Published */
    $handler->display->display_options['filters']['status']['id'] = 'status';
    $handler->display->display_options['filters']['status']['table'] = 'node';
    $handler->display->display_options['filters']['status']['field'] = 'status';
    $handler->display->display_options['filters']['status']['value'] = 1;
    $handler->display->display_options['filters']['status']['group'] = 1;
    $handler->display->display_options['filters']['status']['expose']['operator'] = FALSE;

    /* Filter criterion: Entity translation: Blocking */
    $handler->display->display_options['filters']['blocking']['id'] = 'blocking';
    $handler->display->display_options['filters']['blocking']['table'] = 'entity_translation';
    $handler->display->display_options['filters']['blocking']['field'] = 'blocking';
    $handler->display->display_options['filters']['blocking']['relationship'] = 'entity_translations';
    $handler->display->display_options['filters']['blocking']['value'] = '0';

    /* Filter criterion: Entity translation: Translation status */
    $handler->display->display_options['filters']['status_1']['id'] = 'status_1';
    $handler->display->display_options['filters']['status_1']['table'] = 'entity_translation';
    $handler->display->display_options['filters']['status_1']['field'] = 'status';
    $handler->display->display_options['filters']['status_1']['relationship'] = 'entity_translations';
    $handler->display->display_options['filters']['status_1']['value'] = '1';

    /* Filter criterion: Entity translation: Language */
    $handler->display->display_options['filters']['language']['id'] = 'language';
    $handler->display->display_options['filters']['language']['table'] = 'entity_translation';
    $handler->display->display_options['filters']['language']['field'] = 'language';
    $handler->display->display_options['filters']['language']['relationship'] = 'entity_translations';
    $handler->display->display_options['filters']['language']['value'] = array(
      '***FALLBACK_LANGUAGE***' => '***FALLBACK_LANGUAGE***',
    );

    /* Display: Page */
    $handler = $view
      ->new_display('page', 'Page', 'page');
    $handler->display->display_options['path'] = 'test';
    return $view;
  }

}

Classes

Namesort descending Description
EntityTranslationHierarchyBaseTestCase Base class for Entity Translation Hierarchy module tests.
EntityTranslationHierarchyViewsWebTestCase Functional tests for entity translation.
EntityTranslationHierarchyWebTestCase Functional tests for entity translation.