You are here

public function ParagraphsLibraryTest::testConvertParagraphIntoLibrary in Paragraphs 8

Tests converting paragraph item into library.

File

modules/paragraphs_library/tests/src/Functional/ParagraphsLibraryTest.php, line 352

Class

ParagraphsLibraryTest
Tests paragraphs library functionality.

Namespace

Drupal\Tests\paragraphs_library\Functional

Code

public function testConvertParagraphIntoLibrary() {
  $user = $this
    ->createUser(array_merge($this->admin_permissions, [
    'create paragraphed_test content',
    'edit any paragraphed_test content',
    'administer paragraphs library',
    'administer paragraphs types',
  ]));
  $this
    ->drupalLogin($user);
  $paragraph_type = 'text';
  $this
    ->addParagraphsType($paragraph_type);
  static::fieldUIAddNewField('admin/structure/paragraphs_type/' . $paragraph_type, 'text', 'Text', 'text_long', [], []);
  $edit = [
    'allow_library_conversion' => 1,
  ];
  $this
    ->drupalGet('admin/structure/paragraphs_type/text');
  $this
    ->submitForm($edit, 'Save');

  // Adding library item is available with the administer library permission.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'Add text');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_promote_to_library');
  $this
    ->drupalGet('admin/content/paragraphs/add/default');
  $this
    ->assertSession()
    ->statusCodeEquals(200);

  // Adding library item is not available without appropriate permissions.
  $user_roles = $user
    ->getRoles(TRUE);
  $user_role = end($user_roles);
  user_role_revoke_permissions($user_role, [
    'administer paragraphs library',
  ]);
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'Add text');
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_0_promote_to_library');
  $this
    ->drupalGet('admin/content/paragraphs/add/default');
  $this
    ->assertSession()
    ->statusCodeEquals(403);

  // Enabling a dummy behavior plugin for testing the item label creation.
  $edit = [
    'behavior_plugins[test_text_color][enabled]' => TRUE,
  ];
  $this
    ->drupalGet('admin/structure/paragraphs_type/text');
  $this
    ->submitForm($edit, 'Save');

  // Assign "create paragraph library item" permission to a user.
  user_role_grant_permissions($user_role, [
    'create paragraph library item',
  ]);
  $this
    ->drupalGet('admin/content/paragraphs/add/default');
  $this
    ->assertSession()
    ->statusCodeEquals(200);
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'Add text');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_promote_to_library');
  $this
    ->assertSession()
    ->responseContains('Promote to library');
  $edit = [
    'field_paragraphs[0][subform][field_text][0][value]' => 'Random text for testing converting into library.',
  ];
  $this
    ->submitForm($edit, 'field_paragraphs_0_promote_to_library');
  $this
    ->assertSession()
    ->pageTextContains('From library');
  $this
    ->assertSession()
    ->pageTextContains('Reusable paragraph');
  $this
    ->assertSession()
    ->fieldExists('field_paragraphs[0][subform][field_reusable_paragraph][0][target_id]');
  $edit = [
    'title[0][value]' => 'TextParagraphs',
  ];
  $this
    ->assertSession()
    ->buttonNotExists('field_paragraphs_0_promote_to_library');
  $this
    ->assertSession()
    ->buttonExists('field_paragraphs_0_unlink_from_library');
  $this
    ->submitForm($edit, 'Save');
  $this
    ->drupalGet('node/1');
  $this
    ->assertSession()
    ->pageTextContains('Random text for testing converting into library.');

  // Create library item from existing paragraph item.
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'Add text');
  $edit = [
    'title[0][value]' => 'NodeTitle',
    'field_paragraphs[0][subform][field_text][0][value]' => 'Random text for testing converting into library.',
  ];
  $this
    ->submitForm($edit, 'Save');
  $node = $this
    ->getNodeByTitle('NodeTitle');
  $this
    ->drupalGet('node/' . $node
    ->id() . '/edit');
  $this
    ->submitForm($edit, 'field_paragraphs_0_promote_to_library');
  user_role_grant_permissions($user_role, [
    'administer paragraphs library',
  ]);
  $this
    ->drupalGet('/admin/content/paragraphs');
  $this
    ->assertSession()
    ->pageTextContains('Text');
  $this
    ->assertSession()
    ->pageTextContains('Random text for testing converting into library.');

  // Test disallow convesrion.
  $edit = [
    'allow_library_conversion' => FALSE,
  ];
  $this
    ->drupalGet('admin/structure/paragraphs_type/text');
  $this
    ->submitForm($edit, 'Save');

  /** @var \Drupal\Core\Config\ConfigFactoryInterface $config_factory */
  $config_factory = \Drupal::service('config.factory');
  $third_party = $config_factory
    ->get('paragraphs.paragraphs_type.text')
    ->get('third_party_settings');
  $this
    ->assertFalse(isset($third_party['paragraphs_library']['allow_library_conversion']));
  $this
    ->drupalGet('node/add/paragraphed_test');
  $this
    ->submitForm([], 'Add text');
  $this
    ->assertSession()
    ->responseNotContains('Promote to library');

  // Test that the checkbox is not visible on from_library.
  $this
    ->drupalGet('admin/structure/paragraphs_type/from_library');
  $this
    ->assertSession()
    ->fieldNotExists('allow_library_conversion');
}