Tests support for Paragraphs behavior plugins.
@group paragraphs
Expanded class hierarchy of ParagraphsBehaviorsTest
class ParagraphsBehaviorsTest extends ParagraphsTestBase {
/**
* Modules to enable.
*
* @var string[]
*/
protected static $modules = [
'node',
'paragraphs_test',
];
/**
* Tests that behavior settings have empty leaves removed before being saved.
*/
public function testBehaviorPluginsSettingsFiltering() {
$this
->addParagraphedContentType('paragraphed_test');
$admin = $this
->drupalCreateUser([
'create paragraphed_test content',
'edit any paragraphed_test content',
'edit behavior plugin settings',
'administer paragraphs types',
]);
$this
->drupalLogin($admin);
// Add a text Paragraph type.
$paragraph_type = 'text_paragraph';
$this
->addParagraphsType($paragraph_type);
$this
->addFieldtoParagraphType($paragraph_type, 'field_text', 'text_long');
// Enable the "Test bold text plugin" to have a behavior form.
$this
->drupalGet('/admin/structure/paragraphs_type/' . $paragraph_type);
$edit = [
'behavior_plugins[test_bold_text][enabled]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$this
->assertSame([
'test_bold_text' => [
'enabled' => TRUE,
],
], \Drupal::config("paragraphs.paragraphs_type.{$paragraph_type}")
->get('behavior_plugins'));
// Add a note that uses the behavior plugin give it an empty setting.
$this
->drupalGet('node/add/paragraphed_test');
$edit = [
'title[0][value]' => 'Test Node',
'field_paragraphs[0][subform][field_text][0][value]' => 'Non-bold text',
'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => FALSE,
];
$this
->submitForm($edit, 'Save');
$bolded_elements = $this
->getSession()
->getPage()
->findAll('css', '.bold_plugin_text');
$this
->assertEmpty(count($bolded_elements), 'Test plugin did not add a CSS class.');
// Check that empty leaves are not saved in the behavior settings.
$node = $this
->getNodeByTitle('Test Node', TRUE);
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $node
->get('field_paragraphs')->entity;
$behavior_settings = $paragraph
->getBehaviorSetting('test_bold_text', []);
$expected_settings = [];
self::assertEquals($expected_settings, $behavior_settings);
// Save a non-empty setting.
$this
->drupalGet('node/' . $node
->id() . '/edit');
$edit = [
'field_paragraphs[0][subform][field_text][0][value]' => 'Bold text',
'field_paragraphs[0][behavior_plugins][test_bold_text][bold_text]' => TRUE,
];
$this
->submitForm($edit, 'Save');
$bolded_elements = $this
->getSession()
->getPage()
->findAll('css', '.bold_plugin_text');
$this
->assertGreaterThan(0, count($bolded_elements), 'Test plugin added a CSS class.');
// Check that non-empty leaves are saved in the behavior settings.
\Drupal::entityTypeManager()
->getStorage('paragraph')
->resetCache();
$node = $this
->getNodeByTitle('Test Node', TRUE);
/** @var \Drupal\paragraphs\ParagraphInterface $paragraph */
$paragraph = $node
->get('field_paragraphs')->entity;
$behavior_settings = $paragraph
->getBehaviorSetting('test_bold_text', []);
$expected_settings = [
'bold_text' => 1,
];
self::assertEquals($expected_settings, $behavior_settings);
}
}
Name | Modifiers | Type | Description | Overrides |
---|---|---|---|---|
ParagraphsBehaviorsTest:: |
protected static | property |
Modules to enable. Overrides ParagraphsTestBase:: |
|
ParagraphsBehaviorsTest:: |
public | function | Tests that behavior settings have empty leaves removed before being saved. | |
ParagraphsTestBase:: |
protected | property | List of permissions used by loginAsAdmin(). | |
ParagraphsTestBase:: |
protected | property | Drupal user object created by loginAsAdmin(). | 1 |
ParagraphsTestBase:: |
protected | property | 2 | |
ParagraphsTestBase:: |
function | Creates an user with admin permissions and log in. | ||
ParagraphsTestBase:: |
protected | function |
Removes the default paragraph type. Overrides ParagraphsTestBase:: |
|
ParagraphsTestBase:: |
protected | function |
Sets the Paragraphs widget add mode. Overrides ParagraphsTestBase:: |
|
ParagraphsTestBase:: |
protected | function | Sets the allowed Paragraphs types that can be added. | |
ParagraphsTestBase:: |
protected | function | Sets the default paragraph type. | |
ParagraphsTestBase:: |
protected | function | Sets the weight of a given Paragraphs type. | |
ParagraphsTestBase:: |
protected | function | Sets the Paragraphs widget display mode. | |
ParagraphsTestBase:: |
protected | function | 19 | |
ParagraphsTestBaseTrait:: |
protected | property | The workflow entity. | |
ParagraphsTestBaseTrait:: |
protected | function | Adds a field to a given paragraph type. | |
ParagraphsTestBaseTrait:: |
protected | function | Adds a content type with a Paragraphs field. | |
ParagraphsTestBaseTrait:: |
protected | function | Adds a Paragraphs field to a given entity type. | |
ParagraphsTestBaseTrait:: |
protected | function | Adds a Paragraphs type. | |
ParagraphsTestBaseTrait:: |
protected | function | Adds an icon to a paragraphs type. | |
ParagraphsTestBaseTrait:: |
protected | function | Checks the core version. | |
ParagraphsTestBaseTrait:: |
protected | function | Creates a workflow entity. | |
ParagraphsTestBaseTrait:: |
protected | function | Sets some of the settings of a paragraphs field widget. |