public function RevisionsTranslationsTest::testRevisionsTracking in Entity Usage 8.4
Same name and namespace in other branches
- 8.2 tests/src/FunctionalJavascript/RevisionsTranslationsTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\RevisionsTranslationsTest::testRevisionsTracking()
- 8.3 tests/src/FunctionalJavascript/RevisionsTranslationsTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\RevisionsTranslationsTest::testRevisionsTracking()
Tests the tracking of nodes and revisions.
File
- tests/
src/ FunctionalJavascript/ RevisionsTranslationsTest.php, line 32
Class
- RevisionsTranslationsTest
- Tests tracking of revisions and translations.
Namespace
Drupal\Tests\entity_usage\FunctionalJavascriptCode
public function testRevisionsTracking() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
/** @var \Drupal\entity_usage\EntityUsageInterface $usage_service */
$usage_service = \Drupal::service('entity_usage.usage');
// Create node 1.
$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.');
/** @var \Drupal\node\NodeInterface $node1 */
$node1 = $this
->getLastEntityOfType('node', TRUE);
// Nobody is using this guy for now.
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
// Create node 2 referencing node 1.
$this
->drupalGet('/node/add/eu_test_ct');
$page
->fillField('title[0][value]', 'Node 2');
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 1 ({$node1->id()})");
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 2 has been created.');
/** @var \Drupal\node\NodeInterface $node2 */
$node2 = $this
->getLastEntityOfType('node', TRUE);
$node2_first_revision = $node2
->getRevisionId();
// Check that we correctly registered the relation between N2 and N1.
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_first_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
// Create new revision of N2, removing reference to N1.
$this
->drupalGet("/node/{$node2->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', '');
// Ensure we are creating a new revision.
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision');
$assert_session
->checkboxChecked('Create new revision');
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 2 has been updated.');
$node2 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node2
->id());
// We should still have the usage registered by the first revision.
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_first_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
// Create a third revision of N2, referencing N1 again.
$this
->drupalGet("/node/{$node2->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 1 ({$node1->id()})");
// Ensure we are creating a new revision.
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision');
$assert_session
->checkboxChecked('Create new revision');
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 2 has been updated.');
$node2 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node2
->id());
$node2_third_revision = $node2
->getRevisionId();
// We should now see usages of both revisions.
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_third_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_first_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
// A new target node.
$node3 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 3',
]);
$node3
->save();
// Create a new revision of N2 adding a reference to N3 as well.
$this
->drupalGet("/node/{$node2->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 1 ({$node1->id()})");
$page
->fillField('field_eu_test_related_nodes[1][target_id]', "Node 3 ({$node3->id()})");
// Ensure we are creating a new revision.
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision');
$assert_session
->checkboxChecked('Create new revision');
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 2 has been updated.');
$node2 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node2
->id());
$node2_fourth_revision = $node2
->getRevisionId();
// The new usage is there.
$usage = $usage_service
->listSources($node3);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_fourth_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
// Another revision, removing both references to N1 and N3.
$this
->drupalGet("/node/{$node2->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', '');
$page
->fillField('field_eu_test_related_nodes[1][target_id]', '');
// Ensure we are creating a new revision.
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision');
$assert_session
->checkboxChecked('Create new revision');
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 2 has been updated.');
$node2 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node2
->id());
// References to N1 and N3 are only previous revisions.
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_fourth_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_third_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_first_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$usage = $usage_service
->listSources($node3);
$expected = [
'node' => [
$node2
->id() => [
[
'source_langcode' => $node2
->language()
->getId(),
'source_vid' => $node2_fourth_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
// If we remove 4th revision, the N3 usage should also be deleted.
$this
->drupalGet("/node/{$node2->id()}/revisions/{$node2_fourth_revision}/delete");
$page
->pressButton('Delete');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('has been deleted.');
$usage = $usage_service
->listSources($node3);
$this
->assertEquals([], $usage);
// If we remove a node only being targeted in previous revisions (N1), all
// usages tracked should also be deleted.
$node1
->delete();
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
// If a node has really a lot of revisions, check that we clean all of them
// upon deletion.
$this
->drupalGet('/node/add/eu_test_ct');
$page
->fillField('title[0][value]', 'Node 4');
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 2 ({$node2->id()})");
$page
->pressButton('Save');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 4 has been created.');
/** @var \Drupal\node\NodeInterface $node4 */
$node4 = $this
->getLastEntityOfType('node', TRUE);
$num_revisions = 300;
for ($i = 1; $i < $num_revisions; $i++) {
$node4
->setNewRevision(TRUE);
$node4
->save();
}
$usage = $usage_service
->listSources($node2);
$this
->assertEquals($num_revisions, count($usage['node'][$node4
->id()]));
// Delete the node through the UI and check all usages are gone.
$this
->drupalGet("/node/{$node4->id()}/delete");
$page
->pressButton('Delete');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('has been deleted.');
$usage = $usage_service
->listSources($node2);
$this
->assertEquals([], $usage);
}