<?php
namespace Drupal\Tests\paragraphs\Kernel;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\Tests\paragraphs\FunctionalJavascript\ParagraphsTestBaseTrait;
use Drupal\Tests\paragraphs\Traits\ParagraphsLastEntityQueryTrait;
class ParagraphsEntityMethodsTest extends KernelTestBase {
use ParagraphsTestBaseTrait;
protected static $modules = [
'paragraphs',
'node',
'user',
'system',
'field',
'entity_reference_revisions',
'file',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('node');
$this
->installEntitySchema('paragraph');
$this
->installSchema('system', [
'sequences',
]);
$this
->installSchema('node', [
'node_access',
]);
\Drupal::moduleHandler()
->loadInclude('paragraphs', 'install');
}
public function testParagraphLabel() {
$this
->addParagraphedContentType('paragraphed_test');
$paragraph_type = 'text_paragraph';
$this
->addParagraphsType($paragraph_type);
$this
->addFieldtoParagraphType($paragraph_type, 'field_text', 'string');
$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');
$paragraph = $storage
->loadUnchanged($paragraph
->id());
$this
->assertEquals('Test Node > field_paragraphs', $paragraph
->label());
$node
->set('field_paragraphs', []);
$node
->setNewRevision(TRUE);
$node
->save();
$paragraph = $storage
->loadUnchanged($paragraph
->id());
$this
->assertEquals('Test Node > field_paragraphs (previous revision)', $paragraph
->label());
$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());
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());
}
}