View source
<?php
namespace Drupal\Tests\lingotek\Functional;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\language\Entity\ContentLanguageSettings;
use Drupal\node\Entity\Node;
use Drupal\Tests\TestFileCreationTrait;
class LingotekContentTranslationDocumentUploadHookTest extends LingotekTestBase {
use TestFileCreationTrait;
public static $modules = [
'block',
'node',
'image',
];
protected $node;
protected function setUp() : void {
parent::setUp();
$this
->drupalPlaceBlock('local_tasks_block');
$this
->drupalPlaceBlock('page_title_block');
$this
->drupalCreateContentType([
'type' => 'animal',
'name' => 'Animal',
]);
$this
->createImageField('field_image', 'animal');
ConfigurableLanguage::createFromLangcode('es')
->setThirdPartySetting('lingotek', 'locale', 'es_MX')
->save();
ContentLanguageSettings::loadByEntityTypeBundle('node', 'animal')
->setLanguageAlterable(TRUE)
->save();
\Drupal::service('content_translation.manager')
->setEnabled('node', 'animal', TRUE);
drupal_static_reset();
\Drupal::entityTypeManager()
->clearCachedDefinitions();
$this
->applyEntityUpdates();
$this
->rebuildContainer();
$this
->saveLingotekContentTranslationSettings([
'node' => [
'animal' => [
'profiles' => 'automatic',
'fields' => [
'title' => 1,
'body' => 1,
'field_image' => [
'alt',
],
],
],
],
]);
}
public function testNodeTranslation() {
$this
->drupalLogin($this->rootUser);
$test_image = current($this
->getTestFiles('image'));
$edit = [];
$edit['title[0][value]'] = 'Llamas are cool';
$edit['body[0][value]'] = 'Llamas are very cool';
$edit['langcode[0][value]'] = 'en';
$edit['files[field_image_0]'] = \Drupal::service('file_system')
->realpath($test_image->uri);
$this
->drupalPostForm('node/add/animal', $edit, t('Preview'));
unset($edit['files[field_image_0]']);
$edit['field_image[0][alt]'] = 'Llamas are cool';
$this
->saveAndPublishNodeForm($edit, NULL);
$this->node = Node::load(1);
$data = json_decode(\Drupal::state()
->get('lingotek.uploaded_content', '[]'), TRUE);
$this
->assertUploadedDataFieldCount($data, 4);
$this
->assertTrue(isset($data['title'][0]['value']));
$this
->assertEqual(1, count($data['body'][0]));
$this
->assertTrue(isset($data['body'][0]['value']));
$this
->assertEqual(1, count($data['field_image'][0]));
$this
->assertTrue(isset($data['field_image'][0]['alt']));
$this
->assertTrue(isset($data['animal_date']));
$this
->assertEqual('2016-05-01', $data['animal_date']);
$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.');
$uploaded_url = \Drupal::state()
->get('lingotek.uploaded_url');
$this
->assertIdentical(\Drupal::request()
->getBasePath() . '/animal/2016/llamas-are-cool', $uploaded_url, 'The altered url was used.');
}
}