Skip to content

Instantly share code, notes, and snippets.

View jhedstrom's full-sized avatar

Jonathan Hedstrom jhedstrom

  • Phase2
  • Portland, OR
View GitHub Profile
@jhedstrom
jhedstrom / NodeViewTest.php
Created October 15, 2014 21:34
Gherkin as an abstraction to complex tests
<?php
/**
* @file
* Contains \Drupal\node\Tests\NodeViewTest.
*/
namespace Drupal\node\Tests;
/**
@jhedstrom
jhedstrom / FeatureContext.php
Last active June 24, 2021 12:55
Using the Behat Drupal Extension on sites with basic auth
<?php
/**
* Run before every scenario.
*
* @BeforeScenario
*/
public function beforeScenario() {
if ($basic_auth = $this->getDrupalParameter('basic_auth')) {
$driver = $this->getSession()->getDriver();
if ($driver instanceof Selenium2Driver) {
@jhedstrom
jhedstrom / foo.module
Created May 13, 2014 22:10
Search API Solr - index one node type as another
<?php
/**
* Implements hook_search_api_solr_documents_alter().
*
* Legacy pages should simply be indexed as normal pages.
*/
function MYMODULE_search_api_solr_documents_alter(array &$documents, SearchApiIndex $index, array $items) {
if ($index->item_type == 'node') {
foreach ($documents as $document) {
@jhedstrom
jhedstrom / varnish-probe.php
Created May 7, 2014 23:40
Varnish health probe
<?php
/**
* @file
* A health-check probe for Varnish.
*
* Based on the probe found here
* http://www.lullabot.com/blog/article/configuring-varnish-high-availability-multiple-web-servers
*/
// Register our shutdown function so that no other shutdown functions run before this one.
@jhedstrom
jhedstrom / gist:0c06cf2caca1cac0b517
Created May 1, 2014 23:22
How do disable module shutdown functions on long-running Behat Drupal Extension processes
<?php
/**
* Register a shutdown function.
*
* We manually call shutdown functions, so register one to exit immediately.
*
* @BeforeSuite
*/
public static function registerShutdownFunction() {
@jhedstrom
jhedstrom / FeatureContext.php
Last active August 29, 2015 13:57
Behat steps for the Achievements module
<?php
// Snippet method in FeatureContext class.
/**
* @Then /^I should see the "(?P<achievement>[^"]*)" achievement "(?P<status>[^"]*)"$/
*/
public function iShouldSeeTheAchievement($achievement, $status) {
if (!in_array($status, array('unlocked', 'locked'))) {
throw new \Exception(sprintf("Achievement status should either be 'locked' or 'unlocked', '%s' given instead.", $status));
@jhedstrom
jhedstrom / gist:9631346
Created March 18, 2014 22:38
A CTools content type plugin for the achievements module
<?php
/**
* @file
* Per-user badges/achievements CTools content type plugin.
*/
/**
* The Achievements plugin definition.
*/
$plugin = array(
<?php
/**
* @file
* Custom Entity Reference selection handler.
*/
class MyModuleSelectionHandler extends OgSelectionHandler {
/**
@jhedstrom
jhedstrom / FeatureContext.php
Created October 17, 2013 00:47
Test for custom Google Analytics variables via Behat and Mink.
<?php
/**
* @Then /^the google "(?P<variable>[^"]*)" variable should be set to "(?P<value>[^"]*)"$/
*/
public function assertGoogleAnalyticsCustomVariable($variable, $value) {
$map = array(
// Custom variable name => Custom variable index.
'gender' => 1,
'age range' => 2,
@jhedstrom
jhedstrom / gist:6125918
Created July 31, 2013 20:36
Running Firefox headless for selenium/Behat tests
if [ ! -f /tmp/.X5-lock ]; then /usr/bin/Xvfb :5 -ac -screen 0 1024x768x8 & fi
export DISPLAY=:5.0 # firefox needs this to know where to find a display to run on
java -jar /opt/selenium/selenium-server-standalone-2.26.0.jar > /dev/null 2>&1 &