<?php
namespace Drupal\Tests\paragraphs\Functional\WidgetLegacy;
use Drupal\paragraphs\Entity\Paragraph;
class ParagraphsAdministrationTest extends ParagraphsTestBase {
protected static $modules = array(
'image',
'file',
'views',
);
protected function setUp() : void {
parent::setUp();
$this
->drupalCreateContentType(array(
'type' => 'paragraphs',
'name' => 'Paragraphs',
));
}
public function testParagraphsRevisions() {
$this
->addParagraphedContentType('article', 'paragraphs', 'entity_reference_paragraphs');
$this
->loginAsAdmin([
'create paragraphs content',
'administer node display',
'edit any paragraphs content',
'administer nodes',
]);
$this
->addParagraphsType('text');
static::fieldUIAddNewField('admin/structure/paragraphs_type/text', 'text', 'Text', 'text', array(), array());
$this
->assertSession()
->pageTextContains('Saved Text configuration.');
static::fieldUIAddNewField('admin/structure/types/manage/paragraphs', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array(
'settings[target_type]' => 'paragraph',
'cardinality' => '-1',
), array(
'settings[handler_settings][target_bundles_drag_drop][text][enabled]' => TRUE,
));
$this
->drupalGet('admin/structure/types/manage/paragraphs/fields');
$this
->clickLink('Manage form display');
$this
->submitForm(array(
'fields[field_paragraphs][type]' => 'entity_reference_paragraphs',
), 'Save');
$this
->drupalGet('node/add/paragraphs');
$this
->submitForm(array(), 'field_paragraphs_text_add_more');
$this
->submitForm(array(), 'field_paragraphs_text_add_more');
$edit = [
'title[0][value]' => 'TEST TITEL',
'field_paragraphs[0][subform][field_text][0][value]' => 'Test text 1',
'field_paragraphs[1][subform][field_text][0][value]' => 'Test text 2',
'status[value]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle('TEST TITEL');
$paragraph1 = $node->field_paragraphs[0]->target_id;
$paragraph2 = $node->field_paragraphs[1]->target_id;
$this
->countRevisions($node, $paragraph1, $paragraph2, 1);
$edit = [
'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 1',
'revision' => FALSE,
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->countRevisions($node, $paragraph1, $paragraph2, 1);
$edit = [
'title[0][value]' => 'TEST TITLE',
'field_paragraphs[0][subform][field_text][0][value]' => 'Foo Bar 2',
'revision' => TRUE,
];
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm($edit, 'Save');
$this
->countRevisions($node, $paragraph1, $paragraph2, 2);
$this
->assertSession()
->pageTextNotContains('Foo Bar 1');
$this
->assertSession()
->pageTextContains('Test text 2');
$this
->assertSession()
->pageTextContains('Foo Bar 2');
$this
->assertSession()
->pageTextContains('TEST TITLE');
$this
->drupalGet('node/' . $node
->id() . '/revisions');
$rows = $this
->xpath('//tbody/tr');
$this
->assertEquals(count($rows), 2);
$this
->clickLink('Revert');
$this
->submitForm([], 'Revert');
$this
->drupalGet('node/' . $node
->id());
$this
->assertSession()
->pageTextNotContains('Foo Bar 2');
$this
->assertSession()
->pageTextContains('Test text 2');
$this
->assertSession()
->pageTextContains('Foo Bar 1');
$this
->assertSession()
->pageTextContains('TEST TITEL');
}
public function testParagraphsCreation() {
$this
->addParagraphedContentType('article', 'field_paragraphs', 'entity_reference_paragraphs');
$this
->loginAsAdmin([
'administer site configuration',
'create article content',
'create paragraphs content',
'administer node display',
'administer paragraph display',
'edit any article content',
'delete any article content',
'access files overview',
]);
$this
->drupalGet('admin/structure/paragraphs_type');
$this
->assertSession()
->pageTextContains('There are no Paragraphs types yet.');
$this
->drupalGet('admin/structure/types/manage/paragraphs/fields/add-field');
$this
->getSession()
->getPage()
->fillField('new_storage_type', 'field_ui:entity_reference_revisions:paragraph');
if ($this
->coreVersion('10.3')) {
$this
->getSession()
->getPage()
->pressButton('Continue');
}
$edit = [
'label' => 'Paragraph',
'field_name' => 'paragraph',
];
$this
->submitForm($edit, 'Continue');
$this
->assertSession()
->linkByHrefExists('admin/structure/paragraphs_type/add');
$this
->clickLink('here');
$this
->assertSession()
->addressEquals('admin/structure/paragraphs_type/add');
$this
->drupalGet('admin/structure/paragraphs_type');
$this
->clickLink('Add paragraph type');
$this
->assertSession()
->titleEquals('Add Paragraphs type | Drupal');
$this
->addParagraphsType('text_image');
$this
->drupalGet('admin/structure/paragraphs_type/text_image');
$this
->assertSession()
->titleEquals('Edit text_image paragraph type | Drupal');
static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'text', 'Text', 'text_long', array(), array());
$this
->assertSession()
->pageTextContains('Saved Text configuration.');
static::fieldUIAddNewField('admin/structure/paragraphs_type/text_image', 'image', 'Image', 'image', array(), array(
'settings[alt_field_required]' => FALSE,
));
$this
->assertSession()
->pageTextContains('Saved Image configuration.');
$this
->addParagraphsType('nested_test');
static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_test', 'paragraphs', 'Paragraphs', 'entity_reference_revisions', array(
'settings[target_type]' => 'paragraph',
'cardinality' => '-1',
), array());
$this
->clickLink('Manage form display');
$this
->submitForm([
'fields[field_paragraphs][type]' => 'entity_reference_paragraphs',
], 'field_paragraphs_settings_edit');
$this
->submitForm([
'fields[field_paragraphs][settings_edit_form][settings][add_mode]' => 'select',
], 'Update');
$this
->submitForm([], 'Save');
$this
->addParagraphsType('image');
static::fieldUIAddNewField('admin/structure/paragraphs_type/image', 'image_only', 'Image only', 'image', array(), array());
$this
->assertSession()
->pageTextContains('Saved Image only configuration.');
$this
->drupalGet('admin/structure/paragraphs_type');
$rows = $this
->xpath('//tbody/tr');
$this
->assertEquals(count($rows), 3);
$this
->assertSession()
->pageTextContains('text_image');
$this
->assertSession()
->pageTextContains('image');
$this
->clickLink('Edit');
$this
->assertSession()
->linkExists('Manage fields');
$this
->assertSession()
->linkExists('Manage form display');
$this
->assertSession()
->linkExists('Manage display');
$this
->assertSession()
->titleEquals('Edit image paragraph type | Drupal');
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$field_name = 'field_paragraphs';
$this
->submitForm(array(), $field_name . "_settings_edit");
$edit = array(
'fields[' . $field_name . '][settings_edit_form][settings][add_mode]' => 'button',
);
$this
->submitForm($edit, 'Save');
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$this
->assertSession()
->pageTextContains('Add mode: Buttons');
$this
->submitForm(array(), $field_name . "_settings_edit");
$add_mode_option = $this
->assertSession()
->optionExists('edit-fields-field-paragraphs-settings-edit-form-settings-add-mode', 'button');
$this
->assertTrue($add_mode_option
->hasAttribute('selected'), 'Updated value correctly.');
$this
->drupalGet('node/add/article');
$this
->assertSession()
->responseContains('<div class="paragraphs-dropbutton-wrapper"><input');
$this
->submitForm(array(), 'field_paragraphs_text_image_add_more');
$this
->submitForm(array(), 'field_paragraphs_text_image_add_more');
$files = $this
->getTestFiles('image');
$file_system = \Drupal::service('file_system');
$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]' => $file_system
->realpath($files[0]->uri),
'field_paragraphs[1][subform][field_text][0][value]' => 'Test text 2',
'files[field_paragraphs_1_subform_field_image_0]' => $file_system
->realpath($files[1]->uri),
);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('article Test article has been created.');
$node = $this
->drupalGetNodeByTitle('Test article');
$img1_url = \Drupal::service('file_url_generator')
->generateString(\Drupal::token()
->replace('public://[date:custom:Y]-[date:custom:m]/' . $files[0]->filename));
$img2_url = \Drupal::service('file_url_generator')
->generateString(\Drupal::token()
->replace('public://[date:custom:Y]-[date:custom:m]/' . $files[1]->filename));
$img1_mime = \Drupal::service('file.mime_type.guesser')
->guessMimeType($files[0]->uri);
$img2_mime = \Drupal::service('file.mime_type.guesser')
->guessMimeType($files[1]->uri);
$this
->assertSession()
->pageTextContains('Test text 1');
$this
->assertSession()
->elementExists('css', 'img[src="' . $img1_url . '"]');
$this
->assertSession()
->pageTextContains('Test text 2');
$this
->assertSession()
->elementExists('css', 'img[src="' . $img2_url . '"]');
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$this
->submitForm(array(), "field_paragraphs_settings_edit");
$edit = array(
'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed',
);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Edit mode: Closed');
$this
->submitForm(array(), "field_paragraphs_settings_edit");
$edit_mode_option = $this
->assertSession()
->optionExists('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'closed');
$this
->assertTrue($edit_mode_option
->hasAttribute('selected'), 'Updated value correctly.');
$this
->drupalGet('node/1/edit');
$this
->assertSession()
->responseNotContains('field_paragraphs[0][subform][field_text][0][value]');
$this
->assertSession()
->responseNotContains('field_paragraphs[1][subform][field_text][0][value]');
$this
->assertSession()
->responseContains('<span class="summary-content">Test text 1</span>, <span class="summary-content">' . $files[0]->filename);
$this
->assertSession()
->responseContains('<span class="summary-content">Test text 2</span>, <span class="summary-content">' . $files[1]->filename);
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$this
->submitForm(array(), "field_paragraphs_settings_edit");
$edit = array(
'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview',
);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Edit mode: Preview');
$this
->drupalGet('node/1/edit');
$this
->assertSession()
->responseNotContains('field_paragraphs[0][subform][field_text][0][value]');
$this
->assertSession()
->responseNotContains('field_paragraphs[1][subform][field_text][0][value]');
$this
->assertSession()
->pageTextContains('Test text 1');
$this
->assertSession()
->pageTextContains('Test text 2');
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$this
->submitForm(array(), "field_paragraphs_settings_edit");
$edit_mode_option = $this
->assertSession()
->optionExists('edit-fields-field-paragraphs-settings-edit-form-settings-edit-mode', 'preview');
$this
->assertTrue($edit_mode_option
->hasAttribute('selected'), 'Updated value correctly.');
$edit = array(
'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'open',
);
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/1/edit');
$this
->assertSession()
->responseContains('field_paragraphs[0][subform][field_text][0][value]');
$this
->assertSession()
->responseContains('field_paragraphs[1][subform][field_text][0][value]');
$paragraphs = Paragraph::loadMultiple();
$this
->assertEquals(count($paragraphs), 2, 'Two paragraphs in article');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldValueEquals('field_paragraphs[0][subform][field_text][0][value]', 'Test text 1');
$this
->assertSession()
->elementTextContains('css', 'A[href="' . $img1_url . '"][type^="' . $img1_mime . '"]', $files[0]->filename);
$this
->assertSession()
->fieldValueEquals('field_paragraphs[1][subform][field_text][0][value]', 'Test text 2');
$this
->assertSession()
->elementTextContains('css', 'A[href="' . $img2_url . '"][type^="' . $img2_mime . '"]', $files[1]->filename);
$this
->getSession()
->getPage()
->find('css', '[name="field_paragraphs_1_remove"]')
->press();
$this
->submitForm([], 'Confirm removal');
$this
->assertSession()
->fieldNotExists('field_paragraphs[1][subform][field_text][0][value]');
$this
->assertSession()
->elementNotExists('css', 'A[href="' . $img2_url . '"][type^="' . $img2_mime . '"]');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->elementTextContains('css', 'A[href="' . $img2_url . '"][type^="' . $img2_mime . '"]', $files[1]->filename);
$this
->getSession()
->getPage()
->find('css', '[name="field_paragraphs_1_remove"]')
->press();
$this
->submitForm([], 'Confirm removal');
$this
->assertSession()
->elementNotExists('css', 'A[href="' . $img2_url . '"][type^="' . $img2_mime . '"]');
$edit = [
'field_paragraphs[0][subform][field_image][0][alt]' => 'test_alt',
];
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->elementNotExists('css', 'A[href="' . $img2_url . '"][type^="' . $img2_mime . '"]');
$this
->clickLink('Delete');
$this
->submitForm([], 'Delete');
$this
->assertSession()
->pageTextContains('Test article has been deleted.');
$this
->drupalGet('admin/structure/paragraphs_type/text_image/form-display');
$edit = [
'fields[status][type]' => 'boolean_checkbox',
'fields[status][region]' => 'content',
];
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/add/article');
$this
->submitForm([], 'Add text_image');
$this
->assertSession()
->responseContains('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
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Example published and unpublished');
$this
->clickLink('Edit');
$edit = [
'field_paragraphs[0][subform][status][value]' => FALSE,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextNotContains('Example published and unpublished');
$this
->drupalGet('admin/structure/types/manage/article/fields');
$this
->clickLink('Edit', 1);
$this
->submitForm([
'preview_mode' => '1',
], 'Save');
$this
->drupalGet('admin/structure/paragraphs_type/nested_test/fields');
$this
->clickLink('Edit');
$this
->submitForm([
'required' => TRUE,
], 'Save settings');
$this
->drupalGet('node/add/article');
$this
->submitForm([], 'field_paragraphs_nested_test_add_more');
$edit = [
'field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'image',
];
$this
->submitForm($edit, 'field_paragraphs_0_subform_field_paragraphs_add_more');
$this
->assertSession()
->fieldExists('files[field_paragraphs_0_subform_field_paragraphs_0_subform_field_image_only_0]');
$edit = array(
'title[0][value]' => 'test required',
'files[field_paragraphs_0_subform_field_paragraphs_0_subform_field_image_only_0]' => $file_system
->realpath($files[2]->uri),
);
$this
->submitForm($edit, 'Save');
$edit = [
'field_paragraphs[0][subform][field_paragraphs][0][subform][field_image_only][0][alt]' => 'Alternative_text',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('test required has been created.');
$this
->assertSession()
->responseNotContains('This value should not be null.');
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$select = $this
->xpath('//*[@id="edit-fields-field-paragraphs-type"]')[0];
$this
->assertCount(2, $select
->findAll('css', 'option'));
$this
->assertSession()
->responseContains('value="entity_reference_paragraphs" selected="selected"');
$this
->addParagraphsType('nested_double_test');
static::fieldUIAddExistingField('admin/structure/paragraphs_type/nested_double_test', 'field_paragraphs', 'paragraphs_1');
$this
->clickLink('Manage form display');
$this
->submitForm([
'fields[field_paragraphs][type]' => 'paragraphs',
], 'field_paragraphs_settings_edit');
$this
->submitForm([
'fields[field_paragraphs][settings_edit_form][settings][add_mode]' => 'dropdown',
], 'Update');
$this
->submitForm([], '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('Manage form display');
$this
->submitForm([], 'Save');
$this
->drupalGet('node/add/article');
$this
->submitForm([], 'field_paragraphs_nested_test_add_more');
$edit = [
'field_paragraphs[0][subform][field_paragraphs][add_more][add_more_select]' => 'nested_double_test',
];
$this
->submitForm($edit, 'field_paragraphs_0_subform_field_paragraphs_add_more');
$this
->submitForm([], 'field_paragraphs_0_subform_field_paragraphs_0_subform_field_paragraphs_image_add_more');
$edit = array(
'title[0][value]' => 'Nested twins',
);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Nested twins has been created.');
$this
->assertSession()
->pageTextNotContains('This entity (paragraph: ) cannot be referenced.');
$this
->drupalGet('admin/structure/types/manage/article/fields/node.article.field_paragraphs');
$this
->submitForm([
'required' => FALSE,
], 'Save');
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$this
->submitForm([], 'field_paragraphs_settings_edit');
$this
->submitForm([
'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed',
], 'Update');
$this
->submitForm([], 'Save');
$this
->addParagraphsType('node_test');
static::fieldUIAddNewField('admin/structure/paragraphs_type/node_test', 'entity_reference', 'Entity reference', 'entity_reference', array(
'settings[target_type]' => 'node',
'cardinality' => '-1',
), [
'settings[handler_settings][target_bundles][article]' => TRUE,
'required' => TRUE,
]);
$node = $this
->drupalGetNodeByTitle('Nested twins');
$this
->drupalGet('node/add/article');
$this
->submitForm([], 'field_paragraphs_node_test_add_more');
\Drupal::service('entity_field.manager')
->clearCachedFieldDefinitions();
$edit = [
'field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node
->label() . ' (' . $node
->id() . ')',
'title[0][value]' => 'choke test',
];
$this
->submitForm($edit, 'Save');
$node
->delete();
$this
->clickLink('Edit');
$this
->assertSession()
->fieldExists('field_paragraphs[0][subform][field_entity_reference][0][target_id]');
$this
->assertSession()
->fieldExists('field_paragraphs[0][subform][field_entity_reference][1][target_id]');
$this
->assertSession()
->pageTextContains('The referenced entity (node: 4) does not exist');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('The referenced entity (node: 4) does not exist');
$this
->assertSession()
->pageTextContains('Entity reference (value 1) field is required.');
$this
->submitForm([
'field_paragraphs[0][subform][field_entity_reference][0][target_id]' => 'foo',
], 'field_paragraphs_0_collapse');
$this
->assertSession()
->fieldExists('field_paragraphs[0][subform][field_entity_reference][0][target_id]');
$this
->assertSession()
->fieldExists('field_paragraphs[0][subform][field_entity_reference][1][target_id]');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextMatches('/There are no (entities|content items) matching "foo"./');
$this
->submitForm([], 'field_paragraphs_0_remove');
$elements = $this
->xpath('//*[@name="field_paragraphs_0_confirm_remove"]');
$this
->assertNotEmpty($elements, "'Confirm removal' button appears.");
$this
->submitForm([], 'field_paragraphs_0_restore');
$node = $this
->drupalGetNodeByTitle('Example publish/unpublish');
$edit = [
'field_paragraphs[0][subform][field_entity_reference][0][target_id]' => $node
->label() . ' (' . $node
->id() . ')',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('choke test has been updated.');
$this
->assertSession()
->linkExists('Example publish/unpublish');
$node
->delete();
$this
->drupalGet('admin/structure/types/manage/article/form-display');
$this
->submitForm([], 'field_paragraphs_settings_edit');
$this
->submitForm([
'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'preview',
], 'Update');
$this
->submitForm([], 'Save');
$node = $this
->drupalGetNodeByTitle('choke test');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->SubmitForm([], 'field_paragraphs_0_edit');
$edit = [
'field_paragraphs[0][subform][field_entity_reference][0][target_id]' => 'foo',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextMatches('/There are no (entities|content items) matching "foo"./');
$this
->submitForm([], 'field_paragraphs_0_remove');
$elements = $this
->xpath('//*[@name="field_paragraphs_0_confirm_remove"]');
$this
->assertNotEmpty($elements, "'Confirm removal' button appears.");
$this
->submitForm([], 'field_paragraphs_0_confirm_remove');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('choke test has been updated.');
$this
->drupalGet('node/add/article');
$this
->assertSession()
->pageTextContains('No Paragraph added yet.');
$this
->drupalGet('admin/content/files');
$this
->clickLink('1 place');
$label = $this
->xpath('//tbody/tr/td[1]');
$this
->assertEquals(trim(htmlspecialchars_decode(strip_tags($label[0]
->getText()))), 'test required > field_paragraphs > Paragraphs');
}
private function countRevisions($node, $paragraph1, $paragraph2, $revisions_count) {
$node_revisions_count = \Drupal::entityQuery('node')
->condition('nid', $node
->id())
->accessCheck(TRUE)
->allRevisions()
->count()
->execute();
$this
->assertEquals($revisions_count, $node_revisions_count);
$paragraph1_revisions_count = \Drupal::entityQuery('paragraph')
->condition('id', $paragraph1)
->accessCheck(TRUE)
->allRevisions()
->count()
->execute();
$this
->assertEquals($revisions_count, $paragraph1_revisions_count);
$paragraph2_revisions_count = \Drupal::entityQuery('paragraph')
->condition('id', $paragraph2)
->accessCheck(TRUE)
->allRevisions()
->count()
->execute();
$this
->assertEquals($revisions_count, $paragraph2_revisions_count);
}
}