Newsfeeds

Twig field

New Drupal Modules - 6 May 2018 - 11:22am

A simple module that provides Twig field type.

Categories: Drupal

Ban By Geo Location

New Drupal Modules - 6 May 2018 - 10:41am

Ban by geoip location data

Categories: Drupal

Video Game Deep Cuts: Flash The Artifact, Get The Saga - by Simon Carless

Gamasutra.com Blogs - 6 May 2018 - 7:48am
This week's highlights include an attempt to archive key Flash games, a deeper look at Valve's upcoming Artifact, and a super-deep oral history of Panzer Dragoon Saga, among others.
Categories: Game Theory & Design

Oliver Davies: Creating a Custom PHPUnit Command for Docksal

Planet Drupal - 5 May 2018 - 5:00pm

This week I’ve started writing some custom commands for my Drupal projects that use Docksal, including one to easily run PHPUnit tests in Drupal 8. This is the process of how I created this command.

Categories: Drupal

Oliver Davies: Creating a Custom PHPUnit Command for Docksal

Planet Drupal - 5 May 2018 - 5:00pm
What is Docksal?

Docksal is a local Docker-based development environment for Drupal projects and other frameworks and CMSes. It is our standard tool for local environments for projects at Microserve.

There was a great talk recently at Drupaldelphia about Docksal.

Why write a custom command?

One of the things that Docksal offers (and is covered in the talk) is the ability to add custom commands to the Docksal’s fin CLI, either globally or as part of your project.

As an advocate of automated testing and TDD practitioner, I write a lot of tests and run PHPUnit numerous times a day. I’ve also given talks and have written other posts on this site relating to testing in Drupal.

There are a couple of ways to run PHPUnit with Docksal. The first is to use fin bash to open a shell into the container, move into the docroot directory if needed, and run the phpunit command.

fin bash cd /var/www/docroot ../vendor/bin/phpunit -c core modules/custom

Alternatively, it can be run from the host machine using fin exec.

cd docroot fin exec '../vendor/bin/phpunit -c core modules/custom'

Both of these options require multiple steps as we need to be in the docroot directory where the Drupal code is located before the command can be run, and both have quite long commands to run PHPUnit itself - some of which is repeated every time.

By adding a custom command, I intend to:

  1. Make it easier to get set up to run PHPUnit tests - i.e. setting up a phpunit.xml file.
  2. Make it easier to run the tests that we’d written by shortening the command and making it so it can be run anywhere within our project.

I also hoped to make it project agnostic so that I could add it onto any project and immediately run it.

Creating the command

Each command is a file located within the .docksal/commands directory. The filename is the name of the command (e.g. phpunit) with no file extension.

To create the file, run this from the same directory where your .docksal directory is:

mkdir -p .docksal/commands touch .docksal/commands/phpunit

This will create a new, empty .docksal/commands/phpunit file, and now the phpunit command is now listed under "Custom commands" when we run fin.

You can write commands with any interpreter. I’m going to use bash, so I’ll add the shebang to the top of the file.

#!/usr/bin/env bash

With this in place, I can now run fin phpunit, though there is no output displayed or actions performed as the rest of the file is empty.

Adding a description and help text

Currently the description for our command when we run fin is the default "No description" text. I’d like to add something more relevant, so I’ll start by adding a new description.

fin interprets lines starting with ## as documentation - the first of which it uses as the description.

#!/usr/bin/env bash ## Run automated PHPUnit tests.

Now when I run it, I see the new description.

Any additional lines are used as help text with running fin help phpunit. Here I’ll add an example command to demonstrate how to run it as well as some more in-depth text about what the command will do.

#!/usr/bin/env bash ## Run automated PHPUnit tests. ## ## Usage: fin phpunit <args> ## ## If a core/phpunit.xml file does not exist, copy one from elsehwere. ## Then run the tests.

Now when I run fin help phpunit, I see the new help text.

Adding some content Setting the target

As I want the commands to be run within Docksal’s "cli" container, I can specify that with exec_target. If one isn’t specified, the commands are run locally on the host machine.

#: exec_target = cli Available variables

These variables are provided by fin and are available to use within any custom commands:

  • PROJECT_ROOT - The absolute path to the nearest .docksal directory.
  • DOCROOT - name of the docroot folder.
  • VIRTUAL_HOST - the virtual host name for the project. Such as myproject.docksal.
  • DOCKER_RUNNING - (string) "true" or "false".

Note: If the DOCROOT variable is not defined within the cli container, ensure that it’s added to the environment variables in .docksal/docksal.yml. For example:

version: "2.1" services: cli: environment: - DOCROOT Running phpunit

When you run the phpunit command, there are number of options you can pass to it such as --filter, --testsuite and --group, as well as the path to the tests to execute, such as modules/custom.

I wanted to still be able to do this by running fin phpunit <args> so the commands can be customised when executed. However, as the first half of the command (../vendor/bin/phpunit -c core) is consistent, I can wrap that within my custom command and not need to type it every time.

By using "$@" I can capture any additional arguments, such as the test directory path, and append them to the command to execute.

I’m using $PROJECT_ROOT to prefix the command with the absolute path to phpunit so that I don’t need to be in that directory when I run the custom command, and $DOCROOT to always enter the sub-directory where Drupal is located. In this case, it’s "docroot" though I also use "web" and I’ve seen various others used.

DOCROOT_PATH="${PROJECT_ROOT}/${DOCROOT}" DRUPAL_CORE_PATH="${DOCROOT_PATH}/core" # If there is no phpunit.xml file, copy one from elsewhere. # Otherwise run the tests. ${PROJECT_ROOT}/vendor/bin/phpunit -c ${DRUPAL_CORE_PATH} "$@"

For example, fin phpunit modules/custom would execute /var/www/vendor/bin/phpunit -c /var/www/docroot/core modules/custom within the container.

I can then wrap this within a condition so that the tests are only run when a phpunit.xml file exists, as it is required for them to run successfully.

if [ ! -e ${DRUPAL_CORE_PATH}/phpunit.xml ]; then # If there is no phpunit.xml file, copy one from elsewhere. else ${PROJECT_ROOT}/vendor/bin/phpunit -c ${DRUPAL_CORE_PATH} "$@" fi Creating phpunit.xml - step 1

My first thought was that if a phpunit.xml file doesn’t exist was to duplicate core’s phpunit.xml.dist file. However this isn’t enough to run the tests, as values such as SIMPLETEST_BASE_URL, SIMPLETEST_DB and BROWSERTEST_OUTPUT_DIRECTORY need to be populated.

As the tests wouldn't run at this point, I’ve exited early and displayed a message to the user to edit the new phpunit.xml file and run fin phpunit again.

if [ ! -e ${DRUPAL_CORE_PATH}/phpunit.xml ]; then echo "Copying ${DRUPAL_CORE_PATH}/phpunit.xml.dist to ${DRUPAL_CORE_PATH}/phpunit.xml." echo "Please edit it's values as needed and re-run 'fin phpunit'." cp ${DRUPAL_CORE_PATH}/phpunit.xml.dist ${DRUPAL_CORE_PATH}/phpunit.xml exit 1; else ${PROJECT_ROOT}/vendor/bin/phpunit -c ${DRUPAL_CORE_PATH} "$@" fi

However this isn’t as streamlined as I originally wanted as it still requires the user to perform an additional step before the tests can run.

Creating phpunit.xml - step 2

My second idea was to keep a pre-configured file within the project repository, and to copy that into the expected location. That approach would mean that the project specific values would already be populated, as well as any customisations made to the default settings. I decided on .docksal/drupal/core/phpunit.xml to be the potential location.

Also, if this file is copied then we can go ahead and run the tests straight away rather than needing to exit early.

If a pre-configured file doesn’t exist, then we can default back to copying phpunit.xml.dist.

To avoid duplication, I created a reusable run_tests() function so it could be executed in either scenario.

run_tests() { ${PROJECT_ROOT}/vendor/bin/phpunit -c ${DRUPAL_CORE_PATH} "$@" } if [ ! -e ${DRUPAL_CORE_PATH}/phpunit.xml ]; then if [ -e "${PROJECT_ROOT}/.docksal/drupal/core/phpunit.xml" ]; then echo "Copying ${PROJECT_ROOT}/.docksal/drupal/core/phpunit.xml to ${DRUPAL_CORE_PATH}/phpunit.xml" cp "${PROJECT_ROOT}/.docksal/drupal/core/phpunit.xml" ${DRUPAL_CORE_PATH}/phpunit.xml run_tests "$@" else echo "Copying ${DRUPAL_CORE_PATH}/phpunit.xml.dist to ${DRUPAL_CORE_PATH}/phpunit.xml." echo "Please edit it's values as needed and re-run 'fin phpunit'." cp ${DRUPAL_CORE_PATH}/phpunit.xml.dist ${DRUPAL_CORE_PATH}/phpunit.xml exit 1; fi else run_tests "$@" fi

This means that I can execute less steps and run a much shorter command compared to the original, and even if someone didn’t have a phpunit.xml file created they could have copied into place and have tests running with only one command.

The finished file #!/usr/bin/env bash #: exec_target = cli ## Run automated PHPUnit tests. ## ## Usage: fin phpunit <args> ## ## If a core/phpunit.xml file does not exist, one is copied from ## .docksal/core/phpunit.xml if that file exists, or copied from the default ## core/phpunit.xml.dist file. DOCROOT_PATH="${PROJECT_ROOT}/${DOCROOT}" DRUPAL_CORE_PATH="${DOCROOT_PATH}/core" run_tests() { ${PROJECT_ROOT}/vendor/bin/phpunit -c ${DRUPAL_CORE_PATH} "$@" } if [ ! -e ${DRUPAL_CORE_PATH}/phpunit.xml ]; then if [ -e "${PROJECT_ROOT}/.docksal/drupal/core/phpunit.xml" ]; then echo "Copying ${PROJECT_ROOT}/.docksal/drupal/core/phpunit.xml to ${DRUPAL_CORE_PATH}/phpunit.xml" cp "${PROJECT_ROOT}/.docksal/drupal/core/phpunit.xml" ${DRUPAL_CORE_PATH}/phpunit.xml run_tests "$@" else echo "Copying phpunit.xml.dist to phpunit.xml" echo "Please edit it's values as needed and re-run 'fin phpunit'." cp ${DRUPAL_CORE_PATH}/phpunit.xml.dist ${DRUPAL_CORE_PATH}/phpunit.xml exit 0; fi else run_tests "$@" fi

It’s currently available as a GitHub Gist, though I’m planning on moving it into a public GitHub repository either on my personal account or the Microserve organisation, for people to either use as examples or to download and use directly.

I’ve also started to add other commands to projects such as config-export to standardise the way to export configuration from Drupal 8, run Drupal 7 tests with SimpleTest, and compile front-end assets like CSS within custom themes.

I think it’s a great way to shorten existing commands, or to group multiple commands into one like in this case, and I can see a lot of other potential uses for it during local development and continuous integration. Also being able to run one command like fin init and have it set up everything for your project is very convenient and a big time saver!

Resources
Categories: Drupal

Marquee

New Drupal Modules - 5 May 2018 - 3:46pm
Introduction

The Marquee module implements a custom field formatter to turn text fields into vastly superior and technically impressive scrolling marquees.

Categories: Drupal

Commerce Nordea

New Drupal Modules - 5 May 2018 - 3:06pm
Categories: Drupal

Commerce Verifone

New Drupal Modules - 5 May 2018 - 2:45pm
Categories: Drupal

A-Frame Extra

New Drupal Modules - 5 May 2018 - 2:23pm
Categories: Drupal

Content Moderation & Scheduled Updates integration

New Drupal Modules - 5 May 2018 - 12:37pm

Adds a validator ensuring scheduled updates for content moderation state updates are valid.

Adds a widget to the new state on scheduled update entity allowing user to select valid states of the parent entity of scheduled update.

Requires

Categories: Drupal

Review Roundup

Tabletop Gaming News - 5 May 2018 - 11:00am
Saturdaaaaaaaaaay! Woo! Time to go out and… schedule this post, have some breakfast, get groceries, and then do some gaming! Woo! Going to a friend’s place to hang out, have some game time, do some cooking out on the grill, and just having a good day, all before next week’s blitz that is CMON Expo. […]
Categories: Game Theory & Design

Multi Step Content Form

New Drupal Modules - 5 May 2018 - 10:33am
Categories: Drupal

Is Hacked

New Drupal Modules - 5 May 2018 - 5:43am

The Is Hacked module designed to detect changes in a site vcs repository.
The module makes sense only if you have a vcs command line tool installed on a server and PHP safe mode disabled or configured to allow a vcs command execution.

By default it works with git. You can change the module settings to make it working with other vcs.

Be safe!

Drupal 7 & 8 versions are available.

Categories: Drupal

Filter checkboxes

New Drupal Modules - 5 May 2018 - 3:01am

Module allows you filter big amount of the checkboxes quickly.

How to use it:

$form['options'] = array( '#type' => 'checkboxes', '#title' => 'Title', '#options' => $options, '#filter' => TRUE, );

Just add option #filter to the checkboxes element and module will do the rest.

Categories: Drupal

content entity builder

New Drupal Modules - 4 May 2018 - 11:42pm
One entity One table, keep your database table clean. No bundle, revision support, by design.

This module allow you create content entity type like add a node type, by using add base field to your content entity type, you could keep
your database table clean.

Try your best to use one entity one table instead of one field one table. We will get several benefits:
1, Better performance.
2, it will be much easier to migrate drupal to other system.

Categories: Drupal

Twitch tries 'Bounty Board' approach to setting streamers up for sponsored streams

Social/Online Games - Gamasutra - 4 May 2018 - 4:24pm

This week Twitch rolled out a beta of its Bounty Board program, which aims to cut out third-party marketing agencies by directly connecting Twitch streamers with companies who will pay them to stream. ...

Categories: Game Theory & Design

Wyrd Previews The Sharpshooter For The Other Side

Tabletop Gaming News - 4 May 2018 - 3:00pm
So, I don’t know what it’s like to be killed by a hitman. So I don’t know what it’s like just before you die. But I am going to assume it’s a lot like when you realize a mariachi band is about to play on the train. My apologies to John Mulaney for butchering that […]
Categories: Game Theory & Design

Forbidden Games Bringing Raccoon Tycoon To Kickstarter Next Month

Tabletop Gaming News - 4 May 2018 - 2:00pm
It’s a boom time in Astoria. Everyone’s looking to build new towns. It’s a good opportunity for enterprising individuals to ply their trade. In Raccoon Tycoon, you take on the role of a producer of a key good (such as wheat or stone) and must look to build towns or sell your products for the […]
Categories: Game Theory & Design

Tickets Coming Soon for Fantasy Flight&#8217;s Flight Report

Tabletop Gaming News - 4 May 2018 - 1:00pm
Gen Con has all sorts of important announcements going on during the show as each company talks about the new things they’re working on. For Fantasy Flight, that all happens during their In-Flight Report. This free event is open to the public, but space is limited. If you want in, you’ll need to secure your […]
Categories: Game Theory & Design

Cold &#038; Dark: Better Worlds Now Available in PDF

Tabletop Gaming News - 4 May 2018 - 12:00pm
The galaxy is full of all sorts of planets. With quite a lot of space, there’s lots of opportunity, but also plenty of dangers. For those traveling through space, it’s best to know what worlds are just pretty good, and which ones are better. Thankfully, Modiphius has released Better Worlds, a new sourcebook for Cold […]
Categories: Game Theory & Design

Pages

Subscribe to As If Productions aggregator