<?php
namespace Drupal\Tests\paragraphs\Kernel;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\KernelTests\KernelTestBase;
use Drupal\paragraphs\Entity\Paragraph;
use Drupal\paragraphs\Entity\ParagraphsType;
class ParagraphsIsChangedTest extends KernelTestBase {
protected static $modules = [
'paragraphs',
'user',
'system',
'field',
'entity_reference_revisions',
'file',
];
protected function setUp() : void {
parent::setUp();
$this
->installEntitySchema('user');
$this
->installEntitySchema('paragraph');
$this
->installSchema('system', [
'sequences',
]);
\Drupal::moduleHandler()
->loadInclude('paragraphs', 'install');
$paragraph_type = ParagraphsType::create([
'label' => 'text_paragraph',
'id' => 'text_paragraph',
]);
$paragraph_type
->save();
$this
->addParagraphsField('text_paragraph', 'text', 'string');
}
public function testIsChanged() {
$paragraph = Paragraph::create([
'title' => 'Paragraph',
'type' => 'text_paragraph',
'text' => 'Text Paragraph',
]);
$this
->assertTrue($paragraph
->isChanged(), 'The paragraph is a new entity.');
$paragraph
->save();
$this
->assertFalse($paragraph
->isChanged(), 'Paragraph::isChanged() found no changes after the entity has been saved.');
$paragraph
->set('text', 'New text');
$this
->assertTrue($paragraph
->isChanged(), 'Paragraph::isChanged() found changes after updating text field.');
}
protected function addParagraphsField($paragraph_type_name, $field_name, $field_type, array $field_edit = []) {
$field_storage = FieldStorageConfig::create([
'field_name' => $field_name,
'entity_type' => 'paragraph',
'type' => $field_type,
'cardinality' => '-1',
'settings' => $field_edit,
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => $paragraph_type_name,
'settings' => [
'handler' => 'default:paragraph',
'handler_settings' => [
'target_bundles' => NULL,
],
],
]);
$field
->save();
}
}