View source  
  <?php
namespace Drupal\lingotek\Tests;
use Drupal\Core\Url;
use Drupal\field\Entity\FieldConfig;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Lingotek;
use Drupal\lingotek\LingotekConfigTranslationServiceInterface;
use Drupal\node\Entity\Node;
use Drupal\node\NodeInterface;
class LingotekFieldBodyTranslationTest extends LingotekTestBase {
  
  public static $modules = [
    'block',
    'node',
    'field_ui',
  ];
  
  protected $node;
  protected function setUp() {
    parent::setUp();
    
    $this
      ->drupalPlaceBlock('local_tasks_block');
    $this
      ->drupalPlaceBlock('page_title_block');
    
    $type = $this
      ->drupalCreateContentType(array(
      'type' => 'article',
      'name' => 'Article',
    ));
    node_add_body_field($type);
    
    ConfigurableLanguage::createFromLangcode('es')
      ->setThirdPartySetting('lingotek', 'locale', 'es_MX')
      ->save();
    $edit = [
      'table[node_fields][enabled]' => 1,
      'table[node_fields][profile]' => 'automatic',
    ];
    $this
      ->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
    
    \Drupal::state()
      ->set('lingotek.uploaded_content_type', 'body');
  }
  
  public function testFieldTranslation() {
    
    $this
      ->drupalLogin($this->rootUser);
    $this
      ->drupalGet('/admin/config/regional/config-translation');
    $this
      ->drupalGet('/admin/config/regional/config-translation/node_fields');
    $this
      ->clickLink(t('Translate'));
    $this
      ->clickLink(t('Upload'));
    $this
      ->assertText(t('Body uploaded successfully'));
    
    $data = json_decode(\Drupal::state()
      ->get('lingotek.uploaded_content', '[]'), TRUE);
    $this
      ->verbose(var_export($data, TRUE));
    $this
      ->assertTrue(array_key_exists('label', $data['field.field.node.article.body']));
    
    $this
      ->assertTrue(array_key_exists('description', $data['field.field.node.article.body']));
    
    $used_profile = \Drupal::state()
      ->get('lingotek.used_profile');
    $this
      ->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');
    $this
      ->clickLink(t('Check upload status'));
    $this
      ->assertText('Body status checked successfully');
    $this
      ->clickLink(t('Request translation'));
    $this
      ->assertText(t('Translation to es_MX requested successfully'));
    $this
      ->assertIdentical('es_MX', \Drupal::state()
      ->get('lingotek.added_target_locale'));
    $this
      ->clickLink(t('Check Download'));
    $this
      ->assertText(t('Translation to es_MX status checked successfully'));
    $this
      ->clickLink('Download');
    $this
      ->assertText(t('Translation to es_MX downloaded successfully'));
    
    $basepath = \Drupal::request()
      ->getBasePath();
    $this
      ->assertLinkByHref($basepath . '/admin/structure/types/manage/article/fields/node.article.body/translate/es/edit');
    
    $this
      ->clickLink('Edit', 1);
    $this
      ->assertFieldByName('translation[config_names][field.field.node.article.body][label]', 'Cuerpo');
    $this
      ->assertFieldByName('translation[config_names][field.field.node.article.body][description]', 'Cuerpo del contenido');
  }
  
  public function testEditedFieldBodyTranslation() {
    
    $this
      ->testFieldTranslation();
    
    ConfigurableLanguage::createFromLangcode('eu')
      ->setThirdPartySetting('lingotek', 'locale', 'eu_ES')
      ->save();
    
    $edit = [
      'label' => 'Contents',
    ];
    $this
      ->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
    $this
      ->assertText('Saved Contents configuration.');
    $this
      ->clickLink(t('Translate'));
    
    $this
      ->assertLinkByHref('/admin/lingotek/config/request/node_fields/node.article.body/eu_ES');
    $this
      ->clickLink(t('Request translation'), 1);
    $this
      ->assertText(t('Translation to es_MX requested successfully'));
    $this
      ->assertIdentical('es_MX', \Drupal::state()
      ->get('lingotek.added_target_locale'));
    
    $this
      ->clickLink('Check Download');
    $this
      ->assertText('Translation to es_MX status checked successfully');
    
    $this
      ->clickLink('Download');
    $this
      ->assertText('Translation to es_MX downloaded successfully');
  }
  
  public function testLanguageDisabled() {
    
    $italian = ConfigurableLanguage::createFromLangcode('it')
      ->setThirdPartySetting('lingotek', 'locale', 'it_IT');
    $italian
      ->save();
    
    $this
      ->drupalLogin($this->rootUser);
    $this
      ->drupalGet('/admin/config/regional/config-translation');
    $this
      ->drupalGet('/admin/config/regional/config-translation/node_fields');
    $this
      ->clickLink(t('Translate'));
    $this
      ->clickLink(t('Upload'));
    $this
      ->assertText(t('Body uploaded successfully'));
    
    $data = json_decode(\Drupal::state()
      ->get('lingotek.uploaded_content', '[]'), TRUE);
    $this
      ->verbose(var_export($data, TRUE));
    $this
      ->assertEqual(2, count($data['field.field.node.article.body']));
    $this
      ->assertTrue(array_key_exists('label', $data['field.field.node.article.body']));
    
    $this
      ->assertTrue(array_key_exists('description', $data['field.field.node.article.body']));
    $this
      ->assertIdentical('en_US', \Drupal::state()
      ->get('lingotek.uploaded_locale'));
    
    $used_profile = \Drupal::state()
      ->get('lingotek.used_profile');
    $this
      ->assertIdentical('automatic', $used_profile, 'The automatic profile was used.');
    
    $this
      ->clickLink('Check upload status');
    $this
      ->assertText('Body status checked successfully');
    
    $this
      ->assertLinkByHref('/admin/lingotek/config/request/node_fields/node.article.body/it_IT');
    $this
      ->assertLinkByHref('/admin/lingotek/config/request/node_fields/node.article.body/es_MX');
    $this
      ->assertLinkByHref('/admin/structure/types/manage/article/fields/node.article.body/translate/it/add');
    $this
      ->assertLinkByHref('/admin/structure/types/manage/article/fields/node.article.body/translate/es/add');
    
    $lingotek_config = \Drupal::service('lingotek.configuration');
    $lingotek_config
      ->disableLanguage($italian);
    
    $this
      ->drupalGet('/admin/config/regional/config-translation/node_fields');
    $this
      ->clickLink(t('Translate'));
    
    $this
      ->assertNoLinkByHref('/admin/lingotek/config/request/node_fields/node.article.body/it_IT');
    $this
      ->assertLinkByHref('/admin/lingotek/config/request/node_fields/node.article.body/es_MX');
    $this
      ->assertLinkByHref('/admin/structure/types/manage/article/fields/node.article.body/translate/it/add');
    $this
      ->assertLinkByHref('/admin/structure/types/manage/article/fields/node.article.body/translate/es/add');
  }
  
  public function testUploadingWithAnError() {
    \Drupal::state()
      ->set('lingotek.must_error_in_upload', TRUE);
    
    $this
      ->drupalGet('/admin/config/regional/config-translation/node_fields');
    $this
      ->clickLink(t('Translate'));
    
    $this
      ->clickLink('Upload');
    $this
      ->assertText('Body upload failed. Please try again.');
    
    $fieldConfig = FieldConfig::load('node.article.body');
    
    $translation_service = \Drupal::service('lingotek.config_translation');
    $source_status = $translation_service
      ->getSourceStatus($fieldConfig);
    $this
      ->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
    
    \Drupal::state()
      ->set('lingotek.must_error_in_upload', FALSE);
    $this
      ->clickLink('Upload');
    $this
      ->assertText('Body uploaded successfully');
  }
  
  public function testUpdatingWithAnError() {
    
    $this
      ->drupalGet('/admin/config/regional/config-translation/node_fields');
    $this
      ->clickLink(t('Translate'));
    
    $this
      ->clickLink('Upload');
    $this
      ->assertText('Body uploaded successfully');
    
    $this
      ->clickLink('Check upload status');
    $this
      ->assertText('Body status checked successfully');
    
    $edit = [
      'label' => 'Contents',
    ];
    $this
      ->drupalPostForm('/admin/structure/types/manage/article/fields/node.article.body', $edit, t('Save settings'));
    $this
      ->assertText('Saved Contents configuration.');
    
    $this
      ->drupalGet('/admin/config/regional/config-translation/node_fields');
    $this
      ->clickLink(t('Translate'));
    \Drupal::state()
      ->set('lingotek.must_error_in_upload', TRUE);
    
    $this
      ->clickLink('Upload');
    $this
      ->assertText('Contents update failed. Please try again.');
    
    $fieldConfig = FieldConfig::load('node.article.body');
    
    $translation_service = \Drupal::service('lingotek.config_translation');
    $source_status = $translation_service
      ->getSourceStatus($fieldConfig);
    $this
      ->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
    
    \Drupal::state()
      ->set('lingotek.must_error_in_upload', FALSE);
    $this
      ->clickLink('Upload');
    $this
      ->assertText('Contents has been updated.');
  }
  
  public function testUploadingWithAnErrorViaAPI() {
    $edit = [
      'table[node_fields][enabled]' => 1,
      'table[node_fields][profile]' => 'automatic',
    ];
    $this
      ->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
    \Drupal::state()
      ->set('lingotek.must_error_in_upload', TRUE);
    $this
      ->drupalGet('admin/lingotek/settings');
    
    $edit = [
      'label' => 'Excerpt',
      'new_storage_type' => 'text',
      'field_name' => 'excerpt',
    ];
    $this
      ->drupalPostForm('/admin/structure/types/manage/article/fields/add-field', $edit, 'Save and continue');
    
    $this
      ->assertText('The upload for field_config Excerpt failed. Please try again.');
    
    $fieldConfig = FieldConfig::load('node.article.field_excerpt');
    
    $translation_service = \Drupal::service('lingotek.config_translation');
    $source_status = $translation_service
      ->getSourceStatus($fieldConfig);
    $this
      ->assertEqual(Lingotek::STATUS_ERROR, $source_status, 'The field has been marked as error.');
  }
}