You are here

public function ParagraphsStylePluginTest::testStyleSettingsSummary in Paragraphs Collection 8

Tests style settings summary.

File

tests/src/FunctionalJavascript/ParagraphsStylePluginTest.php, line 356

Class

ParagraphsStylePluginTest
Tests the style selection plugin.

Namespace

Drupal\Tests\paragraphs_collection\FunctionalJavascript

Code

public function testStyleSettingsSummary() {

  // Install Paragraph Collection Test in order to have styles.
  \Drupal::service('module_installer')
    ->install([
    'paragraphs_collection_test',
  ]);
  $this
    ->addParagraphedContentType('paragraphed_test');
  $this
    ->loginAsAdmin([
    'create paragraphed_test content',
    'edit any paragraphed_test content',
    'edit behavior plugin settings',
  ]);

  // Create text paragraph.
  $text_paragraph = Paragraph::create([
    'type' => 'text',
    'paragraphs_text' => [
      'value' => '<p>Test text 1.</p>',
      'format' => 'basic_html',
    ],
  ]);
  $text_paragraph
    ->save();

  // Create a container paragraph for the text paragraph.
  $paragraph = Paragraph::create([
    'title' => 'Demo Paragraph',
    'type' => 'container',
    'paragraphs_container_paragraphs' => [
      $text_paragraph,
    ],
  ]);
  $paragraph
    ->save();

  // Create a node with the paragraphs content.
  $node = Node::create([
    'title' => 'Style plugin test',
    'type' => 'paragraphed_test',
    'field_paragraphs' => [
      $paragraph,
    ],
  ]);
  $node
    ->save();

  // Check the empty summary.
  $behavior_plugins = $paragraph
    ->getParagraphType()
    ->get('behavior_plugins');
  $behavior_plugins['style'] = [
    'enabled' => TRUE,
    'groups' => [
      'bold_test_group' => [
        'default' => '',
      ],
    ],
  ];
  $paragraph
    ->getParagraphType()
    ->set('behavior_plugins', $behavior_plugins);
  $paragraph
    ->getParagraphType()
    ->save();
  $style_plugin = $paragraph
    ->getParagraphType()
    ->getEnabledBehaviorPlugins()['style'];
  $this
    ->assertEquals([], $style_plugin
    ->settingsSummary($paragraph));

  // Use bold style for this container.
  $paragraph
    ->setBehaviorSettings('style', [
    'styles' => [
      'bold_test_group' => 'bold',
    ],
  ]);
  $paragraph
    ->save();
  $this
    ->assertEquals([
    [
      'label' => 'Bold CONTEXT',
      'value' => 'Bold',
    ],
  ], $style_plugin
    ->settingsSummary($paragraph));
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph
    ->getParagraphType()
    ->id());
  $this
    ->assertSession()
    ->pageTextContains('Bold Test Group');

  // Check the settings summary in a closed mode.
  $this
    ->drupalGet('admin/structure/types/manage/paragraphed_test/form-display');
  $this
    ->drupalPostForm(NULL, [], 'field_paragraphs_settings_edit');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->drupalPostForm(NULL, [
    'fields[field_paragraphs][settings_edit_form][settings][edit_mode]' => 'closed',
  ], t('Update'));
  $this
    ->drupalPostForm(NULL, [], 'Save');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertRaw('edit-field-paragraphs-0-top-icons');
  $this
    ->assertRaw('<span class="summary-content">Test text 1.</span></div><div class="paragraphs-plugin-wrapper"><span class="summary-plugin"><span class="summary-plugin-label">Bold CONTEXT</span>Bold');

  // Configure style bold as default.
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph
    ->getType());
  $edit = [
    'behavior_plugins[style][settings][groups_defaults][bold_test_group][default]' => 'bold',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Check that the settings summary does not show the default style.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertRaw('<span class="summary-content">Test text 1.');
  $this
    ->assertNoRaw('Style: Bold');
  $this
    ->assertNoRaw('Style: - Bold -');
}