ModerationStateFieldItemListTest.php 12.2 KB
Newer Older
1 2 3 4 5
<?php

namespace Drupal\Tests\content_moderation\Kernel;

use Drupal\KernelTests\KernelTestBase;
6
use Drupal\language\Entity\ConfigurableLanguage;
7 8
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
9
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
10
use Drupal\workflows\Entity\Workflow;
11 12 13 14 15 16 17 18

/**
 * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
 *
 * @group content_moderation
 */
class ModerationStateFieldItemListTest extends KernelTestBase {

19 20
  use ContentModerationTestTrait;

21 22 23
  /**
   * {@inheritdoc}
   */
24
  public static $modules = [
25 26 27 28 29
    'node',
    'content_moderation',
    'user',
    'system',
    'language',
30
    'workflows',
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49
  ];

  /**
   * @var \Drupal\node\NodeInterface
   */
  protected $testNode;

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

    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('node');
    $this->installEntitySchema('user');
    $this->installEntitySchema('content_moderation_state');
    $this->installConfig('content_moderation');

50 51 52 53
    NodeType::create([
      'type' => 'unmoderated',
    ])->save();

54 55 56 57
    $node_type = NodeType::create([
      'type' => 'example',
    ]);
    $node_type->save();
58
    $workflow = $this->createEditorialWorkflow();
59 60 61
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
    $workflow->save();

62 63 64 65 66 67 68
    $this->testNode = Node::create([
      'type' => 'example',
      'title' => 'Test title',
    ]);
    $this->testNode->save();
    \Drupal::entityTypeManager()->getStorage('node')->resetCache();
    $this->testNode = Node::load($this->testNode->id());
69 70

    ConfigurableLanguage::createFromLangcode('de')->save();
71 72 73 74 75 76
  }

  /**
   * Test the field item list when accessing an index.
   */
  public function testArrayIndex() {
77 78
    $this->assertFalse($this->testNode->isPublished());
    $this->assertEquals('draft', $this->testNode->moderation_state[0]->value);
79 80 81 82 83 84 85 86
  }

  /**
   * Test the field item list when iterating.
   */
  public function testArrayIteration() {
    $states = [];
    foreach ($this->testNode->moderation_state as $item) {
87
      $states[] = $item->value;
88
    }
89
    $this->assertEquals(['draft'], $states);
90 91
  }

92 93 94 95 96 97 98 99 100 101 102 103
  /**
   * @covers ::getValue
   */
  public function testGetValue() {
    $this->assertEquals([['value' => 'draft']], $this->testNode->moderation_state->getValue());
  }

  /**
   * @covers ::get
   */
  public function testGet() {
    $this->assertEquals('draft', $this->testNode->moderation_state->get(0)->value);
104
    $this->expectException(\InvalidArgumentException::class);
105 106 107 108
    $this->testNode->moderation_state->get(2);
  }

  /**
109
   * Tests the item list when it is emptied and appended to.
110
   */
111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
  public function testEmptyStateAndAppend() {
    // This test case mimics the lifecycle of an entity that is being patched in
    // a rest resource.
    $this->testNode->moderation_state->setValue([]);
    $this->assertTrue($this->testNode->moderation_state->isEmpty());
    $this->assertEmptiedModerationFieldItemList();

    $this->testNode->moderation_state->appendItem();
    $this->assertEquals(1, $this->testNode->moderation_state->count());
    $this->assertEquals(NULL, $this->testNode->moderation_state->value);
    $this->assertEmptiedModerationFieldItemList();
  }

  /**
   * Test an empty value assigned to the field item.
   */
  public function testEmptyFieldItem() {
128
    $this->testNode->moderation_state->value = '';
129 130 131
    $this->assertEquals('', $this->testNode->moderation_state->value);
    $this->assertEmptiedModerationFieldItemList();
  }
132

133 134 135 136
  /**
   * Test an empty value assigned to the field item list.
   */
  public function testEmptyFieldItemList() {
137
    $this->testNode->moderation_state = '';
138 139 140
    $this->assertEquals('', $this->testNode->moderation_state->value);
    $this->assertEmptiedModerationFieldItemList();
  }
141

142 143 144 145
  /**
   * Test the field item when it is unset.
   */
  public function testUnsetItemList() {
146
    unset($this->testNode->moderation_state);
147 148 149
    $this->assertEquals(NULL, $this->testNode->moderation_state->value);
    $this->assertEmptiedModerationFieldItemList();
  }
150

151 152 153 154
  /**
   * Test the field item when it is assigned NULL.
   */
  public function testAssignNullItemList() {
155
    $this->testNode->moderation_state = NULL;
156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172
    $this->assertEquals(NULL, $this->testNode->moderation_state->value);
    $this->assertEmptiedModerationFieldItemList();
  }

  /**
   * Assert the set of expectations when the moderation state field is emptied.
   */
  protected function assertEmptiedModerationFieldItemList() {
    $this->assertTrue($this->testNode->moderation_state->isEmpty());
    // Test the empty value causes a violation in the entity.
    $violations = $this->testNode->validate();
    $this->assertCount(1, $violations);
    $this->assertEquals('This value should not be null.', $violations->get(0)->getMessage());
    // Test that incorrectly saving the entity regardless will not produce a
    // change in the moderation state.
    $this->testNode->save();
    $this->assertEquals('draft', Node::load($this->testNode->id())->moderation_state->value);
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  }

  /**
   * Test the list class with a non moderated entity.
   */
  public function testNonModeratedEntity() {
    $unmoderated_node = Node::create([
      'type' => 'unmoderated',
      'title' => 'Test title',
    ]);
    $unmoderated_node->save();
    $this->assertEquals(0, $unmoderated_node->moderation_state->count());

    $unmoderated_node->moderation_state = NULL;
    $this->assertEquals(0, $unmoderated_node->moderation_state->count());
188
    $this->assertCount(0, $unmoderated_node->validate());
189 190
  }

191 192
  /**
   * Tests that moderation state changes also change the related entity state.
193 194
   *
   * @dataProvider moderationStateChangesTestCases
195
   */
196 197 198 199
  public function testModerationStateChanges($initial_state, $final_state, $first_published, $first_is_default, $second_published, $second_is_default) {
    $this->testNode->moderation_state->value = $initial_state;
    $this->assertEquals($first_published, $this->testNode->isPublished());
    $this->assertEquals($first_is_default, $this->testNode->isDefaultRevision());
200 201
    $this->testNode->save();

202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 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
    $this->testNode->moderation_state->value = $final_state;
    $this->assertEquals($second_published, $this->testNode->isPublished());
    $this->assertEquals($second_is_default, $this->testNode->isDefaultRevision());
  }

  /**
   * Data provider for ::testModerationStateChanges
   */
  public function moderationStateChangesTestCases() {
    return [
      'Draft to draft' => [
        'draft',
        'draft',
        FALSE,
        TRUE,
        FALSE,
        TRUE,
      ],
      'Draft to published' => [
        'draft',
        'published',
        FALSE,
        TRUE,
        TRUE,
        TRUE,
      ],
      'Published to published' => [
        'published',
        'published',
        TRUE,
        TRUE,
        TRUE,
        TRUE,
      ],
      'Published to draft' => [
        'published',
        'draft',
        TRUE,
        TRUE,
        FALSE,
        FALSE,
      ],
    ];
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
  /**
   * Test updating the state for an entity without a workflow.
   */
  public function testEntityWithNoWorkflow() {
    $node_type = NodeType::create([
      'type' => 'example_no_workflow',
    ]);
    $node_type->save();
    $test_node = Node::create([
      'type' => 'example_no_workflow',
      'title' => 'Test node with no workflow',
    ]);
    $test_node->save();

    /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
    $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
    $workflow = $content_moderation_info->getWorkflowForEntity($test_node);
    $this->assertNull($workflow);

    $this->assertTrue($test_node->isPublished());
    $test_node->moderation_state->setValue('draft');
    // The entity is still published because there is not a workflow.
    $this->assertTrue($test_node->isPublished());
  }

272 273
  /**
   * Test the moderation_state field after an entity has been serialized.
274 275
   *
   * @dataProvider entityUnserializeTestCases
276
   */
277 278 279 280 281 282 283
  public function testEntityUnserialize($state, $default, $published) {
    $this->testNode->moderation_state->value = $state;

    $this->assertEquals($state, $this->testNode->moderation_state->value);
    $this->assertEquals($default, $this->testNode->isDefaultRevision());
    $this->assertEquals($published, $this->testNode->isPublished());

284 285
    $unserialized = unserialize(serialize($this->testNode));

286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306
    $this->assertEquals($state, $unserialized->moderation_state->value);
    $this->assertEquals($default, $unserialized->isDefaultRevision());
    $this->assertEquals($published, $unserialized->isPublished());
  }

  /**
   * Test cases for ::testEntityUnserialize.
   */
  public function entityUnserializeTestCases() {
    return [
      'Default draft state' => [
        'draft',
        TRUE,
        FALSE,
      ],
      'Non-default published state' => [
        'published',
        TRUE,
        TRUE,
      ],
    ];
307 308
  }

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
  /**
   * Test saving a moderated node with an existing ID.
   *
   * @dataProvider moderatedEntityWithExistingIdTestCases
   */
  public function testModeratedEntityWithExistingId($state) {
    $node = Node::create([
      'title' => 'Test title',
      'type' => 'example',
      'nid' => 999,
      'moderation_state' => $state,
    ]);
    $node->save();
    $this->assertEquals($state, $node->moderation_state->value);
  }

  /**
   * Test cases for ::testModeratedEntityWithExistingId.
   */
  public function moderatedEntityWithExistingIdTestCases() {
    return [
      'Draft non-default state' => [
        'draft',
      ],
      'Published default state' => [
        'published',
      ],
    ];
  }

339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
  /**
   * Test customising the default moderation state.
   */
  public function testWorkflowCustomisedInitialState() {
    $workflow = Workflow::load('editorial');
    $configuration = $workflow->getTypePlugin()->getConfiguration();

    // Test a node for a workflow that hasn't been updated to include the
    // 'default_moderation_state' setting. We must be backwards compatible with
    // configuration that was exported before this change was introduced.
    $this->assertFalse(isset($configuration['default_moderation_state']));
    $legacy_configuration_node = Node::create([
      'title' => 'Test title',
      'type' => 'example',
    ]);
    $this->assertEquals('draft', $legacy_configuration_node->moderation_state->value);
    $legacy_configuration_node->save();
    $this->assertEquals('draft', $legacy_configuration_node->moderation_state->value);

    $configuration['default_moderation_state'] = 'published';
    $workflow->getTypePlugin()->setConfiguration($configuration);
    $workflow->save();

    $updated_default_node = Node::create([
      'title' => 'Test title',
      'type' => 'example',
    ]);
    $this->assertEquals('published', $updated_default_node->moderation_state->value);
    $legacy_configuration_node->save();
    $this->assertEquals('published', $updated_default_node->moderation_state->value);
  }

371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403
  /**
   * Test the field item list when used with existing unmoderated content.
   */
  public function testWithExistingUnmoderatedContent() {
    $node = Node::create([
      'title' => 'Test title',
      'type' => 'unmoderated',
    ]);
    $node->save();
    $translation = $node->addTranslation('de', $node->toArray());
    $translation->title = 'Translated';
    $translation->save();

    $workflow = Workflow::load('editorial');
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'unmoderated');
    $workflow->save();

    // After enabling moderation, both the original node and translation should
    // have a published moderation state.
    $node = Node::load($node->id());
    $translation = $node->getTranslation('de');
    $this->assertEquals('published', $node->moderation_state->value);
    $this->assertEquals('published', $translation->moderation_state->value);

    // After the node has been updated, both the original node and translation
    // should still have a value.
    $node->title = 'Updated title';
    $node->save();
    $translation = $node->getTranslation('de');
    $this->assertEquals('published', $node->moderation_state->value);
    $this->assertEquals('published', $translation->moderation_state->value);
  }

404
}