public function ParagraphsStylePluginTest::testAdvancedStyles

Tests the advanced style functionality.

File

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

Class

ParagraphsStylePluginTest
Tests the style selection plugin.

Namespace

Drupal\Tests\paragraphs_collection\FunctionalJavascript

Code

public function testAdvancedStyles() {

  // 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',
    'use advanced style',
  ]);
  $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);
  $this
    ->click('#edit-behavior-plugins-style-enabled');
  $this
    ->click('#edit-behavior-plugins-style-settings-groups-advanced-test-group');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $edit = [
    'behavior_plugins[style][settings][groups_defaults][advanced_test_group][default]' => '',
  ];
  $this
    ->submitForm($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();

  // Assert a user has no access to super advanced style.
  $options = $this
    ->xpath('//select[contains(@id, :id)]/option', [
    ':id' => 'edit-paragraphs-0-behavior-plugins-style-style',
  ]);
  $this
    ->assertCount(2, $options);
  $this
    ->assertEquals('- Default -', $options[0]
    ->getHtml());
  $this
    ->assertEquals('Advanced', $options[1]
    ->getHtml());

  // Apply advanced style.
  $page
    ->fillField('title[0][value]', 'advanced_style');
  $page
    ->fillField('paragraphs[0][subform][paragraphs_text][0][value]', 'I am text enhanced with advanced style.');
  $page
    ->clickLink('Behavior');
  $page
    ->fillField('paragraphs[0][behavior_plugins][style][style_wrapper][styles][advanced_test_group]', 'advanced');
  $page
    ->pressButton('Save');

  // Advanced style has been applied.
  $this
    ->assertCount(1, $this
    ->cssSelect('.paragraphs-behavior-style--advanced'));

  // Assert that the attributes are visible.
  $this
    ->assertCount(1, $this
    ->cssSelect('[data-attribute="test"]'));

  // Set advanced style as a default one.
  $this
    ->drupalGet('admin/structure/paragraphs_type/' . $paragraph_type);
  $edit = [
    'behavior_plugins[style][settings][groups][advanced_test_group]' => TRUE,
    'behavior_plugins[style][settings][groups_defaults][advanced_test_group][default]' => 'advanced',
  ];
  $this
    ->submitForm($edit, t('Save'));

  // Anonymous users still see the advanced style applied.
  $node = $this
    ->getNodeByTitle('advanced_style');
  $this
    ->drupalLogout();
  $this
    ->drupalGet($node
    ->toUrl());
  $this
    ->assertCount(1, $this
    ->cssSelect('.paragraphs-behavior-style--advanced'));

  // Advanced style can not be changed without the style permission.
  $this
    ->loginAsAdmin([
    'edit any paragraphed_test content',
    'edit behavior plugin settings',
    'use super-advanced style',
  ]);
  $this
    ->drupalGet($node
    ->toUrl('edit-form'));

  // User cannot update the advanced style.
  $styles = $this
    ->xpath('//select[@name="paragraphs[0][behavior_plugins][style][style_wrapper][styles][advanced_test_group]"]');
  $this
    ->assertEquals('disabled', $styles[0]
    ->getAttribute('disabled'));

  // As the user can not access advanced style and as with super-advanced
  // style there would be only element in the list, no style selection is
  // displayed.
  $page
    ->pressButton('List additional actions');
  $page
    ->pressButton('paragraphs_test_style_plugin_add_more');
  $this
    ->assertSession()
    ->assertWaitOnAjaxRequest();
  $styles = $this
    ->xpath('//select[@name="paragraphs[1][behavior_plugins][style][style_wrapper][styles][advanced_test_group]"]');
  $this
    ->assertSession()
    ->elementNotExists('css', 'select[name="paragraphs[1][behavior_plugins][style][style_wrapper][styles][advanced_test_group]"]');
  $this
    ->submitForm([], 'Save');

  // The advanced (default) style was applied to the second text paragraph.
  $this
    ->assertCount(2, $this
    ->cssSelect('.paragraphs-behavior-style--advanced'));
}