View source
<?php
namespace Drupal\Tests\entity_embed\FunctionalJavascript;
use Drupal\Component\Utility\Html;
use Drupal\file\Entity\File;
use Drupal\media\Entity\Media;
class ContentTranslationTest extends EntityEmbedTestBase {
protected $translator;
public static $modules = [
'entity_embed_translation_test',
];
protected function setUp() {
parent::setUp();
$this->translator = $this
->drupalCreateUser([
'use text format full_html',
'administer nodes',
'edit any article content',
'translate any entity',
]);
$this
->config('field.storage.node.body')
->set('translatable', TRUE)
->save();
}
protected function getAutocompleteSuggestions($search_string) {
$page = $this
->getSession()
->getPage();
$autocomplete_field = $field = $page
->findField('entity_id');
$this
->assertNotEmpty($autocomplete_field);
$autocomplete_field
->setValue($search_string);
$this
->getSession()
->getDriver()
->keyDown($autocomplete_field
->getXpath(), ' ');
$this
->assertSession()
->waitOnAutocomplete();
$suggestions = $this
->assertSession()
->waitForElementVisible('css', '.ui-autocomplete');
$this
->assertNotEmpty($suggestions);
return $suggestions
->getText();
}
public function testHostEntityLangcode() {
$node = $this
->createNode([
'type' => 'article',
'title' => 'Clark Kent',
]);
$node_fr = $node
->addTranslation('fr');
$node_fr->title = 'Superhomme';
$node_fr
->save();
\Drupal::service('file_system')
->copy($this->root . '/core/misc/druplicon.png', 'public://Smeagol.jpg');
$file = File::create([
'uri' => 'public://Smeagol.jpg',
'uid' => $this->translator
->id(),
]);
$file
->save();
$media = Media::create([
'bundle' => 'image',
'name' => 'Smeagol likes cheese',
'field_media_image' => [
[
'target_id' => $file
->id(),
'alt' => 'Smeagol likes cheese alt',
'title' => 'Smeagol likes cheese title',
],
],
]);
$media
->save();
$media_fr = $media
->addTranslation('fr');
$media_fr->name = "Gollum n'aime que la bague";
$media_fr->field_media_image
->setValue([
[
'target_id' => $file
->id(),
'alt' => "Gollum n'aime que la bague alt",
'title' => "Gollum n'aime que la bague title",
],
]);
$media_fr
->save();
$host = $this
->createNode([
'type' => 'article',
'title' => 'host',
'body' => [
'value' => '',
'format' => 'full_html',
],
]);
$host_fr = $host
->addTranslation('fr');
$host_fr->title = 'host';
$host_fr->body->value = '';
$host_fr->body->format = 'full_html';
$host_fr->body->lang = 'fr';
$host_fr
->save();
$this
->drupalLogin($this->translator);
$this
->drupalGet('node/' . $host
->id() . '/edit');
$this
->waitForEditor();
$this
->pressEditorButton('test_node');
$this
->assertNotEmpty($this
->assertSession()
->waitForElementVisible('css', '#entity-embed-dialog-form'));
$suggestions = $this
->getAutocompleteSuggestions('clar');
$this
->assertStringContainsString('Clark Kent', $suggestions);
$suggestions = $this
->getAutocompleteSuggestions('super');
$this
->assertStringNotContainsString('Superhomme', $suggestions);
$this
->assertSession()
->fieldExists('entity_id')
->setValue('Clark Kent (1)');
$this
->assertSession()
->elementExists('css', 'button.js-button-next')
->click();
$form = $this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$text = $form
->getText();
$this
->assertStringContainsString('Clark Kent', $text);
$this
->assertStringNotContainsString('Superhomme', $text);
$this
->getSession()
->reload();
$this
->assertSession()
->waitForElementVisible('css', 'a.cke_button__test_media_entity_embed')
->click();
$this
->assertSession()
->waitForId('drupal-modal');
$suggestions = $this
->getAutocompleteSuggestions('Smeagol likes cheese');
$this
->assertStringContainsString('Smeagol likes cheese', $suggestions);
$suggestions = $this
->getAutocompleteSuggestions("Gollum n'aime que la bague");
$this
->assertStringNotContainsString("Gollum n'aime que la bague", $suggestions);
$this
->assertSession()
->fieldExists('entity_id')
->setValue('Smeagol likes cheese (1)');
$this
->assertSession()
->elementExists('css', 'button.js-button-next')
->click();
$form = $this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$text = $form
->getText();
$this
->assertStringContainsString('Smeagol likes cheese', $text);
$this
->assertStringNotContainsString("Gollum n'aime que la bague", $text);
$this
->drupalGet('/fr/node/' . $host
->id() . '/edit');
$this
->waitForEditor();
$this
->pressEditorButton('test_node');
$this
->assertNotEmpty($this
->assertSession()
->waitForElementVisible('css', '#entity-embed-dialog-form'));
$suggestions = $this
->getAutocompleteSuggestions('super');
$this
->assertStringContainsString('Superhomme', $suggestions);
$suggestions = $this
->getAutocompleteSuggestions('clark');
$this
->assertStringNotContainsString('Clark Kent', $suggestions);
$this
->assertSession()
->fieldExists('entity_id')
->setValue('Superhomme (1)');
$this
->assertSession()
->elementExists('css', 'button.js-button-next')
->click();
$form = $this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$text = $form
->getText();
$this
->assertStringContainsString('Superhomme', $text);
$this
->assertStringNotContainsString('Clark Kent', $text);
$this
->assertSession()
->selectExists('attributes[data-entity-embed-display]')
->setValue('entity_reference:entity_reference_label');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->fieldExists('attributes[data-entity-embed-display-settings][link]')
->uncheck();
$this
->assertSession()
->elementExists('css', 'button.button--primary')
->press();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$this
->assertSession()
->pageTextContains('Superhomme');
$this
->getSession()
->reload();
$this
->assertSession()
->waitForElementVisible('css', 'a.cke_button__test_media_entity_embed')
->click();
$this
->assertSession()
->waitForId('drupal-modal');
$suggestions = $this
->getAutocompleteSuggestions("Gollum n'aime que la bague");
$this
->assertStringContainsString("Gollum n'aime que la bague", $suggestions);
$suggestions = $this
->getAutocompleteSuggestions('Smeagol likes cheese');
$this
->assertStringNotContainsString('Smeagol likes cheese', $suggestions);
$this
->assertSession()
->fieldExists('entity_id')
->setValue("Gollum n'aime que la bague (1)");
$this
->assertSession()
->elementExists('css', 'button.js-button-next')
->click();
$form = $this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$text = $form
->getText();
$this
->assertStringContainsString("Gollum n'aime que la bague", $text);
$this
->assertStringNotContainsString('Smeagol likes cheese', $text);
$this
->assertSession()
->selectExists('attributes[data-entity-embed-display]')
->setValue('entity_reference:media_thumbnail');
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assertSession()
->selectExists('attributes[data-entity-embed-display-settings][image_style]')
->setValue('medium');
$this
->assertSession()
->elementExists('css', 'button.button--primary')
->press();
$this
->assertSession()
->assertWaitOnAjaxRequest();
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertStringContainsString('Smeagol.jpg', $img
->getAttribute('src'));
$this
->assertEquals("Gollum n'aime que la bague alt", $img
->getAttribute('alt'));
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->buttonExists('Save')
->press();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertStringContainsString('Smeagol.jpg', $img
->getAttribute('src'));
$this
->assertEquals("Gollum n'aime que la bague alt", $img
->getAttribute('alt'));
$this
->drupalGet('/fr/node/' . $host
->id() . '/edit');
$this
->waitForEditor();
$select_and_edit_embed = "var editor = CKEDITOR.instances['edit-body-0-value'];\n var entityEmbed = editor.widgets.getByElement(editor.editable().findOne('div'));\n entityEmbed.focus();\n editor.execCommand('editdrupalentity');";
$this
->getSession()
->executeScript($select_and_edit_embed);
$this
->assertSession()
->assertWaitOnAjaxRequest();
$form = $this
->assertSession()
->waitForElementVisible('css', 'form.entity-embed-dialog-step--embed');
$text = $form
->getText();
$this
->assertStringContainsString("Gollum n'aime que la bague", $text);
$this
->assertStringNotContainsString('Smeagol likes cheese', $text);
$this
->assertSession()
->elementExists('css', '.ui-dialog-titlebar-close')
->press();
$this
->assertSession()
->waitForElementVisible('css', 'a.cke_button__source')
->click();
$source = $this
->assertSession()
->waitForElementVisible('xpath', "//textarea[contains(@class, 'cke_source')]");
$value = $source
->getValue();
$dom = Html::load($value);
$xpath = new \DOMXPath($dom);
$drupal_entity = $xpath
->query('//drupal-entity')[0];
$drupal_entity
->setAttribute("data-langcode", "en");
$source
->setValue(Html::serialize($dom));
$this
->pressEditorButton('source');
$this
->assignNameToCkeditorIframe();
$this
->getSession()
->switchToIFrame('ckeditor');
$img = $this
->assertSession()
->waitForElementVisible('css', 'img');
$this
->assertStringContainsString('Smeagol.jpg', $img
->getAttribute('src'));
$this
->assertEquals("Smeagol likes cheese alt", $img
->getAttribute('alt'));
$this
->getSession()
->switchToIFrame();
$this
->assertSession()
->buttonExists('Save')
->press();
$img = $this
->assertSession()
->elementExists('css', 'img');
$this
->assertStringContainsString('Smeagol.jpg', $img
->getAttribute('src'));
$this
->assertEquals("Smeagol likes cheese alt", $img
->getAttribute('alt'));
}
}