/**
  * Tests the paragraph creation.
  */
 public function testParagraphsCreation()
 {
     $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes', 'create article content', 'create paragraphs content', 'administer content types', 'administer node fields', 'administer node display', 'administer paragraphs types', 'administer paragraph fields', 'administer paragraph display', 'administer paragraph form display', 'administer node form display', 'edit any article content', 'delete any article content'));
     $this->drupalLogin($admin_user);
     $this->drupalGet('admin/structure/paragraphs_type');
     $this->clickLink(t('Add a Paragraphs type'));
     // Create paragraph type text + image.
     $edit = array('label' => 'Text + Image', 'id' => 'text_image');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Create field types for text and image.
     static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'text', 'Text', 'text_long', array(), array());
     $this->assertText('Saved Text configuration.');
     static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'image', 'Image', 'image', array(), array('settings[alt_field_required]' => FALSE));
     $this->assertText('Saved Image configuration.');
     // Create paragraph type image.
     $edit = array('label' => 'Image only', 'id' => 'image');
     $this->drupalPostForm('admin/structure/paragraphs_type/add', $edit, t('Save'));
     // Create field types for image.
     static::fieldUIAddNewField('admin/structure/paragraphs_type/image', 'image_only', 'Image only', 'image', array(), array());
     $this->assertText('Saved Image only configuration.');
     $this->drupalGet('admin/structure/paragraphs_type');
     $rows = $this->xpath('//tbody/tr');
     // Make sure 2 types are available with their label.
     $this->assertEqual(count($rows), 2);
     $this->assertText('Text + Image');
     $this->assertText('Image only');
     // Make sure there is an edit link for each type.
     $this->clickLink(t('Edit'));
     // Make sure the field UI appears.
     $this->assertLink('Manage fields');
     $this->assertLink('Manage form display');
     $this->assertLink('Manage display');
     $this->assertTitle('Edit paragraph type | Drupal');
     // Create an article with paragraphs field.
     static::fieldUIAddNewField('admin/structure/types/manage/article', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array('settings[target_type]' => 'paragraph', 'cardinality' => '-1'), array('settings[handler_settings][target_bundles_drag_drop][image][enabled]' => TRUE, 'settings[handler_settings][target_bundles_drag_drop][text_image][enabled]' => TRUE, 'description' => 'Help text.'));
     // Configure article fields.
     $this->drupalGet('admin/structure/types/manage/article/fields');
     $this->clickLink(t('Edit'), 1);
     $this->drupalPostForm(NULL, NULL, t('Save settings'));
     $this->clickLink(t('Manage display'));
     $this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_revisions_entity_view'), t('Save'));
     $this->clickLink(t('Manage form display'));
     $this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_paragraphs'), t('Save'));
     // Test for "Add mode" setting.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $field_name = 'field_paragraphs';
     // Click on the widget settings button to open the widget settings form.
     $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
     // Enable setting.
     $edit = array('fields[' . $field_name . '][settings_edit_form][settings][add_mode]' => 'button');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Check if the setting is stored.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $this->assertText('Add mode: Buttons', 'Checking the settings value.');
     $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
     // Assert the 'Buttons' option is selected.
     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-add-mode', 'button', 'Updated value is correct!.');
     // Add two Text + Image paragraphs in article.
     $this->drupalGet('node/add/article');
     // Check if help text is saved correctly.
     $this->assertText('Help text.');
     // Checking changes on article.
     $this->assertRaw('<div class="paragraphs-dropbutton-wrapper"><input', 'Updated value in article.');
     $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_image_add_more');
     $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_image_add_more');
     // Create an 'image' file, upload it.
     $text = 'Trust me I\'m an image';
     file_put_contents('temporary://myImage1.jpg', $text);
     file_put_contents('temporary://myImage2.jpg', $text);
     $edit = array('title[0][value]' => 'Test article', 'field_paragraphs[0][subform][field_text][0][value]' => 'Test text 1', 'files[field_paragraphs_0_subform_field_image_0]' => drupal_realpath('temporary://myImage1.jpg'), 'field_paragraphs[1][subform][field_text][0][value]' => 'Test text 2', 'files[field_paragraphs_1_subform_field_image_0]' => drupal_realpath('temporary://myImage2.jpg'));
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $node = $this->drupalGetNodeByTitle('Test article');
     $img1_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage1.jpg'));
     $img2_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage2.jpg'));
     // Check the text and image after publish.
     $this->assertText('Test text 1');
     $this->assertRaw('<img src="' . $img1_url);
     $this->assertText('Test text 2');
     $this->assertRaw('<img src="' . $img2_url);
     // Tests for "Edit mode" settings.
     // Test for closed setting.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     // Click on the widget settings button to open the widget settings form.
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     // Enable setting.
     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Check if the setting is stored.
     $this->assertText('Edit mode: Closed', 'Checking the settings value.');
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     // Assert the 'Closed' option is selected.
     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'closed', 'Updated value correctly.');
     $this->drupalGet('node/1/edit');
     // The textareas for paragraphs should not be visible.
     $this->assertNoRaw('field_paragraphs[0][subform][field_text][0][value]');
     $this->assertNoRaw('field_paragraphs[1][subform][field_text][0][value]');
     $this->assertNoText('Test text 1');
     $this->assertNoText('Test text 2');
     // Test for preview option.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('Edit mode: Preview', 'Checking the settings value.');
     $this->drupalGet('node/1/edit');
     // The texts in the paragraphs should be visible.
     $this->assertNoRaw('field_paragraphs[0][subform][field_text][0][value]');
     $this->assertNoRaw('field_paragraphs[1][subform][field_text][0][value]');
     $this->assertText('Test text 1');
     $this->assertText('Test text 2');
     // Test for open option.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     // Assert the 'Preview' option is selected.
     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'preview', 'Updated value correctly.');
     // Restore the value to Open for next test.
     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'open');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->drupalGet('node/1/edit');
     // The textareas for paragraphs should be visible.
     $this->assertRaw('field_paragraphs[0][subform][field_text][0][value]');
     $this->assertRaw('field_paragraphs[1][subform][field_text][0][value]');
     $paragraphs = Paragraph::loadMultiple();
     $this->assertEqual(count($paragraphs), 2, 'Two paragraphs in article');
     // Check article edit page.
     $this->drupalGet('node/' . $node->id() . '/edit');
     // Check both paragraphs in edit page.
     $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'Test text 1');
     $this->assertRaw('<a href="' . $img1_url . '" type="image/jpeg; length=21">myImage1.jpg</a>');
     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
     $this->assertRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
     // Remove 2nd paragraph.
     $this->drupalPostForm(NULL, NULL, t('Remove'));
     $this->assertNoField('field_paragraphs[1][subform][field_text][0][value]');
     $this->assertNoRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
     // Restore it again.
     $this->drupalPostForm(NULL, NULL, t('Restore'));
     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
     $this->assertRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
     // @todo enable below test in 2428833.
     //    // Remove it and confirm.
     //    $this->drupalPostAjaxForm(NULL, NULL, array('field_paragraphs_1_subform_field_image_0_remove_button' => t('Remove')));
     //    $this->drupalPostAjaxForm(NULL, NULL, array('field_paragraphs_1_confirm_remove' => t('Confirm removal')));
     //    $this->drupalPostForm(NULL, NULL, t('Save and keep published'));
     //    $current_paragraphs  = Paragraph::loadMultiple();
     //    debug(count($current_paragraphs));
     //    $this->assertEqual(count($current_paragraphs), 1, 'Only one paragraph in article');
     // Delete the node.
     $this->clickLink(t('Delete'));
     $this->drupalPostForm(NULL, NULL, t('Delete'));
     $this->assertText('Test article has been deleted.');
     // @todo enable below tests in 2429335.
     //    // Make sure two paragraph entities have been deleted.
     //    $current_paragraphs = Paragraph::loadMultiple();
     //    $this->assertTrue(empty($current_paragraphs));
 }
 /**
  * Helper function to create our different paragraph types.
  *
  * @param  [type] $paragraph [description]
  * @return [type]            [description]
  */
 private function createParagraphItem($paragraph)
 {
     // Default data for all paragraph types
     $data = ['type' => $paragraph['type']];
     // Every paragraph type might add specific data
     switch ($paragraph['type']) {
         case 'cr_rich_text_paragraph':
             $data['field_body'] = ['value' => $paragraph['body'], 'format' => 'basic_html'];
             $data['field_background'] = $this->expandImage($paragraph['image']);
             break;
         case 'cr_single_message_row':
             $data['field_single_msg_row_lr_title'] = ['value' => $paragraph['title']];
             $data['field_single_msg_row_lr_variant'] = ['value' => $paragraph['variant']];
             $data['field_single_msg_row_lr_body'] = ['value' => $paragraph['body'], 'format' => 'basic_html'];
             $data['field_single_msg_row_lr_image'] = $this->expandImage($paragraph['image']);
             break;
     }
     $paragraph_item = \Drupal\paragraphs\Entity\Paragraph::create($data);
     $paragraph_item->save();
     return $paragraph_item;
 }
 /**
  * Tests the paragraph creation.
  */
 public function testParagraphsCreation()
 {
     $admin_user = $this->drupalCreateUser(array('administer site configuration', 'administer nodes', 'create article content', 'create paragraphs content', 'administer content types', 'administer node fields', 'administer node display', 'administer paragraphs types', 'administer paragraph fields', 'administer paragraph display', 'administer paragraph form display', 'administer node form display', 'edit any article content', 'delete any article content'));
     $this->drupalLogin($admin_user);
     // Assert suggested 'Add a paragraph type' link when there is no type yet.
     $this->drupalGet('admin/structure/paragraphs_type');
     $this->assertText('There is no Paragraphs type yet.');
     $this->drupalGet('admin/structure/types/manage/paragraphs/fields/add-field');
     $edit = ['new_storage_type' => 'field_ui:entity_reference_revisions:paragraph', 'label' => 'Paragraph', 'field_name' => 'paragraph'];
     $this->drupalPostForm(NULL, $edit, 'Save and continue');
     $this->drupalPostForm(NULL, [], 'Save field settings');
     $this->assertLinkByHref('admin/structure/paragraphs_type/add');
     $this->clickLink('here');
     $this->assertUrl('admin/structure/paragraphs_type/add');
     $this->drupalGet('admin/structure/paragraphs_type');
     $this->clickLink(t('Add paragraphs type'));
     // Create paragraph type text + image.
     $edit = array('label' => 'Text + Image', 'id' => 'text_image');
     $this->drupalPostForm(NULL, $edit, t('Save and manage fields'));
     $this->assertUrl('admin/structure/paragraphs_type/text_image/fields');
     // Create field types for text and image.
     static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'text', 'Text', 'text_long', array(), array());
     $this->assertText('Saved Text configuration.');
     static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'image', 'Image', 'image', array(), array('settings[alt_field_required]' => FALSE));
     $this->assertText('Saved Image configuration.');
     // Create paragraph type Nested test.
     $edit = array('label' => 'Nested_test', 'id' => 'nested_test');
     $this->drupalPostForm('admin/structure/paragraphs_type/add', $edit, t('Save and manage fields'));
     static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_test', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array('settings[target_type]' => 'paragraph', 'cardinality' => '-1'), array());
     // Change the add more button to select mode.
     $this->clickLink(t('Manage form display'));
     $this->drupalPostAjaxForm(NULL, ['fields[field_paragraphs][type]' => 'entity_reference_paragraphs'], 'field_paragraphs_settings_edit');
     $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][add_mode]' => 'select'], t('Update'));
     $this->drupalPostForm(NULL, [], t('Save'));
     // Create paragraph type image.
     $edit = array('label' => 'Image only', 'id' => 'image');
     $this->drupalPostForm('admin/structure/paragraphs_type/add', $edit, t('Save and manage fields'));
     $this->assertUrl('admin/structure/paragraphs_type/image/fields');
     // Create field types for image.
     static::fieldUIAddNewField('admin/structure/paragraphs_type/image', 'image_only', 'Image only', 'image', array(), array());
     $this->assertText('Saved Image only configuration.');
     $this->drupalGet('admin/structure/paragraphs_type');
     $rows = $this->xpath('//tbody/tr');
     // Make sure 2 types are available with their label.
     $this->assertEqual(count($rows), 3);
     $this->assertText('Text + Image');
     $this->assertText('Image only');
     // Make sure there is an edit link for each type.
     $this->clickLink(t('Edit'));
     // Make sure the field UI appears.
     $this->assertLink('Manage fields');
     $this->assertLink('Manage form display');
     $this->assertLink('Manage display');
     $this->assertTitle('Edit Image only paragraph type | Drupal');
     // Create an article with paragraphs field.
     static::fieldUIAddNewField('admin/structure/types/manage/article', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array('settings[target_type]' => 'paragraph', 'cardinality' => '-1'), array('description' => 'Help text.'));
     // Configure article fields.
     $this->drupalGet('admin/structure/types/manage/article/fields');
     $this->clickLink(t('Edit'), 1);
     $this->drupalPostForm(NULL, NULL, t('Save settings'));
     $this->clickLink(t('Manage display'));
     $this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_revisions_entity_view'), t('Save'));
     $this->clickLink(t('Manage form display'));
     $this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_paragraphs'), t('Save'));
     // Test for "Add mode" setting.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $field_name = 'field_paragraphs';
     // Click on the widget settings button to open the widget settings form.
     $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
     // Enable setting.
     $edit = array('fields[' . $field_name . '][settings_edit_form][settings][add_mode]' => 'button');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Check if the setting is stored.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $this->assertText('Add mode: Buttons', 'Checking the settings value.');
     $this->drupalPostAjaxForm(NULL, array(), $field_name . "_settings_edit");
     // Assert the 'Buttons' option is selected.
     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-add-mode', 'button', 'Updated value is correct!.');
     // Add two Text + Image paragraphs in article.
     $this->drupalGet('node/add/article');
     // Check if help text is saved correctly.
     $this->assertText('Help text.');
     // Checking changes on article.
     $this->assertRaw('<div class="paragraphs-dropbutton-wrapper"><input', 'Updated value in article.');
     $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_image_add_more');
     $this->drupalPostAjaxForm(NULL, array(), 'field_paragraphs_text_image_add_more');
     // Create an 'image' file, upload it.
     $text = 'Trust me I\'m an image';
     file_put_contents('temporary://myImage1.jpg', $text);
     file_put_contents('temporary://myImage2.jpg', $text);
     $edit = array('title[0][value]' => 'Test article', 'field_paragraphs[0][subform][field_text][0][value]' => 'Test text 1', 'files[field_paragraphs_0_subform_field_image_0]' => drupal_realpath('temporary://myImage1.jpg'), 'field_paragraphs[1][subform][field_text][0][value]' => 'Test text 2', 'files[field_paragraphs_1_subform_field_image_0]' => drupal_realpath('temporary://myImage2.jpg'));
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $node = $this->drupalGetNodeByTitle('Test article');
     $img1_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage1.jpg'));
     $img2_url = file_create_url(\Drupal::token()->replace('public://[date:custom:Y]-[date:custom:m]/myImage2.jpg'));
     // Check the text and image after publish.
     $this->assertText('Test text 1');
     $this->assertRaw('<img src="' . file_url_transform_relative($img1_url));
     $this->assertText('Test text 2');
     $this->assertRaw('<img src="' . file_url_transform_relative($img2_url));
     // Tests for "Edit mode" settings.
     // Test for closed setting.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     // Click on the widget settings button to open the widget settings form.
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     // Enable setting.
     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     // Check if the setting is stored.
     $this->assertText('Edit mode: Closed', 'Checking the settings value.');
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     // Assert the 'Closed' option is selected.
     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'closed', 'Updated value correctly.');
     $this->drupalGet('node/1/edit');
     // The textareas for paragraphs should not be visible.
     $this->assertNoRaw('field_paragraphs[0][subform][field_text][0][value]');
     $this->assertNoRaw('field_paragraphs[1][subform][field_text][0][value]');
     $this->assertNoText('Test text 1');
     $this->assertNoText('Test text 2');
     // Test for preview option.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->assertText('Edit mode: Preview', 'Checking the settings value.');
     $this->drupalGet('node/1/edit');
     // The texts in the paragraphs should be visible.
     $this->assertNoRaw('field_paragraphs[0][subform][field_text][0][value]');
     $this->assertNoRaw('field_paragraphs[1][subform][field_text][0][value]');
     $this->assertText('Test text 1');
     $this->assertText('Test text 2');
     // Test for open option.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $this->drupalPostAjaxForm(NULL, array(), "field_paragraphs_settings_edit");
     // Assert the 'Preview' option is selected.
     $this->assertOptionSelected('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'preview', 'Updated value correctly.');
     // Restore the value to Open for next test.
     $edit = array('fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'open');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->drupalGet('node/1/edit');
     // The textareas for paragraphs should be visible.
     $this->assertRaw('field_paragraphs[0][subform][field_text][0][value]');
     $this->assertRaw('field_paragraphs[1][subform][field_text][0][value]');
     $paragraphs = Paragraph::loadMultiple();
     $this->assertEqual(count($paragraphs), 2, 'Two paragraphs in article');
     // Check article edit page.
     $this->drupalGet('node/' . $node->id() . '/edit');
     // Check both paragraphs in edit page.
     $this->assertFieldByName('field_paragraphs[0][subform][field_text][0][value]', 'Test text 1');
     $this->assertRaw('<a href="' . $img1_url . '" type="image/jpeg; length=21">myImage1.jpg</a>');
     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
     $this->assertRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
     // Remove 2nd paragraph.
     $this->drupalPostForm(NULL, NULL, t('Remove'));
     $this->assertNoField('field_paragraphs[1][subform][field_text][0][value]');
     $this->assertNoRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
     // Restore it again.
     $this->drupalPostForm(NULL, NULL, t('Restore'));
     $this->assertFieldByName('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
     $this->assertRaw('<a href="' . $img2_url . '" type="image/jpeg; length=21">myImage2.jpg</a>');
     // @todo enable below test in 2428833.
     //    // Remove it and confirm.
     //    $this->drupalPostAjaxForm(NULL, NULL, array('field_paragraphs_1_subform_field_image_0_remove_button' => t('Remove')));
     //    $this->drupalPostAjaxForm(NULL, NULL, array('field_paragraphs_1_confirm_remove' => t('Confirm removal')));
     //    $this->drupalPostForm(NULL, NULL, t('Save and keep published'));
     //    $current_paragraphs  = Paragraph::loadMultiple();
     //    debug(count($current_paragraphs));
     //    $this->assertEqual(count($current_paragraphs), 1, 'Only one paragraph in article');
     // Delete the node.
     $this->clickLink(t('Delete'));
     $this->drupalPostForm(NULL, NULL, t('Delete'));
     $this->assertText('Test article has been deleted.');
     // @todo enable below tests in 2429335.
     //    // Make sure two paragraph entities have been deleted.
     //    $current_paragraphs = Paragraph::loadMultiple();
     //    $this->assertTrue(empty($current_paragraphs));
     // Check if the publish/unpublish option works.
     $this->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
     $edit = ['fields[status][type]' => 'boolean_checkbox'];
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->drupalGet('node/add/article');
     $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
     $this->assertRaw('edit-field-paragraphs-0-subform-status-value');
     $edit = ['title[0][value]' => 'Example publish/unpublish', 'field_paragraphs[0][subform][field_text][0][value]' => 'Example published and unpublished'];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $this->assertText(t('Example published and unpublished'));
     $this->clickLink(t('Edit'));
     $edit = ['field_paragraphs[0][subform][status][value]' => FALSE];
     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
     $this->assertNoText(t('Example published and unpublished'));
     // Set the fields as required.
     $this->drupalGet('admin/structure/types/manage/article/fields');
     $this->clickLink('Edit', 1);
     $this->drupalPostForm(NULL, ['required' => TRUE], t('Save settings'));
     $this->drupalGet('admin/structure/paragraphs_type/nested_test/fields');
     $this->clickLink('Edit');
     $this->drupalPostForm(NULL, ['required' => TRUE], t('Save settings'));
     // Add a new article.
     $this->drupalGet('node/add/article');
     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_nested_test_add_more');
     $edit = ['field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'image'];
     $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_subform_field_paragraphs_add_more');
     // Test the new field is displayed.
     $this->assertFieldByName('files[field_paragraphs_0_subform_field_paragraphs_0_subform_field_image_only_0]');
     // Add an image to the required field.
     $edit = array('title[0][value]' => 'test required', 'files[field_paragraphs_0_subform_field_paragraphs_0_subform_field_image_only_0]' => drupal_realpath('temporary://myImage2.jpg'));
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $edit = ['field_paragraphs[0][subform][field_paragraphs][0][subform][field_image_only][0][width]' => 100, 'field_paragraphs[0][subform][field_paragraphs][0][subform][field_image_only][0][height]' => 100, 'field_paragraphs[0][subform][field_paragraphs][0][subform][field_image_only][0][alt]' => 'Alternative_text'];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $this->assertText('test required has been created.');
     $this->assertNoRaw('This value should not be null.');
     // Test that unsupported widgets are not displayed.
     $this->drupalGet('admin/structure/types/manage/article/form-display');
     $select = $this->xpath('//*[@id="edit-fields-field-paragraphs-type"]')[0];
     $this->assertEqual(count($select->option), 2);
     $this->assertRaw('value="entity_reference_paragraphs" selected="selected"');
     // Check that Paragraphs is not displayed as an entity_reference field
     // reference option.
     $this->drupalGet('admin/structure/types/manage/article/fields/add-field');
     $edit = ['new_storage_type' => 'entity_reference', 'label' => 'unsupported field', 'field_name' => 'unsupportedfield'];
     $this->drupalPostForm(NULL, $edit, t('Save and continue'));
     $this->assertNoOption('edit-settings-target-type', 'paragraph');
     // Test that all paragraph types can be referenced if none is selected.
     $edit = array('label' => 'Nested_double_test', 'id' => 'nested_double_test');
     $this->drupalPostForm('admin/structure/paragraphs_type/add', $edit, t('Save and manage fields'));
     static::fieldUIAddExistingField('admin/structure/paragraphs_type/nested_double_test', 'field_paragraphs', 'paragraphs_1');
     $this->clickLink(t('Manage form display'));
     $this->drupalPostForm(NULL, [], 'Save');
     //$this->drupalPostForm(NULL, array('fields[field_paragraphs][type]' => 'entity_reference_revisions_entity_view'), t('Save'));
     static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_double_test', 'paragraphs_2', 'paragraphs_2', 'entity_reference_revisions', array('settings[target_type]' => 'paragraph', 'cardinality' => '-1'), array());
     $this->clickLink(t('Manage form display'));
     $this->drupalPostForm(NULL, [], 'Save');
     $this->drupalPostAjaxForm('node/add/article', [], 'field_paragraphs_nested_test_add_more');
     $edit = ['field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'nested_double_test'];
     $this->drupalPostAjaxForm(NULL, $edit, 'field_paragraphs_0_subform_field_paragraphs_add_more');
     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_subform_field_paragraphs_0_subform_field_paragraphs_image_add_more');
     $edit = array('title[0][value]' => 'Nested twins');
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $this->assertText('Nested twins has been created.');
     $this->assertNoText('This entity (paragraph: ) cannot be referenced.');
     // Set the fields as not required.
     $this->drupalGet('admin/structure/types/manage/article/fields');
     $this->clickLink('Edit', 1);
     $this->drupalPostForm(NULL, ['required' => FALSE], t('Save settings'));
     // Set the Paragraph field edit mode to 'Closed'.
     $this->drupalPostAjaxForm('admin/structure/types/manage/article/form-display', [], 'field_paragraphs_settings_edit');
     $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed'], t('Update'));
     $this->drupalPostForm(NULL, [], t('Save'));
     $edit = array('label' => 'Node_test', 'id' => 'node_test');
     $this->drupalPostForm('admin/structure/paragraphs_type/add', $edit, t('Save and manage fields'));
     // Add a node reference field.
     static::fieldUIAddNewField('admin/structure/paragraphs_type/node_test', 'entity_reference', 'Entity reference', 'entity_reference', array('settings[target_type]' => 'node', 'cardinality' => '-1'), array('settings[handler_settings][target_bundles][article]' => TRUE));
     $node = $this->drupalGetNodeByTitle('Nested twins');
     // Create a node with a reference in a Paragraph.
     $this->drupalPostAjaxForm('node/add/article', [], 'field_paragraphs_node_test_add_more');
     $edit = ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node->label() . ' (' . $node->id() . ')', 'title[0][value]' => 'choke test'];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     // Delete the referenced node.
     $node->delete();
     // Edit the node with the reference.
     $this->clickLink(t('Edit'));
     // Attempt to edit the Paragraph.
     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_edit');
     // Try to collapse with an invalid reference.
     $this->drupalPostAjaxForm(NULL, ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => 'foo'], 'field_paragraphs_0_collapse');
     $this->assertText('There are no entities matching "foo".');
     // Attempt to remove the Paragraph.
     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
     $elements = $this->xpath('//*[@name="field_paragraphs_0_confirm_remove"]');
     $this->assertTrue(!empty($elements), "'Confirm removal' button appears.");
     // Restore the Paragraph and fix the broken reference.
     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_restore');
     $node = $this->drupalGetNodeByTitle('Example publish/unpublish');
     $this->drupalPostForm(NULL, ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node->label() . ' (' . $node->id() . ')'], t('Save and keep published'));
     $this->assertText('choke test has been updated.');
     $this->assertLink('Example publish/unpublish');
     // Delete the new referenced node.
     $node->delete();
     // Set the Paragraph field edit mode to 'Preview'.
     $this->drupalPostAjaxForm('admin/structure/types/manage/article/form-display', [], 'field_paragraphs_settings_edit');
     $this->drupalPostForm(NULL, ['fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview'], t('Update'));
     $this->drupalPostForm(NULL, [], t('Save'));
     $node = $this->drupalGetNodeByTitle('choke test');
     // Attempt to edit the Paragraph.
     $this->drupalPostAjaxForm('node/' . $node->id() . '/edit', [], 'field_paragraphs_0_edit');
     // Try to collapse with an invalid reference.
     $this->drupalPostAjaxForm(NULL, ['field_paragraphs[0][subform][field_entity_reference][0][target_id]' => 'foo'], 'field_paragraphs_0_collapse');
     $this->assertText('There are no entities matching "foo".');
     // Remove the Paragraph and save the node.
     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_remove');
     $elements = $this->xpath('//*[@name="field_paragraphs_0_confirm_remove"]');
     $this->assertTrue(!empty($elements), "'Confirm removal' button appears.");
     $this->drupalPostAjaxForm(NULL, [], 'field_paragraphs_0_confirm_remove');
     $this->drupalPostForm(NULL, [], t('Save and keep published'));
     $this->assertText('choke test has been updated.');
     // Verify that the text displayed is correct when no paragraph has been
     // added yet.
     $this->drupalGet('node/add/article');
     $this->assertText('No Paragraph added yet.');
 }
 /**
  * Tests the revision of paragraphs.
  */
 public function testParagraphsRevisions()
 {
     // Create the paragraph type.
     $paragraph_type = ParagraphsType::create(array('label' => 'test_text', 'id' => 'test_text'));
     $paragraph_type->save();
     $paragraph_type_nested = ParagraphsType::create(array('label' => 'test_nested', 'id' => 'test_nested'));
     $paragraph_type_nested->save();
     // Add a paragraph field to the article.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'nested_paragraph_field', 'entity_type' => 'paragraph', 'type' => 'entity_reference_revisions', 'cardinality' => '-1', 'settings' => array('target_type' => 'paragraph')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'test_nested'));
     $field->save();
     // Add a paragraph field to the article.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'node_paragraph_field', 'entity_type' => 'node', 'type' => 'entity_reference_revisions', 'cardinality' => '-1', 'settings' => array('target_type' => 'paragraph')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'article'));
     $field->save();
     // Add a paragraph field to the user.
     $field_storage = FieldStorageConfig::create(array('field_name' => 'user_paragraph_field', 'entity_type' => 'user', 'type' => 'entity_reference_revisions', 'settings' => array('target_type' => 'paragraph')));
     $field_storage->save();
     $field = FieldConfig::create(array('field_storage' => $field_storage, 'bundle' => 'user'));
     $field->save();
     // Create a paragraph.
     $paragraph1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph1->save();
     // Create another paragraph.
     $paragraph2 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph2->save();
     // Create another paragraph.
     $paragraph3 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph3->save();
     // Create another paragraph.
     $paragraph_nested_children1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph_nested_children1->save();
     // Create another paragraph.
     $paragraph_nested_children2 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph_nested_children2->save();
     // Create another paragraph.
     $paragraph4_nested_parent = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_nested', 'nested_paragraph_field' => [$paragraph_nested_children1, $paragraph_nested_children2]]);
     $paragraph4_nested_parent->save();
     // Create another paragraph.
     $paragraph_user_1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'test_text']);
     $paragraph_user_1->save();
     // Create a node with two paragraphs.
     $node = Node::create(['title' => $this->randomMachineName(), 'type' => 'article', 'node_paragraph_field' => array($paragraph1, $paragraph2, $paragraph3, $paragraph4_nested_parent)]);
     $node->save();
     // Create an user with a paragraph.
     $user = User::create(['name' => 'test', 'user_paragraph_field' => $paragraph_user_1]);
     $user->save();
     $settings = Settings::getAll();
     $settings['paragraph_limit'] = 1;
     new Settings($settings);
     // Unset the parent field name, type and id of paragraph1.
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
     $paragraph = Paragraph::load($paragraph1->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of paragraph2.
     $paragraph = Paragraph::load($paragraph2->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of $paragraph_nested_parent.
     $paragraph = Paragraph::load($paragraph4_nested_parent->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of $paragraph_nested_children1.
     $paragraph = Paragraph::load($paragraph_nested_children1->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Unset the parent field name, type and id of paragraph_user_1.
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph */
     $paragraph = Paragraph::load($paragraph_user_1->id());
     $paragraph->set('parent_field_name', NULL);
     $paragraph->set('parent_type', NULL);
     $paragraph->set('parent_id', NULL);
     $paragraph->setNewRevision(FALSE);
     $paragraph->save();
     // Create a revision for node.
     /** @var \Drupal\node\Entity\Node $node_revision1 */
     $node_revision1 = Node::load($node->id());
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph1_revision1 */
     $paragraph1_revision1 = Paragraph::load($paragraph1->id());
     $paragraph1_revision1->setNewRevision(TRUE);
     $paragraph1_revision1->save();
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph2_revision1 */
     $paragraph2_revision1 = Paragraph::load($paragraph2->id());
     $paragraph2_revision1->setNewRevision(TRUE);
     $paragraph2_revision1->save();
     $node_revision1->set('node_paragraph_field', [$paragraph1_revision1, $paragraph2_revision1]);
     $node_revision1->setNewRevision(TRUE);
     $node_revision1->save();
     // Unset the parent field name, type and id of paragraph2_revision1.
     $paragraph2_revision1 = Paragraph::load($paragraph2_revision1->id());
     $paragraph2_revision1->set('parent_field_name', NULL);
     $paragraph2_revision1->set('parent_type', NULL);
     $paragraph2_revision1->set('parent_id', NULL);
     $paragraph2_revision1->setNewRevision(FALSE);
     $paragraph2_revision1->save();
     // Create another revision for node.
     /** @var \Drupal\node\Entity\Node $node_revision2 */
     $node_revision2 = Node::load($node->id());
     /** @var \Drupal\paragraphs\Entity\Paragraph $paragraph1_revision2 */
     $paragraph1_revision2 = Paragraph::load($paragraph1->id());
     $paragraph1_revision2->setNewRevision(TRUE);
     $paragraph1_revision2->save();
     $node_revision2->set('node_paragraph_field', [$paragraph1_revision2]);
     $node_revision2->setNewRevision(TRUE);
     $node_revision2->save();
     // Deletion of referenced paragraphs should not break updates.
     $paragraph3->delete();
     \Drupal::moduleHandler()->loadInclude('paragraphs', 'post_update.php');
     // Run update function and check #finished.
     $sandbox = [];
     do {
         paragraphs_post_update_set_paragraphs_parent_fields($sandbox);
     } while ($sandbox['#finished'] < 1);
     $node_paragraph1 = Paragraph::load($paragraph1->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($node_paragraph1['parent_id'][0]['value'], $node->id());
     self::assertEquals($node_paragraph1['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($node_paragraph1['parent_field_name'][0]['value'], 'node_paragraph_field');
     $paragraph1_revision1 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision1->getRevisionId())->toArray();
     self::assertEquals($paragraph1_revision1['parent_id'][0]['value'], $node->id());
     self::assertEquals($paragraph1_revision1['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($paragraph1_revision1['parent_field_name'][0]['value'], 'node_paragraph_field');
     $paragraph1_revision2 = \Drupal::entityTypeManager()->getStorage('paragraph')->loadRevision($paragraph1_revision2->getRevisionId())->toArray();
     self::assertEquals($paragraph1_revision2['parent_id'][0]['value'], $node->id());
     self::assertEquals($paragraph1_revision2['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($paragraph1_revision2['parent_field_name'][0]['value'], 'node_paragraph_field');
     $node_paragraph2 = Paragraph::load($paragraph2->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($node_paragraph2['parent_id'][0]['value'], $node->id());
     self::assertEquals($node_paragraph2['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($node_paragraph2['parent_field_name'][0]['value'], 'node_paragraph_field');
     $user_paragraph = Paragraph::load($paragraph_user_1->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($user_paragraph['parent_id'][0]['value'], $user->id());
     self::assertEquals($user_paragraph['parent_type'][0]['value'], $user->getEntityTypeId());
     self::assertEquals($user_paragraph['parent_field_name'][0]['value'], 'user_paragraph_field');
     $nested_paragraph_parent = Paragraph::load($paragraph4_nested_parent->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($nested_paragraph_parent['parent_id'][0]['value'], $node->id());
     self::assertEquals($nested_paragraph_parent['parent_type'][0]['value'], $node->getEntityTypeId());
     self::assertEquals($nested_paragraph_parent['parent_field_name'][0]['value'], 'node_paragraph_field');
     $nested_paragraph_children = Paragraph::load($paragraph_nested_children1->id())->toArray();
     // Check if the fields are properly set.
     self::assertEquals($nested_paragraph_children['parent_id'][0]['value'], $paragraph4_nested_parent->id());
     self::assertEquals($nested_paragraph_children['parent_type'][0]['value'], $paragraph4_nested_parent->getEntityTypeId());
     self::assertEquals($nested_paragraph_children['parent_field_name'][0]['value'], 'nested_paragraph_field');
 }
Esempio n. 5
0
 /**
  * Create default content for entity
  * 
  * @param $content, $type
  *  array of content information
  *  entity type
  *
  * @return
  *  paragraph object id
  */
 public function createDefaultContentForEntity($content, $type)
 {
     $entity = NULL;
     $theme_array = $this->getCustomTheme();
     $theme_name = array_keys($theme_array)[0];
     $file_directory = 'default';
     switch ($type) {
         case 'component':
             $entity = Component::create(['type' => $content['id'], 'name' => ucwords(str_replace('_', ' ', $content['id']))]);
             $entity->save();
             $file_directory = 'components/' . str_replace('_', ' ', $content['id']);
             break;
         case 'paragraph':
             $entity = Paragraph::create(['type' => $content['id'], 'title' => ucwords(str_replace('_', ' ', $content['id']))]);
             $entity->save();
             $file_directory = 'paragraphs/' . str_replace('_', ' ', $content['id']);
             break;
     }
     foreach ($content['fields'] as $field) {
         if ($field['field_type'] == 'image' || $field['field_type'] == 'file') {
             $settings['file_directory'] = $file_directory . '/[date:custom:Y]-[date:custom:m]';
             $uri = drupal_get_path('theme', $theme_name) . '/' . $field['value'];
             if (file_exists($uri) && !is_dir($uri)) {
                 $image = File::create();
                 $image->setFileUri($uri);
                 $image->setOwnerId(\Drupal::currentUser()->id());
                 $image->setMimeType('image/' . pathinfo($field['value'], PATHINFO_EXTENSION));
                 $image->setFileName(drupal_basename($field['value']));
                 $destination_dir = 'public://' . $file_directory;
                 file_prepare_directory($destination_dir, FILE_CREATE_DIRECTORY);
                 $destination = $destination_dir . '/' . basename($field['value']);
                 $file = file_copy($image, $destination);
                 $values = array('target_id' => $file->id());
                 $entity->set($field['field_name'], $values);
             }
         } else {
             $entity->set($field['field_name'], $field['value']);
         }
     }
     $entity->save();
     \Drupal::logger('clutch:workflow')->notice('Create content for type @type - bundle @bundle', array('@type' => $type, '@bundle' => $content['id']));
     return $entity;
 }
 /**
  * Tests the paragraph translation.
  */
 public function testParagraphTranslation()
 {
     $this->drupalGet('admin/config/regional/content-language');
     // Check the settings are saved correctly.
     $this->assertFieldChecked('edit-entity-types-paragraph');
     $this->assertFieldChecked('edit-settings-node-paragraphed-content-demo-translatable');
     $this->assertFieldChecked('edit-settings-paragraph-text-image-translatable');
     $this->assertFieldChecked('edit-settings-paragraph-images-columns-field-images-demo-alt');
     $this->assertFieldChecked('edit-settings-paragraph-images-columns-field-images-demo-title');
     // Check if the publish/unpublish option works.
     $this->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
     $edit = array('fields[status][type]' => 'boolean_checkbox');
     $this->drupalPostForm(NULL, $edit, t('Save'));
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
     $this->assertRaw('edit-field-paragraphs-demo-0-subform-status-value');
     $edit = ['title[0][value]' => 'example_publish_unpublish', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Example published and unpublished'];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $this->assertText(t('Example published and unpublished'));
     $this->clickLink(t('Edit'));
     $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_nested_paragraph_add_more');
     $this->drupalPostAjaxForm(NULL, NULL, 'field_paragraphs_demo_1_subform_field_paragraphs_demo_text_add_more');
     $edit = ['field_paragraphs_demo[0][subform][status][value]' => FALSE, 'field_paragraphs_demo[1][subform][field_paragraphs_demo][0][subform][field_text_demo][0][value]' => 'Dummy text'];
     $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
     $this->assertNoText(t('Example published and unpublished'));
     // Check the parent fields are set properly. Get the node.
     $node = $this->drupalGetNodeByTitle('example_publish_unpublish');
     // Loop over the paragraphs of the node.
     foreach ($node->field_paragraphs_demo->referencedEntities() as $paragraph) {
         $node_paragraph = Paragraph::load($paragraph->id())->toArray();
         // Check if the fields are set properly.
         $this->assertEqual($node_paragraph['parent_id'][0]['value'], $node->id());
         $this->assertEqual($node_paragraph['parent_type'][0]['value'], 'node');
         $this->assertEqual($node_paragraph['parent_field_name'][0]['value'], 'field_paragraphs_demo');
         // If the paragraph is nested type load the child.
         if ($node_paragraph['type'][0]['target_id'] == 'nested_paragraph') {
             $nested_paragraph = Paragraph::load($node_paragraph['field_paragraphs_demo'][0]['target_id'])->toArray();
             // Check if the fields are properly set.
             $this->assertEqual($nested_paragraph['parent_id'][0]['value'], $paragraph->id());
             $this->assertEqual($nested_paragraph['parent_type'][0]['value'], 'paragraph');
             $this->assertEqual($nested_paragraph['parent_field_name'][0]['value'], 'field_paragraphs_demo');
         }
     }
     // Add paragraphed content.
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, NULL, t('Add Text + Image'));
     $edit = array('title[0][value]' => 'Title in english', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in english');
     // The button to remove a paragraph is present.
     $this->assertRaw(t('Remove'));
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $node = $this->drupalGetNodeByTitle('Title in english');
     // The text is present when editing again.
     $this->clickLink(t('Edit'));
     $this->assertText('Title in english');
     $this->assertText('Text in english');
     // Add french translation.
     $this->clickLink(t('Translate'));
     $this->clickLink(t('Add'), 1);
     // Make sure the Add / Remove paragraph buttons are hidden.
     $this->assertNoRaw(t('Remove'));
     $this->assertNoRaw(t('Add Text + Image'));
     // Make sure that the original paragraph text is displayed.
     $this->assertText('Text in english');
     $edit = array('title[0][value]' => 'Title in french', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'Text in french', 'revision' => TRUE, 'revision_log[0][value]' => 'french 1');
     $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
     $this->assertText('Paragraphed article Title in french has been updated.');
     // Check the english translation.
     $this->drupalGet('node/' . $node->id());
     $this->assertText('Title in english');
     $this->assertText('Text in english');
     $this->assertNoText('Title in french');
     $this->assertNoText('Text in french');
     // Check the french translation.
     $this->drupalGet('fr/node/' . $node->id());
     $this->assertText('Title in french');
     $this->assertText('Text in french');
     $this->assertNoText('Title in english');
     // The translation is still present when editing again.
     $this->clickLink(t('Edit'));
     $this->assertText('Title in french');
     $this->assertText('Text in french');
     $edit = array('title[0][value]' => 'Title Change in french', 'field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'New text in french', 'revision' => TRUE, 'revision_log[0][value]' => 'french 2');
     $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
     $this->assertText('Title Change in french');
     $this->assertText('New text in french');
     // Back to the source language.
     $this->drupalGet('node/' . $node->id());
     $this->clickLink(t('Edit'));
     $this->assertText('Title in english');
     $this->assertText('Text in english');
     // Save the original content on second request.
     $this->drupalPostForm(NULL, NULL, t('Save and keep published (this translation)'));
     $this->assertText('Paragraphed article Title in english has been updated.');
     // Test if reverting to old paragraphs revisions works, make sure that
     // the reverted node can be saved again.
     $this->drupalGet('fr/node/' . $node->id() . '/revisions');
     $this->clickLink(t('Revert'));
     $this->drupalPostForm(NULL, ['revert_untranslated_fields' => TRUE], t('Revert'));
     $this->clickLink(t('Edit'));
     $this->assertRaw('Title in french');
     $this->assertText('Text in french');
     $this->drupalPostForm(NULL, [], t('Save and keep published (this translation)'));
     $this->assertNoRaw('The content has either been modified by another user, or you have already submitted modifications');
     $this->assertText('Text in french');
     //Add paragraphed content with untranslatable language
     $this->drupalGet('node/add/paragraphed_content_demo');
     $edit = array('langcode[0][value]' => LanguageInterface::LANGCODE_NOT_SPECIFIED);
     $this->drupalPostForm(NULL, $edit, t('Add Text + Image'));
     $this->assertResponse(200);
     // Make 'Images' paragraph field translatable, enable alt and title fields.
     $this->drupalGet('admin/structure/paragraphs_type/images/fields');
     $this->clickLink('Edit');
     $edit = ['translatable' => 1, 'settings[alt_field]' => 1, 'settings[title_field]' => 1];
     $this->drupalPostForm(NULL, $edit, t('Save settings'));
     // Create a node with an image paragraph, its alt and title text.
     $text = 'Trust me I\'m an image';
     file_put_contents('temporary://Image.jpg', $text);
     $file_path = $this->container->get('file_system')->realpath('temporary://Image.jpg');
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, [], t('Add Images'));
     $this->drupalPostForm(NULL, ['files[field_paragraphs_demo_0_subform_field_images_demo_0][]' => $file_path], t('Upload'));
     $edit = ['title[0][value]' => 'Title EN', 'field_paragraphs_demo[0][subform][field_images_demo][0][alt]' => 'Image alt', 'field_paragraphs_demo[0][subform][field_images_demo][0][title]' => 'Image title', 'field_paragraphs_demo[0][subform][field_images_demo][0][width]' => 100, 'field_paragraphs_demo[0][subform][field_images_demo][0][height]' => 100];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     // Translate the node with the image paragraph.
     $this->clickLink('Translate');
     $this->clickLink(t('Add'), 1);
     $edit = ['title[0][value]' => 'Title FR', 'field_paragraphs_demo[0][subform][field_images_demo][0][alt]' => 'Image alt FR', 'field_paragraphs_demo[0][subform][field_images_demo][0][title]' => 'Image title FR'];
     $this->drupalPostForm(NULL, $edit, t('Save and keep published (this translation)'));
     $this->assertRaw('Title FR');
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, [], t('Add Text'));
     $edit = ['field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'texto', 'title[0][value]' => 'titulo', 'langcode[0][value]' => 'de'];
     $this->drupalPostForm(NULL, $edit, t('Save and publish'));
     $node = $this->drupalGetNodeByTitle('titulo');
     $this->assertParagraphsLangcode($node->id(), 'de');
     // Test langcode matching when Paragraphs and node have different language.
     $paragraph_1 = Paragraph::create(['title' => 'Paragraph', 'type' => 'text', 'langcode' => 'en', 'field_text_demo' => 'english_text_1']);
     $paragraph_1->save();
     $paragraph_2 = Paragraph::create(['title' => 'Paragraph', 'type' => 'text', 'langcode' => 'en', 'field_text_demo' => 'english_text_2']);
     $paragraph_2->save();
     $paragraph_data = $paragraph_2->toArray();
     $paragraph_data['field_text_demo'] = 'german_text_2';
     $paragraph_2->addTranslation('de', $paragraph_data);
     $paragraph_2->save();
     $translated_paragraph = $paragraph_2->getTranslation('en');
     $node = $this->createNode(['langcode' => 'de', 'type' => 'paragraphed_content_demo', 'field_paragraphs_demo' => [$paragraph_1, $translated_paragraph]]);
     $this->drupalGet('node/' . $node->id() . '/edit');
     $this->drupalPostForm(NULL, [], t('Save and keep published'));
     $this->assertText('Paragraphed article ' . $node->label() . ' has been updated.');
     // Check that first paragraph langcode has been updated.
     $paragraph = Paragraph::load($paragraph_1->id());
     $this->assertEqual($paragraph->language()->getId(), 'de');
     $this->assertFalse($paragraph->hasTranslation('en'));
     // Check that second paragraph has two translations.
     $paragraph = Paragraph::load($paragraph_2->id());
     $this->assertTrue($paragraph->hasTranslation('de'));
     $this->assertTrue($paragraph->hasTranslation('en'));
     $this->assertRaw('german_text');
     // Create an english translation of the node.
     $edit = ['field_paragraphs_demo[0][subform][field_text_demo][0][value]' => 'english_translation_1', 'field_paragraphs_demo[1][subform][field_text_demo][0][value]' => 'english_translation_2'];
     $this->drupalPostForm('node/' . $node->id() . '/translations/add/de/en', $edit, t('Save and keep published (this translation)'));
     // Attempt to create a french translation.
     $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
     // Check that the german translation of the paragraphs is displayed.
     $this->assertFieldByName('field_paragraphs_demo[0][subform][field_text_demo][0][value]', 'english_text_1');
     $this->assertFieldByName('field_paragraphs_demo[1][subform][field_text_demo][0][value]', 'german_text_2');
     $this->drupalPostForm(NULL, ['source_langcode[source]' => 'en'], t('Change'));
     // Check that the english translation of the paragraphs is displayed.
     $this->assertFieldByName('field_paragraphs_demo[0][subform][field_text_demo][0][value]', 'english_translation_1');
     $this->assertFieldByName('field_paragraphs_demo[1][subform][field_text_demo][0][value]', 'english_translation_2');
     // Create a node with empty Paragraphs.
     $this->drupalGet('node/add/paragraphed_content_demo');
     $this->drupalPostForm(NULL, [], t('Add Nested Paragraph'));
     $this->drupalPostForm(NULL, ['title[0][value]' => 'empty_node'], t('Save and publish'));
     // Attempt to translate it.
     $this->clickLink(t('Translate'));
     $this->clickLink(t('Add'));
     // Check the add button is not displayed.
     $this->assertEqual(count($this->xpath('//*[@name="field_paragraphs_demo_0_subform_field_paragraphs_demo_images_add_more"]')), 0);
     // Add a non translatable field to Text Paragraph type.
     $edit = ['new_storage_type' => 'text_long', 'label' => 'untranslatable_field', 'field_name' => 'untranslatable_field'];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
     $this->drupalPostForm(NULL, [], t('Save field settings'));
     $this->drupalPostForm(NULL, [], t('Save settings'));
     // Add a non translatable reference field.
     $edit = ['new_storage_type' => 'field_ui:entity_reference:node', 'label' => 'untranslatable_ref_field', 'field_name' => 'untranslatable_ref_field'];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
     $this->drupalPostForm(NULL, [], t('Save field settings'));
     $this->drupalPostForm(NULL, ['settings[handler_settings][target_bundles][paragraphed_content_demo]' => TRUE], t('Save settings'));
     // Add a non translatable link field.
     $edit = ['new_storage_type' => 'link', 'label' => 'untranslatable_link_field', 'field_name' => 'untranslatable_link_field'];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/add-field', $edit, t('Save and continue'));
     $this->drupalPostForm(NULL, [], t('Save field settings'));
     $this->drupalPostForm(NULL, [], t('Save settings'));
     // Attempt to add a translation.
     $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
     $this->assertText('untranslatable_field (all languages)');
     $this->assertText('untranslatable_ref_field (all languages)');
     $this->assertText('untranslatable_link_field (all languages)');
     $this->assertNoText('Text (all languages)');
     // Enable translations for the reference and link field.
     $edit = ['translatable' => TRUE];
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_ref_field', $edit, t('Save settings'));
     $this->drupalPostForm('admin/structure/paragraphs_type/text/fields/paragraph.text.field_untranslatable_link_field', $edit, t('Save settings'));
     // Attempt to add a translation.
     $this->drupalGet('node/' . $node->id() . '/translations/add/de/fr');
     $this->assertText('untranslatable_field (all languages)');
     $this->assertNoText('untranslatable_link_field (all languages)');
     $this->assertNoText('untranslatable_ref_field (all languages)');
     $this->assertNoText('Text (all languages)');
 }
Esempio n. 7
-1
 /**
  * Create associated components for page
  *
  * @param $component
  *   component string with component type and region
  *
  * @return
  *   array of target_id, target_revision_id of component to associate to page
  */
 public function createAssociatedComponent($component)
 {
     $component_info_array = explode('|', $component);
     $component_type = $component_info_array[0];
     $component_position = $component_info_array[1];
     $component_id = \Drupal::entityQuery('component')->condition('type', $component_type)->execute();
     $paragraph = Paragraph::create(['type' => 'associated_component', 'component' => ['target_id' => key($component_id)], 'region' => ['value' => !empty($component_position) ? $component_position : 'content', 'format' => 'string']]);
     $paragraph->save();
     return ['target_id' => $paragraph->id(), 'target_revision_id' => $paragraph->getRevisionId()];
 }