EntityOperationsTest.php 5.35 KB
Newer Older
1 2 3 4 5 6 7
<?php

namespace Drupal\Tests\content_moderation\Kernel;

use Drupal\KernelTests\KernelTestBase;
use Drupal\node\Entity\Node;
use Drupal\node\Entity\NodeType;
8
use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
9 10 11 12 13 14 15 16

/**
 * @coversDefaultClass \Drupal\content_moderation\EntityOperations
 *
 * @group content_moderation
 */
class EntityOperationsTest extends KernelTestBase {

17 18
  use ContentModerationTestTrait;

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

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->installEntitySchema('node');
    $this->installSchema('node', 'node_access');
    $this->installEntitySchema('user');
    $this->installEntitySchema('content_moderation_state');
    $this->installConfig('content_moderation');

    $this->createNodeType();
  }

  /**
45
   * Creates a page node type to test with, ensuring that it's moderated.
46 47 48 49 50 51 52
   */
  protected function createNodeType() {
    $node_type = NodeType::create([
      'type' => 'page',
      'label' => 'Page',
    ]);
    $node_type->save();
53
    $workflow = $this->createEditorialWorkflow();
54 55
    $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
    $workflow->save();
56 57 58
  }

  /**
59
   * Verifies that the process of saving pending revisions works as expected.
60
   */
61
  public function testPendingRevisions() {
62 63 64 65 66
    // Create a new node in draft.
    $page = Node::create([
      'type' => 'page',
      'title' => 'A',
    ]);
67
    $page->moderation_state->value = 'draft';
68 69 70 71
    $page->save();

    $id = $page->id();

72
    // Verify the entity saved correctly, and that the presence of pending
73
    // revisions doesn't affect the default node load.
74
    /** @var \Drupal\node\Entity\Node $page */
75 76 77 78 79 80 81
    $page = Node::load($id);
    $this->assertEquals('A', $page->getTitle());
    $this->assertTrue($page->isDefaultRevision());
    $this->assertFalse($page->isPublished());

    // Moderate the entity to published.
    $page->setTitle('B');
82
    $page->moderation_state->value = 'published';
83 84 85 86 87 88 89 90
    $page->save();

    // Verify the entity is now published and public.
    $page = Node::load($id);
    $this->assertEquals('B', $page->getTitle());
    $this->assertTrue($page->isDefaultRevision());
    $this->assertTrue($page->isPublished());

91
    // Make a new pending revision in Draft.
92
    $page->setTitle('C');
93
    $page->moderation_state->value = 'draft';
94 95 96 97 98 99
    $page->save();

    // Verify normal loads return the still-default previous version.
    $page = Node::load($id);
    $this->assertEquals('B', $page->getTitle());

100
    // Verify we can load the pending revision, even if the mechanism is kind
101 102 103 104 105 106 107 108 109 110 111
    // of gross. Note: revisionIds() is only available on NodeStorageInterface,
    // so this won't work for non-nodes. We'd need to use entity queries. This
    // is a core bug that should get fixed.
    $storage = \Drupal::entityTypeManager()->getStorage('node');
    $revision_ids = $storage->revisionIds($page);
    sort($revision_ids);
    $latest = end($revision_ids);
    $page = $storage->loadRevision($latest);
    $this->assertEquals('C', $page->getTitle());

    $page->setTitle('D');
112
    $page->moderation_state->value = 'published';
113 114 115 116 117 118 119 120 121 122
    $page->save();

    // Verify normal loads return the still-default previous version.
    $page = Node::load($id);
    $this->assertEquals('D', $page->getTitle());
    $this->assertTrue($page->isDefaultRevision());
    $this->assertTrue($page->isPublished());

    // Now check that we can immediately add a new published revision over it.
    $page->setTitle('E');
123
    $page->moderation_state->value = 'published';
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
    $page->save();

    $page = Node::load($id);
    $this->assertEquals('E', $page->getTitle());
    $this->assertTrue($page->isDefaultRevision());
    $this->assertTrue($page->isPublished());
  }

  /**
   * Verifies that a newly-created node can go straight to published.
   */
  public function testPublishedCreation() {
    // Create a new node in draft.
    $page = Node::create([
      'type' => 'page',
      'title' => 'A',
    ]);
141
    $page->moderation_state->value = 'published';
142 143 144 145 146
    $page->save();

    $id = $page->id();

    // Verify the entity saved correctly.
147
    /** @var \Drupal\node\Entity\Node $page */
148 149 150 151 152 153 154 155 156 157 158 159 160 161
    $page = Node::load($id);
    $this->assertEquals('A', $page->getTitle());
    $this->assertTrue($page->isDefaultRevision());
    $this->assertTrue($page->isPublished());
  }

  /**
   * Verifies that an unpublished state may be made the default revision.
   */
  public function testArchive() {
    $page = Node::create([
      'type' => 'page',
      'title' => $this->randomString(),
    ]);
162 163

    $page->moderation_state->value = 'published';
164 165 166 167 168 169 170 171 172 173
    $page->save();

    $id = $page->id();

    // The newly-created page should already be published.
    $page = Node::load($id);
    $this->assertTrue($page->isPublished());

    // When the page is moderated to the archived state, then the latest
    // revision should be the default revision, and it should be unpublished.
174
    $page->moderation_state->value = 'archived';
175 176 177 178 179 180 181 182 183 184
    $page->save();
    $new_revision_id = $page->getRevisionId();

    $storage = \Drupal::entityTypeManager()->getStorage('node');
    $new_revision = $storage->loadRevision($new_revision_id);
    $this->assertFalse($new_revision->isPublished());
    $this->assertTrue($new_revision->isDefaultRevision());
  }

}