3 namespace Drupal\Tests\paragraphs\Functional;
5 use Drupal\paragraphs\Tests\Classic\ParagraphsCoreVersionUiTestTrait;
6 use Drupal\Tests\BrowserTestBase;
7 use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
10 * Tests support for Paragraphs behavior plugins.
14 class ParagraphsExperimentalBehaviorsTest extends BrowserTestBase {
16 use ParagraphsCoreVersionUiTestTrait;
17 use ParagraphsTestBaseTrait;
24 public static $modules = [
30 * Tests that behavior settings have empty leaves removed before being saved.
32 public function testBehaviorPluginsSettingsFiltering() {
33 $this->addParagraphedContentType('paragraphed_test');
35 $admin = $this->drupalCreateUser([
36 'create paragraphed_test content',
37 'edit any paragraphed_test content',
38 'edit behavior plugin settings',
39 'administer paragraphs types',
41 $this->drupalLogin($admin);
43 // Add a text Paragraph type.
44 $paragraph_type = 'text_paragraph';
45 $this->addParagraphsType($paragraph_type);
46 $this->addFieldtoParagraphType($paragraph_type, 'field_text', 'text_long');
48 // Enable the "Test bold text plugin" to have a behavior form.
49 $this->drupalGet('/admin/structure/paragraphs_type/' . $paragraph_type);
51 'behavior_plugins[test_bold_text][enabled]' => TRUE,
53 $this->drupalPostForm(NULL, $edit, 'Save');
55 // Add a note that uses the behavior plugin give it an empty setting.
56 $this->drupalGet('node/add/paragraphed_test');
58 'title[0][value]' => 'Test Node',
59 'field_paragraphs[0][subform][field_text][0][value]' => 'Non-bold text',
60 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => FALSE,
62 $this->drupalPostForm(NULL, $edit, 'Save');
63 $bolded_elements = $this->getSession()->getPage()->findAll('css', '.bold_plugin_text');
64 $this->assertFalse(count($bolded_elements), 'Test plugin did not add a CSS class.');
66 // Check that empty leaves are not saved in the behavior settings.
67 $node = $this->getNodeByTitle('Test Node', TRUE);
68 /** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
69 $paragraph = $node->get('field_paragraphs')->entity;
70 $behavior_settings = $paragraph->getBehaviorSetting('test_bold_text', []);
71 $expected_settings = [];
72 self::assertEquals($expected_settings, $behavior_settings);
74 // Save a non-empty setting.
75 $this->drupalGet('node/' . $node->id() . '/edit');
77 'field_paragraphs[0][subform][field_text][0][value]' => 'Bold text',
78 'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
80 $this->drupalPostForm(NULL, $edit, 'Save');
81 $bolded_elements = $this->getSession()->getPage()->findAll('css', '.bold_plugin_text');
82 $this->assertTrue(count($bolded_elements), 'Test plugin added a CSS class.');
84 // Check that non-empty leaves are saved in the behavior settings.
85 $node = $this->getNodeByTitle('Test Node', TRUE);
86 /** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
87 $paragraph = $node->get('field_paragraphs')->entity;
88 $behavior_settings = $paragraph->getBehaviorSetting('test_bold_text', []);
89 $expected_settings = [
92 self::assertEquals($expected_settings, $behavior_settings);