I would like to ask about PHPUnit of Drupal 8 - drupal

I created Block in Drupal 8 with a custom module.
Is it possible to implement this with PHPUnit?
If you can implement it please tell me how.
I want to realize the test with PHPUnit below.
I would be pleased if you could reply just whether it was possible or not.
moduleNameBlock.php
/**
* #file
* create block
*/
namespace Drupal\moduleName\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Url;
/**
*
* Provides a 'testBlock' block.
* #Block(
* id = "test_block",
* admin_label = #Translation("Test"),
* category = #Translation("Menu"),
* )
*/
class moduleNameBlock extends BlockBase {
/**
* {#inheritdoc}
*/
public function build()
{
$build = [];
$url = '';
$nid = '';
$nid = $this->getCurrentUserNode();
if ( !empty($nid) ) {
$url = Url::fromRoute('entity.node.canonical', ['node' => $nid]);
}
$block = [
'#theme' => 'block_theme',
'#url' => $url,
'#nid' => $nid,
'#cache' => [
'max-age' => 0
]
];
$build['test_block'] = $block;
return $build;
}
/**
* The node associated with the user
* #return nid
*/
private function getCurrentUserNode() {
$user_id = \Drupal\user\Entity\User::load(\Drupal::currentUser()->id());
$nid = $user_id->get('field_name')->getValue();
return $nid[0]['target_id'];
}
}

Yes this is possible by writing a PHPUnit Functional test.
In your module directory create the following structure /tests/src/Functional then create a file like ModuleNameBlockTest.php then you can place the block in the setUp function and create tests to test the block.
<?php
namespace Drupal\Tests\my_module_name\Functional;
use Drupal\Tests\BrowserTestBase;
/**
* Class ModuleNameBlockTest.
*
* #package Drupal\Tests\my_module_name\Functional
* #group my_group
*/
class ModuleNameBlockTest extends BrowserTestBase {
/**
* Modules to enable.
*
* #var array
*/
public static $modules = ['block', 'my_module_name'];
/**
* {#inheritdoc}
*/
protected function setUp() {
parent::setUp();
$adminUser = $this->drupalCreateUser(['administer blocks']);
$this->drupalLogin($adminUser);
$this->drupalPlaceBlock('my_block_name');
$this->drupalLogout($adminUser);
}
/**
* Test the block.
*/
public function testMyAwesomeBlock() {
// Your test logic here.
}
}
You can always look into the source code of Drupal for some examples. E.g. UserBlocksTest.php of the core user module.

Related

Drupal 8.x + GraphQL Module Custom Type/Field Plugin Receiving Error "fields must be an object with field names as keys"

I am attempting to create a module which defines it's own custom Type and associated Field plugins.
When installed, GraphQLi reports the following error in the console:
Uncaught Error: CustomTypeInterface fields must be an object with field names as keys or a function which returns such an object.
Drupal 8.61. I have tried on both GraphQL 3.0-RC2 and 3.x-Dev. Any help would be much appreciated. Thanks.
My code is as follows:
/graphql_custom.info.yml
name: GraphQL Custom Type Example
type: module
description: ''
package: GraphQL
core: 8.x
dependencies:
- graphql_core
/src/CustomObject.php
namespace Drupal\graphql_custom;
class CustomObject {
protected $data;
function __construct(String $data) {
$this->data = $data;
}
function getData() {
return $this->data;
}
}
/src/Plugin/GraphQL/Fields/CustomField.php
<?php
namespace Drupal\graphql_custom\Plugin\GraphQL\Fields;
use Drupal\graphql_custom\CustomObject;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use GraphQL\Type\Definition\ResolveInfo;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\Request;
/**
* Created Custom Object with argument as data.
*
* #GraphQLField(
* id = "custom_field",
* secure = true,
* name = "customfield",
* type = "CustomType",
* nullable = true,
* arguments = {
* "argument" = "String!"
* }
* )
*/
class CustomField extends FieldPluginBase implements ContainerFactoryPluginInterface {
/**
* {#inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition
);
}
/**
* {#inheritdoc}
*/
protected function isLanguageAwareField() {
return FALSE;
}
/**
* {#inheritdoc}
*/
public function resolve($value, array $args, ResolveContext $context, ResolveInfo $info) {
return parent::resolve($value, $args, $context, $info);
}
/**
* {#inheritdoc}
*/
public function resolveValues($value, array $args, ResolveContext $context, ResolveInfo $info) {
$arg = $args['argument'];
$object = new CustomObject($arg);
yield $object;
}
}
/src/Plugin/GraphQL/Fields/CustomFieldData.php
<?php
namespace Drupal\graphql_custom\Plugin\GraphQL\Fields;
use Drupal\graphql_custom\CustomObject;
use Drupal\graphql\Plugin\GraphQL\Fields\FieldPluginBase;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use GraphQL\Type\Definition\ResolveInfo;
/**
* Custom Type Data Field
*
* #GraphQLField(
* id = "custom_field_data",
* secure = true,
* name = "data",
* type = "String",
* parents = {"CustomType"}
* )
*/
class CustomFieldData extends FieldPluginBase {
/**
* {#inheritdoc}
*/
protected function resolveValues($value, array $args, $context, $info) {
if ($value instanceOf CustomObject) {
yield (string) $value->getData();
} else {
yield (string) "Empty";
}
}
}
/src/Plugin/GraphQL/Interfaces/CustomTypeInterface.php
<?php
namespace Drupal\graphql_custom\Plugin\GraphQL\Interfaces;
use Drupal\graphql_custom\CustomObject;
use Drupal\graphql\Annotation\GraphQLInterface;
use Drupal\graphql\Plugin\GraphQL\Interfaces\InterfacePluginBase;
/**
* Interface for Custom Type.
*
* For simplicity reasons, this example does not utilize dependency injection.
*
* #GraphQLInterface(
* id = "custom_type_interface",
* name = "CustomTypeInterface"
* )
*/
class CustomTypeInterface extends InterfacePluginBase {
/**
* {#inheritdoc}
*/
public function resolveType($object) {
if ($object instanceof CustomObject) {
$schemaManager = \Drupal::service('graphql_core.schema_manager');
return $schemaManager->findByName('CustomType', [
GRAPHQL_CORE_TYPE_PLUGIN,
]);
}
}
}
/src/Plugin/GraphQL/Types/CustomType.php
<?php
namespace Drupal\graphql_custom\Plugin\GraphQL\Types;
use Drupal\graphql_custom\CustomObject;
use Drupal\graphql\Plugin\GraphQL\Types\TypePluginBase;
use Drupal\graphql\GraphQL\Execution\ResolveContext;
use GraphQL\Type\Definition\ResolveInfo;
/**
* GraphQL Custom Type.
*
* #GraphQLType(
* id = "custom_type",
* name = "CustomType",
* interfaces = {"CustomTypeInterface"}
* )
*/
class CustomType extends TypePluginBase {
/**
* {#inheritdoc}
*/
public function applies($object, ResolveContext $context, ResolveInfo $info) {
return $object instanceof CustomObject;
}
}
With your fields you should use interface reference in parents instead of type:
parents = {"CustomTypeInterface"}
Another way is to remove interface and use direct type reference as mentioned in your example.

Symfony The annotation does not exist, or could not be auto-loaded - Symfony Validation with Doctrine

We have a legacy app which is not based on symfony. Doctrine is in use and now we would like to add validation to the models. Seems that the Annotations never get autoloaded, even when "use" statements are in use.
[Semantical Error] The annotation "#Symfony\Component\Validator\Constraints\NotBlank" in property Test\Stackoverflow\User::$Username does not exist, or could not be auto-loaded.
Wrote a small demo application to showcase the problem and how we create the entity manager and validation instance.
composer.json:
{
"require": {
"symfony/validator" : "~3.1"
, "doctrine/orm" : "~2.6.1"
}
}
index.php
require_once ('vendor/autoload.php');
// Load Entities, would normally be done over composer since they reside in a package
require_once('test/User.php');
require_once('MyAnnotationTestApp.php');
// create test app
$app = new MyAnnotationsTestApp();
$app->initEntityManager('localhost', 'annotation_test', 'root', 'mysql', 3306);
if(key_exists('test', $_GET)){
// Create entity and validate it
$entity = new \Test\Stackoverflow\User();
$entity->setUsername('StackoverflowUser');
if($app->testAnnotationWithoutLoading($entity)){
print "Seems the validation was working without preloading the asserts\n<br>";
}
if($app->testAnnotationWithLoading($entity)){
print "Seems the validation was working because we loaded the required class ourself.\n<br>";
}
print "\n<br><br>The question is why the required annotation classes never get autoloaded?";
}else{
// Load the validator class otherwise the annotation throws an exception
$notBlankValidator = new \Symfony\Component\Validator\Constraints\NotBlank();
print "We have cerated the tables but also had to load the validator class ourself.\n<br>\n<br>";
// create tables and
$app->updateDatabaseSchema();
print sprintf('Now lets run the test', $_SERVER['REQUEST_URI']);
}
Doctrine user Entity
<?php
namespace Test\Stackoverflow;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\Validator\Constraints as Assert;
/**
* #ORM\Entity()
* #ORM\Table(name="users")
*
*/
class User{
/**
* #ORM\Id
* #ORM\Column(name="Id",type="integer")
* #ORM\GeneratedValue(strategy="AUTO")
*/
protected $Id;
public function getId(){
return $this->Id;
}
/**
* #ORM\Column(type="text", length=80, nullable=false)
* #Assert\NotBlank()
*/
protected $Username;
/**
* #return string
*/
public function getUsername()
{
return $this->Username;
}
/**
* #param string $Username
*/
public function setUsername($Username)
{
$this->Username = $Username;
}
}
Demo App with doctrine/validator initialisation:
<?php
final class MyAnnotationsTestApp {
/**
* #var \Doctrine\ORM\EntityManager
*/
private $entityManager;
/**
* #param string $host
* #param string $database
* #param string $username
* #param string $password
* #param integer $port
* #param array $options
* #return \Doctrine\ORM\EntityManager
*/
public function initEntityManager($host, $database, $username, $password, $port, array $options=null){
if($this->entityManager){
return $this->entityManager;
}
$connectionString = sprintf('mysql://%3$s:%4$s#%1$s/%2$s', $host, $database, $username, $password, $port);
$isDevMode = true;
$dbParams = array(
'url' => $connectionString
, 'driver' => 'pdo_mysql'
, 'driverOptions' => array(
1002 => "SET NAMES utf8mb4"
)
);
$cacheDriver = null;
$config = \Doctrine\ORM\Tools\Setup::createAnnotationMetadataConfiguration(array(), $isDevMode, '.cache/', $cacheDriver, false);
if($cacheDriver){
$config->setMetadataCacheImpl($cacheDriver);
$config->setQueryCacheImpl($cacheDriver);
$config->setResultCacheImpl($cacheDriver);
}
$this->entityManager = \Doctrine\ORM\EntityManager::create($dbParams, $config);
return $this->entityManager;
}
/**
* #return \Doctrine\ORM\EntityManager
*/
public function getEntityManager(){
return $this->entityManager;
}
public function updateDatabaseSchema(){
$metaData = array();
$usedEntities = array(
'Test\Stackoverflow\User'
);
foreach($usedEntities as $entity){
$metaData[] = $this->entityManager->getClassMetadata($entity);
}
$tool = new \Doctrine\ORM\Tools\SchemaTool($this->entityManager);
$tool->updateSchema($metaData);
$this->generateProxies($metaData);
}
/**
* Generate all the proxy classes for orm in the correct directory.
* Proxy dir can be configured over application configuration
*
*
* #throws \Exception
*/
final public function generateProxies($metaData)
{
$em = $this->getEntityManager();
$destPath = $em->getConfiguration()->getProxyDir();
if (!is_dir($destPath)) {
mkdir($destPath, 0777, true);
}
$destPath = realpath($destPath);
if (!file_exists($destPath)) {
throw new \Exception("Proxy destination directory could not be created " . $em->getConfiguration()->getProxyDir());
}
if (!is_writable($destPath)) {
throw new \Exception(
sprintf("Proxies destination directory '<info>%s</info>' does not have write permissions.", $destPath)
);
}
if (count($metaData)) {
// Generating Proxies
$em->getProxyFactory()->generateProxyClasses($metaData, $destPath);
}
}
/**
* #var \Symfony\Component\Validator\Validator\ValidatorInterface
*/
protected $validator;
/**
* #return \Symfony\Component\Validator\Validator\ValidatorInterface
*/
final protected function getValidator(){
if($this->validator){
return $this->validator;
}
$this->validator = \Symfony\Component\Validator\Validation::createValidatorBuilder()
->enableAnnotationMapping()
->getValidator();
return $this->validator;
}
/**
* #param \Test\Stackoverflow\User $entity
* #return bool
*/
final public function testAnnotationWithoutLoading(\Test\Stackoverflow\User $entity){
try {
print "test to validate the entity without preloading the Assert classes\n<br>";
$this->getValidator()->validate($entity);
return true;
} catch(\Exception $e){
print "<strong>Does not work since the Asserts classes never get loaded: </strong> Exception-message: ".$e->getMessage()."\n<br>";
return false;
}
}
/**
* #param \Test\Stackoverflow\User $entity
* #return bool
*/
final public function testAnnotationWithLoading(\Test\Stackoverflow\User $entity){
// Here we force the autoloader to require the class
$notBlankValidator = new \Symfony\Component\Validator\Constraints\NotBlank();
try {
print "Loaded the validator manually, will test of it fails now\n<br>";
$this->getValidator()->validate($entity);
return true;
} catch(\Exception $e){
print "<strong>Was not working: </strong> Exception-message: ".$e->getMessage()."\n<br>";
print sprintf("<strong>Even when we autoload the class it is not working. Type of assert: %s</strong>\n<br>", get_class($notBlankValidator));
return false;
}
}
}
If you are using the Symfony Standard Edition, you must update your
autoload.php file by adding the following code [1]
How are these annotations loaded? From looking at the code you could
guess that the ORM Mapping, Assert Validation and the fully qualified
annotation can just be loaded using the defined PHP autoloaders. This
is not the case however: For error handling reasons every check for
class existence inside the AnnotationReader sets the second parameter
$autoload of class_exists($name, $autoload) to false. To work
flawlessly the AnnotationReader requires silent autoloaders which many
autoloaders are not. Silent autoloading is NOT part of the PSR-0
specification for autoloading. [2]
// at the top of the file
use Doctrine\Common\Annotations\AnnotationRegistry;
// at the end of the file
AnnotationRegistry::registerLoader(function($class) use ($loader) {
$loader->loadClass($class);
return class_exists($class, false);
});
[1] https://symfony.com/blog/symfony2-2-0-rc4-released
[2] https://www.doctrine-project.org/projects/doctrine-annotations/en/1.6/annotations.html

How to override routes extending a Controller in Symfony2

I created a BaseController to make generic REST Api calls, but in some cases, I need to extends it and change some code on it.
class BaseController extends Controller {
/**
* #Route("/api/{entity_name}")
* #Method({"GET"})
* #Template()
*/
public function indexAction($entity_name) {
$inflector = new Inflector();
$class_name = ucfirst($inflector->camelize($entity_name));
$entities = $this->getDoctrine()->getManager()->getRepository('AppApiBundle:' . $class_name)->findAll();
$serializer = $this->get('jms_serializer');
return new JsonResponse($serializer->serialize($entities, 'json'), 200);
}
/**
* #Route("/api/{entity_name}/{id}")
* * #Method({"GET"})
* #Template()
*/
public function getAction($entity_name, $id) {
$inflector = new Inflector();
$class_name = ucfirst($inflector->camelize($entity_name));
$entitiy = $this->getDoctrine()->getManager()->getRepository('AppApiBundle:' . $class_name)->find($id);
$serializer = $this->get('jms_serializer');
return new JsonResponse($serializer->serialize($entitiy, 'json'), 200);
}
/**
* #Route("/api/{entity_name}")
* #Method({"POST"})
* #Template()
*/
public function postAction($entity_name) {
return array(
// ...
);
}
/**
* #Route("/api/{entity_name}/{id}")
* #Method({"PUT"})
* #Template()
*/
public function putAction($entity_name, $id) {
return array(
// ...
);
}
/**
* #Route("/api/{entity_name}/{id}")
* #Method({"DELETE"})
* #Template()
*/
public function deleteAction($entity_name, $id) {
return array(
// ...
);
}
}
class CountryController extends BaseController {
/**
* #Route("/api/country")
* #Method({"GET"})
* #Template()
*/
public function getAllAction() {
/**
* Another business rules here
*/
return array();
}
}
Then, if I call "/api/country" Symfony2 calls the BaseController's "/api/{entity_name}" route instead of the overrided route CountryController's "/api/country".
Does someone know how to solve this issue?
Thanks in advance.
Symfony checks the routes in the order it encounters them. You'll need to change to order of the entries in your routing configuration file.
Alternatively add a requirement parameter to your #Route annotation that excludes 'country' as a possible match.

Test that each page doesn't exceed allowed SQL count in Symfony?

I wish I could have an isSQLCountLessThan() function or something.
$browser = new sfTestFunctional(new sfBrowser());
$browser
->get('/some/page')
->with('response')->begin()
->isStatusCode(200)
// ...
->isSQLCountLessThan(20) // imagine how cool :)
->end();
Is there a way to have such?
I once created tester for this purpose. It's based on how it's done in web debug toolbar (sfWebDebugPanelDoctrine class).
I extended sfTesterDoctrine so it behaves the same. Only assertion method is added to check query count.
You could also overwrite debug() method to show query statistics.
<?php
/*
* (c) 2010 Jakub Zalas
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
/**
* #package zTestPlugin
* #subpackage test
* #author Jakub Zalas <jakub#zalas.pl>
*/
class zTesterDoctrine extends sfTesterDoctrine
{
/**
* #param integer $limit
* #return sfTestFunctionalBase|sfTester
*/
public function assertSqlCountLessThan($limit)
{
$queryCount = $this->countDoctrineEvents();
$this->tester->cmp_ok($queryCount, '<', (int) $limit, sprintf('There are less than "%d" SQL queries performed', $limit));
return $this->getObjectToReturn();
}
/**
* #return integer
*/
protected function countDoctrineEvents()
{
return count($this->getDoctrineEvents());
}
/**
* #return array
*/
protected function getDoctrineEvents()
{
if (!$databaseManager = $this->browser->getContext()->getDatabaseManager())
{
throw new LogicConnection('The current context does not include a database manager.');
}
$events = array();
foreach ($databaseManager->getNames() as $name)
{
$database = $databaseManager->getDatabase($name);
if ($database instanceof sfDoctrineDatabase && $profiler = $database->getProfiler())
{
foreach ($profiler->getQueryExecutionEvents() as $event)
{
$events[$event->getSequence()] = $event;
}
}
}
ksort($events);
return $events;
}
}
Example usage:
$browser = new sfTestFunctional(new sfBrowser());
$browser->setTester('doctrine', 'zTesterDoctrine');
$browser
->get('/some/page')
->with('response')->begin()
->isStatusCode(200)
->end()
->with('doctrine')->begin()
->assertSqlCountLessThan(20) // imagine how cool :)
->end()
->end();

Inserting a form into a block in Drupal?

Is there any command or method that I can use to insert the contents of a form (e.g. the user registration form) into a block?
In Drupal 7, it looks like this:
function yourmodule_block_view($delta='')
{
switch($delta) {
case 'your_block_name':
$block['subject'] = null; // Most forms don't have a subject
$block['content'] = drupal_get_form('yourmodule_form_function');
break;
}
return $block;
}
The form array returned by drupal_get_form will be automatically rendered.
yourmodule_form_function is a function (in your module or an existing Drupal module) that returns the form array;
drupal_get_form($form_id) - put it in a module's hook_block ($op=='view') or even... shudder... inside a block with PHP filter on.
You need to find the form id first - look for a hidden input with the name form_id within the form. Its value should be the the form id.
Also, you could simply use the Form Block module.
Drupal 8+ solution
Create the form. Then, to create the block use something like this:
<?php
namespace Drupal\my_module\Plugin\Block;
use Drupal\Core\Block\BlockBase;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\my_module\Form\MyForm;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides the My Block block.
*
* #Block(
* id = "my_block",
* admin_label = #Translation("My Block")
* )
*/
class MyBlock extends BlockBase implements ContainerFactoryPluginInterface {
/**
* The form builder.
*
* #var \Drupal\Core\Form\FormBuilder
*/
protected $formBuilder;
/**
* Constructs a new MyBlock object.
*
* #param array $configuration
* A configuration array containing information about the plugin instance.
* #param string $plugin_id
* The plugin_id for the plugin instance.
* #param mixed $plugin_definition
* The plugin implementation definition.
* #param \Symfony\Component\DependencyInjection\ContainerInterface $container
* Our service container.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, ContainerInterface $container) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->formBuilder = $container->get('form_builder');
}
/**
* {#inheritdoc}
*/
public static function create(ContainerInterface $container, array $configuration, $plugin_id, $plugin_definition) {
return new static(
$configuration,
$plugin_id,
$plugin_definition,
$container
);
}
/**
* {#inheritdoc}
*/
public function build() {
$form = $this->formBuilder->getForm(MyForm::class);
return $form;
// // Or return a render array.
// // in mytheme.html.twig use {{ form }} and {{ data }}.
// return [
// '#theme' => 'mytheme',
// "#form" => $form,
// "#data" => $data,
// ];
}
}

Resources