public function ParagraphsStylePluginTest::testGlobalStyleSettings

Tests global settings for style plugin.

File

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

Class

ParagraphsStylePluginTest
Tests the style selection plugin.

Namespace

Drupal\Tests\paragraphs_collection\FunctionalJavascript

Code

public function testGlobalStyleSettings() {

  // Install paragraphs collection test to use test style plugins.
  \Drupal::service('module_installer')
    ->install([
    'paragraphs_collection_test',
  ]);
  $this
    ->addParagraphedContentType('paragraphed_test', 'paragraphs');
  $this
    ->loginAsAdmin([
    'edit any paragraphed_test content',
    'edit behavior plugin settings',
  ]);

  // 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', 'paragraphs_text');
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $this
    ->click('#edit-behavior-plugins-style-enabled');
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-bold-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-regular-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([], t('Save'));
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);

  // Assert global settings.
  $this
    ->drupalGet('admin/reports/paragraphs_collection/styles');
  $this
    ->assertSession()
    ->checkboxNotChecked('styles[bold][enabled]');
  $this
    ->assertSession()
    ->checkboxNotChecked('styles[italic][enabled]');
  $this
    ->assertSession()
    ->checkboxNotChecked('styles[regular][enabled]');
  $this
    ->assertSession()
    ->checkboxNotChecked('styles[underline][enabled]');

  // Add a node with paragraphs and check the available styles.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $page = $this
    ->getSession()
    ->getPage();
  $page
    ->pressButton('List additional actions');
  $page
    ->pressButton('paragraphs_' . $paragraph_type . '_add_more');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $page
    ->clickLink('Behavior');
  $options = $this
    ->xpath('//*[contains(@class,"paragraphs-plugin-form-element")]/option');
  $this
    ->assertCount(6, $options);
  $page
    ->fillField('title[0][value]', 'global_settings');
  $edit = [
    'paragraphs[0][behavior_plugins][style][style_wrapper][styles][bold_test_group]' => 'bold',
  ];
  $this
    ->submitForm($edit, 'Save');
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--bold');
  $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-bold-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $this
    ->submitForm([], t('Save'));

  // Update global settings and enable two styles.
  $this
    ->drupalGet('admin/reports/paragraphs_collection/styles');
  $this
    ->click('#edit-styles-italic-enabled');
  $this
    ->submitForm([], 'Save configuration');
  $node = $this
    ->getNodeByTitle('global_settings');
  $this
    ->drupalGet('node/' . $node
    ->id());

  // Assert that the class of the plugin is not added if disabled.
  $this
    ->assertSession()
    ->responseNotContains('paragraphs-behavior-style--bold');
  $this
    ->clickLink('Edit');

  // Assert that only the two enabled styles are available.
  $options = $this
    ->xpath('//*[contains(@class,"paragraphs-plugin-form-element")]/option');
  $this
    ->assertCount(2, $options);
  $this
    ->assertEquals($options[0]
    ->getHtml(), '- Default -');
  $this
    ->assertEquals($options[1]
    ->getHtml(), 'Italic');
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $options = $this
    ->xpath('//*[contains(@name,"behavior_plugins[style][settings][groups_defaults][italic_test_group][default]")]/option');
  $this
    ->assertCount(2, $options);
  $this
    ->assertEquals($options[0]
    ->getHtml(), '- None -');
  $this
    ->assertEquals($options[1]
    ->getHtml(), 'Italic');

  // Enable bold and italic styles.
  $this
    ->drupalGet('admin/reports/paragraphs_collection/styles');
  $edit = [
    'styles[bold][enabled]' => TRUE,
    'styles[italic][enabled]' => TRUE,
  ];
  $this
    ->submitForm($edit, 'Save configuration');

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

  // Set the paragraph style to bold.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $page
    ->clickLink('Behavior');
  $this
    ->submitForm([
    'paragraphs[0][behavior_plugins][style][style_wrapper][styles][italic_test_group]' => 'bold',
  ], t('Save'));
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--bold');

  // Assert that the selection is correctly displayed.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertSession()
    ->optionExists('edit-paragraphs-0-behavior-plugins-style-style-wrapper-styles-italic-test-group', 'bold');

  // Disable the bold style.
  $this
    ->drupalGet('admin/reports/paragraphs_collection/styles');
  $edit = [
    'styles[bold][enabled]' => FALSE,
  ];
  $this
    ->submitForm($edit, 'Save configuration');

  // The plugin should fallback on the default style defined.
  $this
    ->drupalGet('node/' . $node
    ->id());
  $this
    ->assertSession()
    ->responseContains('paragraphs-behavior-style--italic');
}