Skip to content
Snippets Groups Projects
Verified Commit 4a0bdc34 authored by Dave Long's avatar Dave Long
Browse files

Issue #2936995 by viappidu, mcaddz, acbramley, slydevil, yash.rode, sokru,...

Issue #2936995 by viappidu, mcaddz, acbramley, slydevil, yash.rode, sokru, quietone, Sandeep_k, smustgrave, amateescu, AaronMcHale, lauriii, longwave: Add taxonomy term revision UI
parent bf4e65cf
No related branches found
No related tags found
No related merge requests found
Showing
with 550 additions and 2 deletions
...@@ -8,3 +8,4 @@ name: Forums ...@@ -8,3 +8,4 @@ name: Forums
vid: forums vid: forums
description: 'Forum navigation vocabulary' description: 'Forum navigation vocabulary'
weight: -10 weight: -10
new_revision: false
...@@ -88,6 +88,7 @@ protected function getExpectedDocument() { ...@@ -88,6 +88,7 @@ protected function getExpectedDocument() {
'status' => TRUE, 'status' => TRUE,
'dependencies' => [], 'dependencies' => [],
'name' => 'Llama', 'name' => 'Llama',
'new_revision' => FALSE,
'description' => NULL, 'description' => NULL,
'weight' => 0, 'weight' => 0,
'drupal_internal__vid' => 'llama', 'drupal_internal__vid' => 'llama',
......
...@@ -5,3 +5,4 @@ name: Track changes import term ...@@ -5,3 +5,4 @@ name: Track changes import term
vid: track_changes_import_term vid: track_changes_import_term
description: '' description: ''
weight: 0 weight: 0
new_revision: false
...@@ -5,3 +5,4 @@ name: Tags ...@@ -5,3 +5,4 @@ name: Tags
vid: tags vid: tags
description: 'Use tags to group articles on similar topics into categories.' description: 'Use tags to group articles on similar topics into categories.'
weight: 0 weight: 0
new_revision: false
...@@ -40,6 +40,9 @@ taxonomy.vocabulary.*: ...@@ -40,6 +40,9 @@ taxonomy.vocabulary.*:
weight: weight:
type: integer type: integer
label: 'Weight' label: 'Weight'
new_revision:
type: boolean
label: 'Whether a new revision should be created by default'
field.formatter.settings.entity_reference_rss_category: field.formatter.settings.entity_reference_rss_category:
type: mapping type: mapping
......
...@@ -32,7 +32,12 @@ ...@@ -32,7 +32,12 @@
* "views_data" = "Drupal\taxonomy\TermViewsData", * "views_data" = "Drupal\taxonomy\TermViewsData",
* "form" = { * "form" = {
* "default" = "Drupal\taxonomy\TermForm", * "default" = "Drupal\taxonomy\TermForm",
* "delete" = "Drupal\taxonomy\Form\TermDeleteForm" * "delete" = "Drupal\taxonomy\Form\TermDeleteForm",
* "revision-delete" = \Drupal\Core\Entity\Form\RevisionDeleteForm::class,
* "revision-revert" = \Drupal\Core\Entity\Form\RevisionRevertForm::class,
* },
* "route_provider" = {
* "revision" = \Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider::class,
* }, * },
* "translation" = "Drupal\taxonomy\TermTranslationHandler" * "translation" = "Drupal\taxonomy\TermTranslationHandler"
* }, * },
...@@ -40,6 +45,7 @@ ...@@ -40,6 +45,7 @@
* data_table = "taxonomy_term_field_data", * data_table = "taxonomy_term_field_data",
* revision_table = "taxonomy_term_revision", * revision_table = "taxonomy_term_revision",
* revision_data_table = "taxonomy_term_field_revision", * revision_data_table = "taxonomy_term_field_revision",
* show_revision_ui = TRUE,
* translatable = TRUE, * translatable = TRUE,
* entity_keys = { * entity_keys = {
* "id" = "tid", * "id" = "tid",
...@@ -63,6 +69,10 @@ ...@@ -63,6 +69,10 @@
* "delete-form" = "/taxonomy/term/{taxonomy_term}/delete", * "delete-form" = "/taxonomy/term/{taxonomy_term}/delete",
* "edit-form" = "/taxonomy/term/{taxonomy_term}/edit", * "edit-form" = "/taxonomy/term/{taxonomy_term}/edit",
* "create" = "/taxonomy/term", * "create" = "/taxonomy/term",
* "revision" = "/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term_revision}/view",
* "revision-delete-form" = "/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term_revision}/delete",
* "revision-revert-form" = "/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term_revision}/revert",
* "version-history" = "/taxonomy/term/{taxonomy_term}/revisions",
* }, * },
* permission_granularity = "bundle", * permission_granularity = "bundle",
* collection_permission = "access taxonomy overview", * collection_permission = "access taxonomy overview",
......
...@@ -57,6 +57,7 @@ ...@@ -57,6 +57,7 @@
* "vid", * "vid",
* "description", * "description",
* "weight", * "weight",
* "new_revision",
* } * }
* ) * )
*/ */
...@@ -104,6 +105,13 @@ public function getDescription() { ...@@ -104,6 +105,13 @@ public function getDescription() {
return $this->description; return $this->description;
} }
/**
* The default revision setting for a vocabulary.
*
* @var bool
*/
protected $new_revision = FALSE;
/** /**
* {@inheritdoc} * {@inheritdoc}
*/ */
...@@ -161,4 +169,18 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti ...@@ -161,4 +169,18 @@ public static function postDelete(EntityStorageInterface $storage, array $entiti
} }
} }
/**
* {@inheritdoc}
*/
public function setNewRevision($new_revision) {
$this->new_revision = $new_revision;
}
/**
* {@inheritdoc}
*/
public function shouldCreateNewRevision() {
return $this->new_revision;
}
} }
...@@ -69,6 +69,15 @@ protected function buildPermissions(VocabularyInterface $vocabulary) { ...@@ -69,6 +69,15 @@ protected function buildPermissions(VocabularyInterface $vocabulary) {
"create terms in $id" => ['title' => $this->t('%vocabulary: Create terms', $args)], "create terms in $id" => ['title' => $this->t('%vocabulary: Create terms', $args)],
"delete terms in $id" => ['title' => $this->t('%vocabulary: Delete terms', $args)], "delete terms in $id" => ['title' => $this->t('%vocabulary: Delete terms', $args)],
"edit terms in $id" => ['title' => $this->t('%vocabulary: Edit terms', $args)], "edit terms in $id" => ['title' => $this->t('%vocabulary: Edit terms', $args)],
"view term revisions in $id" => ['title' => $this->t('%vocabulary: View term revisions', $args)],
"revert term revisions in $id" => [
'title' => $this->t('%vocabulary: Revert term revisions', $args),
'description' => $this->t('To revert a revision you also need permission to edit the taxonomy term.'),
],
"delete term revisions in $id" => [
'title' => $this->t('%vocabulary: Delete term revisions', $args),
'description' => $this->t('To delete a revision you also need permission to delete the taxonomy term.'),
],
]; ];
} }
......
...@@ -46,6 +46,25 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter ...@@ -46,6 +46,25 @@ protected function checkAccess(EntityInterface $entity, $operation, AccountInter
return AccessResult::neutral()->setReason("The following permissions are required: 'delete terms in {$entity->bundle()}' OR 'administer taxonomy'."); return AccessResult::neutral()->setReason("The following permissions are required: 'delete terms in {$entity->bundle()}' OR 'administer taxonomy'.");
case 'view revision':
case 'view all revisions':
if ($account->hasPermission("view term revisions in {$entity->bundle()}") || $account->hasPermission("view all taxonomy revisions")) {
return AccessResult::allowed()->cachePerPermissions();
}
return AccessResult::neutral()->setReason("The following permissions are required: 'view revisions in {$entity->bundle()}' OR 'view all taxonomy revisions'.");
case 'revert':
if (($account->hasPermission("revert term revisions in {$entity->bundle()}") && $account->hasPermission("edit terms in {$entity->bundle()}")) || $account->hasPermission("revert all taxonomy revisions")) {
return AccessResult::allowed()->cachePerPermissions();
}
return AccessResult::neutral()->setReason("The following permissions are required: 'revert term revisions in {$entity->bundle()}' OR 'revert all taxonomy revisions'.");
case 'delete revision':
if (($account->hasPermission("delete term revisions in {$entity->bundle()}") && $account->hasPermission("delete terms in {$entity->bundle()}")) || $account->hasPermission("delete all taxonomy revisions")) {
return AccessResult::allowed()->cachePerPermissions();
}
return AccessResult::neutral()->setReason("The following permissions are required: 'delete term revisions in {$entity->bundle()}' OR 'delete all taxonomy revisions'.");
default: default:
// No opinion. // No opinion.
return AccessResult::neutral()->cachePerPermissions(); return AccessResult::neutral()->cachePerPermissions();
......
...@@ -76,6 +76,13 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -76,6 +76,13 @@ public function form(array $form, FormStateInterface $form_state) {
'#default_value' => $vocabulary->getDescription(), '#default_value' => $vocabulary->getDescription(),
]; ];
$form['revision'] = [
'#type' => 'checkbox',
'#title' => $this->t('Create new revision'),
'#default_value' => $vocabulary->shouldCreateNewRevision(),
'#description' => $this->t('Create a new revision by default for this vocabulary.'),
];
// $form['langcode'] is not wrapped in an // $form['langcode'] is not wrapped in an
// if ($this->moduleHandler->moduleExists('language')) check because the // if ($this->moduleHandler->moduleExists('language')) check because the
// language_select form element works also without the language module being // language_select form element works also without the language module being
...@@ -117,6 +124,7 @@ public function form(array $form, FormStateInterface $form_state) { ...@@ -117,6 +124,7 @@ public function form(array $form, FormStateInterface $form_state) {
*/ */
public function save(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) {
$vocabulary = $this->entity; $vocabulary = $this->entity;
$vocabulary->setNewRevision($form_state->getValue(['revision']));
// Prevent leading and trailing spaces in vocabulary names. // Prevent leading and trailing spaces in vocabulary names.
$vocabulary->set('name', trim($vocabulary->label())); $vocabulary->set('name', trim($vocabulary->label()));
......
...@@ -3,11 +3,12 @@ ...@@ -3,11 +3,12 @@
namespace Drupal\taxonomy; namespace Drupal\taxonomy;
use Drupal\Core\Config\Entity\ConfigEntityInterface; use Drupal\Core\Config\Entity\ConfigEntityInterface;
use Drupal\Core\Entity\RevisionableEntityBundleInterface;
/** /**
* Provides an interface defining a taxonomy vocabulary entity. * Provides an interface defining a taxonomy vocabulary entity.
*/ */
interface VocabularyInterface extends ConfigEntityInterface { interface VocabularyInterface extends ConfigEntityInterface, RevisionableEntityBundleInterface {
/** /**
* Denotes that no term in the vocabulary has a parent. * Denotes that no term in the vocabulary has a parent.
...@@ -32,4 +33,12 @@ interface VocabularyInterface extends ConfigEntityInterface { ...@@ -32,4 +33,12 @@ interface VocabularyInterface extends ConfigEntityInterface {
*/ */
public function getDescription(); public function getDescription();
/**
* Sets whether a new revision should be created by default.
*
* @param bool $new_revision
* TRUE if a new revision should be created by default.
*/
public function setNewRevision($new_revision);
} }
...@@ -5,9 +5,33 @@ ...@@ -5,9 +5,33 @@
* Install, update and uninstall functions for the taxonomy module. * Install, update and uninstall functions for the taxonomy module.
*/ */
use Drupal\Core\Entity\Form\RevisionDeleteForm;
use Drupal\Core\Entity\Form\RevisionRevertForm;
use Drupal\Core\Entity\Routing\RevisionHtmlRouteProvider;
use Drupal\Core\StringTranslation\TranslatableMarkup;
/** /**
* Implements hook_update_last_removed(). * Implements hook_update_last_removed().
*/ */
function taxonomy_update_last_removed() { function taxonomy_update_last_removed() {
return 8702; return 8702;
} }
/**
* Update entity definition to handle revision routes.
*/
function taxonomy_update_10100(&$sandbox = NULL): TranslatableMarkup {
$entityDefinitionUpdateManager = \Drupal::entityDefinitionUpdateManager();
$definition = $entityDefinitionUpdateManager->getEntityType('taxonomy_term');
$routeProviders = $definition->get('route_provider');
$routeProviders['revision'] = RevisionHtmlRouteProvider::class;
$definition
->setFormClass('revision-delete', RevisionDeleteForm::class)
->setFormClass('revision-revert', RevisionRevertForm::class)
->set('route_provider', $routeProviders)
->setLinkTemplate('revision-delete-form', '/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term}/delete')
->setLinkTemplate('revision-revert-form', '/taxonomy/term/{taxonomy_term}/revision/{taxonomy_term}/revert')
->setLinkTemplate('version-history', '/taxonomy/term/{taxonomy_term}/revisions');
$entityDefinitionUpdateManager->updateEntityType($definition);
return \t('Added revision routes to Taxonomy Term entity type.');
}
...@@ -5,5 +5,14 @@ access taxonomy overview: ...@@ -5,5 +5,14 @@ access taxonomy overview:
title: 'Access the taxonomy vocabulary overview page' title: 'Access the taxonomy vocabulary overview page'
description: 'Get an overview of all taxonomy vocabularies.' description: 'Get an overview of all taxonomy vocabularies.'
revert all taxonomy revisions:
title: 'Revert all term revisions'
delete all taxonomy revisions:
title: 'Delete all term revisions'
view all taxonomy revisions:
title: 'View all term revisions'
permission_callbacks: permission_callbacks:
- Drupal\taxonomy\TaxonomyPermissions::permissions - Drupal\taxonomy\TaxonomyPermissions::permissions
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
* Post update functions for Taxonomy. * Post update functions for Taxonomy.
*/ */
use Drupal\Core\Config\Entity\ConfigEntityUpdater;
/** /**
* Implements hook_removed_post_updates(). * Implements hook_removed_post_updates().
*/ */
...@@ -19,3 +21,13 @@ function taxonomy_removed_post_updates() { ...@@ -19,3 +21,13 @@ function taxonomy_removed_post_updates() {
'taxonomy_post_update_clear_views_argument_validator_plugins_cache' => '10.0.0', 'taxonomy_post_update_clear_views_argument_validator_plugins_cache' => '10.0.0',
]; ];
} }
/**
* Re-save Taxonomy configurations with new_revision config.
*/
function taxonomy_post_update_set_new_revision(&$sandbox = NULL) {
\Drupal::classResolver(ConfigEntityUpdater::class)
->update($sandbox, 'taxonomy_vocabulary', function () {
return TRUE;
});
}
...@@ -55,6 +55,7 @@ protected function getExpectedNormalizedEntity() { ...@@ -55,6 +55,7 @@ protected function getExpectedNormalizedEntity() {
'name' => 'Llama', 'name' => 'Llama',
'description' => NULL, 'description' => NULL,
'weight' => 0, 'weight' => 0,
'new_revision' => FALSE,
]; ];
} }
......
<?php
declare(strict_types=1);
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Taxonomy term revision delete form test.
*
* @group taxonomy
* @coversDefaultClass \Drupal\Core\Entity\Form\RevisionDeleteForm
*/
class TaxonomyRevisionDeleteTest extends BrowserTestBase {
use TaxonomyTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'taxonomy',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected $permissions = [
'view term revisions in test',
'delete all taxonomy revisions',
];
/**
* Vocabulary for testing.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
private $vocabulary;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->vocabulary = $this->createVocabulary(['vid' => 'test', 'name' => 'Test']);
}
/**
* Tests revision delete.
*/
public function testDeleteForm(): void {
$termName = $this->randomMachineName();
$entity = Term::create([
'vid' => $this->vocabulary->id(),
'name' => $termName,
]);
$entity->setRevisionCreationTime((new \DateTimeImmutable('11 January 2009 4pm'))->getTimestamp())
->setRevisionTranslationAffected(TRUE);
$entity->setNewRevision();
$entity->save();
$revisionId = $entity->getRevisionId();
$this->drupalLogin($this->drupalCreateUser($this->permissions));
// Cannot delete latest revision.
$this->drupalGet($entity->toUrl('revision-delete-form'));
$this->assertSession()->statusCodeEquals(403);
// Create a new latest revision.
$entity
->setRevisionCreationTime((new \DateTimeImmutable('11 January 2009 5pm'))->getTimestamp())
->setRevisionTranslationAffected(TRUE)
->setNewRevision();
$entity->save();
// Reload the entity.
$revision = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
->loadRevision($revisionId);
$this->drupalGet($revision->toUrl('revision-delete-form'));
$this->assertSession()->pageTextContains('Are you sure you want to delete the revision from Sun, 01/11/2009 - 16:00?');
$this->assertSession()->buttonExists('Delete');
$this->assertSession()->linkExists('Cancel');
$countRevisions = static function (): int {
return (int) \Drupal::entityTypeManager()->getStorage('taxonomy_term')
->getQuery()
->accessCheck(FALSE)
->allRevisions()
->count()
->execute();
};
$count = $countRevisions();
$this->submitForm([], 'Delete');
$this->assertEquals($count - 1, $countRevisions());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals(sprintf('taxonomy/term/%s/revisions', $entity->id()));
$this->assertSession()->pageTextContains(sprintf('Revision from Sun, 01/11/2009 - 16:00 of Test %s has been deleted.', $termName));
$this->assertSession()->elementsCount('css', 'table tbody tr', 1);
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Taxonomy term revision form test.
*
* @group taxonomy
* @coversDefaultClass \Drupal\Core\Entity\Form\RevisionRevertForm
*/
class TaxonomyRevisionRevertTest extends BrowserTestBase {
use TaxonomyTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'taxonomy',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected $permissions = [
'view term revisions in test',
'revert all taxonomy revisions',
];
/**
* Vocabulary for testing.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
private $vocabulary;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->vocabulary = $this->createVocabulary(['vid' => 'test', 'name' => 'Test']);
}
/**
* Tests revision revert.
*/
public function testRevertForm(): void {
$termName = $this->randomMachineName();
$entity = Term::create([
'vid' => $this->vocabulary->id(),
'name' => $termName,
]);
$entity->setRevisionCreationTime((new \DateTimeImmutable('11 January 2009 4pm'))->getTimestamp())
->setRevisionTranslationAffected(TRUE);
$entity->setNewRevision();
$entity->save();
$revisionId = $entity->getRevisionId();
$this->drupalLogin($this->drupalCreateUser($this->permissions));
// Cannot revert latest revision.
$this->drupalGet($entity->toUrl('revision-revert-form'));
$this->assertSession()->statusCodeEquals(403);
// Create a new latest revision.
$entity
->setRevisionCreationTime((new \DateTimeImmutable('11 January 2009 5pm'))->getTimestamp())
->setRevisionTranslationAffected(TRUE)
->setNewRevision();
$entity->save();
// Reload the entity.
$revision = \Drupal::entityTypeManager()->getStorage('taxonomy_term')
->loadRevision($revisionId);
$this->drupalGet($revision->toUrl('revision-revert-form'));
$this->assertSession()->pageTextContains('Are you sure you want to revert to the revision from Sun, 01/11/2009 - 16:00?');
$this->assertSession()->buttonExists('Revert');
$this->assertSession()->linkExists('Cancel');
$countRevisions = static function (): int {
return (int) \Drupal::entityTypeManager()->getStorage('taxonomy_term')
->getQuery()
->accessCheck(FALSE)
->allRevisions()
->count()
->execute();
};
$count = $countRevisions();
$this->submitForm([], 'Revert');
$this->assertEquals($count + 1, $countRevisions());
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals(sprintf('taxonomy/term/%s/revisions', $entity->id()));
$this->assertSession()->pageTextContains(sprintf('Test %s has been reverted to the revision from Sun, 01/11/2009 - 16:00.', $termName));
$this->assertSession()->elementsCount('css', 'table tbody tr', 3);
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Tests the new_revision setting of taxonomy vocabularies.
*
* @group taxonomy
*/
class TaxonomyRevisionTest extends BrowserTestBase {
use TaxonomyTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'taxonomy',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* Tests default revision settings on vocabularies.
*/
public function testVocabularyTermRevision() {
$assert = $this->assertSession();
$vocabulary1 = $this->createVocabulary(['new_revision' => TRUE]);
$vocabulary2 = $this->createVocabulary(['new_revision' => FALSE]);
$user = $this->createUser([
'administer taxonomy',
]);
$term1 = $this->createTerm($vocabulary1);
$term2 = $this->createTerm($vocabulary2);
// Create some revisions so revision checkbox is visible.
$term1 = $this->createTaxonomyTermRevision($term1);
$term2 = $this->createTaxonomyTermRevision($term2);
$this->drupalLogin($user);
$this->drupalGet($term1->toUrl('edit-form'));
$assert->statusCodeEquals(200);
$assert->checkboxChecked('Create new revision');
$this->drupalGet($term2->toUrl('edit-form'));
$assert->statusCodeEquals(200);
$assert->checkboxNotChecked('Create new revision');
}
}
<?php
declare(strict_types=1);
namespace Drupal\Tests\taxonomy\Functional;
use Drupal\Tests\BrowserTestBase;
use Drupal\taxonomy\Entity\Term;
use Drupal\Tests\taxonomy\Traits\TaxonomyTestTrait;
/**
* Taxonomy term version history test.
*
* @group taxonomy
* @coversDefaultClass \Drupal\Core\Entity\Controller\VersionHistoryController
*/
class TaxonomyRevisionVersionHistoryTest extends BrowserTestBase {
use TaxonomyTestTrait;
/**
* {@inheritdoc}
*/
protected static $modules = [
'system',
'taxonomy',
];
/**
* {@inheritdoc}
*/
protected $defaultTheme = 'stark';
/**
* {@inheritdoc}
*/
protected $permissions = [
'view term revisions in test',
'revert all taxonomy revisions',
'delete all taxonomy revisions',
];
/**
* Vocabulary for testing.
*
* @var \Drupal\taxonomy\VocabularyInterface
*/
private $vocabulary;
/**
* {@inheritdoc}
*/
protected function setUp(): void {
parent::setUp();
$this->vocabulary = $this->createVocabulary(['vid' => 'test', 'name' => 'Test']);
}
/**
* Tests version history page.
*/
public function testVersionHistory(): void {
$entity = Term::create([
'vid' => $this->vocabulary->id(),
'name' => 'Test taxonomy term',
]);
$entity
->setDescription('Description 1')
->setRevisionCreationTime((new \DateTimeImmutable('1st June 2020 7am'))->getTimestamp())
->setRevisionLogMessage('first revision log')
->setRevisionUser($this->drupalCreateUser(name: 'first author'))
->setNewRevision();
$entity->save();
$entity
->setDescription('Description 2')
->setRevisionCreationTime((new \DateTimeImmutable('2nd June 2020 8am'))->getTimestamp())
->setRevisionLogMessage('second revision log')
->setRevisionUser($this->drupalCreateUser(name: 'second author'))
->setNewRevision();
$entity->save();
$entity
->setDescription('Description 3')
->setRevisionCreationTime((new \DateTimeImmutable('3rd June 2020 9am'))->getTimestamp())
->setRevisionLogMessage('third revision log')
->setRevisionUser($this->drupalCreateUser(name: 'third author'))
->setNewRevision();
$entity->save();
$this->drupalLogin($this->drupalCreateUser($this->permissions));
$this->drupalGet($entity->toUrl('version-history'));
$this->assertSession()->elementsCount('css', 'table tbody tr', 3);
// Order is newest to oldest revision by creation order.
$row1 = $this->assertSession()->elementExists('css', 'table tbody tr:nth-child(1)');
// Latest revision does not have revert or delete revision operation.
$this->assertSession()->elementNotExists('named', ['link', 'Revert'], $row1);
$this->assertSession()->elementNotExists('named', ['link', 'Delete'], $row1);
$this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(1)', 'Current revision');
$this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(1)', 'third revision log');
$this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(1)', '06/03/2020 - 09:00 by third author');
$row2 = $this->assertSession()->elementExists('css', 'table tbody tr:nth-child(2)');
$this->assertSession()->elementExists('named', ['link', 'Revert'], $row2);
$this->assertSession()->elementExists('named', ['link', 'Delete'], $row2);
$this->assertSession()->elementTextNotContains('css', 'table tbody tr:nth-child(2)', 'Current revision');
$this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(2)', 'second revision log');
$this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(2)', '06/02/2020 - 08:00 by second author');
$row3 = $this->assertSession()->elementExists('css', 'table tbody tr:nth-child(3)');
$this->assertSession()->elementExists('named', ['link', 'Revert'], $row3);
$this->assertSession()->elementExists('named', ['link', 'Delete'], $row3);
$this->assertSession()->elementTextNotContains('css', 'table tbody tr:nth-child(2)', 'Current revision');
$this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(3)', 'first revision log');
$this->assertSession()->elementTextContains('css', 'table tbody tr:nth-child(3)', '06/01/2020 - 07:00 by first author');
}
}
...@@ -7,6 +7,7 @@ ...@@ -7,6 +7,7 @@
use Drupal\Core\Language\LanguageInterface; use Drupal\Core\Language\LanguageInterface;
use Drupal\taxonomy\Entity\Vocabulary; use Drupal\taxonomy\Entity\Vocabulary;
use Drupal\taxonomy\Entity\Term; use Drupal\taxonomy\Entity\Term;
use Drupal\taxonomy\TermInterface;
use Drupal\taxonomy\VocabularyInterface; use Drupal\taxonomy\VocabularyInterface;
/** /**
...@@ -63,4 +64,20 @@ public function createTerm(VocabularyInterface $vocabulary, $values = []) { ...@@ -63,4 +64,20 @@ public function createTerm(VocabularyInterface $vocabulary, $values = []) {
return $term; return $term;
} }
/**
* Creates a new revision for a given taxonomy term.
*
* @param \Drupal\taxonomy\TermInterface $term
* A taxonomy term object.
*
* @return \Drupal\taxonomy\TermInterface
* The new taxonomy term object.
*/
protected function createTaxonomyTermRevision(TermInterface $term) {
$term->set('name', $this->randomMachineName());
$term->setNewRevision();
$term->save();
return $term;
}
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment