NodePreviewAnonymousTest.php 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. <?php
  2. namespace Drupal\Tests\node\Functional;
  3. use Drupal\Core\Session\AccountInterface;
  4. use Drupal\Tests\BrowserTestBase;
  5. use Drupal\user\Entity\Role;
  6. /**
  7. * Tests the node entity preview functionality for anonymous user.
  8. *
  9. * @group node
  10. */
  11. class NodePreviewAnonymousTest extends BrowserTestBase {
  12. /**
  13. * Enable node module to test on the preview.
  14. *
  15. * @var array
  16. */
  17. public static $modules = ['node'];
  18. /**
  19. * {@inheritdoc}
  20. */
  21. protected function setUp() {
  22. parent::setUp();
  23. // Create Basic page node type.
  24. $this->drupalCreateContentType([
  25. 'type' => 'page',
  26. 'name' => 'Basic page',
  27. 'display_submitted' => FALSE,
  28. ]);
  29. // Grant create and editing permissions to anonymous user:
  30. $anonymous_role = Role::load(AccountInterface::ANONYMOUS_ROLE);
  31. $anonymous_role->grantPermission('create page content');
  32. $anonymous_role->save();
  33. }
  34. /**
  35. * Checks the node preview functionality for anonymous users.
  36. */
  37. public function testAnonymousPagePreview() {
  38. $title_key = 'title[0][value]';
  39. $body_key = 'body[0][value]';
  40. // Fill in node creation form and preview node.
  41. $edit = [
  42. $title_key => $this->randomMachineName(),
  43. $body_key => $this->randomMachineName(),
  44. ];
  45. $this->drupalPostForm('node/add/page', $edit, t('Preview'));
  46. // Check that the preview is displaying the title, body and term.
  47. $this->assertSession()->linkExists(t('Back to content editing'));
  48. $this->assertSession()->responseContains($edit[$body_key]);
  49. $this->assertSession()->titleEquals($edit[$title_key] . ' | Drupal');
  50. }
  51. }