Credit: photo by enjoiskate8
Dummy content is very useful for testing and other purposes. In this blog post I'll present some of the many tools that Drupal has to create dummy content. I'll explain why I think Migrate is the tool that is most suitable for this task.
I have presented about this topic at the at the Drupal Dev Days 2022. and at Drupaljam 2022.
What is Dummy content?
Dummy content is everything you would like to have available when you start a new website: so that could be pages, users, taxonomy terms. So it is not just nodes, it could also be menu's, paragraphs, files or media as well. In Drupal terms: everything that is not config.
Dummy content can also be referred to as "demo" or "default" content. This depend on the goals for which you want to use it.
Another import feature of dummy content is that it can be stored in version control so that you have full control over it and you can deploy it to various environments
Purposes
Dummy content is good for automatic and manual testing: it provides reliable and predictable data. For example BackstopJS will compare screenshots of the website before and after a certain change. For this you need the content to be stable.
Also for theming and designing it is great to have dummy content available so that the website already contains menu's, blocks, headers, footers and nodes with all kind of possible paragraphs combinations, when you pass on your work to a colleague.
Obviously for demo's and distributions you like to include dummy content so that you can showcase the product.
Finally dummy content can be used as "starter" content. For example if your site depends on certain taxonomy terms or menu items to be available. .
Tools
There are various modules and methods that will help you generate dummy content:
- Default content
- Migrate default content
- Devel generate
- Realistic dummy content
- Install profiles
- Migrate & Migrate tools
Default Content
The contrib module Default Content creates entities (nodes, users, terms) during installation of a module or profile. You can create the content as you would normally do in Drupal and Default Content contains an function to export the content as YAML files.
During the export any referenced entities are included in the export file. Because of the entity references in the YAML files, they are not suitable to be edited in a text editor. The module does a good job in moving content between environments, but given that it lacks an update function, it is hard to create dummy data inside text files and deploy these to other environments.
Migrate Default Content
Migrate Default Content creates and updates entities using Drush. The text files that are needed to define the dummy content are very easy to read and write and therefore it is suitable for writing dummy content manually.
So Migrate Default Content is a good tool for generating predictable & reliable dummy content on each environment. But it is a kind of simplification of Migrate and it makes one wonder why not use Migrate directly.
Devel Generate
Devel Generate is probably the most well know tool for generating dummy data. It is part of the Devel module and it allows to create and delete entities using the Drupal admin UI or Drush commands. Devel Generate creates chaotic content as it will try to fill fields with whatever it can imagine. The end result is often not recognizable as realistic content for the end user. But is does a good job at creating random test content and testing field configurations.
For a bit more realistic content you should check out the Realistic Dummy Content module which replaces the Devel Generate test images with stock photos and which is able to fill fields with predefined strings and files.
Note that there is ecosystem of contrib modules around Devel Generate that allows all kind of dummy content to be generated.
Install profiles
Dummy content could also be provided by a a Drupal install profile. This will create entities from code, but this code is hard to write and maintain. So this will require proper resources. Have a look at the Drupal core Umami profile for examples of creating dummy content in this way.
Migrate & Migrate tools
Migrate allows you to create, update and delete dummy entities using Drush commands. The dummy content needs to be defined inside a migration configuration file in YAML format. These YAML files are more or less human readable and writable and that makes them suitable for generating dummy content.
Example code for dummy content with Migrate
Under the hood
Recently it has become possible to use migrations as plugins. This means that changes in the migration configuration file are available after doing a cache rebuild. In the old days migrations were configuration so that it was much more cumbersome to make any code changes available to Drupal. To use migrations as a plugin, they need to be stored in a folder named "migrations" within a custom module.
Another feature that was recently added to Migrate is the embedded_data source plugin this allows you to keep data and configuration in the same file and as a result you can create migration YAML files for dummy data.
Thanks to Migrate Tools it is possible to run these migrations from the command line, which is essential for developing migrations.
My github repository contains a website with a custom module called "dummystuff" which has a subfolder "migrations" that contains seven migration files that show case how migrate can be used to generate dummy content:
- articles.yml: dummy news articles, including references to files (for image fields) and taxonomy terms (as tags)
- files.yml: dummy managed files in Drupal. These are being used in the image fields of news articles
- menus.yml: dummy menu items for the main and footer menu
- pages.yml: dummy basic pages, including references to text paragraphs
- tags.yml: dummy Tags terms
- Texts.yml: dummy text paragraphs with a header and body field
- Users.yml: dummy users in various roles
Additional notes
When using Migrate to generate dummy content there are a couple of things to be aware of. First, every entity must be migrated separately. This can lead to many migration files which at some point will become hard to control.
Secondly, blocks seem to be hard to create using migrate. A bit more research is needed on how to generate dummy blocks with Migrate.
Since the migrations are plugins they will not show up in the Migrate UI admin screens. This is a known problem that has no solutions yet.
Finally, if you use Migrate you have much more possibilities than discussed in this blog post. You could for example start including CSV files with dummy data in your migrations.
I hope I will be able to further experiment with this technique in my upcoming projects in order to see if it can be improved and to see what other possibilities it offers. If you use it too, then please drop me a note.
References
- Presentation at DrupalCamp Frankfurt covering Default Content & Migrate Default Content
- Blogpost by Mediacurrent about creating dummy content with Migrate
- Migrate course that contains embedded_data plugin examples
- Example code for generating dummy content with Migrate
- Presentation slides