DrupalDay Santiago de Compostela 2016
Jose Luis Bellido (jlbellido)
@jose_lakatos
https://github.com/jlbellido
https://www.drupal.org/u/jlbellido
/**
* Tests the behavior when there are no actions to list in the admin page.
*/
public function testEmptyActionList() {
// Create a user with permission to view the actions administration pages.
$this->drupalLogin($this->drupalCreateUser(['administer actions']));
// Ensure the empty text appears on the action list page.
/** @var $storage \Drupal\Core\Entity\EntityStorageInterface */
$storage = $this->container->get('entity.manager')->getStorage('action');
$actions = $storage->loadMultiple();
$storage->delete($actions);
$this->drupalGet('/admin/config/system/actions');
$this->assertRaw('There is no Action yet.');
}
core/modules/action/src/Tests/ActionListTest.php
/**
* {@inheritdoc}
*/
protected function setUp() {
parent::setUp();
$this->installSchema('system', 'sequences');
$this->installSchema('node', 'node_access');
$this->installEntitySchema('user');
$this->installEntitySchema('node');
$this->installConfig('filter');
$this->installConfig('node');
$this->accessHandler = $this->container->get('entity_type.manager')
->getAccessControlHandler('node');
// Clear permissions for authenticated users.
$this->config('user.role.' . RoleInterface::AUTHENTICATED_ID)
->set('permissions', [])
->save();
// Create user 1 who has special permissions.
$this->drupalCreateUser();
// Create a node type.
$this->drupalCreateContentType(array(
'type' => 'page',
'name' => 'Basic page',
'display_submitted' => FALSE,
));
}
$entity_storage = $this->getMock('Drupal\Core\Entity\EntityStorageInterface');
$entity_storage->expects($this->any())
->method('loadMultiple')
->will($this->returnValue($actions));
// Mock a user entity:
$mock_user = $this->prophesize(User::class);
$mock_user->getAccountName()->willReturn('example username');
The goal: Remove Simpletest from Core and use PHPUnit as test runner.
New class BrowserTestBase.php
Now happening:
Node must have a fixed text depending on their type. The patterns are as follows:
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function d8_testable_code_example_node_presave(Drupal\Core\Entity\EntityInterface $entity) {
// Updates the title to a fix string:
// - Basic pages: "Page: @title"
// - Articles: "Awesome article by @author".
switch ($entity->bundle()) {
case 'page':
$generated_title = _d8_testable_code_page_title_generate($entity);
break;
case 'article':
$generated_title = _d8_testable_code_article_title_generate($entity);
break;
default:
$generated_title = $entity->getTitle();
}
$entity->setTitle($generated_title);
}
/**
* Set the title "Page: @title" to the given Node.
* @param \Drupal\node\NodeInterface $node
* @return string
*/
function _d8_testable_code_page_title_generate(\Drupal\node\NodeInterface $node) {
return (string) new FormattableMarkup('Page: @title', ['@title' => $node->getTitle()]);
}
/**
* Set the title "Awesome article by @author" to the given Node.
* @param \Drupal\node\NodeInterface $node
* @return string
*/
function _d8_testable_code_article_title_generate(\Drupal\node\NodeInterface $node) {
$author_name = $node->getOwner()->getAccountName();
return (string) new FormattableMarkup('Awesome article by @author', ['@author' => $author_name]);
}
services:
d8_testable_code_example.node_title_generator:
class: Drupal\d8_testable_code_example\NodeTitleGenerator
arguments: []
d8_testable_code_example.services.yml
d8_testable_code_example.services.yml
/**
* Class NodeTitleGenerator.
*
* @package Drupal\d8_testable_code_example
*/
class NodeTitleGenerator implements NodeTitleGeneratorInterface {
/**
* Generate and update the title of the given node depending on the bundle
* as follows:
* - Basic pages: "Page: @title"
* - Articles: "Awesome article by @author".
*
* @param \Drupal\node\NodeInterface $node .
* @return string
*/
public function generateTitle(NodeInterface $node) {
switch ($node->bundle()) {
case 'page':
$title = $this->generate_page_title($node);
break;
case 'article':
$title = $this->generate_article_title($node);
break;
default:
$title = $node->getTitle();
break;
}
return (string)$title;
}
/**
* Implements hook_ENTITY_TYPE_presave().
*/
function d8_testable_code_example_node_presave(\Drupal\node\NodeInterface $node) {
$generated_title = \Drupal::service('d8_testable_code_example.node_title_generator')
->generateTitle($node);
$node->setTitle($generated_title);
}
Use StringTranslationTratit.php
new TranslatableMarkup('Other', array(), array('context' => 'Entity type group'));
Use TranslatableMarkup() Class
abstract class FormBase implements FormInterface, ContainerInjectionInterface {
...
use StringTranslationTrait;
...
Update the Module.services.yml
Inject it into the constructor.
services:
d8_testable_code_example.node_title_generator:
class: Drupal\d8_testable_code_example\NodeTitleGenerator
arguments: ['@language_manager']
/**
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $language_manager;
public function __construct(LanguageManagerInterface $language_manager) {
/**
* Instead of usinf the language_manager service as: \Drupal::service('language_manager')
* We inject it and from now we can use it as : $this->language_manager...
*/
$this->language_manager = $language_manager;
}
public function testGeneratePageTitleTest() {
// Mock a user entity:
$mock_user = $this->prophesize(User::class);
$mock_user->getAccountName()->willReturn('example username');
// Mock the node entity:
$mock_node = $this->prophesize(Node::class);
$mock_node->getTitle()->willReturn('Title example');
$mock_node->bundle()->willReturn('page');
$mock_node->getOwner()->willReturn($mock_user->reveal());
$page_node = $mock_node->reveal();
$generated_title = $this->node_title_generator->generateTitle($page_node);
$this->assertEquals('Page: Title example', $generated_title);
$mock_node->bundle()->willReturn('article');
$article_node = $mock_node->reveal();
$generated_title = $this->node_title_generator->generateTitle($article_node);
$this->assertEquals('Awesome article by example username', $generated_title);
}
$ ../vendor/phpunit/phpunit/phpunit ../modules/custom/ --debug
PHPUnit 4.8.27 by Sebastian Bergmann and contributors.
Starting test 'Drupal\Tests\d8_testable_code_example\Kernel\NodeTitleGenerateKernelTest::testGeneratePageTitle'.
.
Starting test 'Drupal\Tests\d8_testable_code_example\Kernel\NodeTitleGenerateKernelTest::testGenerateArticleTitle'.
.
Starting test 'Drupal\Tests\d8_testable_code_example\Unit\NodeTitleGenerateUnitTest::testGeneratePageTitleTest'.
.
Time: 2.7 seconds, Memory: 6.00Mb
OK (3 tests, 10 assertions)
El Camino se hace caminando