public function RevisionsTranslationsTest::testRevisionsTranslationsTracking in Entity Usage 8.2
Same name and namespace in other branches
- 8.4 tests/src/FunctionalJavascript/RevisionsTranslationsTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\RevisionsTranslationsTest::testRevisionsTranslationsTracking()
- 8.3 tests/src/FunctionalJavascript/RevisionsTranslationsTest.php \Drupal\Tests\entity_usage\FunctionalJavascript\RevisionsTranslationsTest::testRevisionsTranslationsTracking()
Tests the tracking of nodes with revisions and translations.
File
- tests/
src/ FunctionalJavascript/ RevisionsTranslationsTest.php, line 298
Class
- RevisionsTranslationsTest
- Tests tracking of revisions and translations.
Namespace
Drupal\Tests\entity_usage\FunctionalJavascriptCode
public function testRevisionsTranslationsTracking() {
$session = $this
->getSession();
$page = $session
->getPage();
$assert_session = $this
->assertSession();
/** @var \Drupal\entity_usage\EntityUsage $usage_service */
$usage_service = \Drupal::service('entity_usage.usage');
// Create some additional languages.
foreach ([
'es',
'ca',
] as $langcode) {
ConfigurableLanguage::createFromLangcode($langcode)
->save();
}
// Let the logged-in user do multi-lingual stuff.
/** @var \Drupal\user\RoleInterface $authenticated_role */
$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();
// Set our content type as translatable.
$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.');
// Enable the usage page controller for nodes.
$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.');
// Create a target Node 1 in EN.
$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.');
/** @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 a target Node 2 in ES.
$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.');
/** @var \Drupal\node\NodeInterface $node2 */
$node2 = $this
->getLastEntityOfType('node', TRUE);
// Nobody is using this guy for now.
$usage = $usage_service
->listSources($node2);
$this
->assertEquals([], $usage);
// Create Node 3 in EN referencing Node 1.
$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.');
/** @var \Drupal\node\NodeInterface $node3 */
$node3 = $this
->getLastEntityOfType('node', TRUE);
$node3_first_revision = $node3
->getRevisionId();
// Translate Node 3 to ES but referencing Node 2 instead.
$this
->drupalGet("/es/node/{$node3->id()}/translations/add/en/es");
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 2 ({$node2->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 (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();
// Check usages are the ones we expect.
$usage = $usage_service
->listSources($node1);
// Node 1 is referenced from both revisions of Node 3 in EN.
$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);
// Node 2 is referenced from last revision of Node 3 in ES.
$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);
// Check that the usage page of both N1 and N2 show what we expect.
$this
->drupalGet("/node/{$node1->id()}/usage");
// Only one usage is there, from the EN translation.
$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());
// There's no second row.
$assert_session
->elementNotExists('xpath', '//table/tbody/tr[2]');
$this
->drupalGet("/node/{$node2->id()}/usage");
// Only one usage is there, from the ES translation.
$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());
// There's no second row.
$assert_session
->elementNotExists('xpath', '//table/tbody/tr[2]');
// If only a translation is updated, we register correctly the new usage.
$this
->drupalGet("/es/node/{$node3->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 (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);
// Node2 is only being used in the previous ES revision.
$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);
// Node3 is being used by all revisions of the EN version.
$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);
// If a translation is deleted, only that usage is deleted.
$this
->drupalGet("/es/node/{$node3->id()}/edit");
// Reference Node 2 back in the ES translation.
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 2 ({$node2->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 (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();
// Node 2 is now referenced from last revision of Node 3 in ES, and from a
// previous revision of the ES translation.
$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);
// Bye ES translation.
$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');
// Node 2 usage shouldn't be registered anymore.
$usage = $usage_service
->listSources($node2);
$this
->assertEquals([], $usage);
// Node is 1 still tracked in all revisions in EN.
$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);
// If the main language is deleted, all records are deleted.
$this
->drupalGet("/es/node/{$node3->id()}/translations/add/en/es");
// We will add a ES translation again, so we make sure it is deleted later.
$page
->fillField('field_eu_test_related_nodes[0][target_id]', "Node 2 ({$node2->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 (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();
// The usage from the translation is there.
$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);
// Bye source node in main language.
$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');
// Everything is wiped out.
$usage = $usage_service
->listSources($node1);
$this
->assertEquals([], $usage);
$usage = $usage_service
->listSources($node2);
$this
->assertEquals([], $usage);
}