3 namespace Drupal\paragraphs\Tests\Classic;
5 use Drupal\paragraphs\Entity\ParagraphsType;
8 * Tests Paragraphs types.
12 class ParagraphsTypesTest extends ParagraphsTestBase {
15 * Tests the deletion of Paragraphs types.
17 public function testRemoveTypesWithContent() {
18 $this->loginAsAdmin();
20 // Add a Paragraphed test content.
21 $this->addParagraphedContentType('paragraphed_test', 'paragraphs', 'entity_reference_paragraphs');
22 $this->addParagraphsType('paragraph_type_test');
23 $this->addParagraphsType('text');
25 // Attempt to delete the content type not used yet.
26 $this->drupalGet('admin/structure/paragraphs_type');
27 $this->clickLink(t('Delete'));
28 $this->assertText('This action cannot be undone.');
29 $this->clickLink(t('Cancel'));
31 // Add a test node with a Paragraph.
32 $this->drupalGet('node/add/paragraphed_test');
33 $this->drupalPostAjaxForm(NULL, [], 'paragraphs_paragraph_type_test_add_more');
34 $edit = ['title[0][value]' => 'test_node'];
35 $this->drupalPostForm(NULL, $edit, t('Save'));
36 $this->assertText('paragraphed_test test_node has been created.');
38 // Attempt to delete the paragraph type already used.
39 $this->drupalGet('admin/structure/paragraphs_type');
40 $this->clickLink(t('Delete'));
41 $this->assertText('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.');
46 * Tests the paragraph type icon settings.
48 public function testParagraphTypeIcon() {
50 /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
51 $file_usage = \Drupal::service('file.usage');
53 /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
54 $entity_repository = \Drupal::service('entity.repository');
56 $admin_user = $this->drupalCreateUser([
57 'administer paragraphs types',
58 'access files overview',
60 $this->drupalLogin($admin_user);
61 // Add the paragraph type with icon.
62 $this->drupalGet('admin/structure/paragraphs_type/add');
63 $this->assertText('Paragraph type icon');
64 $test_files = $this->drupalGetTestFiles('image');
65 $fileSystem = \Drupal::service('file_system');
67 'label' => 'Test paragraph type',
68 'id' => 'test_paragraph_type_icon',
69 'files[icon_file]' => $fileSystem->realpath($test_files[0]->uri),
71 $this->drupalPostForm(NULL, $edit, t('Save and manage fields'));
72 $this->assertText('Saved the Test paragraph type Paragraphs type.');
74 // Check if the icon has been saved.
75 $this->drupalGet('admin/structure/paragraphs_type');
76 $this->assertRaw('image-test.png');
77 $this->clickLink('Edit');
78 $this->assertText('image-test.png');
80 // Check that the icon file usage has been registered.
81 $paragraph_type = ParagraphsType::load('test_paragraph_type_icon');
82 $file = $entity_repository->loadEntityByUuid('file', $paragraph_type->get('icon_uuid'));
83 $usages = $file_usage->listUsage($file);
84 $this->assertEqual($usages['paragraphs']['paragraphs_type']['test_paragraph_type_icon'], 1);
86 // Tests calculateDependencies method.
87 $dependencies = $paragraph_type->getDependencies();
88 $dependencies_uuid[] = explode(':', $dependencies['content'][0]);
89 $this->assertEqual($paragraph_type->get('icon_uuid'), $dependencies_uuid[0][2]);
92 $this->drupalGet('admin/structure/paragraphs_type/test_paragraph_type_icon');
93 $this->drupalPostAjaxForm(NULL, [], 'icon_file_remove_button');
94 $this->drupalPostForm(NULL, [], t('Save'));
96 // Check that the icon file usage has been deregistered.
97 $usages = $file_usage->listUsage($file);
98 $this->assertEqual($usages, []);
102 * Test the paragraph type description settings.
104 public function testParagraphTypeDescription() {
105 $admin_user = $this->drupalCreateUser(['administer paragraphs types']);
106 $this->drupalLogin($admin_user);
107 // Add the paragraph type with description.
108 $this->drupalGet('admin/structure/paragraphs_type/add');
109 $this->assertText('Description');
110 $label = 'Test paragraph type';
111 $description_markup = 'Use <em>Test paragraph type</em> to test the functionality of descriptions.';
112 $description_text = 'Use Test paragraph type to test the functionality of descriptions.';
115 'id' => 'test_paragraph_type_description',
116 'description' => $description_markup,
118 $this->drupalPostForm(NULL, $edit, t('Save and manage fields'));
119 $this->assertText("Saved the $label Paragraphs type.");
121 // Check if the description has been saved.
122 $this->drupalGet('admin/structure/paragraphs_type');
123 $this->assertText('Description');
124 $this->assertText($description_text);
125 $this->assertRaw($description_markup);
126 //Check if description is at Description column
127 $header_position = count($this->xpath('//table/thead/tr/th[.="Description"]/preceding-sibling::th'));
128 $row_position = count($this->xpath('//table/tbody/tr/td[.="' . $description_text . '"]/preceding-sibling::td'));
129 $this->assertEqual($header_position, $row_position);
130 $this->clickLink('Edit');
131 $this->assertText('Use <em>Test paragraph type</em> to test the functionality of descriptions.');