3 namespace Drupal\Tests\content_translation\Functional;
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
7 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
10 * Base class for pending revision translation tests.
12 abstract class ContentTranslationPendingRevisionTestBase extends ContentTranslationTestBase {
14 use ContentTypeCreationTrait;
15 use ContentModerationTestTrait;
20 public static $modules = ['language', 'content_translation', 'content_moderation', 'node'];
25 * @var \Drupal\Core\Entity\ContentEntityStorageInterface
30 * Permissions common to all test accounts.
34 protected $commonPermissions;
37 * The current test account.
39 * @var \Drupal\Core\Session\AccountInterface
41 protected $currentAccount;
46 protected function setUp() {
47 $this->entityTypeId = 'node';
48 $this->bundle = 'article';
50 $this->commonPermissions = [
51 'view any unpublished content',
52 "translate {$this->bundle} {$this->entityTypeId}",
53 "create content translations",
54 'use editorial transition create_new_draft',
55 'use editorial transition publish',
56 'use editorial transition archive',
57 'use editorial transition archived_draft',
58 'use editorial transition archived_published',
63 /** @var \Drupal\Core\Entity\EntityTypeManagerInterface $entity_type_manager */
64 $entity_type_manager = $this->container->get('entity_type.manager');
65 $this->storage = $entity_type_manager->getStorage($this->entityTypeId);
67 // @todo Remove this line once https://www.drupal.org/node/2945928 is fixed.
68 $this->config('node.settings')->set('use_admin_theme', '1')->save();
72 * Enables content moderation for the test entity type and bundle.
74 protected function enableContentModeration() {
75 $this->drupalLogin($this->rootUser);
76 $workflow_id = 'editorial';
77 $this->drupalGet('/admin/config/workflow/workflows');
78 $edit['bundles[' . $this->bundle . ']'] = TRUE;
79 $this->drupalPostForm('admin/config/workflow/workflows/manage/' . $workflow_id . '/type/' . $this->entityTypeId, $edit, t('Save'));
80 // Ensure the parent environment is up-to-date.
81 // @see content_moderation_workflow_insert()
82 \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
83 \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
84 /** @var \Drupal\Core\Routing\RouteBuilderInterface $router_builder */
85 $router_builder = $this->container->get('router.builder');
86 $router_builder->rebuildIfNeeded();
92 protected function getEditorPermissions() {
93 $editor_permissions = [
94 "edit any {$this->bundle} content",
95 "delete any {$this->bundle} content",
96 "view {$this->bundle} revisions",
97 "delete {$this->bundle} revisions",
99 return array_merge($editor_permissions, $this->commonPermissions);
105 protected function getTranslatorPermissions() {
106 return array_merge(parent::getTranslatorPermissions(), $this->commonPermissions);
112 protected function setupBundle() {
113 parent::setupBundle();
114 $this->createContentType(['type' => $this->bundle]);
115 $this->createEditorialWorkflow();
119 * Loads the active revision translation for the specified entity.
121 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
122 * The entity being edited.
123 * @param string $langcode
124 * The translation language code.
126 * @return \Drupal\Core\Entity\ContentEntityInterface|null
127 * The active revision translation or NULL if none could be identified.
129 protected function loadRevisionTranslation(ContentEntityInterface $entity, $langcode) {
130 // Explicitly invalidate the cache for that node, as the call below is
131 // statically cached.
132 $this->storage->resetCache([$entity->id()]);
133 $revision_id = $this->storage->getLatestTranslationAffectedRevisionId($entity->id(), $langcode);
134 /** @var \Drupal\Core\Entity\ContentEntityInterface $revision */
135 $revision = $revision_id ? $this->storage->loadRevision($revision_id) : NULL;
136 return $revision && $revision->hasTranslation($langcode) ? $revision->getTranslation($langcode) : NULL;
140 * Returns the edit URL for the specified entity.
142 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
143 * The entity being edited.
145 * @return \Drupal\Core\Url
148 protected function getEditUrl(ContentEntityInterface $entity) {
149 if ($entity->access('update', $this->loggedInUser)) {
150 $url = $entity->toUrl('edit-form');
153 $url = $entity->toUrl('drupal:content-translation-edit');
154 $url->setRouteParameter('language', $entity->language()->getId());
160 * Returns the delete translation URL for the specified entity.
162 * @param \Drupal\Core\Entity\ContentEntityInterface $entity
163 * The entity being edited.
165 * @return \Drupal\Core\Url
166 * The delete translation URL.
168 protected function getDeleteUrl(ContentEntityInterface $entity) {
169 if ($entity->access('delete', $this->loggedInUser)) {
170 $url = $entity->toUrl('delete-form');
173 $url = $entity->toUrl('drupal:content-translation-delete');
174 $url->setRouteParameter('language', $entity->language()->getId());