View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\comment\Entity\CommentType;
use Drupal\comment\Tests\CommentTestTrait;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\lingotek\Entity\LingotekProfile;
class LingotekContentEntityGetProfileHookTest extends LingotekTestBase {
use CommentTestTrait;
public static $modules = [
'block',
'comment',
'node',
];
protected $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',
]);
$this
->drupalCreateContentType([
'type' => 'null_profile',
'name' => 'NullProfileContentType',
]);
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX')
->save();
$comment_type = CommentType::create([
'id' => 'comment',
'label' => 'Comment',
'description' => '',
'target_entity_type_id' => 'node',
]);
$comment_type
->save();
$this
->addDefaultCommentField('node', 'article');
ContentLanguageSettings::loadByEntityTypeBundle('node', 'article')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'article', TRUE);
ContentLanguageSettings::loadByEntityTypeBundle('node', 'null_profile')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'null_profile', TRUE);
ContentLanguageSettings::loadByEntityTypeBundle('comment', 'comment')
->setLanguageAlterable(FALSE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('comment', 'comment', TRUE);
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$this
->applyEntityUpdates();
$this
->rebuildContainer();
}
public function testProfileOverrideOnUploadTranslation() {
$this
->saveLingotekContentTranslationSettings([
'node' => [
'article' => [
'profiles' => 'manual',
'fields' => [
'title' => 1,
'body' => 1,
],
],
],
'comment' => [
'comment' => [
'profiles' => 'automatic',
'fields' => [
'comment_body' => 1,
],
],
],
]);
$profile1 = LingotekProfile::create([
'id' => 'group_1',
'label' => 'Group 1',
'auto_upload' => TRUE,
'intelligence_metadata' => [
'override' => TRUE,
'use_business_division' => TRUE,
'business_division' => 'Group 1',
],
]);
$profile1
->save();
$profile2 = LingotekProfile::create([
'id' => 'group_2',
'label' => 'Group 2',
'auto_upload' => TRUE,
'intelligence_metadata' => [
'override' => TRUE,
'use_business_division' => TRUE,
'business_division' => 'Group 2',
],
]);
$profile2
->save();
$this
->drupalGet("/admin/lingotek/settings/profile/group_1/edit");
$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]'] = 'group_1';
$this
->saveAndPublishNodeForm($edit);
$edit = [];
$edit['subject[0][value]'] = 'Group 1 test';
$edit['comment_body[0][value]'] = 'Group 1 test body';
$this
->drupalPostForm(NULL, $edit, 'Save');
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), TRUE);
$this
->assertUploadedDataFieldCount($data, 1);
$this
->assertTrue(isset($data['comment_body'][0]['value']));
$this
->assertEquals('Group 1', $data['_lingotek_metadata']['_intelligence']['business_division']);
$edit = [];
$edit['title[0][value]'] = 'Llamas are cool 2';
$edit['body[0][value]'] = 'Llamas are very cool 2';
$edit['langcode[0][value]'] = 'en';
$edit['lingotek_translation_management[lingotek_translation_profile]'] = 'group_2';
$this
->saveAndPublishNodeForm($edit);
$edit = [];
$edit['subject[0][value]'] = 'Group 2 test';
$edit['comment_body[0][value]'] = 'Group 2 test body';
$this
->drupalPostForm(NULL, $edit, 'Save');
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), TRUE);
$this
->assertUploadedDataFieldCount($data, 1);
$this
->assertTrue(isset($data['comment_body'][0]['value']));
$this
->assertEquals('Group 2', $data['_lingotek_metadata']['_intelligence']['business_division']);
}
public function testGetProfileWithoutParentProfile() {
$edit = [];
$edit['title[0][value]'] = 'Llamas are cool';
$edit['body[0][value]'] = 'Llamas are very cool';
$edit['langcode[0][value]'] = 'en';
$this
->saveAndPublishNodeForm($edit);
$edit = [];
$edit['subject[0][value]'] = 'First test';
$edit['comment_body[0][value]'] = 'First test body';
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->saveLingotekContentTranslationSettings([
'node' => [
'article' => [
'profiles' => 'manual',
'fields' => [
'title' => 1,
'body' => 1,
],
],
],
'comment' => [
'comment' => [
'profiles' => 'manual',
'fields' => [
'comment_body' => 1,
],
],
],
]);
$this
->goToContentBulkManagementForm('comment');
$this
->drupalGet('node/1');
$edit = [];
$edit['subject[0][value]'] = 'Second test';
$edit['comment_body[0][value]'] = 'Second test body';
$this
->drupalPostForm(NULL, $edit, 'Save');
$this
->goToContentBulkManagementForm('comment');
}
public function testProfileWithNullProfile() {
$this
->saveLingotekContentTranslationSettings([
'node' => [
'null_profile' => [
'profiles' => 'manual',
'fields' => [
'title' => 1,
],
],
],
]);
$edit = [];
$edit['title[0][value]'] = 'Llamas are cool';
$edit['langcode[0][value]'] = 'en';
$this
->saveAndPublishNodeForm($edit, 'null_profile');
$this
->goToContentBulkManagementForm();
}
}