Skip to content

Instantly share code, notes, and snippets.

@neclimdul
Created October 15, 2013 19:05
Show Gist options
  • Save neclimdul/6996948 to your computer and use it in GitHub Desktop.
Save neclimdul/6996948 to your computer and use it in GitHub Desktop.
content translation local task tests.
<?php
/**
* @file
* Contains \Drupal\content_translation\Plugin\Derivative\ContentTranslationLocalTasks.
*/
namespace Drupal\content_translation\Plugin\Derivative;
use Drupal\Component\Plugin\Derivative\DerivativeBase;
use Drupal\Core\Entity\EntityManager;
use Drupal\Core\Plugin\Discovery\ContainerDerivativeInterface;
use Drupal\Core\Routing\RouteProviderInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides dynamic local tasks for content translation.
*/
class ContentTranslationLocalTasks extends DerivativeBase implements ContainerDerivativeInterface {
/**
* The entity manager.
*
* @var \Drupal\Core\Entity\EntityManager
*/
protected $entityManager;
/**
* The route provider.
*
* @var \Drupal\Core\Routing\RouteProviderInterface
*/
protected $routeProvider;
/**
* Constructs a new ContentTranslationLocalTasks.
*
* @param \Drupal\Core\Entity\EntityManager $entity_manager
* The entity manager.
* @param \Drupal\Core\Routing\RouteProviderInterface $route_provider
* The route provider.
*/
public function __construct(EntityManager $entity_manager, RouteProviderInterface $route_provider) {
$this->entityManager = $entity_manager;
$this->routeProvider = $route_provider;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container, $base_plugin_id) {
return new static(
$container->get('entity.manager'),
$container->get('router.route_provider')
);
}
/**
* {@inheritdoc}
*/
public function getDerivativeDefinitions(array $base_plugin_definition) {
// Create tabs for all possible entity types.
foreach ($this->entityManager->getDefinitions() as $entity_type => $entity_info) {
if ($entity_info['translatable'] && isset($entity_info['translation'])) {
$path = '/' . preg_replace('/%(.*)/', '{$1}', $entity_info['menu_base_path']);
if ($routes = $this->routeProvider->getRoutesByPattern($path)->all()) {
// Find the route name for the entity page.
$entity_tab = $entity_route_name = key($routes);
// Find the route name for the translation overview.
$translation_route_name = "content_translation.translation_overview_$entity_type";
$translation_tab = $translation_route_name;
$this->derivatives[$translation_tab] = $base_plugin_definition + array(
'tab_root_id' => $entity_tab,
'entity_type' => $entity_type,
);
$this->derivatives[$translation_tab]['title'] = \Drupal::translation()->translate('Translate');
$this->derivatives[$translation_tab]['route_name'] = $translation_route_name;
}
}
}
return parent::getDerivativeDefinitions($base_plugin_definition);
}
}
<?php
/**
* @file
* Contains \Drupal\block\Tests\Menu\BlockLocalTasksTest.
*/
namespace Drupal\content_translation\Tests\Menu;
use Drupal\Tests\Core\Menu\LocalTaskIntegrationTest;
/**
* Tests existence of block local tasks.
*
* @group Drupal
* @group Block
*/
class ContentTranslationLocalTasksTest extends LocalTaskIntegrationTest {
public static function getInfo() {
return array(
'name' => 'Content translation local tasks test',
'description' => 'Test content translation local tasks.',
'group' => 'Content Translation',
);
}
public function setUp() {
$this->moduleList = array(
'content_translation' => 'core/modules/content_translation/content_translation.info',
'node' => 'core/modules/node/node.info',
);
parent::setUp();
// Entity manager stub for derivative building.
$entity_manager = $this->getMockBuilder('Drupal\Core\Entity\EntityManager')
->disableOriginalConstructor()
->getMock();
$entity_manager->expects($this->any())
->method('getDefinitions')
->will($this->returnValue(array(
'node' => array(
'translatable' => true,
'translation' => array(
'content_translation' => array(
// things.
),
),
'menu_base_path' => 'node/%node',
),
)));
\Drupal::getContainer()->set('entity.manager', $entity_manager);
// Route provider for injecting node.view into derivative lookup.
$collection = $this->getMockBuilder('Symfony\Component\Routing\RouteCollection')
->disableOriginalConstructor()
->setMethods(array('all'))
->getMock();
$collection->expects($this->any())
->method('all')
->will($this->returnValue(array('node.view' => array())));
$route_provider = $this->getMockBuilder('Drupal\Core\Routing\RouteProvider')
->disableOriginalConstructor()
->getMock();
$route_provider->expects($this->any())
->method('getRoutesByPattern')
->will($this->returnValue($collection));
\Drupal::getContainer()->set('router.route_provider', $route_provider);
// Stub for t().
$string_translation = $this->getMockBuilder('Drupal\Core\StringTranslation\TranslationManager')
->disableOriginalConstructor()
->getMock();
$string_translation->expects($this->any())
->method('translate')
->will($this->returnCallback(function($string) {return $string;}));
\Drupal::getContainer()->set('string_translation', $string_translation);
}
/**
* Tests the block admin display local tasks.
*
* @dataProvider providerTestBlockAdminDisplay
*/
public function testBlockAdminDisplay($route, $expected) {
$this->assertLocalTasks($route, $expected);
}
/**
* Provides a list of routes to test.
*/
public function providerTestBlockAdminDisplay() {
return array(
array('node.view', array(array(
'content_translation.local_tasks:content_translation.translation_overview_node',
'node.view',
'node.page_edit',
'node.delete_confirm',
'node.revision_overview',
))),
array('content_translation.translation_overview_node', array(array(
'content_translation.local_tasks:content_translation.translation_overview_node',
'node.view',
'node.page_edit',
'node.delete_confirm',
'node.revision_overview',
))),
);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment