NodeAccessTest.php 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. <?php
  2. namespace Drupal\Tests\content_moderation\Kernel;
  3. use Drupal\KernelTests\KernelTestBase;
  4. use Drupal\node\Entity\NodeType;
  5. use Drupal\Tests\content_moderation\Traits\ContentModerationTestTrait;
  6. use Drupal\Tests\node\Traits\NodeCreationTrait;
  7. use Drupal\Tests\user\Traits\UserCreationTrait;
  8. /**
  9. * Tests with node access enabled.
  10. *
  11. * @group content_moderation
  12. */
  13. class NodeAccessTest extends KernelTestBase {
  14. use NodeCreationTrait;
  15. use UserCreationTrait;
  16. use ContentModerationTestTrait;
  17. /**
  18. * The moderation information service.
  19. *
  20. * @var \Drupal\content_moderation\ModerationInformationInterface
  21. */
  22. protected $moderationInformation;
  23. /**
  24. * {@inheritdoc}
  25. */
  26. public static $modules = [
  27. 'content_moderation',
  28. 'filter',
  29. 'node',
  30. 'node_access_test',
  31. 'system',
  32. 'user',
  33. 'workflows',
  34. ];
  35. /**
  36. * {@inheritdoc}
  37. */
  38. protected function setUp() {
  39. parent::setUp();
  40. $this->installEntitySchema('content_moderation_state');
  41. $this->installEntitySchema('node');
  42. $this->installEntitySchema('user');
  43. $this->installEntitySchema('workflow');
  44. $this->installConfig(['content_moderation', 'filter']);
  45. $this->installSchema('system', ['sequences']);
  46. $this->installSchema('node', ['node_access']);
  47. // Add a moderated node type.
  48. $node_type = NodeType::create([
  49. 'type' => 'page',
  50. 'label' => 'Page',
  51. ]);
  52. $node_type->save();
  53. $workflow = $this->createEditorialWorkflow();
  54. $workflow->getTypePlugin()->addEntityTypeAndBundle('node', 'page');
  55. $workflow->save();
  56. $this->moderationInformation = \Drupal::service('content_moderation.moderation_information');
  57. }
  58. /**
  59. * Tests for moderation information methods with node access.
  60. */
  61. public function testModerationInformation() {
  62. // Create an admin user.
  63. $user = $this->createUser([], NULL, TRUE);
  64. \Drupal::currentUser()->setAccount($user);
  65. // Create a node.
  66. $node = $this->createNode(['type' => 'page']);
  67. $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id()));
  68. $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id()));
  69. // Create a non-admin user.
  70. $user = $this->createUser();
  71. \Drupal::currentUser()->setAccount($user);
  72. $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getDefaultRevisionId('node', $node->id()));
  73. $this->assertEquals($node->getRevisionId(), $this->moderationInformation->getLatestRevisionId('node', $node->id()));
  74. }
  75. }