View source
<?php
namespace Drupal\Tests\paragraphs\Functional\WidgetLegacy;
use Drupal\paragraphs\Entity\ParagraphsType;
class ParagraphsTypesTest extends ParagraphsTestBase {
public function testRemoveTypesWithContent() {
$this
->loginAsAdmin();
$this
->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
$this
->addParagraphsType('paragraph_type_test');
$this
->addParagraphsType('text');
$this
->drupalGet('admin/structure/paragraphs_type');
$this
->clickLink('Delete');
$this
->assertSession()
->pageTextContains('This action cannot be undone.');
$this
->clickLink('Cancel');
$this
->drupalGet('node/add/paragraphed_test');
$this
->submitForm([], 'paragraphs_paragraph_type_test_add_more');
$edit = [
'title[0][value]' => 'test_node',
];
$this
->submitForm($edit, 'Save');
$this
->assertSession()
->pageTextContains('paragraphed_test test_node has been created.');
$this
->drupalGet('admin/structure/paragraphs_type');
$this
->clickLink('Delete');
$this
->assertSession()
->pageTextContains('paragraph_type_test Paragraphs type is used by 1 piece of content on your site. You can not remove this paragraph_type_test Paragraphs type until you have removed all from the content.');
}
public function testParagraphTypeIcon() {
$file_usage = \Drupal::service('file.usage');
$entity_repository = \Drupal::service('entity.repository');
$admin_user = $this
->drupalCreateUser([
'administer paragraphs types',
'access files overview',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/paragraphs_type/add');
$this
->assertSession()
->pageTextContains('Paragraph type icon');
$test_files = $this
->getTestFiles('image');
$fileSystem = \Drupal::service('file_system');
$edit = [
'label' => 'Test paragraph type',
'id' => 'test_paragraph_type_icon',
'files[icon_file]' => $fileSystem
->realpath($test_files[0]->uri),
];
$this
->submitForm($edit, 'Save and manage fields');
$this
->assertSession()
->pageTextContains('Saved the Test paragraph type Paragraphs type.');
$this
->drupalGet('admin/structure/paragraphs_type');
$this
->assertSession()
->responseContains('image-test.png');
$this
->clickLink('Edit');
$this
->assertSession()
->pageTextContains('image-test.png');
$paragraph_type = ParagraphsType::load('test_paragraph_type_icon');
$file = $entity_repository
->loadEntityByUuid('file', $paragraph_type
->get('icon_uuid'));
$usages = $file_usage
->listUsage($file);
$this
->assertEquals($usages['paragraphs']['paragraphs_type']['test_paragraph_type_icon'], 1);
$dependencies = $paragraph_type
->getDependencies();
$dependencies_uuid[] = explode(':', $dependencies['content'][0]);
$this
->assertEquals($paragraph_type
->get('icon_uuid'), $dependencies_uuid[0][2]);
$this
->drupalGet('admin/structure/paragraphs_type/test_paragraph_type_icon');
$this
->submitForm([], 'icon_file_remove_button');
$this
->submitForm([], 'Save');
$usages = $file_usage
->listUsage($file);
$this
->assertEquals($usages, []);
}
public function testParagraphTypeDefaultIcon() {
$entity_repository = \Drupal::service('entity.repository');
$admin_user = $this
->drupalCreateUser([
'administer paragraphs types',
'access files overview',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/paragraphs_type/add');
$this
->assertSession()
->pageTextContains('Paragraph type icon');
$test_files = $this
->getTestFiles('image');
$fileSystem = \Drupal::service('file_system');
$edit = [
'label' => 'Test paragraph type',
'id' => 'test_paragraph_type_icon',
'files[icon_file]' => $fileSystem
->realpath($test_files[0]->uri),
];
$this
->submitForm($edit, 'Save and manage fields');
$this
->assertSession()
->pageTextContains('Saved the Test paragraph type Paragraphs type.');
$paragraph_type = ParagraphsType::load('test_paragraph_type_icon');
$file = $entity_repository
->loadEntityByUuid('file', $paragraph_type
->get('icon_uuid'));
$file
->delete();
$this
->resetAll();
$this
->drupalGet('admin/structure/paragraphs_type');
$default_icon_name = 'test_paragraph_type_icon-default-icon.png';
$this
->assertSession()
->responseContains($default_icon_name);
$this
->clickLink('Edit');
$this
->assertSession()
->pageTextContains($default_icon_name);
$file = $entity_repository
->loadEntityByUuid('file', $paragraph_type
->get('icon_uuid'));
$this
->assertNotEmpty($file);
}
public function testParagraphTypeDescription() {
$admin_user = $this
->drupalCreateUser([
'administer paragraphs types',
]);
$this
->drupalLogin($admin_user);
$this
->drupalGet('admin/structure/paragraphs_type/add');
$this
->assertSession()
->pageTextContains('Description');
$label = 'Test paragraph type';
$description_markup = 'Use <em>Test paragraph type</em> to test the functionality of descriptions.';
$description_text = 'Use Test paragraph type to test the functionality of descriptions.';
$edit = [
'label' => $label,
'id' => 'test_paragraph_type_description',
'description' => $description_markup,
];
$this
->submitForm($edit, 'Save and manage fields');
$this
->assertSession()
->pageTextContains("Saved the {$label} Paragraphs type.");
$this
->drupalGet('admin/structure/paragraphs_type');
$this
->assertSession()
->pageTextContains('Description');
$this
->assertSession()
->pageTextContains($description_text);
$this
->assertSession()
->responseContains($description_markup);
$header_position = count($this
->xpath('//table/thead/tr/th[.="Description"]/preceding-sibling::th'));
$row_position = count($this
->xpath('//table/tbody/tr/td[.="' . $description_text . '"]/preceding-sibling::td'));
$this
->assertEquals($header_position, $row_position);
$this
->clickLink('Edit');
$this
->assertSession()
->responseContains('Use <em>Test paragraph type</em> to test the functionality of descriptions.');
}
}