View source
<?php
namespace Drupal\lingotek\Tests\Form;
use Drupal\Core\Extension\ModuleHandlerInterface;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Entity\LingotekProfile;
use Drupal\lingotek\LingotekConfigurationServiceInterface;
use Drupal\lingotek\LingotekProfileInterface;
use Drupal\lingotek\Tests\LingotekTestBase;
class LingotekProfileFormTest extends LingotekTestBase {
public static $modules = [
'node',
];
public function testDefaultProfilesPresent() {
$this
->drupalGet('admin/lingotek/settings');
$this
->assertFieldChecked('edit-profile-automatic-auto-upload');
$this
->assertFieldChecked('edit-profile-automatic-auto-download');
$this
->assertNoFieldChecked('edit-profile-manual-auto-upload');
$this
->assertNoFieldChecked('edit-profile-manual-auto-download');
$this
->assertNoFieldChecked('edit-profile-disabled-auto-upload');
$this
->assertNoFieldChecked('edit-profile-disabled-auto-download');
$this
->assertNoLinkByHref('/admin/lingotek/settings/profile/automatic/edit');
$this
->assertNoLinkByHref('/admin/lingotek/settings/profile/manual/edit');
$this
->assertNoLinkByHref('/admin/lingotek/settings/profile/disabled/edit');
$this
->assertFieldDisabled('edit-profile-automatic-auto-upload');
$this
->assertFieldDisabled('edit-profile-automatic-auto-download');
$this
->assertFieldDisabled('edit-profile-manual-auto-upload');
$this
->assertFieldDisabled('edit-profile-manual-auto-download');
$this
->assertFieldDisabled('edit-profile-disabled-auto-upload');
$this
->assertFieldDisabled('edit-profile-disabled-auto-download');
}
public function testAddingProfile() {
$this
->drupalGet('admin/lingotek/settings');
$this
->clickLink(t('Add new Translation Profile'));
$profile_id = strtolower($this
->randomMachineName());
$profile_name = $this
->randomString();
$edit = [
'id' => $profile_id,
'label' => $profile_name,
'auto_upload' => 1,
'auto_download' => 1,
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$this
->assertText(t('The Lingotek profile has been successfully saved.'));
$this
->assertLinkByHref("/admin/lingotek/settings/profile/{$profile_id}/edit");
$this
->assertFieldChecked("edit-profile-{$profile_id}-auto-upload");
$this
->assertFieldChecked("edit-profile-{$profile_id}-auto-download");
$this
->assertFieldEnabled("edit-profile-{$profile_id}-auto-upload");
$this
->assertFieldEnabled("edit-profile-{$profile_id}-auto-download");
$profile = LingotekProfile::load($profile_id);
$this
->assertTrue($profile
->hasAutomaticUpload());
$this
->assertTrue($profile
->hasAutomaticDownload());
$this
->assertIdentical('default', $profile
->getProject());
$this
->assertIdentical('default', $profile
->getVault());
$this
->assertIdentical('default', $profile
->getWorkflow());
}
public function testEditingProfile() {
$profile = LingotekProfile::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
]);
$profile
->save();
$profile_id = $profile
->id();
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/edit");
$edit = [
'auto_upload' => FALSE,
'auto_download' => 1,
'project' => 'test_project',
'vault' => 'test_vault',
'workflow' => 'test_workflow',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$profile = LingotekProfile::load($profile_id);
$this
->assertFalse($profile
->hasAutomaticUpload());
$this
->assertTrue($profile
->hasAutomaticDownload());
$this
->assertIdentical('test_project', $profile
->getProject());
$this
->assertIdentical('test_vault', $profile
->getVault());
$this
->assertIdentical('test_workflow', $profile
->getWorkflow());
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/edit");
$this
->assertNoFieldChecked("edit-auto-upload");
$this
->assertFieldChecked("edit-auto-download");
$this
->assertOptionSelected('edit-project', 'test_project');
$this
->assertOptionSelected('edit-vault', 'test_vault');
$this
->assertOptionSelected('edit-workflow', 'test_workflow');
}
public function testDeletingProfile() {
$profile = LingotekProfile::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
]);
$profile
->save();
$profile_id = $profile
->id();
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/delete");
$this
->assertText('This action cannot be undone.');
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertRaw(t('The lingotek profile %profile has been deleted.', [
'%profile' => $profile
->label(),
]));
$profile = LingotekProfile::load($profile_id);
$this
->assertNull($profile);
}
public function testDeletingProfileBeingUsedInContent() {
$profile = LingotekProfile::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
]);
$profile
->save();
$profile_id = $profile
->id();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
$edit = [
'node[article][enabled]' => 1,
'node[article][profiles]' => 'automatic',
'node[article][fields][title]' => 1,
'node[article][fields][body]' => 1,
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-content-form');
$edit = array();
$edit['title[0][value]'] = 'Llamas are cool';
$edit['body[0][value]'] = 'Llamas are very cool';
$edit['langcode[0][value]'] = 'en';
$edit['lingotek_translation_profile'] = $profile_id;
$this
->drupalPostForm('node/add/article', $edit, t('Save and publish'));
$this
->assertUrl('/node/1', [], 'Node has been created.');
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/delete");
$this
->assertText(t('This action cannot be undone.'));
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertNoRaw(t('The lingotek profile %profile has been deleted.', [
'%profile' => $profile
->label(),
]));
$this
->assertRaw(t('The Lingotek profile %profile is being used so cannot be deleted.', [
'%profile' => $profile
->label(),
]));
$profile = LingotekProfile::load($profile_id);
$this
->assertNotNull($profile);
}
public function testDeletingProfileBeingUsedInConfigSettings() {
$profile = LingotekProfile::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
]);
$profile
->save();
$profile_id = $profile
->id();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
$edit = [
'table[node_type][enabled]' => 1,
'table[node_type][profile]' => $profile_id,
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-configuration-form');
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/delete");
$this
->assertText(t('This action cannot be undone.'));
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertNoRaw(t('The lingotek profile %profile has been deleted.', [
'%profile' => $profile
->label(),
]));
$this
->assertRaw(t('The Lingotek profile %profile is being used so cannot be deleted.', [
'%profile' => $profile
->label(),
]));
$profile = LingotekProfile::load($profile_id);
$this
->assertNotNull($profile);
}
public function testDeletingProfileBeingUsedInContentSettings() {
$profile = LingotekProfile::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
]);
$profile
->save();
$profile_id = $profile
->id();
$this
->drupalCreateContentType([
'type' => 'article',
'name' => 'Article',
]);
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
$edit = [
'node[article][enabled]' => 1,
'node[article][profiles]' => $profile_id,
'node[article][fields][title]' => 1,
'node[article][fields][body]' => 1,
];
$this
->drupalPostForm('admin/lingotek/settings', $edit, 'Save', [], [], 'lingoteksettings-tab-content-form');
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/delete");
$this
->assertText(t('This action cannot be undone.'));
$this
->drupalPostForm(NULL, [], t('Delete'));
$this
->assertNoRaw(t('The lingotek profile %profile has been deleted.', [
'%profile' => $profile
->label(),
]));
$this
->assertRaw(t('The Lingotek profile %profile is being used so cannot be deleted.', [
'%profile' => $profile
->label(),
]));
$profile = LingotekProfile::load($profile_id);
$this
->assertNotNull($profile);
}
public function testProfileSettingsOverride() {
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX')
->save();
ConfigurableLanguage::createFromLangcode('de')
->setThirdPartySetting('lingotek', 'locale', 'de_DE')
->save();
$profile = LingotekProfile::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
]);
$profile
->save();
$profile_id = $profile
->id();
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/edit");
$this
->assertOptionSelected('edit-language-overrides-es-overrides', 'default');
$this
->assertOptionSelected('edit-language-overrides-en-overrides', 'default');
$edit = [
'auto_upload' => FALSE,
'auto_download' => 1,
'project' => 'default',
'vault' => 'default',
'workflow' => 'another_workflow',
'language_overrides[es][overrides]' => 'custom',
'language_overrides[es][custom][auto_download]' => FALSE,
'language_overrides[es][custom][workflow]' => 'test_workflow',
'language_overrides[de][overrides]' => 'custom',
'language_overrides[de][custom][auto_download]' => FALSE,
'language_overrides[de][custom][workflow]' => 'default',
];
$this
->drupalPostForm(NULL, $edit, t('Save'));
$profile = LingotekProfile::load($profile_id);
$this
->assertFalse($profile
->hasAutomaticUpload());
$this
->assertTrue($profile
->hasAutomaticDownload());
$this
->assertIdentical('default', $profile
->getProject());
$this
->assertIdentical('default', $profile
->getVault());
$this
->assertIdentical('another_workflow', $profile
->getWorkflow());
$this
->assertIdentical('test_workflow', $profile
->getWorkflowForTarget('es'));
$this
->assertIdentical('default', $profile
->getWorkflowForTarget('de'));
$this
->assertFalse($profile
->hasAutomaticDownloadForTarget('es'));
$this
->assertFalse($profile
->hasAutomaticDownloadForTarget('de'));
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/edit");
$this
->assertNoFieldChecked("edit-auto-upload");
$this
->assertFieldChecked("edit-auto-download");
$this
->assertOptionSelected('edit-project', 'default');
$this
->assertOptionSelected('edit-vault', 'default');
$this
->assertOptionSelected('edit-workflow', 'another_workflow');
$this
->assertOptionSelected('edit-language-overrides-es-overrides', 'custom');
$this
->assertOptionSelected('edit-language-overrides-de-overrides', 'custom');
$this
->assertOptionSelected('edit-language-overrides-en-overrides', 'default');
$this
->assertNoFieldChecked('edit-language-overrides-es-custom-auto-download');
$this
->assertNoFieldChecked('edit-language-overrides-de-custom-auto-download');
$this
->assertFieldChecked('edit-language-overrides-en-custom-auto-download');
$this
->assertOptionSelected('edit-language-overrides-es-custom-workflow', 'test_workflow');
$this
->assertOptionSelected('edit-language-overrides-de-custom-workflow', 'default');
$this
->assertOptionSelected('edit-language-overrides-en-custom-workflow', 'default');
$selects = $this
->xpath('//details[@id="edit-language-overrides"]/*/*//select');
$this
->assertEqual(count($selects), 2 * 3, 'There are options for all the potential language overrides.');
$this
->assertEqual((string) $selects[0]['id'], 'edit-language-overrides-de-overrides', 'Languages are ordered alphabetically.');
}
public function testLanguageDisabled() {
$configLingotek = \Drupal::service('lingotek.configuration');
$es = ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX');
$de = ConfigurableLanguage::createFromLangcode('de')
->setThirdPartySetting('lingotek', 'locale', 'de_DE');
$es
->save();
$de
->save();
$profile = LingotekProfile::create([
'id' => strtolower($this
->randomMachineName()),
'label' => $this
->randomString(),
]);
$profile
->save();
$profile_id = $profile
->id();
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/edit");
$this
->assertFieldByName('language_overrides[es][overrides]');
$this
->assertOptionSelected('edit-language-overrides-de-overrides', 'default');
$this
->assertOptionSelected('edit-language-overrides-de-overrides', 'default');
$configLingotek
->disableLanguage($es);
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/edit");
$this
->assertNoFieldByName('language_overrides[es][overrides]');
$this
->assertOptionSelected('edit-language-overrides-de-overrides', 'default');
$configLingotek
->enableLanguage($es);
$this
->drupalGet("/admin/lingotek/settings/profile/{$profile_id}/edit");
$this
->assertFieldByName('language_overrides[es][overrides]');
$this
->assertOptionSelected('edit-language-overrides-es-overrides', 'default');
$this
->assertOptionSelected('edit-language-overrides-de-overrides', 'default');
}
protected function assertFieldDisabled($id, $message = '') {
$elements = $this
->xpath('//input[@id=:id]', array(
':id' => $id,
));
return $this
->assertTrue(isset($elements[0]) && !empty($elements[0]['disabled']), $message ? $message : t('Field @id is disabled.', array(
'@id' => $id,
)), t('Browser'));
}
protected function assertFieldEnabled($id, $message = '') {
$elements = $this
->xpath('//input[@id=:id]', array(
':id' => $id,
));
return $this
->assertTrue(isset($elements[0]) && empty($elements[0]['disabled']), $message ? $message : t('Field @id is enabled.', array(
'@id' => $id,
)), t('Browser'));
}
}