View source
<?php
namespace Drupal\Tests\entity_usage\FunctionalJavascript;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\Tests\entity_track\Traits\EntityTrackLastEntityQueryTrait;
use Drupal\user\Entity\Role;
class RevisionsTranslationsTest extends EntityUsageJavascriptTestBase {
use EntityTrackLastEntityQueryTrait;
protected static $modules = [
'language',
'content_translation',
];
public function testRevisionsTracking() {
$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);
$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.');
$node2 = $this
->getLastEntityOfType('node', TRUE);
$node2_first_revision = $node2
->getRevisionId();
$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);
$this
->drupalGet("/node/{$node2->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', '');
$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());
$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);
$this
->drupalGet("/node/{$node2->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 1 ({$node1->id()})");
$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();
$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);
$node3 = Node::create([
'type' => 'eu_test_ct',
'title' => 'Node 3',
]);
$node3
->save();
$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()})");
$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();
$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);
$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]', '');
$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());
$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);
$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);
$node1
->delete();
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$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.');
$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()]));
$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);
}
public function testRevisionsTranslationsTracking() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
$usage_service = \Drupal::service('entity_usage.usage');
foreach ([
'es',
'ca',
] as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
$authenticated_role = Role::load('authenticated');
$authenticated_role
->grantPermission('administer content translation');
$authenticated_role
->grantPermission('translate any entity');
$authenticated_role
->grantPermission('create content translations');
$authenticated_role
->grantPermission('administer languages');
$authenticated_role
->grantPermission('administer entity usage');
$authenticated_role
->grantPermission('access entity usage statistics');
$authenticated_role
->save();
$this
->drupalGet('/admin/config/regional/content-language');
$page
->checkField('entity_types[node]');
$page
->checkField('settings[node][eu_test_ct][translatable]');
$page
->pressButton('Save configuration');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('Settings successfully updated.');
$this
->drupalGet('/admin/config/entity-usage/settings');
$page
->checkField('local_task_enabled_entity_types[entity_types][node]');
$page
->pressButton('Save configuration');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('The configuration options have been saved.');
$this
->drupalGet('/node/add/eu_test_ct');
$page
->fillField('title[0][value]', 'Node 1');
$assert_session
->elementExists('css', 'select[name="langcode[0][value]"]');
$page
->selectFieldOption('langcode[0][value]', 'en');
$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);
$this
->drupalGet('/node/add/eu_test_ct');
$page
->fillField('title[0][value]', 'Node 2');
$assert_session
->elementExists('css', 'select[name="langcode[0][value]"]');
$page
->selectFieldOption('langcode[0][value]', 'es');
$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($node2);
$this
->assertEquals([], $usage);
$this
->drupalGet('/node/add/eu_test_ct');
$page
->fillField('title[0][value]', 'Node 3');
$assert_session
->elementExists('css', 'select[name="langcode[0][value]"]');
$page
->selectFieldOption('langcode[0][value]', 'en');
$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 3 has been created.');
$node3 = $this
->getLastEntityOfType('node', TRUE);
$node3_first_revision = $node3
->getRevisionId();
$this
->drupalGet("/es/node/{$node3->id()}/translations/add/en/es");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 2 ({$node2->id()})");
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision (all languages)');
$assert_session
->checkboxChecked('Create new revision (all languages)');
$page
->pressButton('Save (this translation)');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 3 has been updated.');
$node3 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node3
->id());
$node3_second_revision = $node3
->getRevisionId();
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node3
->id() => [
[
'source_langcode' => 'en',
'source_vid' => $node3_second_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => 'en',
'source_vid' => $node3_first_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$usage = $usage_service
->listSources($node2);
$expected = [
'node' => [
$node3
->id() => [
[
'source_langcode' => 'es',
'source_vid' => $node3_second_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$this
->drupalGet("/node/{$node1->id()}/usage");
$first_row_title = $this
->xpath('//table/tbody/tr[1]/td[1]')[0];
$this
->assertEquals($node3
->label(), $first_row_title
->getText());
$first_row_type = $this
->xpath('//table/tbody/tr[1]/td[2]')[0];
$this
->assertEquals('Content', $first_row_type
->getText());
$first_row_langcode = $this
->xpath('//table/tbody/tr[1]/td[3]')[0];
$this
->assertEquals('English', $first_row_langcode
->getText());
$first_row_field_label = $this
->xpath('//table/tbody/tr[1]/td[4]')[0];
$this
->assertEquals('Related nodes', $first_row_field_label
->getText());
$assert_session
->elementNotExists('xpath', '//table/tbody/tr[2]');
$this
->drupalGet("/node/{$node2->id()}/usage");
$first_row_title = $this
->xpath('//table/tbody/tr[1]/td[1]')[0];
$this
->assertEquals($node3
->label(), $first_row_title
->getText());
$first_row_type = $this
->xpath('//table/tbody/tr[1]/td[2]')[0];
$this
->assertEquals('Content', $first_row_type
->getText());
$first_row_langcode = $this
->xpath('//table/tbody/tr[1]/td[3]')[0];
$this
->assertEquals('English', $first_row_langcode
->getText());
$first_row_field_label = $this
->xpath('//table/tbody/tr[1]/td[4]')[0];
$this
->assertEquals('Related nodes', $first_row_field_label
->getText());
$first_row_used_in = $this
->xpath('//table/tbody/tr[1]/td[6]')[0];
$this
->assertEquals('Translations or previous revisions', $first_row_used_in
->getText());
$assert_session
->elementNotExists('xpath', '//table/tbody/tr[2]');
$this
->drupalGet("/es/node/{$node3->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', '');
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision (all languages)');
$assert_session
->checkboxChecked('Create new revision (all languages)');
$page
->pressButton('Save (this translation)');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 3 has been updated.');
$node3 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node3
->id());
$node3_third_revision = $node3
->getRevisionId();
$usage = $usage_service
->listSources($node2);
$expected = [
'node' => [
$node3
->id() => [
[
'source_langcode' => 'es',
'source_vid' => $node3_second_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node3
->id() => [
[
'source_langcode' => 'en',
'source_vid' => $node3_third_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => 'en',
'source_vid' => $node3_second_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => 'en',
'source_vid' => $node3_first_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$this
->drupalGet("/es/node/{$node3->id()}/edit");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 2 ({$node2->id()})");
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision (all languages)');
$assert_session
->checkboxChecked('Create new revision (all languages)');
$page
->pressButton('Save (this translation)');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 3 has been updated.');
$node3 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node3
->id());
$node3_fourth_revision = $node3
->getRevisionId();
$usage = $usage_service
->listSources($node2);
$expected = [
'node' => [
$node3
->id() => [
[
'source_langcode' => 'es',
'source_vid' => $node3_fourth_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => 'es',
'source_vid' => $node3_second_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$this
->drupalGet("/es/node/{$node3->id()}/delete");
$assert_session
->pageTextContains('Are you sure you want to delete the Spanish translation of the content');
$page
->pressButton('Delete Spanish translation');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('has been deleted');
$usage = $usage_service
->listSources($node2);
$this
->assertEquals([], $usage);
$usage = $usage_service
->listSources($node1);
$expected = [
'node' => [
$node3
->id() => [
[
'source_langcode' => 'en',
'source_vid' => $node3_fourth_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => 'en',
'source_vid' => $node3_third_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => 'en',
'source_vid' => $node3_second_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
[
'source_langcode' => 'en',
'source_vid' => $node3_first_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$this
->drupalGet("/es/node/{$node3->id()}/translations/add/en/es");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 2 ({$node2->id()})");
$revision_tab = $page
->find('css', 'a[href="#edit-revision-information"]');
$revision_tab
->click();
$page
->checkField('Create new revision (all languages)');
$assert_session
->checkboxChecked('Create new revision (all languages)');
$page
->pressButton('Save (this translation)');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('eu_test_ct Node 3 has been updated.');
$node3 = \Drupal::entityTypeManager()
->getStorage('node')
->loadUnchanged($node3
->id());
$node3_fifth_revision = $node3
->getRevisionId();
$usage = $usage_service
->listSources($node2);
$expected = [
'node' => [
$node3
->id() => [
[
'source_langcode' => 'es',
'source_vid' => $node3_fifth_revision,
'method' => 'entity_reference',
'field_name' => 'field_eu_test_related_nodes',
'count' => 1,
],
],
],
];
$this
->assertEquals($expected, $usage);
$this
->drupalGet("/node/{$node3->id()}/delete");
$assert_session
->pageTextContains('The following content item translations will be deleted');
$page
->pressButton('Delete all translations');
$session
->wait(500);
$this
->saveHtmlOutput();
$assert_session
->pageTextContains('has been deleted');
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$usage = $usage_service
->listSources($node2);
$this
->assertEquals([], $usage);
}
}