View source  
  <?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Entity\LingotekProfile;
class LingotekNodeTranslationAppendTypeTitleOptionTest extends LingotekTestBase {
  
  public static $modules = [
    'block',
    'node',
  ];
  
  protected function setUp() : void {
    parent::setUp();
    
    $this
      ->drupalPlaceBlock('page_title_block', [
      'region' => 'content',
      'weight' => -5,
    ]);
    $this
      ->drupalPlaceBlock('local_tasks_block', [
      'region' => 'content',
      'weight' => -10,
    ]);
    
    $this
      ->drupalCreateContentType([
      'type' => 'article',
      'name' => 'Article',
    ]);
    
    ConfigurableLanguage::createFromLangcode('es')
      ->setThirdPartySetting('lingotek', 'locale', 'es_MX')
      ->save();
    
    ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
      ->setLanguageAlterable(TRUE)
      ->save();
    \Drupal::service('content_translation.manager')
      ->setEnabled('node', 'article', TRUE);
    drupal_static_reset();
    \Drupal::entityTypeManager()
      ->clearCachedDefinitions();
    $this
      ->applyEntityUpdates();
    
    $this
      ->rebuildContainer();
    $this
      ->saveLingotekContentTranslationSettings([
      'node' => [
        'article' => [
          'profiles' => 'manual',
          'fields' => [
            'title' => 1,
            'body' => 1,
          ],
        ],
      ],
    ]);
  }
  
  protected function setSettingsAppendTypeTitle($value) {
    $this
      ->drupalGet('/admin/lingotek/settings');
    $edit = [
      'append_type_to_title' => $value,
    ];
    $this
      ->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], 'lingoteksettings-tab-preferences-form');
  }
  
  public function testSettingsAppendTypeTitle() {
    $this
      ->setSettingsAppendTypeTitle(TRUE);
    
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'manual';
    $this
      ->saveAndPublishNodeForm($edit);
    $this
      ->goToContentBulkManagementForm();
    
    $this
      ->clickLink('EN');
    
    $uploaded_title = \Drupal::state()
      ->get('lingotek.uploaded_title');
    $this
      ->assertIdentical('article (node): Llamas are cool', $uploaded_title, 'The node title was used appending type.');
  }
  
  public function testSettingsNoAppendTypeTitle() {
    $this
      ->setSettingsAppendTypeTitle(FALSE);
    
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'manual';
    $this
      ->saveAndPublishNodeForm($edit);
    $this
      ->goToContentBulkManagementForm();
    
    $this
      ->clickLink('EN');
    
    $uploaded_title = \Drupal::state()
      ->get('lingotek.uploaded_title');
    $this
      ->assertIdentical('Llamas are cool', $uploaded_title, 'The node title was used without appending type.');
  }
  
  public function testProfileAppendTypeTitle() {
    $this
      ->setSettingsAppendTypeTitle(FALSE);
    $profile = LingotekProfile::create([
      'id' => 'custom',
      'label' => 'Profile with overrides',
      'auto_upload' => FALSE,
      'auto_download' => FALSE,
      'append_type_to_title' => 'yes',
    ]);
    $profile
      ->save();
    
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'custom';
    $this
      ->saveAndPublishNodeForm($edit);
    $this
      ->goToContentBulkManagementForm();
    
    $this
      ->clickLink('EN');
    
    $uploaded_title = \Drupal::state()
      ->get('lingotek.uploaded_title');
    $this
      ->assertIdentical('article (node): Llamas are cool', $uploaded_title, 'The node title was used appending type.');
  }
  
  public function testProfileNoAppendTypeTitle() {
    $this
      ->setSettingsAppendTypeTitle(TRUE);
    $profile = LingotekProfile::create([
      'id' => 'custom',
      'label' => 'Profile with overrides',
      'auto_upload' => FALSE,
      'auto_download' => FALSE,
      'append_type_to_title' => 'no',
    ]);
    $profile
      ->save();
    
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'custom';
    $this
      ->saveAndPublishNodeForm($edit);
    $this
      ->goToContentBulkManagementForm();
    
    $this
      ->clickLink('EN');
    
    $uploaded_title = \Drupal::state()
      ->get('lingotek.uploaded_title');
    $this
      ->assertIdentical('Llamas are cool', $uploaded_title, 'The node title was used without appending type.');
  }
  
  public function testProfileUseGlobalSettingsAppendTypeTitle() {
    $this
      ->setSettingsAppendTypeTitle(TRUE);
    $profile = LingotekProfile::create([
      'id' => 'custom',
      'label' => 'Profile with overrides',
      'auto_upload' => FALSE,
      'auto_download' => FALSE,
      'append_type_to_title' => 'global_setting',
    ]);
    $profile
      ->save();
    
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'custom';
    $this
      ->saveAndPublishNodeForm($edit);
    $this
      ->goToContentBulkManagementForm();
    
    $this
      ->clickLink('EN');
    
    $uploaded_title = \Drupal::state()
      ->get('lingotek.uploaded_title');
    $this
      ->assertIdentical('article (node): Llamas are cool', $uploaded_title, 'The node title was used appending type.');
  }
  
  public function testProfileUseGlobalSettingsNoAppendTypeTitle() {
    $this
      ->setSettingsAppendTypeTitle(FALSE);
    $profile = LingotekProfile::create([
      'id' => 'custom',
      'label' => 'Profile with overrides',
      'auto_upload' => FALSE,
      'auto_download' => FALSE,
      'append_type_to_title' => 'global_setting',
    ]);
    $profile
      ->save();
    
    $edit = [];
    $edit['title[0][value]'] = 'Llamas are cool';
    $edit['body[0][value]'] = 'Llamas are very cool';
    $edit['langcode[0][value]'] = 'en';
    $edit['lingotek_translation_management[lingotek_translation_profile]'] = 'custom';
    $this
      ->saveAndPublishNodeForm($edit);
    $this
      ->goToContentBulkManagementForm();
    
    $this
      ->clickLink('EN');
    
    $uploaded_title = \Drupal::state()
      ->get('lingotek.uploaded_title');
    $this
      ->assertIdentical('Llamas are cool', $uploaded_title, 'The node title was used without appending type.');
  }
}