View source
<?php
namespace Drupal\Tests\entity_usage\FunctionalJavascript;
use Drupal\field\Entity\FieldConfig;
use Drupal\field\Entity\FieldStorageConfig;
use Drupal\node\Entity\Node;
use Drupal\Tests\entity_usage\Traits\EntityUsageLastEntityQueryTrait;
use Drupal\Tests\entity_usage\Traits\PostRequestTrackingTrait;
class EmbeddedContentTest extends EntityUsageJavascriptTestBase {
use EntityUsageLastEntityQueryTrait;
use PostRequestTrackingTrait;
public function testEntityEmbed() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$usage_service = \Drupal::service('entity_usage.usage');
$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);
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$embedded_text = '<drupal-entity data-embed-button="node" data-entity-embed-display="entity_reference:entity_reference_label" data-entity-embed-display-settings="{"link":1}" data-entity-type="node" data-entity-uuid="' . $node1
->uuid() . '"></drupal-entity>';
$node2 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 2',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node2
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2
->getRevisionId(),
],
],
],
];
$this
->assertEquals($expected, $usage);
$embedded_text .= '<p>Foo bar</p>' . '<drupal-entity data-embed-button="node" data-entity-embed-display="entity_reference:entity_reference_label" data-entity-embed-display-settings="{"link":1}" data-entity-type="node" data-entity-uuid="' . $node2
->uuid() . '"></drupal-entity>';
$node3 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 3',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node3
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listTargets($node3);
$expected = [
'node' => [
$node2
->id(),
$node1
->id(),
],
];
$this
->assertEquals($expected, $usage);
}
public function testLinkIt() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$usage_service = \Drupal::service('entity_usage.usage');
$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);
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$embedded_text = '<p>foo <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="' . $node1
->uuid() . '">linked text</a> bar</p>';
$node2 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 2',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node2
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2
->getRevisionId(),
],
],
],
];
$this
->assertEquals($expected, $usage);
$embedded_text .= '<p>Foo bar</p>' . '<p>foo2 <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="' . $node2
->uuid() . '">linked text 2</a> bar 2</p>';
$node3 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 3',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node3
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listTargets($node3);
$expected = [
'node' => [
$node2
->id(),
$node1
->id(),
],
];
$this
->assertEquals($expected, $usage);
$embedded_text = '<p>foo <a data-entity-substitution="canonical" data-entity-type="node" data-entity-uuid="c7cae398-3c36-47d4-8ef0-a17902e76ff4">I do not exists</a> bar</p>';
$node4 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 4',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node4
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listTargets($node4);
$this
->assertEquals([], $usage);
}
public function testHtmlLink() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$usage_service = \Drupal::service('entity_usage.usage');
$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);
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$embedded_text = '<p>foo <a href="/node/' . $node1
->id() . '">linked text</a> bar</p>';
$node2 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 2',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node2
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2
->getRevisionId(),
],
],
],
];
$this
->assertEquals($expected, $usage);
$embedded_text .= '<p>Foo bar</p>' . '<p>foo2 <a href="/node/' . $node2
->id() . '">linked text 2</a> bar 2</p>';
$node3 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 3',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node3
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listTargets($node3);
$expected = [
'node' => [
$node2
->id(),
$node1
->id(),
],
];
$this
->assertEquals($expected, $usage);
$embedded_text = '<p>foo <a href="/node/4324">linked text foo 2</a> bar</p>';
$node4 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 4',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node4
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listTargets($node4);
$this
->assertEquals([], $usage);
$embedded_text = '<p>foo <a href="' . $node4
->toUrl()
->setAbsolute(TRUE)
->toString() . '">linked text</a> bar</p>';
$current_request = \Drupal::request();
$config = \Drupal::configFactory()
->getEditable('entity_usage.settings');
$config
->set('site_domains', [
$current_request
->getHttpHost() . $current_request
->getBasePath(),
]);
$config
->save();
$node5 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 5',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node5
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listSources($node4);
$expected = [
'node' => [
$node5
->id() => [
[
'source_langcode' => $node5
->language()
->getId(),
'source_vid' => $node5
->getRevisionId(),
],
],
],
];
$this
->assertEquals($expected, $usage);
$storage = FieldStorageConfig::create([
'field_name' => 'field_eu_test_normal_text',
'entity_type' => 'node',
'type' => 'text',
]);
$storage
->save();
FieldConfig::create([
'bundle' => 'eu_test_ct',
'entity_type' => 'node',
'field_name' => 'field_eu_test_normal_text',
'label' => 'Normal text',
])
->save();
$alias_url = '/i-am-an-alias';
\Drupal::service('path.alias_storage')
->save('/node/' . $node5
->id(), $alias_url, $node5
->language()
->getId());
$embedded_text = '<p>foo <a href="' . $alias_url . '">linked text</a> bar</p>';
$node6 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 6',
'field_eu_test_rich_text' => [
'value' => $embedded_text,
'format' => 'eu_test_text_format',
],
]);
$node6
->save();
$this
->triggerPostRequestTracking();
$usage = $usage_service
->listSources($node5);
$expected = [
'node' => [
$node6
->id() => [
[
'source_langcode' => $node6
->language()
->getId(),
'source_vid' => $node6
->getRevisionId(),
],
],
],
];
$this
->assertEquals($expected, $usage);
}
}