Commit 0b132ec5 authored by catch's avatar catch

Issue #2551217 by larowlan, legolasbo: Data loss using Preview on content types with image field

parent 585f6d92
......@@ -83,7 +83,10 @@ public function form(array $form, FormStateInterface $form_state) {
if ($preview = $store->get($uuid)) {
/** @var $preview \Drupal\Core\Form\FormStateInterface */
$form_state = $preview;
foreach ($preview->getValues() as $name => $value) {
$form_state->setValue($name, $value);
}
// Rebuild the form.
$form_state->setRebuild();
......
......@@ -12,6 +12,8 @@
use Drupal\Component\Utility\Unicode;
use Drupal\Core\Language\LanguageInterface;
use Drupal\entity_reference\Tests\EntityReferenceTestTrait;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\NodeType;
/**
......@@ -29,7 +31,7 @@ class PagePreviewTest extends NodeTestBase {
*
* @var array
*/
public static $modules = array('node', 'taxonomy', 'comment');
public static $modules = array('node', 'taxonomy', 'comment', 'image', 'file');
/**
* The name of the created field.
......@@ -68,6 +70,25 @@ protected function setUp() {
$this->term = $term;
// Create an image field.
FieldStorageConfig::create([
'field_name' => 'field_image',
'entity_type' => 'node',
'type' => 'image',
'settings' => [],
'cardinality' => FieldStorageConfig::CARDINALITY_UNLIMITED,
])->save();
$field_config = FieldConfig::create([
'field_name' => 'field_image',
'label' => 'Images',
'entity_type' => 'node',
'bundle' => 'page',
'required' => FALSE,
'settings' => [],
]);
$field_config->save();
// Create a field.
$this->fieldName = Unicode::strtolower($this->randomMachineName());
$handler_settings = array(
......@@ -95,6 +116,17 @@ protected function setUp() {
'type' => 'entity_reference_label',
))
->save();
entity_get_form_display('node', 'page', 'default')
->setComponent('field_image', array(
'type' => 'image_image',
'settings' => [],
))
->save();
entity_get_display('node', 'page', 'default')
->setComponent('field_image')
->save();
}
/**
......@@ -110,7 +142,14 @@ function testPagePreview() {
$edit[$title_key] = $this->randomMachineName(8);
$edit[$body_key] = $this->randomMachineName(16);
$edit[$term_key] = $this->term->getName();
$this->drupalPostForm('node/add/page', $edit, t('Preview'));
// Upload an image.
$test_image = current($this->drupalGetTestFiles('image', 39325));
$edit['files[field_image_0][]'] = drupal_realpath($test_image->uri);
$this->drupalPostForm('node/add/page', $edit, t('Upload'));
// Add an alt tag and preview the node.
$this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Preview'));
// Check that the preview is displaying the title, body and term.
$this->assertTitle(t('@title | Drupal', array('@title' => $edit[$title_key])), 'Basic page title is preview.');
......@@ -141,6 +180,7 @@ function testPagePreview() {
$this->assertFieldByName($title_key, $edit[$title_key], 'Title field displayed.');
$this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
$this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.');
$this->assertFieldByName('field_image[0][alt]', 'Picture of llamas');
// Return to page preview to check everything is as expected.
$this->drupalPostForm(NULL, array(), t('Preview'));
......@@ -156,8 +196,9 @@ function testPagePreview() {
$this->assertFieldByName($body_key, $edit[$body_key], 'Body field displayed.');
$this->assertFieldByName($term_key, $edit[$term_key] . ' (' . $this->term->id() . ')', 'Term field displayed.');
// Save the node.
$this->drupalPostForm('node/add/page', $edit, t('Save'));
// Save the node - this is a new POST, so we need to upload the image.
$this->drupalPostForm('node/add/page', $edit, t('Upload'));
$this->drupalPostForm(NULL, ['field_image[0][alt]' => 'Picture of llamas'], t('Save'));
$node = $this->drupalGetNodeByTitle($edit[$title_key]);
// Check the term was displayed on the saved node.
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment