Issue #2828438 by Adita, timmillwood, Sam152, rachel_norfolk, jp.stacey: Exception when adding tab to a node managed by content moderation

8.3.x
Alex Pott 2017-01-16 09:34:11 +00:00
parent b7ff214793
commit a76eef7ea3
6 changed files with 127 additions and 8 deletions

View File

@ -2,6 +2,7 @@
namespace Drupal\content_moderation\Plugin\Menu;
use Drupal\Core\Entity\ContentEntityInterface;
use Drupal\Core\Menu\LocalTaskDefault;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Routing\RouteMatchInterface;
@ -25,9 +26,9 @@ class EditTab extends LocalTaskDefault implements ContainerFactoryPluginInterfac
protected $moderationInfo;
/**
* The entity.
* The entity if determinable from the route or FALSE.
*
* @var \Drupal\Core\Entity\ContentEntityInterface
* @var \Drupal\Core\Entity\ContentEntityInterface|FALSE
*/
protected $entity;
@ -69,8 +70,8 @@ class EditTab extends LocalTaskDefault implements ContainerFactoryPluginInterfac
* {@inheritdoc}
*/
public function getRouteParameters(RouteMatchInterface $route_match) {
// Override the node here with the latest revision.
$this->entity = $route_match->getParameter($this->pluginDefinition['entity_type_id']);
$entity_parameter = $route_match->getParameter($this->pluginDefinition['entity_type_id']);
$this->entity = $entity_parameter instanceof ContentEntityInterface ? $route_match->getParameter($this->pluginDefinition['entity_type_id']) : FALSE;
return parent::getRouteParameters($route_match);
}
@ -78,8 +79,8 @@ class EditTab extends LocalTaskDefault implements ContainerFactoryPluginInterfac
* {@inheritdoc}
*/
public function getTitle() {
if (!$this->moderationInfo->isModeratedEntity($this->entity)) {
// Moderation isn't enabled.
// If the entity couldn't be loaded or moderation isn't enabled.
if (!$this->entity || !$this->moderationInfo->isModeratedEntity($this->entity)) {
return parent::getTitle();
}
@ -96,8 +97,10 @@ class EditTab extends LocalTaskDefault implements ContainerFactoryPluginInterfac
// @todo https://www.drupal.org/node/2779933 write a test for this.
$tags = parent::getCacheTags();
// Tab changes if node or node-type is modified.
$tags = array_merge($tags, $this->entity->getCacheTags());
$tags[] = $this->entity->getEntityType()->getBundleEntityType() . ':' . $this->entity->bundle();
if ($this->entity) {
$tags = array_merge($tags, $this->entity->getCacheTags());
$tags[] = $this->entity->getEntityType()->getBundleEntityType() . ':' . $this->entity->bundle();
}
return $tags;
}

View File

@ -0,0 +1,9 @@
name: 'Content moderation test local task'
type: module
description: 'Provides a local task for testing.'
package: Testing
version: VERSION
core: 8.x
dependencies:
- content_moderation
- node

View File

@ -0,0 +1,4 @@
entity.node.test_local_task_without_upcast_node:
route_name: entity.node.test_local_task_without_upcast_node
base_route: entity.node.canonical
title: 'Task Without Upcast Node'

View File

@ -0,0 +1,7 @@
entity.node.test_local_task_without_upcast_node:
path: '/node/{node}/task-without-upcast-node'
defaults:
_title: 'Page Without Upcast Node'
_controller: '\Drupal\content_moderation_test_local_task\Controller\TestLocalTaskController::methodWithoutUpcastNode'
requirements:
_access: 'TRUE'

View File

@ -0,0 +1,17 @@
<?php
namespace Drupal\content_moderation_test_local_task\Controller;
/**
* A test controller.
*/
class TestLocalTaskController {
/**
* A method which does not hint the node parameter to avoid upcasting.
*/
public function methodWithoutUpcastNode($node) {
return ['#markup' => 'It works!'];
}
}

View File

@ -0,0 +1,79 @@
<?php
namespace Drupal\Tests\content_moderation\Functional;
use Drupal\simpletest\ContentTypeCreationTrait;
use Drupal\simpletest\NodeCreationTrait;
use Drupal\Tests\BrowserTestBase;
use Drupal\workflows\Entity\Workflow;
/**
* Test the content moderation local task.
*
* @group content_moderation
*/
class LocalTaskTest extends BrowserTestBase {
use ContentTypeCreationTrait;
use NodeCreationTrait;
/**
* {@inheritdoc}
*/
public static $modules = [
'content_moderation_test_local_task',
'content_moderation',
'block',
];
/**
* A test node.
*
* @var \Drupal\node\NodeInterface
*/
protected $testNode;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('local_tasks_block', ['id' => 'tabs_block']);
$this->drupalLogin($this->createUser(['bypass node access']));
$node_type = $this->createContentType();
// Now enable moderation for subsequent nodes.
$workflow = Workflow::load('editorial');
$workflow->getTypePlugin()->addEntityTypeAndBundle('node', $node_type->id());
$workflow->save();
$this->testNode = $this->createNode([
'type' => $node_type->id(),
'moderation_state' => 'draft',
]);
}
/**
* Tests local tasks behave with content_moderation enabled.
*/
public function testLocalTasks() {
$this->drupalGet(sprintf('node/%s', $this->testNode->id()));
$this->assertTasks(TRUE);
$this->clickLink('Task Without Upcast Node');
$this->assertTasks(FALSE);
}
/**
* Assert the correct tasks appear.
*/
protected function assertTasks($with_upcast_node) {
$this->assertSession()->linkExists('View');
$this->assertSession()->linkExists('Task Without Upcast Node');
$this->assertSession()->linkExists($with_upcast_node ? 'Edit draft' : 'Edit');
$this->assertSession()->linkExists('Delete');
}
}