diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationContextualLinks.php b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationContextualLinks.php index 6238799cce8f49d01f87401971184b3ff31fc88d..45575d0d9117ad1d557e5420bdcc360ec21c01aa 100644 --- a/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationContextualLinks.php +++ b/core/modules/content_translation/lib/Drupal/content_translation/Plugin/Derivative/ContentTranslationContextualLinks.php @@ -8,9 +8,9 @@ namespace Drupal\content_translation\Plugin\Derivative; use Drupal\Component\Plugin\Derivative\DerivativeBase; -use Drupal\Core\Entity\EntityManagerInterface; use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface; use Drupal\Core\Routing\RouteProviderInterface; +use Drupal\content_translation\ContentTranslationManagerInterface; use Symfony\Component\DependencyInjection\ContainerInterface; /** @@ -21,20 +21,20 @@ class ContentTranslationContextualLinks extends DerivativeBase implements ContainerDerivativeInterface { /** - * The entity manager. + * The content translation manager. * - * @var \Drupal\Core\Entity\EntityManagerInterface + * @var \Drupal\content_translation\ContentTranslationManagerInterface */ - protected $entityManager; + protected $contentTranslationManager; /** * Constructs a new ContentTranslationContextualLinks. * - * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager - * The entity manager. + * @param \Drupal\content_translation\ContentTranslationManagerInterface $content_translation_manager + * The content translation manager. */ - public function __construct(EntityManagerInterface $entity_manager) { - $this->entityManager = $entity_manager; + public function __construct(ContentTranslationManagerInterface $content_translation_manager) { + $this->contentTranslationManager = $content_translation_manager; } /** @@ -42,7 +42,7 @@ public function __construct(EntityManagerInterface $entity_manager) { */ public static function create(ContainerInterface $container, $base_plugin_id) { return new static( - $container->get('entity.manager') + $container->get('content_translation.manager') ); } @@ -50,13 +50,11 @@ public static function create(ContainerInterface $container, $base_plugin_id) { * {@inheritdoc} */ public function getDerivativeDefinitions(array $base_plugin_definition) { - // Create contextual links for all possible entity types. - foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) { - if ($entity_info->isTranslatable()) { - $this->derivatives[$entity_type]['title'] = t('Translate'); - $this->derivatives[$entity_type]['route_name'] = $entity_info->getLinkTemplate('drupal:content-translation-overview'); - $this->derivatives[$entity_type]['group'] = $entity_type; - } + // Create contextual links for translatable entity types. + foreach ($this->contentTranslationManager->getSupportedEntityTypes() as $entity_type => $entity_info) { + $this->derivatives[$entity_type]['title'] = t('Translate'); + $this->derivatives[$entity_type]['route_name'] = $entity_info->getLinkTemplate('drupal:content-translation-overview'); + $this->derivatives[$entity_type]['group'] = $entity_type; } return parent::getDerivativeDefinitions($base_plugin_definition); } diff --git a/core/modules/content_translation/lib/Drupal/content_translation/Tests/ContentTranslationContextualLinksTest.php b/core/modules/content_translation/lib/Drupal/content_translation/Tests/ContentTranslationContextualLinksTest.php new file mode 100644 index 0000000000000000000000000000000000000000..4b71a167d491243a552ca70fa94b5e53fd1927c1 --- /dev/null +++ b/core/modules/content_translation/lib/Drupal/content_translation/Tests/ContentTranslationContextualLinksTest.php @@ -0,0 +1,144 @@ +<?php + +/** + * @file + * Contains \Drupal\content_translation\Tests\ContentTranslationContextualLinksTest. + */ + +namespace Drupal\content_translation\Tests; + +use Drupal\Component\Utility\Json; +use Drupal\node\Entity\NodeType; +use Drupal\simpletest\WebTestBase; + +/** + * Tests that contextual links are available for content translation. + */ +class ContentTranslationContextualLinksTest extends WebTestBase { + + /** + * The bundle being tested. + * + * @var string + */ + protected $bundle; + + /** + * The content type being tested. + * + * @var NodeType + */ + protected $contentType; + + /** + * The 'translator' user to use during testing. + * + * @var \Drupal\user\UserInterface + */ + protected $translator; + + /** + * Modules to enable. + * + * @var array + */ + public static $modules = array('content_translation', 'contextual', 'node'); + + /** + * The profile to install as a basis for testing. + * + * @var string + */ + protected $profile = 'testing'; + + public static function getInfo() { + return array( + 'name' => 'Content translation contextual links', + 'description' => 'Tests contextual links for content translation.', + 'group' => 'Content Translation UI', + ); + } + + function setUp() { + parent::setUp(); + + // Create a content type. + $this->bundle = $this->randomName(); + $this->contentType = $this->drupalCreateContentType(array('type' => $this->bundle)); + + // Create a translator user. + $permissions = array( + 'access contextual links', + 'administer nodes', + "edit any $this->bundle content", + 'translate any entity', + ); + $this->translator = $this->drupalCreateUser($permissions); + } + + /** + * Tests that a contextual link is available for translating a node. + */ + public function testContentTranslationContextualLinks() { + // Create a node. + $title = $this->randomString(); + $this->drupalCreateNode(array('type' => $this->bundle, 'title' => $title)); + $node = $this->drupalGetNodeByTitle($title); + + // Check that the translate link appears on the node page. + $this->drupalLogin($this->translator); + $translate_link = 'node/' . $node->id() . '/translations'; + + $response = $this->renderContextualLinks(array('node:node=1:'), 'node/' . $node->id()); + $this->assertResponse(200); + $json = Json::decode($response); + $this->drupalSetContent($json['node:node=1:']); + $this->assertLinkByHref($translate_link, 0, 'The contextual link to translate the node is shown.'); + + // Check that the link leads to the translate page. + $this->drupalGet($translate_link); + $this->assertRaw(t('Translations of %label', array('%label' => $node->label())), 'The contextual link leads to the translate page.'); + } + + /** + * Get server-rendered contextual links for the given contextual link ids. + * + * Copied from \Drupal\contextual\Tests\ContextualDynamicContextTest::renderContextualLinks(). + * + * @param array $ids + * An array of contextual link ids. + * @param string $current_path + * The Drupal path for the page for which the contextual links are rendered. + * + * @return string + * The response body. + */ + protected function renderContextualLinks($ids, $current_path) { + // Build POST values. + $post = array(); + for ($i = 0; $i < count($ids); $i++) { + $post['ids[' . $i . ']'] = $ids[$i]; + } + + // Serialize POST values. + foreach ($post as $key => $value) { + // Encode according to application/x-www-form-urlencoded + // Both names and values needs to be urlencoded, according to + // http://www.w3.org/TR/html4/interact/forms.html#h-17.13.4.1 + $post[$key] = urlencode($key) . '=' . urlencode($value); + } + $post = implode('&', $post); + + // Perform HTTP request. + return $this->curlExec(array( + CURLOPT_URL => url('contextual/render', array('absolute' => TRUE, 'query' => array('destination' => $current_path))), + CURLOPT_POST => TRUE, + CURLOPT_POSTFIELDS => $post, + CURLOPT_HTTPHEADER => array( + 'Accept: application/json', + 'Content-Type: application/x-www-form-urlencoded', + ), + )); + } + +}