You are here

public function ParagraphsStylePluginTest::testStyleSelection in Paragraphs Collection 8

Tests the style selection plugin settings and functionality.

File

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

Class

ParagraphsStylePluginTest
Tests the style selection plugin.

Namespace

Drupal\Tests\paragraphs_collection\FunctionalJavascript

Code

public function testStyleSelection() {

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

  // Create Paragraph type with Style plugin enabled.
  $paragraph_type = 'test_style_plugin';
  $this
    ->addParagraphsType($paragraph_type);

  // Add a text field.
  $this
    ->fieldUIAddExistingField('admin/structure/paragraphs_type/' . $paragraph_type, 'paragraphs_text', $paragraph_type);
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $edit = [
    'behavior_plugins[style][enabled]' => TRUE,
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));
  $this
    ->assertSession()
    ->pageTextContains('The style plugin cannot be enabled if no groups are selected.');
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-regular-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $edit = [
    'behavior_plugins[style][settings][groups_defaults][regular_test_group][default]' => '',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Create paragraphed content.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->pressButton('List additional actions');
  $page
    ->pressButton('paragraphs_test_style_plugin_add_more');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Check that we have style plugin.
  $this
    ->assertSession()
    ->pageTextContains('style');
  $this
    ->assertField('paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]');

  // Check that the style options are sorted alphabetically.
  $options = $this
    ->xpath('//select[contains(@id, :id)]/option', [
    ':id' => 'edit-paragraphs-0-behavior-plugins-style-style',
  ]);
  $this
    ->assertEquals('- Default -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $this
    ->assertEquals('Overridden style Module', $options[2]
    ->getHtml());
  $this
    ->assertEquals('Regular', $options[3]
    ->getHtml());

  // Restrict the paragraphs type to the "Italic Test Group" style group.
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $this
    ->assertFieldByName('behavior_plugins[style][settings][groups][italic_test_group]');
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-italic-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-regular-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $edit = [
    'behavior_plugins[style][settings][groups_defaults][italic_test_group][default]' => '',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Check that the style without a style group is no longer available.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $page
    ->pressButton('List additional actions');
  $page
    ->pressButton('paragraphs_test_style_plugin_add_more');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();

  // Since Italic Group defines only two styles, assert that only they appear.
  $options = $this
    ->xpath('//select[contains(@id, :id)]/option', [
    ':id' => 'edit-paragraphs-0-behavior-plugins-style-style',
  ]);
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals('- Default -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $this
    ->assertEquals('Italic', $options[2]
    ->getHtml());

  // Configure Regular as a default style.
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $this
    ->assertFieldByName('behavior_plugins[style][settings][groups_defaults][italic_test_group][default]', '');
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-italic-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-regular-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $edit = [
    'behavior_plugins[style][settings][groups_defaults][regular_test_group][default]' => 'regular',
  ];
  $this
    ->drupalPostForm(NULL, $edit, t('Save'));

  // Regular style should be shown first in the list.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $page
    ->pressButton('List additional actions');
  $page
    ->pressButton('paragraphs_test_style_plugin_add_more');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $page
    ->clickLink('Behavior');
  $option = $this
    ->assertSession()
    ->selectExists('paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]')
    ->getValue();
  $this
    ->assertEquals('regular', $option);
  $options = $this
    ->xpath('//select[contains(@id, :id)]/option', [
    ':id' => 'edit-paragraphs-0-behavior-plugins-style-style',
  ]);
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals('- Regular -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());

  // Default style should be applied.
  $page
    ->clickLink('Content');
  $page
    ->fillField('title[0][value]', 'style_plugin_node');
  $page
    ->fillField('paragraphs[0][subform][paragraphs_text][0][value]', 'I am regular text.');
  $this
    ->drupalPostForm(NULL, [], 'Save');

  // Assert the theme suggestion added by the style plugin.
  $this
    ->assertSession()
    ->pageTextContains('paragraph__test_style_plugin__regular');
  $style = $this
    ->xpath('//div[@class="regular regular-wrapper paragraphs-behavior-style--regular paragraph paragraph--type--test-style-plugin paragraph--view-mode--default"]')[0];
  $this
    ->assertNotNull($style);

  // Assert default value for the style selection.
  $node = $this
    ->getNodeByTitle('style_plugin_node');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $option = $this
    ->assertSession()
    ->selectExists('paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]')
    ->getValue();
  $this
    ->assertEquals('regular', $option);

  // Update the styles group configuration.
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-bold-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-overline-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-empty-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $edit = [
    'behavior_plugins[style][settings][groups_defaults][bold_test_group][default]' => 'bold',
  ];
  $this
    ->drupalPostForm(NULL, $edit, 'Save');

  // Assert the values on the behavior form.
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->assertSession()
    ->pageTextNotContains('Bold CONTEXT');
  $this
    ->assertSession()
    ->pageTextNotContains('Empty Test Group');

  // Regular and Overline style groups are visible.
  $this
    ->assertOptionSelected('edit-paragraphs-0-behavior-plugins-style-style-wrapper-styles-regular-test-group', 'regular', 'Regular style group has a default option applied.');
  $this
    ->assertOptionSelected('edit-paragraphs-0-behavior-plugins-style-style-wrapper-styles-overline-test-group', '', 'There is no configured default value for Overline style group.');

  // Bold and Empty style groups are not visible as they have exactly one
  // item in the list.
  $this
    ->assertNoRaw('edit-paragraphs-0-behavior-plugins-style-style-wrapper-styles-bold-test-group');
  $this
    ->assertNoRaw('edit-paragraphs-0-behavior-plugins-style-style-wrapper-styles-empty-test-group');
  $this
    ->drupalPostForm(NULL, [], 'Save');

  // Regular style has been selected through the form.
  $this
    ->assertCount(1, $this
    ->cssSelect('.paragraphs-behavior-style--regular'));

  // Default Bold style has been applied in the background.
  $this
    ->assertCount(1, $this
    ->cssSelect('.paragraphs-behavior-style--bold'));

  // Overline style has not been applied as it has no default option.
  $this
    ->assertSession()
    ->elementNotExists('css', '.paragraphs-behavior-style--overline');

  // Empty style has not been applied as it has no default option nor styles.
  $this
    ->assertSession()
    ->elementNotExists('css', '.paragraphs-behavior-style--empty');
  $edit = [
    // Set default style for the overline group.
    'behavior_plugins[style][settings][groups_defaults][overline_test_group][default]' => 'overline',
    // Remove default style for the bold group.
    'behavior_plugins[style][settings][groups_defaults][bold_test_group][default]' => '',
  ];
  $this
    ->drupalPostForm('admin/structure/paragraphs_type/' . $paragraph_type, $edit, 'Save');
  $edit = [
    'styles[bold][enabled]' => TRUE,
    'styles[italic][enabled]' => TRUE,
    'styles[underline][enabled]' => TRUE,
    'styles[overline][enabled]' => TRUE,
    // Disable regular style.
    'styles[regular][enabled]' => FALSE,
  ];
  $this
    ->drupalPostForm('admin/reports/paragraphs_collection/styles', $edit, 'Save configuration');
  $this
    ->drupalGet($node
    ->toUrl());

  // The new default overline style applies to the previously saved paragraph.
  $this
    ->assertCount(1, $this
    ->cssSelect('.paragraphs-behavior-style--overline'));

  // The bold style has no default and no longer applies.
  $this
    ->assertSession()
    ->elementNotExists('css', '.paragraphs-behavior-style--bold');

  // The regular style is disabled and no longer applies.
  $this
    ->assertSession()
    ->elementNotExists('css', '.paragraphs-behavior-style--regular');

  // Default overline style is selected and all overline styles are disabled.
  // The empty form element should not be displayed.
  $edit = [
    'styles[bold][enabled]' => FALSE,
    'styles[italic][enabled]' => TRUE,
    'styles[underline][enabled]' => TRUE,
    // Disable overline style.
    'styles[overline][enabled]' => FALSE,
    'styles[regular][enabled]' => FALSE,
  ];
  $this
    ->drupalPostForm('admin/reports/paragraphs_collection/styles', $edit, 'Save configuration');
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));
  $this
    ->assertNoFieldByName('paragraphs[0][behavior_plugins][style][style_wrapper][styles][overline_test_group]');
  $this
    ->assertNoRaw('edit-paragraphs-0-behavior-plugins-style');
}