Skip to content

Instantly share code, notes, and snippets.

@joachim-n
joachim-n / KernelTestWithMockedClientRequestsExampleTest.php
Created June 18, 2024 09:14
Mocking Guzzle to handle client requests in a test
<?php
namespace Drupal\Tests\external_entities\Kernel;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\KernelTests\KernelTestBase;
use GuzzleHttp\Promise\FulfilledPromise;
use GuzzleHttp\Psr7\Response;
use Psr\Http\Message\RequestInterface;
@joachim-n
joachim-n / gist:a75ceed3186d63dc2a13e66bc799596d
Created March 26, 2024 13:51
inline call for loop list vs variable assignment
<?php
// THIS DOESN'T WORK!
// SOME WEIRD INTERNAL CACHING IS HAPPENING - whichever is executed second
// is faster!
function getLoopList() {
return range(1, 10);
}
@joachim-n
joachim-n / gist:4f1c046d61a68384c416eb806a904249
Created December 1, 2023 10:25
Script for switching a project to use a git clone of a Drupal contrib module
#!/usr/bin/env bash
# Script for switching a project to use a git clone of a Drupal contrib module.
if [ ! -d repos/$1 ]; then
# Get the version of the module that the project currently has installed
# so we can clone the same version. This ensures that Composer will accept to
# install it.
VERSION=`php -r "require_once 'vendor/autoload.php'; print \Composer\InstalledVersions::getReference('drupal/$1');"`
@joachim-n
joachim-n / gist:56e37400a7e953c2d2015d342716f783
Created November 29, 2023 11:04
Mock discovery in plugin manager
<?php
namespace Drupal\Tests\list_predefined_options\Kernel;
use Drupal\Component\Plugin\Discovery\DiscoveryInterface;
use Drupal\Component\Plugin\Exception\InvalidPluginDefinitionException;
use Drupal\Core\DependencyInjection\ContainerBuilder;
use Drupal\Core\DependencyInjection\ServiceModifierInterface;
use Drupal\KernelTests\KernelTestBase;
use Drupal\list_predefined_options\ListOptionsManager;
@joachim-n
joachim-n / kernel test form submission
Created April 7, 2023 12:33
Make a POST request to a form
/**
* Make a POST request to a form.
*
* @param string $url
* @param array $data
*/
protected function doPostForm(string $url, array $data = []) {
$request = Request::create($url);
$response = $this->doRequest($request);
$this->assertEquals(Response::HTTP_OK, $response->getStatusCode());
public static function check($checking_key, $data) {
try {
serialize($data);
}
catch (\Throwable $e) {
// The data fucked up. Go into it to see where.
ddm("CATCH $checking_key");
if (is_array($data)) {
foreach ($data as $key => $val) {
static::check($key, $val);
@joachim-n
joachim-n / dashcam-blank.js
Created July 10, 2022 20:51
Bookmarklet for filling in Dashcam submission form
// Fill in the details with your own.
// Paste this as the URL of a new Firefox bookmark.
// Use the bookmark when on https://secureform.nextbase.co.uk/
javascript: {
document.getElementById('signature').value = '';
document.getElementById('name').value = '';
// Doesn't work yet.
// document.getElementById('preferredContact').value = '0';
document.getElementById('email').value = '';
document.getElementById('phone').value = '';
#!/usr/bin/perl -p
# show what the hell is happening with line endings
s[ (?<! \r ) \n ]
[UNIX\n]x;
s[ \r\n]
[WIN\n]x;
@joachim-n
joachim-n / gist:989eb0d1ca54ce756350
Created February 3, 2015 13:08
Drush shell script to diff and write a patch file.
#!/usr/bin/env drush
<?php
/**
* @file
* Drush shell script for creating a patch.
* Requires the following setup:
* - You are on a git feature branch for your issue.
* - The branch name is of the format '1234-description-of-feature' where 1234
* is the drupal.org issue number.
* - The feature branch is rebased to the major development branch you want to
@joachim-n
joachim-n / gist:9626938
Created March 18, 2014 18:56
validation handler
$selected_sessions = array();
foreach (array_keys($form_state['values']['timeslots']) as $timeslot_id) {
foreach (array_keys($form_state['values']['timeslots'][$timeslot_id]['rooms']) as $room_id) {
$cell_form_state_value = $form_state['values']['timeslots'][$timeslot_id]['rooms'][$room_id]['session'];
if (!empty($cell_form_state_value)) {
$selected_sessions[$cell_form_state_value][] = array($timeslot_id, $room_id);
}
}
}