Drupal

Debugging eMails in Symfony 2/3 and Drupal 7/8

Submitted by Peter Majmesku on Fri, 06/30/2017 - 22:33

The best tool for debugging eMails locally in my opinion is Mailcatcher. It catches all mails being sent via a specific port and shows them to you via an neat interface in your webbrowser. Firstly install Mailcatcher. It's programmed in Ruby and I could install it easily on Ubuntu and Mac. The instructions can be found at https://mailcatcher.me/.

Symfony 2/3

In the full stack edition it comes with symfony/swiftmailer-bundle. Make sure these settings are in your parameters.yml + parameters.yml.dist file.

mailer_transport: smtp
mailer_host: 127.0.0.1:1025
mailer_user: ~
mailer_password: ~

Afterwards you can send a test mail via the console:

bin/console swiftmailer:email:send

The mails will be shown to you like in the screenshot below.

The Mailcatcher Interface in Webbrowser

Drupal 7/8

Beside Drupal 8 install the "SMTP Authentication Support". You can find it's project webpage at https://www.drupal.org/project/smtp. Then visit the following page in your Drupal 8 instance: http://your-neat-webpage.dev/admin/config/system/smtp. Make sure the following settings are set in the config form (there's also a possibility to set them in the settings.php file).

SMTP server: 127.0.0.1
SMTP backup server: LEAVE BLANK
SMTP port: 1025
Use encrypted protocol: No

SMTP AUTHENTICATION: LEAVE USERNAME + PASSWORD BLANK

E-MAIL OPTIONS: ANY EMAIL AND FROM NAME

SEND TEST E-MAIL: ANY E-MAIL ADDRESS

Check "Enable debugging".

First part of settings form in Drupal 8
Second part of settings form in Drupal 8

Drupal 8: Debug Kernel, Functional and Unit Tests in PhpStorm by Xdebug

Submitted by Peter Majmesku on Sun, 04/16/2017 - 20:21

The following article is valid for unix based systems like Ubuntu Linux. It teaches you, how to setup your PhpStorm IDE to debug your unit tests via the Xdebug PHP extension in Drupal 8.

  1. Make sure you have read the article at https://www.drupal.org/docs/8/phpunit/running-phpunit-tests to understand how to run automated tests in Drupal 8.
  2. Make sure you have read the article at https://www.jetbrains.com/help/phpstorm/2017.1/configuring-xdebug.html to understand how to setup the Xdebug PHP extension on your system and configure it within PhpStorm.

phpunit.xml Configuration File

For the PhpStorm config you need to make sure to have the phpunit.xml file created by copying the ~/PhpstormProjects/drupal-8.x/core/phpunit.xml.dist into the same directory. Afterwards you must set the environment variables for 

  • SIMPLETEST_BASE_URL
  • SIMPLETEST_DB
    • Example value: mysql://user:password@localhost/d8-4.x
    • You can also use a SQLite database (faster). Mind the two slashes between the host and the filesystem location: sqlite://localhost//tmp/test.sqlite
  • BROWSERTEST_OUTPUT_DIRECTORY
    • Example value: ~/PhpstormProjects/drupal-8.4.x-dev/sites/simpletest/browser_output
  • PHP_IDE_CONFIG
    • Example value: serverName=d8-4x.dev
    • Be careful in this line! Here you set a key-value pair in the value field. Do not use any quotes here. The full line entry in my config is: <env name="PHP_IDE_CONFIG" value="serverName=d8-4x.dev"/>
  • XDEBUG_CONFIG
    • Example value: idekey=PHPSTORM
    • Again: a key-value pair as value.

in your newly created phpunit.xml file.

PHPUnit Config in Your PhpStorm Project

Make sure you have configured:

  • path to the autoloader script from composer. It is located in ~/PhpstormProjects/drupal-8.4/vendor/autoload.php
  • path to your phpunit.xml file

The config window must look according to the following screenshot (be careful about the paths on your system):

 

PHPUnit config

Define the Server Setting and Map the Tests Directory

Set exactly the same value, which you have previously set in your phpunit.xml file inside the PHP_IDE_CONFIG environment variable for your server. You must also map the directory where your tests are. Do your settings according to the screenshot below.

Server setting directory mapping.

Uncheck the option  "Force break at first line when no path mapping specified" in PHPStorm

You might experience an error like "Cannot find file 'home/user/project/root/-' locally". Therefor you must uncheck the option "Force break at first line when no path mapping specified" at Settings/Languages & Frameworks/PHP/Debug in PHPStorm.

Screenshot
Screenshot

Path to CLI PHP Interpreter

Set the path to the PHP interpreter which is running your command line based scripts. It is usually located at /usr/bin/php. The config must look like in the following screenshots.

php interpreter
php cli

Run a Test File

For being able to execute your test quickly, open the PHP file with your test and hit the debug icon.

Run the test

Step into your PHP code

Happy debugging

Drupal 8: Execute automated tests and debug them in PhpStorm

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:51

To test Drupal 8 tests by the command line, you need to do the following. You need to export two environment variables first:

export SIMPLETEST_DB=mysql://root:password@localhost/database;
export SIMPLETEST_BASE_URL=http://host;

Then you can execute the test by the following command, being in the Drupal root folder:

vendor/bin/phpunit -c core modules/my_module/tests/src/Kernel/MyTest.php

Make sure to set the environment variable for PhpStorm:

export XDEBUG_CONFIG="remote_enable=1 remote_mode=req remote_port=9000 remote_host=127.0.0.1 remote_connect_back=0"

phpunit.xml 

There's a phpunit.xml.dist file in PHP projects. That's a template for your PHPUnit configuration. You can specifiy there some PHP ini settings. By this method you do not need to export the variables like above. Copy it to phpunit.xml and set there your settings like follows:

 

<phpunit bootstrap="tests/bootstrap.php" colors="true"

beStrictAboutTestsThatDoNotTestAnything="true"

beStrictAboutOutputDuringTests="true"

beStrictAboutChangesToGlobalState="true"

checkForUnintentionallyCoveredCode="false"

printerClass="\Drupal\Tests\Listeners\HtmlOutputPrinter">

<php>

<!-- Set error reporting to E_ALL. -->

<ini name="error_reporting" value="32767"/>

<!-- Do not limit the amount of memory tests take to run. -->

<ini name="memory_limit" value="-1"/>

<!-- Example SIMPLETEST_BASE_URL value: http://localhost -->

<env name="SIMPLETEST_BASE_URL" value="http://pbt-d8"/>

<!-- Example SIMPLETEST_DB value: mysql://username:password@localhost/databasename#table_prefix -->

<env name="SIMPLETEST_DB" value="mysql://root:password@localhost/pbt-d8"/>

<!-- Example BROWSERTEST_OUTPUT_DIRECTORY value: /path/to/webroot/sites/simpletest/browser_output -->

<env name="BROWSERTEST_OUTPUT_DIRECTORY" value="/home/peter/d8-tests"/>

</php>

</phpunit>

Afterwards you are able to run your test without the manual environment variables setting, just by specifying your configuration directory by the "-c" parameter. Example:

vendor/bin/phpunit -c modules/permissions_by_term modules/permissions_by_term/tests/src/Kernel/SelectTermTest.php

This command cannot execute SimpleTest tests, which inherit from the WebTestBase class. Only unit tests and tests which inherits from the KernelTestBase class can be run.

Runs a SimpleTest

// --- SimpleTests
php core/scripts/run-tests.sh --url http://d8-paragraphs:8888 --browser --verbose --color --concurrency 4 --class 'Drupal\paragraphs\Tests\ParagraphsPreviewTest'

Troubleshooting

If the debug mode is switched on in PhpStorm and the debugger does not stop at the breakpoint and you are getting the following output in the debugger window:

Remote file path '/home/user/Websites/my-project/-' is not mapped to any file path in project

 If you have already setup path mapping, then there's probably any error in your code. Chances are, that it is in the first line of the setUp() method of your test class, which inherits from KernelTestBase. Then you probably do not have Drupal 8.2.x but Drupal 8.1.x. If it's in the first line, you can insert any code line or insert

xdebug_break(); 

With this function the debugger will be forced to stop on a specific line.


PhpStorm IDE settings (version 2016.1.2)

If you are executing the tests from the command line (and probably also via the IDE itself) it's important that you setup the path mappings, so that Xdebug can recognize your test's location. You can to this in PhpStorm's settings at "Languages & Frameworks > PHP > Servers". Create a host for your Drupal website host and set the path mapping to your tests directory. An example you can find in the screenshot below: 

PhpStorm Path Mapping

Drush: Rebuild the cache of a single Drupal (8) site in a multi-site setup with the magic "-l" parameter

Submitted by Peter Majmesku on Mon, 08/29/2016 - 20:02

After lots of googling, testing, reading and so on, I've finally found out the Drush command to flush the cache of a single Drupal (8) site in a multi-site setup.

drush -l 8888.notes cache-rebuild

If your site is located at /sites/8888.notes you need the command above. The "-l" parameter is necessary. I could not find out what "-l" means, but this parameter seemed necessary. 

Tags
Subscribe to Drupal