View source
<?php
namespace Drupal\Tests\comment_alter\Functional;
use Drupal\Tests\comment_alter\Functional\CommentAlterTestBase;
class CommentAlterNodeReferenceTest extends CommentAlterTestBase {
public static $modules = [
'node',
'entity_reference',
];
protected $targetNode;
protected function addNodeReferenceField($cardinality) {
$referenced = $this
->createContentType();
$referencedType = $referenced
->id();
$this->targetNode = $this
->createNode([
'type' => $referenced
->id(),
]);
return $this
->addField('entity_reference', 'entity_reference_autocomplete', [
'settings' => [
'target_type' => 'node',
],
'cardinality' => $cardinality,
], [
'handler' => 'default',
'handler_settings' => [
'target_bundles' => [
$referenced
->id(),
],
'auto_create' => TRUE,
],
]);
}
public function testEntityReferenceFieldSingle() {
$field_name = $this
->addNodeReferenceField(1);
$this
->createEntityObject();
$new_node = $this
->randomMachineName();
$this
->postComment([
"comment_alter_fields[{$field_name}][0][target_id]" => $new_node,
]);
$this
->assertCommentDiff([
$field_name => [
[
NULL,
$new_node,
],
],
]);
$this
->assertCommentSettings($field_name);
$this
->assertRevisionDelete();
$this
->assertAlterableField($field_name);
}
public function testEntityReferenceFieldMultiple() {
$field_name = $this
->addNodeReferenceField(-1);
$this
->createEntityObject([
$field_name => [
'target_id' => $this->targetNode
->id(),
],
]);
$new_node = $this
->randomMachineName();
$this
->postComment([
"comment_alter_fields[{$field_name}][1][target_id]" => $new_node,
]);
$this
->assertCommentDiff([
$field_name => [
[
$this->targetNode
->getTitle(),
$this->targetNode
->getTitle(),
],
[
NULL,
$new_node,
],
],
]);
$this
->assertCommentSettings($field_name);
$this
->assertRevisionDelete();
$this
->assertAlterableField($field_name);
}
}