546a6334adf022edab4af109ab77553bdea56547
[yaffs-website] / web / core / modules / node / tests / src / Functional / NodeFormButtonsTest.php
1 <?php
2
3 namespace Drupal\Tests\node\Functional;
4
5 /**
6  * Tests all the different buttons on the node form.
7  *
8  * @group node
9  */
10 class NodeFormButtonsTest extends NodeTestBase {
11
12   use AssertButtonsTrait;
13
14   /**
15    * A normal logged in user.
16    *
17    * @var \Drupal\user\UserInterface
18    */
19   protected $webUser;
20
21   /**
22    * A user with permission to bypass access content.
23    *
24    * @var \Drupal\user\UserInterface
25    */
26   protected $adminUser;
27
28   protected function setUp() {
29     parent::setUp();
30
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']);
35   }
36
37   /**
38    * Tests that the right buttons are displayed for saving nodes.
39    */
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);
44
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')]);
48
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'));
53
54     // Get the node.
55     $node_1 = $node_storage->load(1);
56     $this->assertTrue($node_1->isPublished(), 'Node is published');
57
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')]);
61
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');
68
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');
75
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')]);
79
80     // Create a node as a normal user.
81     $this->drupalLogout();
82     $this->drupalLogin($this->webUser);
83
84     // Verify the buttons for a normal user.
85     $this->drupalGet('node/add/article');
86     $this->assertButtons([t('Save')], FALSE);
87
88     // Create the node.
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');
93
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');
102
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();
112
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
115     // a node.
116     $fields = \Drupal::entityManager()->getFieldDefinitions('node', 'article');
117     $fields['status']->getConfig('article')
118       ->setDefaultValue(FALSE)
119       ->save();
120
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')]);
125
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');
133   }
134
135 }