3 namespace Drupal\Tests\node\Functional;
6 * Tests all the different buttons on the node form.
10 class NodeFormButtonsTest extends NodeTestBase {
12 use AssertButtonsTrait;
15 * A normal logged in user.
17 * @var \Drupal\user\UserInterface
22 * A user with permission to bypass access content.
24 * @var \Drupal\user\UserInterface
28 protected function setUp() {
31 // Create a user that has no access to change the state of the node.
32 $this->webUser = $this->drupalCreateUser(['create article content', 'edit own article content']);
33 // Create a user that has access to change the state of the node.
34 $this->adminUser = $this->drupalCreateUser(['administer nodes', 'bypass node access']);
38 * Tests that the right buttons are displayed for saving nodes.
40 public function testNodeFormButtons() {
41 $node_storage = $this->container->get('entity.manager')->getStorage('node');
42 // Log in as administrative user.
43 $this->drupalLogin($this->adminUser);
45 // Verify the buttons on a node add form.
46 $this->drupalGet('node/add/article');
47 $this->assertButtons([t('Save and publish'), t('Save as unpublished')]);
49 // Save the node and assert it's published after clicking
50 // 'Save and publish'.
51 $edit = ['title[0][value]' => $this->randomString()];
52 $this->drupalPostForm('node/add/article', $edit, t('Save and publish'));
55 $node_1 = $node_storage->load(1);
56 $this->assertTrue($node_1->isPublished(), 'Node is published');
58 // Verify the buttons on a node edit form.
59 $this->drupalGet('node/' . $node_1->id() . '/edit');
60 $this->assertButtons([t('Save and keep published'), t('Save and unpublish')]);
62 // Save the node and verify it's still published after clicking
63 // 'Save and keep published'.
64 $this->drupalPostForm(NULL, $edit, t('Save and keep published'));
65 $node_storage->resetCache([1]);
66 $node_1 = $node_storage->load(1);
67 $this->assertTrue($node_1->isPublished(), 'Node is published');
69 // Save the node and verify it's unpublished after clicking
70 // 'Save and unpublish'.
71 $this->drupalPostForm('node/' . $node_1->id() . '/edit', $edit, t('Save and unpublish'));
72 $node_storage->resetCache([1]);
73 $node_1 = $node_storage->load(1);
74 $this->assertFalse($node_1->isPublished(), 'Node is unpublished');
76 // Verify the buttons on an unpublished node edit screen.
77 $this->drupalGet('node/' . $node_1->id() . '/edit');
78 $this->assertButtons([t('Save and keep unpublished'), t('Save and publish')]);
80 // Create a node as a normal user.
81 $this->drupalLogout();
82 $this->drupalLogin($this->webUser);
84 // Verify the buttons for a normal user.
85 $this->drupalGet('node/add/article');
86 $this->assertButtons([t('Save')], FALSE);
89 $edit = ['title[0][value]' => $this->randomString()];
90 $this->drupalPostForm('node/add/article', $edit, t('Save'));
91 $node_2 = $node_storage->load(2);
92 $this->assertTrue($node_2->isPublished(), 'Node is published');
94 // Log in as an administrator and unpublish the node that just
95 // was created by the normal user.
96 $this->drupalLogout();
97 $this->drupalLogin($this->adminUser);
98 $this->drupalPostForm('node/' . $node_2->id() . '/edit', [], t('Save and unpublish'));
99 $node_storage->resetCache([2]);
100 $node_2 = $node_storage->load(2);
101 $this->assertFalse($node_2->isPublished(), 'Node is unpublished');
103 // Log in again as the normal user, save the node and verify
104 // it's still unpublished.
105 $this->drupalLogout();
106 $this->drupalLogin($this->webUser);
107 $this->drupalPostForm('node/' . $node_2->id() . '/edit', [], t('Save'));
108 $node_storage->resetCache([2]);
109 $node_2 = $node_storage->load(2);
110 $this->assertFalse($node_2->isPublished(), 'Node is still unpublished');
111 $this->drupalLogout();
113 // Set article content type default to unpublished. This will change the
114 // the initial order of buttons and/or status of the node when creating
116 $fields = \Drupal::entityManager()->getFieldDefinitions('node', 'article');
117 $fields['status']->getConfig('article')
118 ->setDefaultValue(FALSE)
121 // Verify the buttons on a node add form for an administrator.
122 $this->drupalLogin($this->adminUser);
123 $this->drupalGet('node/add/article');
124 $this->assertButtons([t('Save as unpublished'), t('Save and publish')]);
126 // Verify the node is unpublished by default for a normal user.
127 $this->drupalLogout();
128 $this->drupalLogin($this->webUser);
129 $edit = ['title[0][value]' => $this->randomString()];
130 $this->drupalPostForm('node/add/article', $edit, t('Save'));
131 $node_3 = $node_storage->load(3);
132 $this->assertFalse($node_3->isPublished(), 'Node is unpublished');