View source
<?php
namespace Drupal\Tests\entity_usage\FunctionalJavascript;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\Tests\entity_usage\Traits\EntityUsageLastEntityQueryTrait;
class DynamicEntityReferenceTest extends EntityUsageJavascriptTestBase {
use EntityUsageLastEntityQueryTrait;
protected static $modules = [
'dynamic_entity_reference',
];
public function testDynamicEntityReferenceTracking() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$usage_service = \Drupal::service('entity_usage.usage');
$field_storage = FieldStorageConfig::create([
'field_name' => 'field_der1',
'entity_type' => 'node',
'type' => 'dynamic_entity_reference',
'cardinality' => FieldStorageDefinitionInterface::CARDINALITY_UNLIMITED,
'settings' => [
'exclude_entity_types' => FALSE,
'entity_type_ids' => [
'node' => 'node',
'user' => 'user',
],
],
]);
$field_storage
->save();
$field = FieldConfig::create([
'field_storage' => $field_storage,
'bundle' => 'eu_test_ct',
'settings' => [
'node' => [
'handler' => 'default:node',
'handler_settings' => [
'target_bundles' => [
'eu_test_ct' => 'eu_test_ct',
],
'sort' => [
'field' => '_none',
],
'auto_create' => FALSE,
'auto_create_bundle' => '',
],
],
'user' => [
'handler' => 'default:user',
'handler_settings' => [
'include_anonymous' => FALSE,
'filter' => [
'type' => '_none',
],
'target_bundles' => NULL,
'sort' => [
'field' => '_none',
],
'auto_create' => FALSE,
],
],
],
]);
$field
->save();
\Drupal::service('entity_display.repository')
->getFormDisplay('node', 'eu_test_ct', 'default')
->setComponent('field_der1', [
'type' => 'dynamic_entity_reference_default',
])
->save();
\Drupal::service('entity_display.repository')
->getViewDisplay('node', 'eu_test_ct', 'default')
->setComponent('field_der1', [
'type' => 'dynamic_entity_reference_label',
])
->save();
$config = \Drupal::configFactory()
->getEditable('entity_usage.settings');
$config
->set('track_enabled_target_entity_types', [
'node',
'user',
]);
$config
->save();
$this
->drupalGet('/node/add/eu_test_ct');
$page
->fillField('title[0][value]', 'Node 1');
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 1 has been created.');
$node1 = $this
->getLastEntityOfType('node', TRUE);
$user1 = $this
->drupalCreateUser([]);
$this
->drupalGet('/node/add/eu_test_ct');
$page
->fillField('title[0][value]', 'Node 2');
$page
->selectFieldOption('field_der1[0][target_type]', 'node');
$page
->fillField('field_der1[0][target_id]', "Node 1 ({$node1->id()})");
$page
->pressButton('edit-field-der1-add-more');
$this
->waitForAjaxToFinish();
$page
->selectFieldOption('field_der1[1][target_type]', 'user');
$page
->fillField('field_der1[1][target_id]', "{$user1->getDisplayName()} ({$user1->id()})");
$this
->saveHtmlOutput();
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 2 has been created.');
$node2 = $this
->getLastEntityOfType('node', TRUE);
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
0 => [
'source_langcode' => 'en',
'source_vid' => $node2
->getRevisionId(),
'method' => 'dynamic_entity_reference',
'field_name' => 'field_der1',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$usage = $usage_service
->listSources($user1);
$this
->assertEquals($expected, $usage);
$this
->drupalGet("/node/{$node2->id()}/edit");
$page
->fillField('field_der1[0][target_id]', '');
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 2 has been updated.');
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$expected = [
'node' => [
$node2
->id() => [
0 => [
'source_langcode' => 'en',
'source_vid' => $node2
->getRevisionId(),
'method' => 'dynamic_entity_reference',
'field_name' => 'field_der1',
'count' => 1,
],
],
],
];
$usage = $usage_service
->listSources($user1);
$this
->assertEquals($expected, $usage);
$node2
->delete();
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$usage = $usage_service
->listSources($user1);
$this
->assertEquals([], $usage);
}
}