Updated Drupal to 8.6. This goes with the following updates because it's possible...
[yaffs-website] / web / core / modules / content_translation / tests / src / Functional / ContentTranslationPendingRevisionTestBase.php
1 <?php
2
3 namespace Drupal\Tests\content_translation\Functional;
4
5 use Drupal\Core\Entity\ContentEntityInterface;
6 use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
7 use Drupal\Tests\node\Traits\ContentTypeCreationTrait;
8
9 /**
10  * Base class for pending revision translation tests.
11  */
12 abstract class ContentTranslationPendingRevisionTestBase extends ContentTranslationTestBase {
13
14   use ContentTypeCreationTrait;
15   use ContentModerationTestTrait;
16
17   /**
18    * {@inheritdoc}
19    */
20   public static $modules = ['language', 'content_translation', 'content_moderation', 'node'];
21
22   /**
23    * The entity storage.
24    *
25    * @var \Drupal\Core\Entity\ContentEntityStorageInterface
26    */
27   protected $storage;
28
29   /**
30    * Permissions common to all test accounts.
31    *
32    * @var string[]
33    */
34   protected $commonPermissions;
35
36   /**
37    * The current test account.
38    *
39    * @var \Drupal\Core\Session\AccountInterface
40    */
41   protected $currentAccount;
42
43   /**
44    * {@inheritdoc}
45    */
46   protected function setUp() {
47     $this->entityTypeId = 'node';
48     $this->bundle = 'article';
49
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',
59     ];
60
61     parent::setUp();
62
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);
66
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();
69   }
70
71   /**
72    * Enables content moderation for the test entity type and bundle.
73    */
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();
87   }
88
89   /**
90    * {@inheritdoc}
91    */
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",
98     ];
99     return array_merge($editor_permissions, $this->commonPermissions);
100   }
101
102   /**
103    * {@inheritdoc}
104    */
105   protected function getTranslatorPermissions() {
106     return array_merge(parent::getTranslatorPermissions(), $this->commonPermissions);
107   }
108
109   /**
110    * {@inheritdoc}
111    */
112   protected function setupBundle() {
113     parent::setupBundle();
114     $this->createContentType(['type' => $this->bundle]);
115     $this->createEditorialWorkflow();
116   }
117
118   /**
119    * Loads the active revision translation for the specified entity.
120    *
121    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
122    *   The entity being edited.
123    * @param string $langcode
124    *   The translation language code.
125    *
126    * @return \Drupal\Core\Entity\ContentEntityInterface|null
127    *   The active revision translation or NULL if none could be identified.
128    */
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;
137   }
138
139   /**
140    * Returns the edit URL for the specified entity.
141    *
142    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
143    *   The entity being edited.
144    *
145    * @return \Drupal\Core\Url
146    *   The edit URL.
147    */
148   protected function getEditUrl(ContentEntityInterface $entity) {
149     if ($entity->access('update', $this->loggedInUser)) {
150       $url = $entity->toUrl('edit-form');
151     }
152     else {
153       $url = $entity->toUrl('drupal:content-translation-edit');
154       $url->setRouteParameter('language', $entity->language()->getId());
155     }
156     return $url;
157   }
158
159   /**
160    * Returns the delete translation URL for the specified entity.
161    *
162    * @param \Drupal\Core\Entity\ContentEntityInterface $entity
163    *   The entity being edited.
164    *
165    * @return \Drupal\Core\Url
166    *   The delete translation URL.
167    */
168   protected function getDeleteUrl(ContentEntityInterface $entity) {
169     if ($entity->access('delete', $this->loggedInUser)) {
170       $url = $entity->toUrl('delete-form');
171     }
172     else {
173       $url = $entity->toUrl('drupal:content-translation-delete');
174       $url->setRouteParameter('language', $entity->language()->getId());
175     }
176     return $url;
177   }
178
179 }