public function ParagraphsStylePluginTest::testMultipleGroups

Tests the multiple style selection plugin settings and functionality.

File

paragraphs_collection/tests/src/FunctionalJavascript/ParagraphsStylePluginTest.php, line 582

Class

ParagraphsStylePluginTest
Tests the style selection plugin.

Namespace

Drupal\Tests\paragraphs_collection\FunctionalJavascript

Code

public function testMultipleGroups() {

  // 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);

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

  // Create a paragraphed test node and check the style classes.
  $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();

  // Since Italic Group defines only two styles, assert that only they appear.
  $page
    ->clickLink('Behavior');
  $options = $this
    ->xpath('//select[contains(@id, :id)]/option', [
    ':id' => 'edit-paragraphs-0-behavior-plugins-style-style',
  ]);
  $this
    ->assertCount(2, $options);
  $this
    ->assertEquals('- Italic -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $page
    ->clickLink('Content');
  $edit = [
    'title[0][value]' => 'title_to_remember',
    'paragraphs[0][subform][paragraphs_text][0][value]' => 'text to apply styles',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--italic');

  // Configure two groups and set their defaults.
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $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',
    'behavior_plugins[style][settings][groups_defaults][italic_test_group][default]' => 'italic',
  ];
  $this
    ->submitForm($edit, t('Save'));

  // Check the selects elements for each enabled group and check the classes.
  $node = $this
    ->getNodeByTitle('title_to_remember');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $options = $this
    ->xpath('//select[contains(@name, :name)]/option', [
    ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]',
  ]);
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals('- Regular -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $options = $this
    ->xpath('//select[contains(@name, :name)]/option', [
    ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][italic_test_group]',
  ]);
  $this
    ->assertCount(2, $options);
  $this
    ->assertEquals('- Italic -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--italic');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--regular');

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

  // Check that there is only one select and only one style class.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $options = $this
    ->xpath('//select[contains(@name, :name)]/option', [
    ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]',
  ]);
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals('- Bold -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Overridden style Module', $options[1]
    ->getHtml());
  $this
    ->assertEquals('Regular', $options[2]
    ->getHtml());
  $styles = $this
    ->xpath('//select[contains(@name, :name)]', [
    ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][italic_test_group]',
  ]);
  $this
    ->assertEquals([], $styles);
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseNotContains('paragraphs-behavior-style--italic');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--bold');

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

  // Check that there is only one select and only one style class.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $options = $this
    ->xpath('//select[contains(@name, :name)]/option', [
    ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]',
  ]);
  $this
    ->assertCount(3, $options);
  $this
    ->assertEquals('- Regular -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $options = $this
    ->xpath('//select[contains(@name, :name)]/option', [
    ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][italic_test_group]',
  ]);
  $this
    ->assertCount(2, $options);
  $this
    ->assertEquals('- Italic -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $options = $this
    ->xpath('//select[contains(@name, :name)]/option', [
    ':name' => 'paragraphs[0][behavior_plugins][style][style_wrapper][styles][underline_test_group]',
  ]);
  $this
    ->assertCount(2, $options);
  $this
    ->assertEquals('- Underline -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Bold', $options[1]
    ->getHtml());
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--italic');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--regular');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--underline');

  // Change a plugin.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $page
    ->clickLink('Behavior');
  $edit = [
    'paragraphs[0][behavior_plugins][style][style_wrapper][styles][regular_test_group]' => 'bold',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--italic');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--bold');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--underline');

  // Assert the theme suggestion added by the style plugin.
  $this
    ->assertSession()
    ->pageTextContains('paragraph__test_style_plugin__bold');
  $this
    ->assertSession()
    ->pageTextContains('paragraph__test_style_plugin__italic');
}