ModerationStateFieldItemListTest.php 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Drupal\node\Entity\Node;
  5. use Drupal\node\Entity\NodeType;
  6. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  7. /**
  8. * @coversDefaultClass \Drupal\content_moderation\Plugin\Field\ModerationStateFieldItemList
  9. *
  10. * @group content_moderation
  11. */
  12. class ModerationStateFieldItemListTest extends KernelTestBase {
  13. use ContentModerationTestTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static $modules = [
  18. 'node',
  19. 'content_moderation',
  20. 'user',
  21. 'system',
  22. 'language',
  23. 'workflows',
  24. ];
  25. /**
  26. * @var \Drupal\node\NodeInterface
  27. */
  28. protected $testNode;
  29. /**
  30. * {@inheritdoc}
  31. */
  32. protected function setUp() {
  33. parent::setUp();
  34. $this->installSchema('node', 'node_access');
  35. $this->installEntitySchema('node');
  36. $this->installEntitySchema('user');
  37. $this->installEntitySchema('content_moderation_state');
  38. $this->installConfig('content_moderation');
  39. NodeType::create([
  40. 'type' => 'unmoderated',
  41. ])->save();
  42. $node_type = NodeType::create([
  43. 'type' => 'example',
  44. ]);
  45. $node_type->save();
  46. $workflow = $this->createEditorialWorkflow();
  47. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'example');
  48. $workflow->save();
  49. $this->testNode = Node::create([
  50. 'type' => 'example',
  51. 'title' => 'Test title',
  52. ]);
  53. $this->testNode->save();
  54. \Drupal::entityTypeManager()->getStorage('node')->resetCache();
  55. $this->testNode = Node::load($this->testNode->id());
  56. }
  57. /**
  58. * Test the field item list when accessing an index.
  59. */
  60. public function testArrayIndex() {
  61. $this->assertFalse($this->testNode->isPublished());
  62. $this->assertEquals('draft', $this->testNode->moderation_state[0]->value);
  63. }
  64. /**
  65. * Test the field item list when iterating.
  66. */
  67. public function testArrayIteration() {
  68. $states = [];
  69. foreach ($this->testNode->moderation_state as $item) {
  70. $states[] = $item->value;
  71. }
  72. $this->assertEquals(['draft'], $states);
  73. }
  74. /**
  75. * @covers ::getValue
  76. */
  77. public function testGetValue() {
  78. $this->assertEquals([['value' => 'draft']], $this->testNode->moderation_state->getValue());
  79. }
  80. /**
  81. * @covers ::get
  82. */
  83. public function testGet() {
  84. $this->assertEquals('draft', $this->testNode->moderation_state->get(0)->value);
  85. $this->setExpectedException(\InvalidArgumentException::class);
  86. $this->testNode->moderation_state->get(2);
  87. }
  88. /**
  89. * Tests the computed field when it is unset or set to an empty value.
  90. */
  91. public function testSetEmptyState() {
  92. $this->testNode->moderation_state->value = '';
  93. $this->assertEquals('draft', $this->testNode->moderation_state->value);
  94. $this->testNode->moderation_state = '';
  95. $this->assertEquals('draft', $this->testNode->moderation_state->value);
  96. unset($this->testNode->moderation_state);
  97. $this->assertEquals('draft', $this->testNode->moderation_state->value);
  98. $this->testNode->moderation_state = NULL;
  99. $this->assertEquals('draft', $this->testNode->moderation_state->value);
  100. }
  101. /**
  102. * Test the list class with a non moderated entity.
  103. */
  104. public function testNonModeratedEntity() {
  105. $unmoderated_node = Node::create([
  106. 'type' => 'unmoderated',
  107. 'title' => 'Test title',
  108. ]);
  109. $unmoderated_node->save();
  110. $this->assertEquals(0, $unmoderated_node->moderation_state->count());
  111. $unmoderated_node->moderation_state = NULL;
  112. $this->assertEquals(0, $unmoderated_node->moderation_state->count());
  113. }
  114. /**
  115. * Tests that moderation state changes also change the related entity state.
  116. *
  117. * @dataProvider moderationStateChangesTestCases
  118. */
  119. public function testModerationStateChanges($initial_state, $final_state, $first_published, $first_is_default, $second_published, $second_is_default) {
  120. $this->testNode->moderation_state->value = $initial_state;
  121. $this->assertEquals($first_published, $this->testNode->isPublished());
  122. $this->assertEquals($first_is_default, $this->testNode->isDefaultRevision());
  123. $this->testNode->save();
  124. $this->testNode->moderation_state->value = $final_state;
  125. $this->assertEquals($second_published, $this->testNode->isPublished());
  126. $this->assertEquals($second_is_default, $this->testNode->isDefaultRevision());
  127. }
  128. /**
  129. * Data provider for ::testModerationStateChanges
  130. */
  131. public function moderationStateChangesTestCases() {
  132. return [
  133. 'Draft to draft' => [
  134. 'draft',
  135. 'draft',
  136. FALSE,
  137. TRUE,
  138. FALSE,
  139. TRUE,
  140. ],
  141. 'Draft to published' => [
  142. 'draft',
  143. 'published',
  144. FALSE,
  145. TRUE,
  146. TRUE,
  147. TRUE,
  148. ],
  149. 'Published to published' => [
  150. 'published',
  151. 'published',
  152. TRUE,
  153. TRUE,
  154. TRUE,
  155. TRUE,
  156. ],
  157. 'Published to draft' => [
  158. 'published',
  159. 'draft',
  160. TRUE,
  161. TRUE,
  162. FALSE,
  163. FALSE,
  164. ],
  165. ];
  166. }
  167. /**
  168. * Test updating the state for an entity without a workflow.
  169. */
  170. public function testEntityWithNoWorkflow() {
  171. $node_type = NodeType::create([
  172. 'type' => 'example_no_workflow',
  173. ]);
  174. $node_type->save();
  175. $test_node = Node::create([
  176. 'type' => 'example_no_workflow',
  177. 'title' => 'Test node with no workflow',
  178. ]);
  179. $test_node->save();
  180. /** @var \Drupal\content_moderation\ModerationInformationInterface $content_moderation_info */
  181. $content_moderation_info = \Drupal::service('content_moderation.moderation_information');
  182. $workflow = $content_moderation_info->getWorkflowForEntity($test_node);
  183. $this->assertNull($workflow);
  184. $this->assertTrue($test_node->isPublished());
  185. $test_node->moderation_state->setValue('draft');
  186. // The entity is still published because there is not a workflow.
  187. $this->assertTrue($test_node->isPublished());
  188. }
  189. /**
  190. * Test the moderation_state field after an entity has been serialized.
  191. *
  192. * @dataProvider entityUnserializeTestCases
  193. */
  194. public function testEntityUnserialize($state, $default, $published) {
  195. $this->testNode->moderation_state->value = $state;
  196. $this->assertEquals($state, $this->testNode->moderation_state->value);
  197. $this->assertEquals($default, $this->testNode->isDefaultRevision());
  198. $this->assertEquals($published, $this->testNode->isPublished());
  199. $unserialized = unserialize(serialize($this->testNode));
  200. $this->assertEquals($state, $unserialized->moderation_state->value);
  201. $this->assertEquals($default, $unserialized->isDefaultRevision());
  202. $this->assertEquals($published, $unserialized->isPublished());
  203. }
  204. /**
  205. * Test cases for ::testEntityUnserialize.
  206. */
  207. public function entityUnserializeTestCases() {
  208. return [
  209. 'Default draft state' => [
  210. 'draft',
  211. TRUE,
  212. FALSE,
  213. ],
  214. 'Non-default published state' => [
  215. 'published',
  216. TRUE,
  217. TRUE,
  218. ],
  219. ];
  220. }
  221. /**
  222. * Test saving a moderated node with an existing ID.
  223. *
  224. * @dataProvider moderatedEntityWithExistingIdTestCases
  225. */
  226. public function testModeratedEntityWithExistingId($state) {
  227. $node = Node::create([
  228. 'title' => 'Test title',
  229. 'type' => 'example',
  230. 'nid' => 999,
  231. 'moderation_state' => $state,
  232. ]);
  233. $node->save();
  234. $this->assertEquals($state, $node->moderation_state->value);
  235. }
  236. /**
  237. * Test cases for ::testModeratedEntityWithExistingId.
  238. */
  239. public function moderatedEntityWithExistingIdTestCases() {
  240. return [
  241. 'Draft non-default state' => [
  242. 'draft',
  243. ],
  244. 'Published default state' => [
  245. 'published',
  246. ],
  247. ];
  248. }
  249. }