You are here

public function ParagraphsStylePluginTest::testGlobalStyleSettings in Paragraphs Collection 8

Tests global settings for style plugin.

File

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

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
    ->drupalPostForm(NULL, [], t('Save'));
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);

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

  // 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
    ->drupalPostForm(NULL, $edit, 'Save');
  $this
    ->assertRaw('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
    ->drupalPostForm(NULL, [], t('Save'));

  // Update global settings and enable two styles.
  $this
    ->drupalGet('admin/reports/paragraphs_collection/styles');
  $this
    ->click('#edit-styles-italic-enabled');
  $this
    ->drupalPostForm(NULL, [], '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
    ->assertNoRaw('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.
  $edit = [
    'styles[bold][enabled]' => TRUE,
    'styles[italic][enabled]' => TRUE,
  ];
  $this
    ->drupalPostForm('admin/reports/paragraphs_collection/styles', $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
    ->drupalPostForm(NULL, $edit, t('Save'));

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

  // Assert that the selection is correctly displayed.
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->assertOptionSelected('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
    ->drupalPostForm(NULL, $edit, 'Save configuration');

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