<?php
namespace Drupal\Tests\paragraphs_library\Functional;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\field_ui\Traits\FieldUiTestTrait;
use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
use Drupal\Tests\paragraphs\Traits\ParagraphsCoreVersionUiTestTrait;
class ParagraphsLibraryItemTest extends BrowserTestBase {
use ParagraphsTestBaseTrait, FieldUiTestTrait, ParagraphsCoreVersionUiTestTrait;
protected static $modules = [
'node',
'paragraphs_library',
'block',
'field_ui',
];
protected $defaultTheme = 'starterkit_theme';
public function setUp() : void {
parent::setUp();
$this
->addParagraphedContentType('paragraphed_test', 'field_paragraphs');
$this
->addParagraphsType('text');
$this
->addFieldtoParagraphType('text', 'field_text', 'text');
$this
->addParagraphsType('paragraphs_container');
$this
->addParagraphsField('paragraphs_container', 'paragraphs_container_paragraphs', 'paragraph');
$admin = $this
->drupalCreateUser([
'create paragraphed_test content',
'edit any paragraphed_test content',
'view paragraphed_test revisions',
'administer paragraphs library',
]);
$this
->drupalLogin($admin);
$this
->placeDefaultBlocks();
}
public function testLibraryItemsAccessControl() {
$role = $this
->createRole([
'create paragraph library item',
]);
$user = $this
->createUser([]);
$user
->addRole($role);
$user
->save();
$this
->drupalLogin($user);
$this
->drupalGet('admin/content/paragraphs/add/default');
$this
->getSession()
->getPage()
->pressButton('Add text');
$edit = [
'label[0][value]' => 'Library item',
'paragraphs[0][subform][field_text][0][value]' => 'Item content',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('Paragraph Library item has been created');
$this
->assertSession()
->statusCodeEquals(403);
$matched_library_items = $this->container
->get('entity_type.manager')
->getStorage('paragraphs_library_item')
->loadByProperties([
'label' => 'Library item',
]);
$library_item = reset($matched_library_items);
$library_item_id = $library_item
->id();
$this
->assertLibraryItemAccess($library_item_id, 403, 'edit');
$this
->assertLibraryItemAccess($library_item_id, 403, 'delete');
user_role_grant_permissions($role, [
'edit paragraph library item',
]);
$this
->assertLibraryItemAccess($library_item_id, 200, 'edit');
$this
->assertLibraryItemAccess($library_item_id, 403, 'delete');
$this->container
->get('module_installer')
->install([
'paragraphs_type_permissions',
]);
$this
->assertLibraryItemAccess($library_item_id, 403, 'edit');
user_role_grant_permissions($role, [
'update paragraph content text',
]);
$this
->assertLibraryItemAccess($library_item_id, 200, 'edit');
$this
->assertLibraryItemAccess($library_item_id, 403, 'delete');
user_role_revoke_permissions($role, [
'create paragraph library item',
'edit paragraph library item',
]);
user_role_grant_permissions($role, [
'administer paragraphs library',
]);
$this
->assertLibraryItemAccess($library_item_id, 200, 'edit');
$this
->assertLibraryItemAccess($library_item_id, 403, 'delete');
user_role_grant_permissions($role, [
'delete paragraph content text',
]);
$this
->assertLibraryItemAccess($library_item_id, 200, 'delete');
}
protected function assertLibraryItemAccess($library_item_id, $response_code, $operation) {
$this
->drupalGet("admin/content/paragraphs/{$library_item_id}/{$operation}");
$this
->assertSession()
->statusCodeEquals($response_code);
}
public function testNoConversionSideEffects() {
$text_paragraph = Paragraph::create([
'type' => 'text',
'field_text' => [
'value' => 'Test text 1',
'format' => 'plain_text',
],
]);
$text_paragraph
->save();
$container_paragraph = Paragraph::create([
'type' => 'paragraphs_container',
'paragraphs_container_paragraphs' => [
$text_paragraph,
],
]);
$container_paragraph
->save();
$node = Node::create([
'type' => 'paragraphed_test',
'title' => 'Test Node',
'field_paragraphs' => [
$container_paragraph,
],
]);
$node
->save();
ParagraphsType::load('paragraphs_container')
->setThirdPartySetting('paragraphs_library', 'allow_library_conversion', TRUE)
->save();
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Promote to library');
$this
->submitForm([], 'Save');
$this
->assertSession()
->pageTextContains('Test text 1');
$node = $this
->drupalGetNodeByTitle('Test Node');
$library_item = $node
->get('field_paragraphs')->entity
->get('field_reusable_paragraph')->entity;
$this
->drupalGet('/admin/content/paragraphs/' . $library_item
->id() . '/edit');
$this
->getSession()
->getPage()
->fillField('Label', 'Test Library Item');
$this
->getSession()
->getPage()
->findButton('paragraphs_0_subform_paragraphs_container_paragraphs_0_remove')
->press();
$this
->submitForm([], 'Save');
$this
->drupalGet('/admin/content/paragraphs/' . $library_item
->id());
$this
->assertSession()
->pageTextNotContains('Test text 1');
$this
->drupalGet('/node/' . $node
->id());
$this
->assertSession()
->pageTextNotContains('Test text 1');
$this
->drupalGet('/node/' . $node
->id() . '/revisions');
$this
->getSession()
->getPage()
->find('css', '.node-revision-table')
->find('xpath', '(//tbody//tr)[2]//a')
->click();
$revision_url = $this
->getSession()
->getCurrentUrl();
$this
->assertStringContainsString('/node/' . $node
->id() . '/revisions/', $revision_url);
$this
->assertStringContainsString('view', $revision_url);
$this
->assertSession()
->pageTextContains('Test text 1');
$this
->drupalGet('/admin/content/paragraphs/' . $library_item
->id() . '/edit');
$this
->submitForm([], 'Add text');
$this
->getSession()
->getPage()
->fillField('field_text', 'Test text 2');
$this
->submitForm([], 'Save');
$this
->drupalGet('/admin/content/paragraphs/' . $library_item
->id());
$this
->assertSession()
->pageTextContains('Test text 2');
$this
->drupalGet('/node/' . $node
->id());
$this
->assertSession()
->pageTextContains('Test text 2');
$this
->drupalGet('/node/' . $node
->id() . '/edit');
$this
->submitForm([], 'Unlink from library');
$this
->getSession()
->getPage()
->findButton('field_paragraphs_0_subform_paragraphs_container_paragraphs_0_remove')
->press();
$this
->submitForm([], 'Save');
$this
->drupalGet('/node/' . $node
->id());
$this
->assertSession()
->pageTextNotContains('Test text 2');
$this
->drupalGet('/admin/content/paragraphs/' . $library_item
->id());
$this
->assertSession()
->pageTextContains('Test text 2');
}
public function testLibraryItemUsageTab() {
$admin = $this
->drupalCreateUser([
'create paragraphed_test content',
'edit any paragraphed_test content',
'administer paragraphs library',
'delete all revisions',
'revert all revisions',
'administer content types',
'administer node fields',
'administer paragraphs types',
'administer node form display',
'administer paragraph fields',
'administer paragraph form display',
'access entity usage statistics',
]);
$this
->drupalLogin($admin);
$this
->addParagraphsType('test_content');
$this
->addParagraphsType('nested_paragraph');
$this
->fieldUIAddNewField('admin/structure/paragraphs_type/test_content', 'paragraphs_text', 'Test content', 'text_long', [], []);
$this
->fieldUIAddNewField('admin/structure/paragraphs_type/nested_paragraph', 'err_field', 'Nested', 'field_ui:entity_reference_revisions:paragraph', [
'settings[target_type]' => 'paragraph',
'cardinality' => '-1',
], []);
$this
->drupalGet('admin/content/paragraphs/add/default');
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$page
->pressButton('Add nested_paragraph');
$page
->find('css', '.paragraphs-subform')
->pressButton('Add test_content');
$edit = [
'label[0][value]' => 'Test usage nested paragraph',
'paragraphs[0][subform][field_err_field][0][subform][field_paragraphs_text][0][value]' => 'Example text for revision in nested paragraph.',
];
$this
->submitForm($edit, 'Save');
$assert_session
->pageTextContains('Paragraph Test usage nested paragraph has been created.');
$this
->drupalGet('node/add/paragraphed_test');
$page
->pressButton('Add From library');
$edit = [
'title[0][value]' => 'Test content',
'field_paragraphs[0][subform][field_reusable_paragraph][0][target_id]' => 'Test usage nested paragraph',
];
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle('Test content');
$this
->drupalGet('admin/content/paragraphs');
$this
->clickLink('Test usage nested paragraph');
$this
->clickLink('Usage');
$assert_session
->pageTextContains('Entity usage information for Test usage nested paragraph');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(1)', 'Test content > field_paragraphs');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(2)', 'Paragraph');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(3)', 'English');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(4)', 'Reusable paragraph');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(5)', 'Published');
$assert_session
->elementContains('css', '.breadcrumb ol li:nth-child(1)', 'Home');
$assert_session
->elementContains('css', '.breadcrumb ol li:nth-child(2)', 'Paragraphs library');
$assert_session
->elementContains('css', '.breadcrumb ol li:nth-child(3)', 'Test usage nested paragraph');
$node = $this
->drupalGetNodeByTitle('Test content');
$this
->drupalGet($node
->toUrl('edit-form'));
$this
->submitForm([], 'Unlink from library');
$this
->submitForm([
'revision' => TRUE,
], 'Save');
$this
->drupalGet('admin/content/paragraphs');
$this
->clickLink('Test usage nested paragraph');
$this
->clickLink('Usage');
$assert_session
->pageTextContains('Entity usage information for Test usage nested paragraph');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(1)', 'Test content > field_paragraphs (previous revision)');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(2)', 'Paragraph');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(3)', 'English');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(4)', 'Reusable paragraph');
$assert_session
->elementContains('css', 'table tbody tr td:nth-child(5)', 'Published');
}
public function testLibraryItemDeleteWarningMessage() {
$page = $this
->getSession()
->getPage();
$assert_session = $this
->assertSession();
$this
->drupalGet('admin/content/paragraphs/add/default');
$page
->pressButton('Add text');
$edit = [
'label[0][value]' => 'Test usage warning message',
'paragraphs[0][subform][field_text][0][value]' => 'Example text.',
];
$this
->submitForm($edit, 'Save');
$assert_session
->pageTextContains('Paragraph Test usage warning message has been created.');
$this
->drupalGet('node/add/paragraphed_test');
$page
->pressButton('Add From library');
$edit = [
'title[0][value]' => 'Test content',
'field_paragraphs[0][subform][field_reusable_paragraph][0][target_id]' => 'Test usage warning message',
];
$this
->submitForm($edit, 'Save');
$node = $this
->drupalGetNodeByTitle('Test content');
$library_item = $node
->get('field_paragraphs')->entity
->get('field_reusable_paragraph')->entity;
$this
->drupalGet('/admin/content/paragraphs/' . $library_item
->id() . '/delete');
$assert_session
->pageTextContains('There are recorded usages of this entity.');
}
}