Version 1
[yaffs-website] / web / core / modules / content_moderation / tests / src / Functional / ModerationStateNodeTest.php
1 <?php
2
3 namespace Drupal\Tests\content_moderation\Functional;
4
5 use Drupal\Core\Url;
6 use Drupal\node\Entity\Node;
7
8 /**
9  * Tests general content moderation workflow for nodes.
10  *
11  * @group content_moderation
12  */
13 class ModerationStateNodeTest extends ModerationStateTestBase {
14
15   /**
16    * {@inheritdoc}
17    */
18   protected function setUp() {
19     parent::setUp();
20     $this->drupalLogin($this->adminUser);
21     $this->createContentTypeFromUi('Moderated content', 'moderated_content', TRUE);
22     $this->grantUserPermissionToCreateContentOfType($this->adminUser, 'moderated_content');
23   }
24
25   /**
26    * Tests creating and deleting content.
27    */
28   public function testCreatingContent() {
29     $this->drupalPostForm('node/add/moderated_content', [
30       'title[0][value]' => 'moderated content',
31     ], t('Save and Create New Draft'));
32     $node = $this->getNodeByTitle('moderated content');
33     if (!$node) {
34       $this->fail('Test node was not saved correctly.');
35     }
36     $this->assertEqual('draft', $node->moderation_state->value);
37
38     $path = 'node/' . $node->id() . '/edit';
39     // Set up published revision.
40     $this->drupalPostForm($path, [], t('Save and Publish'));
41     \Drupal::entityTypeManager()->getStorage('node')->resetCache([$node->id()]);
42     /* @var \Drupal\node\NodeInterface $node */
43     $node = \Drupal::entityTypeManager()->getStorage('node')->load($node->id());
44     $this->assertTrue($node->isPublished());
45     $this->assertEqual('published', $node->moderation_state->value);
46
47     // Verify that the state field is not shown.
48     $this->assertNoText('Published');
49
50     // Delete the node.
51     $this->drupalPostForm('node/' . $node->id() . '/delete', [], t('Delete'));
52     $this->assertText(t('The Moderated content moderated content has been deleted.'));
53
54     // Disable content moderation.
55     $this->drupalPostForm('admin/structure/types/manage/moderated_content/moderation', ['workflow' => ''], t('Save'));
56     $this->drupalGet('admin/structure/types/manage/moderated_content/moderation');
57     $this->assertOptionSelected('edit-workflow', '');
58     // Ensure the parent environment is up-to-date.
59     // @see content_moderation_workflow_insert()
60     \Drupal::service('entity_type.bundle.info')->clearCachedBundles();
61     \Drupal::service('entity_field.manager')->clearCachedFieldDefinitions();
62
63     // Create a new node.
64     $this->drupalPostForm('node/add/moderated_content', [
65       'title[0][value]' => 'non-moderated content',
66     ], t('Save and publish'));
67
68     $node = $this->getNodeByTitle('non-moderated content');
69     if (!$node) {
70       $this->fail('Non-moderated test node was not saved correctly.');
71     }
72     $this->assertEqual(NULL, $node->moderation_state->value);
73   }
74
75   /**
76    * Tests edit form destinations.
77    */
78   public function testFormSaveDestination() {
79     // Create new moderated content in draft.
80     $this->drupalPostForm('node/add/moderated_content', [
81       'title[0][value]' => 'Some moderated content',
82       'body[0][value]' => 'First version of the content.',
83     ], t('Save and Create New Draft'));
84
85     $node = $this->drupalGetNodeByTitle('Some moderated content');
86     $edit_path = sprintf('node/%d/edit', $node->id());
87
88     // After saving, we should be at the canonical URL and viewing the first
89     // revision.
90     $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
91     $this->assertText('First version of the content.');
92
93     // Create a new draft; after saving, we should still be on the canonical
94     // URL, but viewing the second revision.
95     $this->drupalPostForm($edit_path, [
96       'body[0][value]' => 'Second version of the content.',
97     ], t('Save and Create New Draft'));
98     $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
99     $this->assertText('Second version of the content.');
100
101     // Make a new published revision; after saving, we should be at the
102     // canonical URL.
103     $this->drupalPostForm($edit_path, [
104       'body[0][value]' => 'Third version of the content.',
105     ], t('Save and Publish'));
106     $this->assertUrl(Url::fromRoute('entity.node.canonical', ['node' => $node->id()]));
107     $this->assertText('Third version of the content.');
108
109     // Make a new forward revision; after saving, we should be on the "Latest
110     // version" tab.
111     $this->drupalPostForm($edit_path, [
112       'body[0][value]' => 'Fourth version of the content.',
113     ], t('Save and Create New Draft'));
114     $this->assertUrl(Url::fromRoute('entity.node.latest_version', ['node' => $node->id()]));
115     $this->assertText('Fourth version of the content.');
116   }
117
118   /**
119    * Tests pagers aren't broken by content_moderation.
120    */
121   public function testPagers() {
122     // Create 51 nodes to force the pager.
123     foreach (range(1, 51) as $delta) {
124       Node::create([
125         'type' => 'moderated_content',
126         'uid' => $this->adminUser->id(),
127         'title' => 'Node ' . $delta,
128         'status' => 1,
129         'moderation_state' => 'published',
130       ])->save();
131     }
132     $this->drupalLogin($this->adminUser);
133     $this->drupalGet('admin/content');
134     $element = $this->cssSelect('nav.pager li.is-active a');
135     $url = $element[0]->getAttribute('href');
136     $query = [];
137     parse_str(parse_url($url, PHP_URL_QUERY), $query);
138     $this->assertEqual(0, $query['page']);
139   }
140
141 }