Commit bc73813e authored by larowlan's avatar larowlan

Issue #3041326 by Sam152, amateescu, Berdir: Remove 'title' and 'description'...

Issue #3041326 by Sam152, amateescu, Berdir: Remove 'title' and 'description' from MenuSettingsConstraintValidator when used with content moderation by creating a draft of menu link content when a draft of it's parent content is created
parent 2358d611
......@@ -124,6 +124,7 @@ function _menu_ui_node_save(NodeInterface $node, array $values) {
$entity->menu_name->value = $values['menu_name'];
$entity->parent->value = $values['parent'];
$entity->weight->value = isset($values['weight']) ? $values['weight'] : 0;
$entity->isDefaultRevision($node->isDefaultRevision());
$entity->save();
}
......
......@@ -15,5 +15,8 @@
class MenuSettingsConstraint extends Constraint {
public $message = 'You can only change the menu settings for the <em>published</em> version of this content.';
public $messageWeight = 'You can only change the menu item weight for the <em>published</em> version of this content.';
public $messageParent = 'You can only change the parent menu item for the <em>published</em> version of this content.';
public $messageRemove = 'You can only remove the menu item in the <em>published</em> version of this content.';
}
......@@ -25,40 +25,36 @@ public function validate($entity, Constraint $constraint) {
$values['parent'] = $parent;
}
// Handle the case when a menu link is added to a pending revision.
if (!$defaults['entity_id'] && $values['enabled']) {
$violation_path = 'menu';
}
// Handle the case when the menu link is deleted in a pending revision.
elseif (empty($values['enabled']) && $defaults['entity_id']) {
$violation_path = 'menu';
if (empty($values['enabled']) && $defaults['entity_id']) {
$this->context->buildViolation($constraint->messageRemove)
->atPath('menu')
->setInvalidValue($entity)
->addViolation();
}
// Handle all the other menu link changes in a pending revision.
// Handle all the other non-revisionable menu link changes in a pending
// revision.
elseif ($defaults['entity_id']) {
if (($values['title'] != $defaults['title'])) {
$violation_path = 'menu.title';
}
elseif (($values['description'] != $defaults['description'])) {
$violation_path = 'menu.description';
}
elseif ($defaults['entity_id'] && ($values['menu_name'] != $defaults['menu_name'])) {
$violation_path = 'menu.menu_parent';
if ($defaults['entity_id'] && ($values['menu_name'] != $defaults['menu_name'])) {
$this->context->buildViolation($constraint->messageParent)
->atPath('menu.menu_parent')
->setInvalidValue($entity)
->addViolation();
}
elseif (isset($values['parent']) && ($values['parent'] != $defaults['parent'])) {
$violation_path = 'menu.menu_parent';
$this->context->buildViolation($constraint->messageParent)
->atPath('menu.menu_parent')
->setInvalidValue($entity)
->addViolation();
}
elseif (($values['weight'] != $defaults['weight'])) {
$violation_path = 'menu.weight';
}
}
if ($violation_path) {
$this->context->buildViolation($constraint->message)
->atPath($violation_path)
$this->context->buildViolation($constraint->messageWeight)
->atPath('menu.weight')
->setInvalidValue($entity)
->addViolation();
}
}
}
}
}
......@@ -79,72 +79,64 @@ public function testMenuUiWithPendingRevisions() {
$this->assertSession()->linkExists('Test menu link');
// Try to change the menu link title and save a new non-default (draft)
// Try to change the menu link weight and save a new non-default (draft)
// revision.
$edit = [
'menu[title]' => 'Test menu link draft',
'menu[weight]' => 1,
'moderation_state[0][state]' => 'draft',
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Check that the menu settings were not applied.
$this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.');
$this->assertSession()->linkExists('Test menu link');
$this->assertSession()->linkNotExists('Test menu link draft');
$this->assertSession()->pageTextContains('You can only change the menu item weight for the published version of this content.');
// Try to change the menu link description and save a new non-default
// (draft) revision.
// Try to change the menu link parent and save a new non-default (draft)
// revision.
$edit = [
'menu[description]' => 'Test menu link description',
'menu[menu_parent]' => 'main:test_page_test.front_page',
'moderation_state[0][state]' => 'draft',
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Check that the menu settings were not applied.
$this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.');
$this->assertSession()->pageTextContains('You can only change the parent menu item for the published version of this content.');
// Try to change the menu link weight and save a new non-default (draft)
// revision.
// Try to delete the menu link and save a new non-default (draft) revision.
$edit = [
'menu[weight]' => 1,
'menu[enabled]' => 0,
'moderation_state[0][state]' => 'draft',
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Check that the menu settings were not applied.
$this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.');
$this->assertSession()->pageTextContains('You can only remove the menu item in the published version of this content.');
$this->assertSession()->linkExists('Test menu link');
// Try to change the menu link parent and save a new non-default (draft)
// revision.
// Try to change the menu link title and description and save a new
// non-default (draft) revision.
$edit = [
'menu[menu_parent]' => 'main:test_page_test.front_page',
'menu[title]' => 'Test menu link draft',
'menu[description]' => 'Test menu link description',
'moderation_state[0][state]' => 'draft',
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
$this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()]));
// Check that the menu settings were not applied.
$this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.');
// Ensure the content was not immediately published.
$this->assertSession()->linkExists('Test menu link');
// Try to delete the menu link and save a new non-default (draft) revision.
// Publish the node and ensure the new link text was published.
$edit = [
'menu[enabled]' => 0,
'moderation_state[0][state]' => 'draft',
'moderation_state[0][state]' => 'published',
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Check that the menu settings were not applied.
$this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.');
$this->assertSession()->linkExists('Test menu link');
$this->assertSession()->linkExists('Test menu link draft');
// Try to save a new non-default (draft) revision without any changes and
// check that the error message is not shown.
$edit = ['moderation_state[0][state]' => 'draft'];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
// Check that the menu settings were not applied.
$this->assertSession()->pageTextNotContains('You can only change the menu settings for the published version of this content.');
$this->assertSession()->linkExists('Test menu link');
// Create a node.
$node = $this->drupalCreateNode();
......@@ -153,14 +145,24 @@ public function testMenuUiWithPendingRevisions() {
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
$this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()]));
// Add a menu link and save and create a new non-default (draft) revision.
// Add a menu link and save and create a new non-default (draft) revision
// and ensure it's not immediately published.
$edit = [
'menu[enabled]' => 1,
'menu[title]' => 'Test menu link',
'menu[title]' => 'Second test menu link',
'moderation_state[0][state]' => 'draft',
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
$this->assertSession()->pageTextContains('You can only change the menu settings for the published version of this content.');
$this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()]));
$this->assertSession()->linkNotExists('Second test menu link');
// Publish the content and ensure the new menu link shows up.
$edit = [
'moderation_state[0][state]' => 'published',
];
$this->drupalPostForm('node/' . $node->id() . '/edit', $edit, t('Save'));
$this->assertSession()->responseContains(t('Page %label has been updated.', ['%label' => $node->toLink($node->label())->toString()]));
$this->assertSession()->linkExists('Second test menu link');
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment