<?php

class RevisioningETTestCase extends DrupalWebTestCase {

  protected $profile = 'minimal';

  public static function getInfo() {
    return array(
      'name' => "Revisioning Entity translation integration",
      'description' => "Test the revisioning logic integration with the Entity translation module.",
      'group' => "Revisioning",
    );
  }

  public function setUp() {
    parent::setUp('revisioning');
    if (module_enable(array('entity_translation'), TRUE)) {
      // Enable another language.
      $info = language_negotiation_info();
      locale_add_language('fr');
      language_negotiation_set('language_content', array(
        'locale-url' => $info['locale-url'],
        'language-default' => $info['language-default'],
      ));

      // Create a content type with a translatable field. Make sure we enable
      // translations for it.
      node_type_save((object) array(
        'type' => 'test_ct',
        'name' => 'Test content type',
        'base' => 'node_content',
      ));
      field_create_field(array(
        'field_name' => 'test_field',
        'cardinality' => 1,
        'type' => 'text',
        'translatable' => 1,
      ));
      field_create_instance(array(
        'entity_type' => 'node',
        'bundle' => 'test_ct',
        'field_name' => 'test_field',
        'label' => 'Test field',
      ));
      module_load_include('inc', 'entity_translation', 'entity_translation.node');
      variable_set('language_content_type_test_ct', ENTITY_TRANSLATION_ENABLED);
      variable_set('entity_translation_settings_node__test_ct', array(
        'default_language' => 'en',
        'hide_language_selector' => 0,
        'exclude_language_none' => 1,
        'lock_language' => 0,
        'shared_fields_original_only' => 1,
      ));

      // Activate revisioning.
      variable_set('node_options_test_ct', array('revision_moderation', 'revision'));

      // Clear all caches.
      $this->resetAll();

      // Create a new admin user and log in.
      $admin = $this->drupalCreateUser(array_keys(module_invoke_all('permission')));
      $this->drupalLogin($admin);
    }
    else {
      $this->fail('This test requires the Entity Translation module to run.');
    }
  }

  public function testET() {
    // Create a node.
    $node = $this->drupalCreateNode(array(
      'type' => 'test_ct',
      'language' => 'en',
      'test_field' => array(
        'en' => array(
          array('value' => $this->randomName(20) . '--en'),
        ),
      ),
      // This is to prevent a Fatal Error. It is fixed in the latest dev release
      // of Drupal 7 (not yet published in Drupal 7.44).
      'body' => array(
        LANGUAGE_NONE => array(array()),
        'en' => array(array()),
      ),
    ));

    // Publish the current revision, and create a new one, pending.
    _revisioning_publish_latest_revision($node);
    $published_vid = $node->vid;
    $node->title = $this->randomName(8);
    node_save($node);
    $this->assertEqual(2, revisioning_get_number_of_revisions($node->nid));

    // Go to the translation tab. Should show an "add" link. Add a new
    // translation.
    $this->drupalGet("node/{$node->nid}/translate");
    $this->assertLink('add');
    $this->clickLink('add');
    $edit = array(
      'test_field[fr][0][value]' => $this->randomName(8) . '--fr',
    );
    $this->drupalPost(NULL, $edit, 'Save');
    $this->drupalGet("fr/node/{$node->nid}/revisions/{$node->vid}/view");
    $this->assertText('--fr');
    $this->drupalGet("node/{$node->nid}/revisions/{$node->vid}/view");
    $this->assertText('--en');

    // Should have registered the translation, and there should be no more "add"
    // link. It should not have created a new revision either, and should not
    // have published the currently pending revision.
    $this->drupalGet("node/{$node->nid}/translate");
    $this->assertNoLink('add');
    $this->assertEqual(2, revisioning_get_number_of_revisions($node->nid));
    $this->assertEqual($published_vid, revisioning_get_current_node_revision_id($node->nid));
  }
}
