oliverdavies.uk/source/_daily_emails/2024-02-17.md

60 lines
2.6 KiB
Markdown
Raw Permalink Normal View History

---
title: Another way to create test module configuration
date: 2024-02-17
permalink: daily/2024/02/17/another-way-to-create-test-module-configuration
snippet: |
2024-09-08 22:09:54 +00:00
There's another way to export configuration for Drupal test modules using Drush...
tags:
2024-09-08 22:09:54 +00:00
- software-development
- automated-testing
- test-driven-development
- drupal
- php
---
{% block content %}
In one of the lessons in my [free automated testing in Drupal email course][atdc], I explain how I create configuration that I need within my tests, such as adding a custom field:
> But how do you know what to name the configuration files and what content to put in them?
>
> Rather than trying to write them by hand, I create the configuration I need, such as fields, within a Drupal site and then export and edit the files I need.
As well as creating the fields in the Drupal UI, I was also using it to export the configuration files I needed:
> Once Drupal is installed and the configuration has been created, you can go to - /admin/config/development/configuration/single/export and select the configuration type and name.
>
> The filename is shown at the bottom of the page, and you can copy the content into files within your module.
## There's another way
After reading that lesson, somene replied and reminded me that there's a `--destination` option you can use with the `drush config:export` command.
Instead of exporting to the standard configuration directory, I can do it to a temporary directory:
```language-shell
run drush cex --destination /app/.ignored/config
```
Everyhing in a `.ignored` direcotry is automatically ignored by Git, and to get the files I need, I can use Linux's `find` command to find any files that contain the field name and copy them into my test module directory:
```language-shell
find .ignored/config \
-type f \
-name \*drupal_project\* \
-exec cp -r {} web/modules/custom/foo/modules/foo_test/config/install \;
```
I still need to edit the files to remove the `uuid` and `_core` values, but this approach means less clicking in the Drupal UI which makes me more productive.
I used this approach when [writing my SaaS code yesterday][yesterday] and it worked well.
[atdc]: {{site.url}}/atdc
[yesterday]: {{site.url}}/daily/2024/02/16/keep-logic-within-tests-for-as-long-as-you-can
{% endblock %}
{% block cta %}
P.S. Do you need immediate access to an expert Drupal Developer? [With my Drupal development subscription][subscription], make unlimited requests for a fixed monthly price in less time than posting to a job board!
[subscription]: {{site.url}}/subscription
{% endblock %}