View source
<?php
namespace Drupal\Tests\paragraphs\Functional\WidgetStable;
class ParagraphsBehaviorsTest extends ParagraphsTestBase {
public static $modules = [
'image',
'file',
'views',
];
public function testBehaviorPluginsFields() {
$this
->addParagraphedContentType('paragraphed_test');
$this
->loginAsAdmin([
'create paragraphed_test content',
'edit any paragraphed_test content',
]);
$paragraph_type = 'text_paragraph';
$this
->addParagraphsType($paragraph_type);
static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->assertSession()
->fieldValueEquals('behavior_plugins[test_text_color][settings][default_color]', 'blue');
$this
->assertSession()
->pageTextContains('Behavior plugins are only supported by the stable paragraphs widget');
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
'behavior_plugins[test_text_color][enabled]' => TRUE,
'behavior_plugins[test_text_color][settings][default_color]' => 'red',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Red can not be used as the default color.');
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
'behavior_plugins[test_text_color][enabled]' => FALSE,
'behavior_plugins[test_text_color][settings][default_color]' => 'red',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Saved the text_paragraph Paragraphs type.');
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->assertSession()
->checkboxChecked('edit-behavior-plugins-test-bold-text-enabled');
$this
->assertSession()
->checkboxNotChecked('edit-behavior-plugins-test-text-color-enabled');
$this
->assertSession()
->fieldValueEquals('behavior_plugins[test_text_color][settings][default_color]', 'blue');
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
'behavior_plugins[test_text_color][enabled]' => TRUE,
'behavior_plugins[test_text_color][settings][default_color]' => 'green',
];
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Saved the text_paragraph Paragraphs type.');
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->fieldNotExists('field_paragraphs[0][behavior_plugins][test_text_color][text_color]');
$this
->loginAsAdmin([
'create paragraphed_test content',
'edit any paragraphed_test content',
'edit behavior plugin settings',
]);
$this
->drupalGet('node/add/paragraphed_test');
$this
->assertSession()
->fieldValueEquals('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', 'green');
$plugin_text = 'green';
$edit = [
'title[0][value]' => 'paragraphs_plugins_test',
'field_paragraphs[0][subform][field_text][0][value]' => 'amazing_plugin_test',
'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
];
$behavior_xpath = $this
->xpath("//div[@id = 'edit-field-paragraphs-0-top']/following-sibling::*[1][@id = 'edit-field-paragraphs-0-behavior-plugins-test-bold-text']");
$this
->assertNotEquals($behavior_xpath, FALSE, 'Behavior form position incorrect');
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('The only allowed values are blue and red.');
$plugin_text = 'red';
$edit = [
'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $plugin_text,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->responseContains('class="red_plugin_text');
$this
->clickLink('Edit');
$this
->assertSession()
->fieldValueEquals('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', $plugin_text);
$updated_text = 'blue';
$edit = [
'field_paragraphs[0][behavior_plugins][test_text_color][text_color]' => $updated_text,
'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->responseNotContains('class="red_plugin_text');
$this
->assertSession()
->responseContains('class="bold_plugin_text blue_plugin_text');
$this
->clickLink('Edit');
$this
->assertSession()
->fieldValueEquals('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', $updated_text);
$this
->assertSession()
->fieldValueEquals('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]', TRUE);
$this
->loginAsAdmin([
'edit any paragraphed_test content',
]);
$node = $this
->getNodeByTitle('paragraphs_plugins_test');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->assertSession()
->fieldNotExists('field_paragraphs[0][behavior_plugins][test_text_color][text_color]');
$this
->assertSession()
->fieldNotExists('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]');
$this
->submitForm([], 'Save');
$this
->assertSession()
->responseContains('class="bold_plugin_text blue_plugin_text');
$this
->assertSession()
->responseNotContains('class="red_plugin_text');
$paragraph_type = 'text_paragraph_test';
$this
->addParagraphsType($paragraph_type);
static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text_test', 'Text', 'text_long', [], []);
static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'image', 'Image', 'image', [], []);
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->assertSession()
->fieldNotExists('behavior_plugins[test_bold_text][enabled]');
$this
->assertSession()
->fieldExists('behavior_plugins[test_text_color][enabled]');
$this
->assertSession()
->fieldExists('behavior_plugins[test_field_selection][enabled]');
$this
->assertSession()
->pageTextContains('Choose paragraph field to be applied.');
$this
->assertSession()
->optionExists('edit-behavior-plugins-test-field-selection-settings-field-selection-filter', 'Image');
$this
->assertSession()
->optionExists('edit-behavior-plugins-test-field-selection-settings-field-selection', 'Image');
$this
->assertSession()
->optionExists('edit-behavior-plugins-test-field-selection-settings-field-selection', 'Text');
$edit = [
'behavior_plugins[test_dummy_behavior][enabled]' => TRUE,
'behavior_plugins[test_text_color][enabled]' => TRUE,
];
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/add/paragraphed_test');
$this
->submitForm([], 'field_paragraphs_text_paragraph_test_add_more');
$edit = [
'title[0][value]' => 'paragraph with no fields',
'field_paragraphs[0][subform][field_text_test][0][value]' => 'my behavior plugin does not have any field',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->responseContains('dummy_plugin_text');
$this
->addParagraphsType('fieldless');
$this
->drupalGet('admin/structure/paragraphs_type/fieldless');
$this
->submitForm([
'behavior_plugins[test_dummy_behavior][enabled]' => TRUE,
], 'Save');
$this
->drupalGet('node/add/paragraphed_test');
$this
->submitForm([], 'field_paragraphs_fieldless_add_more');
$edit = [
'title[0][value]' => 'Fieldless',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->statusCodeEquals(200);
$this
->drupalGet('node/add/paragraphed_test');
$this
->submitForm([], 'field_paragraphs_text_paragraph_test_add_more');
$edit = [
'title[0][value]' => 'field_override_test',
'field_paragraphs[0][subform][field_text_test][0][value]' => 'This is a test',
];
$this
->submitForm($edit, 'Save');
$node = $this
->getNodeByTitle('field_override_test');
$this
->drupalGet('node/' . $node
->id() . '/edit');
$this
->submitForm([], 'field_paragraphs_0_collapse');
$this
->assertSession()
->responseContains('class="paragraphs-description paragraphs-collapsed-description"><div class="paragraphs-content-wrapper"><span class="summary-content">This is a test');
}
public function testCollapsedSummary() {
$this
->addParagraphedContentType('paragraphed_test');
$this
->loginAsAdmin([
'create paragraphed_test content',
'edit any paragraphed_test content',
'edit behavior plugin settings',
]);
$paragraph_type = 'text_paragraph';
$this
->addParagraphsType($paragraph_type);
static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
$this
->setParagraphsWidgetMode('paragraphed_test', 'field_paragraphs', 'closed');
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
'behavior_plugins[test_text_color][enabled]' => TRUE,
];
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->submitForm($edit, 'Save');
$paragraph_type = 'nested_paragraph';
$this
->addParagraphsType($paragraph_type);
$this
->addParagraphsField('nested_paragraph', 'paragraphs', 'paragraph');
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
];
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/add/paragraphed_test');
$this
->submitForm([], 'field_paragraphs_nested_paragraph_add_more');
$this
->submitForm([], 'field_paragraphs_1_subform_paragraphs_text_paragraph_add_more');
$this
->assertSession()
->fieldExists('field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]');
$this
->assertSession()
->fieldExists('field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]');
$edit = [
'title[0][value]' => 'collapsed_test',
'field_paragraphs[0][subform][field_text][0][value]' => 'first_paragraph',
'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
'field_paragraphs[1][subform][paragraphs][0][subform][field_text][0][value]' => 'nested_paragraph',
'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->clickLink('Edit');
$this
->assertSession()
->elementExists('xpath', '//*[@id="edit-field-paragraphs-0-top-icons"]/span[@class="paragraphs-icon paragraphs-icon-bold"]');
$this
->assertSession()
->elementExists('xpath', '//*[@id="edit-field-paragraphs-1-top-icons"]/span[@class="paragraphs-badge" and @title="1 child"]');
$this
->assertSession()
->elementExists('xpath', '//*[@id="edit-field-paragraphs-1-top-icons"]/span[@class="paragraphs-icon paragraphs-icon-bold"]');
$this
->assertSession()
->responseContains('first_paragraph</span></div><div class="paragraphs-plugin-wrapper"><span class="summary-plugin"><span class="summary-plugin-label">Bold</span>Yes</span><span class="summary-plugin"><span class="summary-plugin-label">Text color</span>blue</span>');
$this
->assertSession()
->responseContains('nested_paragraph</span></div><div class="paragraphs-plugin-wrapper"><span class="summary-plugin"><span class="summary-plugin-label">Bold</span>Yes</span></div></div>');
$this
->drupalGet('node/add/paragraphed_test');
$this
->submitForm([], 'field_paragraphs_nested_paragraph_add_more');
$edit = [
'title[0][value]' => 'collapsed_test',
];
$this
->submitForm($edit, 'Save');
$this
->clickLink('Edit');
$this
->assertSession()
->responseContains('class="paragraphs-description paragraphs-collapsed-description">');
}
public function testBehaviorSubform() {
$this
->addParagraphedContentType('paragraphed_test');
$this
->loginAsAdmin([
'create paragraphed_test content',
'edit any paragraphed_test content',
'edit behavior plugin settings',
]);
$paragraph_type = 'text_paragraph';
$this
->addParagraphsType($paragraph_type);
static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
'behavior_plugins[test_text_color][enabled]' => TRUE,
];
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->submitForm($edit, 'Save');
$paragraph_type = 'nested_paragraph';
$this
->addParagraphsType($paragraph_type);
static::fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'nested', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
'settings[target_type]' => 'paragraph',
'cardinality' => '-1',
], []);
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
];
$this
->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
$this
->submitForm($edit, 'Save');
$this
->drupalGet('node/add/paragraphed_test');
$this
->submitForm([], 'field_paragraphs_nested_paragraph_add_more');
$this
->submitForm([], 'field_paragraphs_text_paragraph_add_more');
$this
->submitForm([], 'field_paragraphs_0_subform_field_nested_text_paragraph_add_more');
$edit = [
'title[0][value]' => 'collapsed_test',
'field_paragraphs[0][subform][field_nested][0][subform][field_text][0][value]' => 'nested text paragraph',
'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
'field_paragraphs[1][subform][field_text][0][value]' => 'first_paragraph',
'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->clickLink('Edit');
$edit = [
'field_paragraphs[0][_weight]' => 1,
'field_paragraphs[1][behavior_plugins][test_bold_text][bold_text]' => FALSE,
'field_paragraphs[1][behavior_plugins][test_text_color][text_color]' => 'red',
'field_paragraphs[1][_weight]' => 0,
];
$this
->submitForm($edit, 'Save');
$this
->clickLink('Edit');
$this
->assertSession()
->fieldValueEquals('field_paragraphs[0][behavior_plugins][test_text_color][text_color]', 'red');
}
}