ContentModerationStateTest.php 12.4 KB
Newer Older
1 2 3 4 5 6
<?php

namespace Drupal\Tests\content_moderation\Kernel;

use Drupal\content_moderation\Entity\ContentModerationState;
use Drupal\content_moderation\Entity\ModerationState;
7 8
use Drupal\entity_test\Entity\EntityTestBundle;
use Drupal\entity_test\Entity\EntityTestWithBundle;
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
use Drupal\KernelTests\KernelTestBase;
use Drupal\language\Entity\ConfigurableLanguage;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
use Drupal\node\NodeInterface;

/**
 * Tests links between a content entity and a content_moderation_state entity.
 *
 * @group content_moderation
 */
class ContentModerationStateTest extends KernelTestBase {

  /**
   * {@inheritdoc}
   */
  public static $modules = [
26
    'entity_test',
27 28 29 30 31 32
    'node',
    'content_moderation',
    'user',
    'system',
    'language',
    'content_translation',
33
    'text',
34 35 36 37 38 39 40 41 42 43 44
  ];

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();

    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
45
    $this->installEntitySchema('entity_test_with_bundle');
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    $this->installEntitySchema('content_moderation_state');
    $this->installConfig('content_moderation');
  }

  /**
   * Tests basic monolingual content moderation through the API.
   */
  public function testBasicModeration() {
    $node_type = NodeType::create([
      'type' => 'example',
    ]);
    $node_type->setThirdPartySetting('content_moderation', 'enabled', TRUE);
    $node_type->setThirdPartySetting('content_moderation', 'allowed_moderation_states', ['draft', 'published']);
    $node_type->setThirdPartySetting('content_moderation', 'default_moderation_state', 'draft');
    $node_type->save();
    $node = Node::create([
      'type' => 'example',
      'title' => 'Test title',
    ]);
    $node->save();
    $node = $this->reloadNode($node);
    $this->assertEquals('draft', $node->moderation_state->entity->id());

    $published = ModerationState::load('published');
    $node->moderation_state->entity = $published;
    $node->save();

    $node = $this->reloadNode($node);
    $this->assertEquals('published', $node->moderation_state->entity->id());

    // Change the state without saving the node.
    $content_moderation_state = ContentModerationState::load(1);
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    $content_moderation_state->save();

    $node = $this->reloadNode($node, 3);
    $this->assertEquals('draft', $node->moderation_state->entity->id());
    $this->assertFalse($node->isPublished());

    // Get the default revision.
    $node = $this->reloadNode($node);
    $this->assertTrue($node->isPublished());
    $this->assertEquals(2, $node->getRevisionId());

    $node->moderation_state->target_id = 'published';
    $node->save();

    $node = $this->reloadNode($node, 4);
    $this->assertEquals('published', $node->moderation_state->entity->id());

    // Get the default revision.
    $node = $this->reloadNode($node);
    $this->assertTrue($node->isPublished());
    $this->assertEquals(4, $node->getRevisionId());

  }

  /**
   * Tests basic multilingual content moderation through the API.
   */
  public function testMultilingualModeration() {
    // Enable French.
    ConfigurableLanguage::createFromLangcode('fr')->save();
    $node_type = NodeType::create([
      'type' => 'example',
    ]);
    $node_type->setThirdPartySetting('content_moderation', 'enabled', TRUE);
    $node_type->setThirdPartySetting('content_moderation', 'allowed_moderation_states', ['draft', 'published']);
    $node_type->setThirdPartySetting('content_moderation', 'default_moderation_state', 'draft');
    $node_type->save();
    $english_node = Node::create([
      'type' => 'example',
      'title' => 'Test title',
    ]);
    // Revision 1 (en).
    $english_node
      ->setPublished(FALSE)
      ->save();
    $this->assertEquals('draft', $english_node->moderation_state->entity->id());
    $this->assertFalse($english_node->isPublished());

    // Create a French translation.
    $french_node = $english_node->addTranslation('fr', ['title' => 'French title']);
    $french_node->setPublished(FALSE);
    // Revision 1 (fr).
    $french_node->save();
    $french_node = $this->reloadNode($english_node)->getTranslation('fr');
    $this->assertEquals('draft', $french_node->moderation_state->entity->id());
    $this->assertFalse($french_node->isPublished());

    // Move English node to create another draft.
    $english_node = $this->reloadNode($english_node);
    $english_node->moderation_state->target_id = 'draft';
    // Revision 2 (en, fr).
    $english_node->save();
    $english_node = $this->reloadNode($english_node);
    $this->assertEquals('draft', $english_node->moderation_state->entity->id());

    // French node should still be in draft.
    $french_node = $this->reloadNode($english_node)->getTranslation('fr');
    $this->assertEquals('draft', $french_node->moderation_state->entity->id());

    // Publish the French node.
    $french_node->moderation_state->target_id = 'published';
    // Revision 3 (en, fr).
    $french_node->save();
    $french_node = $this->reloadNode($french_node)->getTranslation('fr');
    $this->assertTrue($french_node->isPublished());
    $this->assertEquals('published', $french_node->moderation_state->entity->id());
    $this->assertTrue($french_node->isPublished());
    $english_node = $french_node->getTranslation('en');
    $this->assertEquals('draft', $english_node->moderation_state->entity->id());

    // Publish the English node.
    $english_node->moderation_state->target_id = 'published';
    // Revision 4 (en, fr).
    $english_node->save();
    $english_node = $this->reloadNode($english_node);
    $this->assertTrue($english_node->isPublished());

    // Move the French node back to draft.
    $french_node = $this->reloadNode($english_node)->getTranslation('fr');
    $this->assertTrue($french_node->isPublished());
    $french_node->moderation_state->target_id = 'draft';
    // Revision 5 (en, fr).
    $french_node->save();
    $french_node = $this->reloadNode($english_node, 5)->getTranslation('fr');
    $this->assertFalse($french_node->isPublished());
    $this->assertTrue($french_node->getTranslation('en')->isPublished());

    // Republish the French node.
    $french_node->moderation_state->target_id = 'published';
    // Revision 6 (en, fr).
    $french_node->save();
    $french_node = $this->reloadNode($english_node)->getTranslation('fr');
    $this->assertTrue($french_node->isPublished());

    // Change the EN state without saving the node.
    $content_moderation_state = ContentModerationState::load(1);
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    // Revision 7 (en, fr).
    $content_moderation_state->save();
    $english_node = $this->reloadNode($french_node, $french_node->getRevisionId() + 1);

    $this->assertEquals('draft', $english_node->moderation_state->entity->id());
    $french_node = $this->reloadNode($english_node)->getTranslation('fr');
    $this->assertEquals('published', $french_node->moderation_state->entity->id());

    // This should unpublish the French node.
    $content_moderation_state = ContentModerationState::load(1);
    $content_moderation_state = $content_moderation_state->getTranslation('fr');
    $content_moderation_state->set('moderation_state', 'draft');
    $content_moderation_state->setNewRevision(TRUE);
    // Revision 8 (en, fr).
    $content_moderation_state->save();

    $english_node = $this->reloadNode($english_node, $english_node->getRevisionId());
    $this->assertEquals('draft', $english_node->moderation_state->entity->id());
    $french_node = $this->reloadNode($english_node, '8')->getTranslation('fr');
    $this->assertEquals('draft', $french_node->moderation_state->entity->id());
    // Switching the moderation state to an unpublished state should update the
    // entity.
    $this->assertFalse($french_node->isPublished());

    // Get the default english node.
    $english_node = $this->reloadNode($english_node);
    $this->assertTrue($english_node->isPublished());
    $this->assertEquals(6, $english_node->getRevisionId());
  }

218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
  /**
   * Tests that a non-translatable entity type with a langcode can be moderated.
   */
  public function testNonTranslatableEntityTypeModeration() {
    // Make the 'entity_test_with_bundle' entity type revisionable.
    $entity_type = clone \Drupal::entityTypeManager()->getDefinition('entity_test_with_bundle');
    $keys = $entity_type->getKeys();
    $keys['revision'] = 'revision_id';
    $entity_type->set('entity_keys', $keys);
    \Drupal::state()->set('entity_test_with_bundle.entity_type', $entity_type);
    \Drupal::entityDefinitionUpdateManager()->applyUpdates();

    // Create a test bundle.
    $entity_test_bundle = EntityTestBundle::create([
      'id' => 'example',
    ]);
    $entity_test_bundle->setThirdPartySetting('content_moderation', 'enabled', TRUE);
    $entity_test_bundle->setThirdPartySetting('content_moderation', 'allowed_moderation_states', [
      'draft',
      'published'
    ]);
    $entity_test_bundle->setThirdPartySetting('content_moderation', 'default_moderation_state', 'draft');
    $entity_test_bundle->save();

    // Check that the tested entity type is not translatable.
    $entity_type = \Drupal::entityTypeManager()->getDefinition('entity_test_with_bundle');
    $this->assertFalse($entity_type->isTranslatable(), 'The test entity type is not translatable.');

    // Create a test entity.
    $entity_test_with_bundle = EntityTestWithBundle::create([
      'type' => 'example'
    ]);
    $entity_test_with_bundle->save();
    $this->assertEquals('draft', $entity_test_with_bundle->moderation_state->entity->id());

    $entity_test_with_bundle->moderation_state->target_id = 'published';
    $entity_test_with_bundle->save();

    $this->assertEquals('published', EntityTestWithBundle::load($entity_test_with_bundle->id())->moderation_state->entity->id());
  }

  /**
   * Tests that a non-translatable entity type without a langcode can be
   * moderated.
   */
  public function testNonLangcodeEntityTypeModeration() {
    // Make the 'entity_test_with_bundle' entity type revisionable and unset
    // the langcode entity key.
    $entity_type = clone \Drupal::entityTypeManager()->getDefinition('entity_test_with_bundle');
    $keys = $entity_type->getKeys();
    $keys['revision'] = 'revision_id';
    unset($keys['langcode']);
    $entity_type->set('entity_keys', $keys);
    \Drupal::state()->set('entity_test_with_bundle.entity_type', $entity_type);
    \Drupal::entityDefinitionUpdateManager()->applyUpdates();

    // Create a test bundle.
    $entity_test_bundle = EntityTestBundle::create([
      'id' => 'example',
    ]);
    $entity_test_bundle->setThirdPartySetting('content_moderation', 'enabled', TRUE);
    $entity_test_bundle->setThirdPartySetting('content_moderation', 'allowed_moderation_states', [
      'draft',
      'published'
    ]);
    $entity_test_bundle->setThirdPartySetting('content_moderation', 'default_moderation_state', 'draft');
    $entity_test_bundle->save();

    // Check that the tested entity type is not translatable.
    $entity_type = \Drupal::entityTypeManager()->getDefinition('entity_test_with_bundle');
    $this->assertFalse($entity_type->isTranslatable(), 'The test entity type is not translatable.');

    // Create a test entity.
    $entity_test_with_bundle = EntityTestWithBundle::create([
      'type' => 'example'
    ]);
    $entity_test_with_bundle->save();
    $this->assertEquals('draft', $entity_test_with_bundle->moderation_state->entity->id());

    $entity_test_with_bundle->moderation_state->target_id = 'published';
    $entity_test_with_bundle->save();

    $this->assertEquals('published', EntityTestWithBundle::load($entity_test_with_bundle->id())->moderation_state->entity->id());
  }

303 304 305 306 307
  /**
   * Reloads the node after clearing the static cache.
   *
   * @param \Drupal\node\NodeInterface $node
   *   The node to reload.
alexpott's avatar
alexpott committed
308
   * @param int|false $revision_id
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324
   *   The specific revision ID to load. Defaults FALSE and just loads the
   *   default revision.
   *
   * @return \Drupal\node\NodeInterface
   *   The reloaded node.
   */
  protected function reloadNode(NodeInterface $node, $revision_id = FALSE) {
    $storage = \Drupal::entityTypeManager()->getStorage('node');
    $storage->resetCache([$node->id()]);
    if ($revision_id) {
      return $storage->loadRevision($revision_id);
    }
    return $storage->load($node->id());
  }

}