You are here

public function ParagraphsEntityMethodsTest::testParagraphLabel in Paragraphs 8

Tests the ::label() behavior on the paragraph entity.

File

tests/src/Kernel/ParagraphsEntityMethodsTest.php, line 52

Class

ParagraphsEntityMethodsTest
Tests some methods from the Paragraph entity.

Namespace

Drupal\Tests\paragraphs\Kernel

Code

public function testParagraphLabel() {
  $this
    ->addParagraphedContentType('paragraphed_test');
  $paragraph_type = 'text_paragraph';
  $this
    ->addParagraphsType($paragraph_type);
  $this
    ->addFieldtoParagraphType($paragraph_type, 'field_text', 'string');

  // Create a node with a paragraph.
  $paragraph = Paragraph::create([
    'type' => 'text_paragraph',
    'field_text' => 'Example text that is very long and needs to be shortened.',
  ]);
  $paragraph
    ->save();
  $node = Node::create([
    'title' => 'Test Node',
    'type' => 'paragraphed_test',
    'field_paragraphs' => [
      $paragraph,
    ],
  ]);
  $node
    ->save();
  $storage = \Drupal::entityTypeManager()
    ->getStorage('paragraph');

  // By this point the label should include the parent entity's label and the
  // field label.
  $paragraph = $storage
    ->loadUnchanged($paragraph
    ->id());
  $this
    ->assertEquals('Test Node > field_paragraphs', $paragraph
    ->label());

  // Create a new revision without the paragraph and verify the label on the
  // paragraph entity reflects that.

  /** @var \Drupal\node\NodeInterface $node */
  $node
    ->set('field_paragraphs', []);
  $node
    ->setNewRevision(TRUE);
  $node
    ->save();
  $paragraph = $storage
    ->loadUnchanged($paragraph
    ->id());
  $this
    ->assertEquals('Test Node > field_paragraphs (previous revision)', $paragraph
    ->label());

  // Delete the node and check if the label reflects that.
  $node
    ->delete();
  $paragraph = $storage
    ->loadUnchanged($paragraph
    ->id());
  $this
    ->assertEquals('Orphaned text_paragraph: Example text that is very long and needs to be sh…', $paragraph
    ->label());
  $paragraph3 = Paragraph::create([
    'type' => 'text_paragraph',
    'field_text' => 'Example text3',
  ]);
  $paragraph3
    ->save();
  $node3 = Node::create([
    'title' => 'Test Node 3',
    'type' => 'paragraphed_test',
    'field_paragraphs' => [
      $paragraph3,
    ],
  ]);
  $node3
    ->save();
  $paragraph3 = $storage
    ->loadUnchanged($paragraph3
    ->id());
  $this
    ->assertEquals('Test Node 3 > field_paragraphs', $paragraph3
    ->label());

  // If we delete the field on the node type, the paragraph becomes orphan.
  FieldStorageConfig::load('node.field_paragraphs')
    ->delete();
  \Drupal::service('entity.memory_cache')
    ->deleteAll();
  $paragraph3 = $storage
    ->loadUnchanged($paragraph3
    ->id());
  $this
    ->assertEquals('Orphaned text_paragraph: Example text3', $paragraph3
    ->label());
}