ModerationStateAccessTest.php 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Functional;
  3. use Drupal\node\Entity\Node;
  4. use Drupal\node\Entity\NodeType;
  5. use Drupal\Tests\BrowserTestBase;
  6. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  7. /**
  8. * Tests the view access control handler for moderation state entities.
  9. *
  10. * @group content_moderation
  11. */
  12. class ModerationStateAccessTest extends BrowserTestBase {
  13. use ContentModerationTestTrait;
  14. /**
  15. * {@inheritdoc}
  16. */
  17. public static $modules = [
  18. 'content_moderation_test_views',
  19. 'content_moderation',
  20. ];
  21. /**
  22. * Test the view operation access handler with the view permission.
  23. */
  24. public function testViewShowsCorrectStates() {
  25. $node_type_id = 'test';
  26. $this->createNodeType('Test', $node_type_id);
  27. $permissions = [
  28. 'access content',
  29. 'view all revisions',
  30. ];
  31. $editor1 = $this->drupalCreateUser($permissions);
  32. $this->drupalLogin($editor1);
  33. $node_1 = Node::create([
  34. 'type' => $node_type_id,
  35. 'title' => 'Draft node',
  36. 'uid' => $editor1->id(),
  37. ]);
  38. $node_1->moderation_state->value = 'draft';
  39. $node_1->save();
  40. $node_2 = Node::create([
  41. 'type' => $node_type_id,
  42. 'title' => 'Published node',
  43. 'uid' => $editor1->id(),
  44. ]);
  45. $node_2->moderation_state->value = 'published';
  46. $node_2->save();
  47. // Resave the node with a new state.
  48. $node_2->setTitle('Archived node');
  49. $node_2->moderation_state->value = 'archived';
  50. $node_2->save();
  51. // Now show the View, and confirm that the state labels are showing.
  52. $this->drupalGet('/latest');
  53. $page = $this->getSession()->getPage();
  54. $this->assertTrue($page->hasContent('Draft'));
  55. $this->assertTrue($page->hasContent('Archived'));
  56. $this->assertFalse($page->hasContent('Published'));
  57. // Now log in as an admin and test the same thing.
  58. $permissions = [
  59. 'access content',
  60. 'view all revisions',
  61. ];
  62. $admin1 = $this->drupalCreateUser($permissions);
  63. $this->drupalLogin($admin1);
  64. $this->drupalGet('/latest');
  65. $page = $this->getSession()->getPage();
  66. $this->assertEquals(200, $this->getSession()->getStatusCode());
  67. $this->assertTrue($page->hasContent('Draft'));
  68. $this->assertTrue($page->hasContent('Archived'));
  69. $this->assertFalse($page->hasContent('Published'));
  70. }
  71. /**
  72. * Creates a new node type.
  73. *
  74. * @param string $label
  75. * The human-readable label of the type to create.
  76. * @param string $machine_name
  77. * The machine name of the type to create.
  78. *
  79. * @return \Drupal\node\Entity\NodeType
  80. * The node type just created.
  81. */
  82. protected function createNodeType($label, $machine_name) {
  83. /** @var \Drupal\node\Entity\NodeType $node_type */
  84. $node_type = NodeType::create([
  85. 'type' => $machine_name,
  86. 'label' => $label,
  87. ]);
  88. $node_type->save();
  89. $workflow = $this->createEditorialWorkflow();
  90. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', $machine_name);
  91. $workflow->save();
  92. return $node_type;
  93. }
  94. }