3 namespace Drupal\Tests\paragraphs\Kernel;
5 use Drupal\field\Entity\FieldConfig;
6 use Drupal\field\Entity\FieldStorageConfig;
7 use Drupal\KernelTests\KernelTestBase;
8 use Drupal\paragraphs\Entity\Paragraph;
9 use Drupal\paragraphs\Entity\ParagraphsType;
12 * Tests \Drupal\Paragraphs\Entity\Paragraph::isChanged().
16 class ParagraphsIsChangedTest extends KernelTestBase {
21 public static $modules = [
26 'entity_reference_revisions',
33 protected function setUp() {
35 $this->installEntitySchema('user');
36 $this->installEntitySchema('paragraph');
37 $this->installSchema('system', ['sequences']);
38 \Drupal::moduleHandler()->loadInclude('paragraphs', 'install');
40 // Create a text paragraph type.
41 $paragraph_type = ParagraphsType::create([
42 'label' => 'text_paragraph',
43 'id' => 'text_paragraph',
45 $paragraph_type->save();
46 $this->addParagraphsField('text_paragraph', 'text', 'string');
50 * Tests the functionality of the isChanged() function.
52 public function testIsChanged() {
53 // Create a paragraph.
54 $paragraph = Paragraph::create([
55 'title' => 'Paragraph',
56 'type' => 'text_paragraph',
57 'text' => 'Text Paragraph',
59 $this->assertTrue($paragraph->isChanged(), 'The paragraph is a new entity.');
61 // Save the paragraph and assert no changes.
63 $this->assertFalse($paragraph->isChanged(), 'Paragraph::isChanged() found no changes after the entity has been saved.');
65 // Update the revision author field, which should be skipped from checking
66 // for changes in Paragraph::isChanged().
67 $paragraph->setRevisionAuthorId(3);
68 $this->assertFalse($paragraph->isChanged(), 'Paragraph::isChanged() found no changes after updating revision_uid field.');
70 $paragraph->set('text', 'New text');
71 $this->assertTrue($paragraph->isChanged(), 'Paragraph::isChanged() found changes after updating text field.');
75 * Adds a field to a given paragraph type.
77 * @param string $paragraph_type_name
78 * Paragraph type name to be used.
79 * @param string $field_name
80 * Paragraphs field name to be used.
81 * @param string $field_type
83 * @param array $field_edit
84 * Edit settings for the field.
86 protected function addParagraphsField($paragraph_type_name, $field_name, $field_type, array $field_edit = []) {
87 // Add a paragraphs field.
88 $field_storage = FieldStorageConfig::create([
89 'field_name' => $field_name,
90 'entity_type' => 'paragraph',
91 'type' => $field_type,
92 'cardinality' => '-1',
93 'settings' => $field_edit,
95 $field_storage->save();
96 $field = FieldConfig::create([
97 'field_storage' => $field_storage,
98 'bundle' => $paragraph_type_name,
100 'handler' => 'default:paragraph',
101 'handler_settings' => ['target_bundles' => NULL],