Commit b38f0a01 authored by willzyx's avatar willzyx

Issue #2896727 by willzyx: Move element info methods to a dedicated controller...

Issue #2896727 by willzyx: Move element info methods to a dedicated controller and add test coverage
parent 65d9e227
......@@ -138,16 +138,6 @@ devel.switch:
_permission: 'switch users'
_csrf_token: 'TRUE'
devel.elements_page:
path: '/devel/elements'
defaults:
_controller: '\Drupal\devel\Controller\DevelController::elementsPage'
_title: 'Element Info'
options:
_admin_route: TRUE
requirements:
_permission: 'access devel information'
devel.cache_clear:
path: '/devel/cache/clear'
defaults:
......@@ -254,3 +244,24 @@ devel.layout_info:
requirements:
_permission: 'access devel information'
_module_dependencies: 'layout_discovery'
# Element info
devel.elements_page:
path: '/devel/elements'
defaults:
_controller: '\Drupal\devel\Controller\ElementInfoController::elementList'
_title: 'Element Info'
options:
_admin_route: TRUE
requirements:
_permission: 'access devel information'
devel.elements_page.detail:
path: '/devel/elements/{element_name}'
defaults:
_controller: '\Drupal\devel\Controller\ElementInfoController::elementDetail'
_title: 'Element @element_name'
options:
_admin_route: TRUE
requirements:
_permission: 'access devel information'
......@@ -53,25 +53,6 @@ class DevelController extends ControllerBase {
return $this->dumper->exportAsRenderable($hooks);
}
/**
* Builds the elements info overview page.
*
* @return array
* Array of page elements to render.
*/
public function elementsPage() {
$element_info_manager = \Drupal::service('element_info');
$elements_info = array();
foreach ($element_info_manager->getDefinitions() as $element_type => $definition) {
$elements_info[$element_type] = $definition + $element_info_manager->getInfo($element_type);
}
ksort($elements_info);
return $this->dumper->exportAsRenderable($elements_info);
}
/**
* Builds the fields info overview page.
*
......
<?php
namespace Drupal\devel\Controller;
use Drupal\Component\Serialization\Json;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Render\ElementInfoManagerInterface;
use Drupal\Core\Url;
use Drupal\devel\DevelDumperManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
/**
* Provides route responses for the element info page.
*/
class ElementInfoController extends ControllerBase {
/**
* Element info manager service.
*
* @var \Drupal\Core\Render\ElementInfoManagerInterface
*/
protected $elementInfo;
/**
* The dumper service.
*
* @var \Drupal\devel\DevelDumperManagerInterface
*/
protected $dumper;
/**
* EventInfoController constructor.
*
* @param \Drupal\Core\Render\ElementInfoManagerInterface $element_info
* Element info manager service.
* @param \Drupal\devel\DevelDumperManagerInterface $dumper
* The dumper service.
*/
public function __construct(ElementInfoManagerInterface $element_info, DevelDumperManagerInterface $dumper) {
$this->elementInfo = $element_info;
$this->dumper = $dumper;
}
/**
* {@inheritdoc}
*/
public static function create(ContainerInterface $container) {
return new static(
$container->get('element_info'),
$container->get('devel.dumper')
);
}
/**
* Builds the element overview page.
*
* @return array
* A render array as expected by the renderer.
*/
public function elementList() {
$headers = [
$this->t('Name'),
$this->t('Provider'),
$this->t('Class'),
$this->t('Operations'),
];
$rows = [];
foreach ($this->elementInfo->getDefinitions() as $element_type => $definition) {
$row['name'] = [
'data' => $element_type,
'class' => 'table-filter-text-source',
];
$row['provider'] = [
'data' => $definition['provider'],
'class' => 'table-filter-text-source',
];
$row['class'] = [
'data' => $definition['class'],
'class' => 'table-filter-text-source',
];
$row['operations']['data'] = [
'#type' => 'operations',
'#links' => [
'devel' => [
'title' => $this->t('Devel'),
'url' => Url::fromRoute('devel.elements_page.detail', ['element_name' => $element_type]),
'attributes' => [
'class' => ['use-ajax'],
'data-dialog-type' => 'modal',
'data-dialog-options' => Json::encode([
'width' => 700,
'minHeight' => 500,
]),
],
],
],
];
$rows[$element_type] = $row;
}
ksort($rows);
$output['#attached']['library'][] = 'system/drupal.system.modules';
$output['filters'] = [
'#type' => 'container',
'#attributes' => [
'class' => ['table-filter', 'js-show'],
],
];
$output['filters']['text'] = [
'#type' => 'search',
'#title' => $this->t('Search'),
'#size' => 30,
'#placeholder' => $this->t('Enter element id, provider or class'),
'#attributes' => [
'class' => ['table-filter-text'],
'data-table' => '.devel-filter-text',
'autocomplete' => 'off',
'title' => $this->t('Enter a part of the element id, provider or class to filter by.'),
],
];
$output['elements'] = [
'#type' => 'table',
'#header' => $headers,
'#rows' => $rows,
'#empty' => $this->t('No elements found.'),
'#sticky' => TRUE,
'#attributes' => [
'class' => ['devel-element-list', 'devel-filter-text'],
],
];
return $output;
}
/**
* Returns a render array representation of the element.
*
* @param string $element_name
* The name of the element to retrieve.
*
* @return array
* A render array containing the element.
*
* @throws \Symfony\Component\HttpKernel\Exception\NotFoundHttpException
* If the requested element is not defined.
*/
public function elementDetail($element_name) {
if (!$element = $this->elementInfo->getDefinition($element_name, FALSE)) {
throw new NotFoundHttpException();
}
$element += $this->elementInfo->getInfo($element_name);
return $this->dumper->exportAsRenderable($element, $element_name);
}
}
<?php
namespace Drupal\Tests\devel\Functional;
use Behat\Mink\Element\NodeElement;
use Drupal\Core\Url;
use Drupal\Tests\BrowserTestBase;
/**
* Tests element info pages and links.
*
* @group devel
*/
class DevelElementInfoTest extends BrowserTestBase {
/**
* {@inheritdoc}
*/
public static $modules = ['devel', 'block'];
/**
* The user for the test.
*
* @var \Drupal\user\UserInterface
*/
protected $develUser;
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->drupalPlaceBlock('system_menu_block:devel');
$this->drupalPlaceBlock('page_title_block');
$this->develUser = $this->drupalCreateUser(['access devel information']);
$this->drupalLogin($this->develUser);
}
/**
* Tests element info menu link.
*/
public function testElementInfoMenuLink() {
$this->drupalPlaceBlock('system_menu_block:devel');
// Ensures that the element info link is present on the devel menu and that
// it points to the correct page.
$this->drupalGet('');
$this->clickLink('Element Info');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->addressEquals('/devel/elements');
$this->assertSession()->pageTextContains('Element Info');
}
/**
* Tests element list page.
*/
public function testElementList() {
$this->drupalGet('/devel/elements');
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains('Element Info');
$page = $this->getSession()->getPage();
// Ensures that the element list table is found.
$table = $page->find('css', 'table.devel-element-list');
$this->assertNotNull($table);
// Ensures that the expected table headers are found.
$headers = $table->findAll('css', 'thead th');
$this->assertEquals(4, count($headers));
$expected_headers = ['Name', 'Provider', 'Class', 'Operations'];
$actual_headers = array_map(function (NodeElement $element) {
return $element->getText();
}, $headers);
$this->assertSame($expected_headers, $actual_headers);
// Tests the presence of some (arbitrarily chosen) elements in the table.
$expected_elements = [
'button' => [
'class' => 'Drupal\Core\Render\Element\Button',
'provider' => 'core',
],
'form' => [
'class' => 'Drupal\Core\Render\Element\Form',
'provider' => 'core',
],
'html' => [
'class' => 'Drupal\Core\Render\Element\Html',
'provider' => 'core',
],
];
foreach ($expected_elements as $element_name => $element) {
$row = $table->find('css', sprintf('tbody tr:contains("%s")', $element_name));
$this->assertNotNull($row);
/** @var $cells \Behat\Mink\Element\NodeElement[] */
$cells = $row->findAll('css', 'td');
$this->assertEquals(4, count($cells));
$cell = $cells[0];
$this->assertEquals($element_name, $cell->getText());
$this->assertTrue($cell->hasClass('table-filter-text-source'));
$cell = $cells[1];
$this->assertEquals($element['provider'], $cell->getText());
$this->assertTrue($cell->hasClass('table-filter-text-source'));
$cell = $cells[2];
$this->assertEquals($element['class'], $cell->getText());
$this->assertTrue($cell->hasClass('table-filter-text-source'));
$cell = $cells[3];
$actual_href = $cell->findLink('Devel')->getAttribute('href');
$expected_href = Url::fromRoute('devel.elements_page.detail', ['element_name' => $element_name])->toString();
$this->assertEquals($expected_href, $actual_href);
}
// Ensures that the page is accessible only to the users with the adequate
// permissions.
$this->drupalLogout();
$this->drupalGet('devel/elements');
$this->assertSession()->statusCodeEquals(403);
}
/**
* Tests element detail page.
*/
public function testElementDetail() {
$element_name = 'button';
// Ensures that the page works as expected.
$this->drupalGet("/devel/elements/$element_name");
$this->assertSession()->statusCodeEquals(200);
$this->assertSession()->pageTextContains("Element $element_name");
// Ensures that the page returns a 404 error if the requested element is
// not defined.
$this->drupalGet('/devel/elements/not_exists');
$this->assertSession()->statusCodeEquals(404);
// Ensures that the page is accessible ony to users with the adequate
// permissions.
$this->drupalLogout();
$this->drupalGet("/devel/elements/$element_name");
$this->assertSession()->statusCodeEquals(403);
}
}
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