public function ParagraphsTypesTest::testParagraphTypeIcon

Tests the paragraph type icon settings.

File

paragraphs/tests/src/Functional/WidgetLegacy/ParagraphsTypesTest.php, line 48

Class

ParagraphsTypesTest
Tests Paragraphs types.

Namespace

Drupal\Tests\paragraphs\Functional\WidgetLegacy

Code

public function testParagraphTypeIcon() {

  /** @var \Drupal\file\FileUsage\FileUsageInterface $file_usage */
  $file_usage = \Drupal::service('file.usage');

  /** @var \Drupal\Core\Entity\EntityRepositoryInterface $entity_repository */
  $entity_repository = \Drupal::service('entity.repository');
  $admin_user = $this
    ->drupalCreateUser([
    'administer paragraphs types',
    'access files overview',
  ]);
  $this
    ->drupalLogin($admin_user);

  // Add the paragraph type with icon.
  $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.');

  // Check if the icon has been saved.
  $this
    ->drupalGet('admin/structure/paragraphs_type');
  $this
    ->assertSession()
    ->responseContains('image-test.png');
  $this
    ->clickLink('Edit');
  $this
    ->assertSession()
    ->pageTextContains('image-test.png');

  // Check that the icon file usage has been registered.
  $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);

  // Tests calculateDependencies method.
  $dependencies = $paragraph_type
    ->getDependencies();
  $dependencies_uuid[] = explode(':', $dependencies['content'][0]);
  $this
    ->assertEquals($paragraph_type
    ->get('icon_uuid'), $dependencies_uuid[0][2]);

  // Delete the icon.
  $this
    ->drupalGet('admin/structure/paragraphs_type/test_paragraph_type_icon');
  $this
    ->submitForm([], 'icon_file_remove_button');
  $this
    ->submitForm([], 'Save');

  // Check that the icon file usage has been deregistered.
  $usages = $file_usage
    ->listUsage($file);
  $this
    ->assertEquals($usages, []);
}