function ContentModerationResaveTest::testContentModerationResave
Tests that Content Moderation works with entities being resaved.
File
- 
              core/
modules/ content_moderation/ tests/ src/ Kernel/ ContentModerationResaveTest.php, line 79  
Class
- ContentModerationResaveTest
 - Tests Content Moderation with entities that get re-saved automatically.
 
Namespace
Drupal\Tests\content_moderation\KernelCode
public function testContentModerationResave() : void {
  $entity = $this->entityStorage
    ->create();
  $this->assertSame('draft', $entity->get('moderation_state')->value);
  $this->assertNull(\Drupal::state()->get('content_moderation_test_resave'));
  $this->assertNull(ContentModerationState::loadFromModeratedEntity($entity));
  $content_moderation_state_query = $this->contentModerationStateStorage
    ->getQuery()
    ->accessCheck(FALSE)
    ->count();
  $this->assertSame(0, (int) $content_moderation_state_query->execute());
  $content_moderation_state_revision_query = $this->contentModerationStateStorage
    ->getQuery()
    ->accessCheck(FALSE)
    ->allRevisions()
    ->count();
  $this->assertSame(0, (int) $content_moderation_state_revision_query->execute());
  // The test module will re-save the entity in its hook_insert()
  // implementation creating the content moderation state entity before
  // Content Moderation's hook_insert() has run for the initial save
  // operation.
  $entity->save();
  $this->assertSame('draft', $entity->get('moderation_state')->value);
  $this->assertTrue(\Drupal::state()->get('content_moderation_test_resave'));
  $content_moderation_state = ContentModerationState::loadFromModeratedEntity($entity);
  $this->assertInstanceOf(ContentModerationState::class, $content_moderation_state);
  $this->assertSame(1, (int) $content_moderation_state_query->execute());
  $this->assertSame(1, (int) $content_moderation_state_revision_query->execute());
}
Buggy or inaccurate documentation? Please file an issue. Need support? Need help programming? Connect with the Drupal community.