Intermittent issue with missing data-media-id

Created on 21 July 2025, 3 months ago

Problem/Motivation

Sometimes media test fails with error

Error: data-media-id is missing. Possible reasons:
 * Element is not a media item.
 * Automated Testing Kit is not installed and enabled:

     `drush en automated_testing_kit`

 * Issue in the module, check: https://www.drupal.org/project/issues/automated_testing_kit?categories=1

Steps to reproduce

Run tests against default installation

πŸ› Bug report
Status

Active

Version

2.0

Component

Code

Created by

Live updates comments and jobs are added and updated live.
Sign in to follow issues

Merge Requests

Comments & Activities

  • Issue created by @ilyaukin
  • πŸ‡ΊπŸ‡ΈUnited States aangel
  • First commit to issue fork.
  • πŸ‡©πŸ‡ͺGermany harkonn

    data-media-id was missing for images in public filesystem, when the same test-image was uploaded in different months. This is more a problem for developing the tests while the end of a month occurs and you run the same tests in the same project. Deleting the media-entity is not enough, the files from the filesystem must be removed also.

    Explanation:
    Drupal puts the test images in the public filesystem in a new folder for each month the test runs, examples:
    - sites/default/files/2025-07/RobotsAtDesk.png
    - sites/default/files/2025-07/RobotsAtDesk_0.png
    - sites/default/files/2025-07/RobotsAtDesk_1.png
    - sites/default/files/2025-08/RobotsAtDesk.png
    - sites/default/files/2025-08/RobotsAtDesk_0.png
    - sites/default/files/2025-08/RobotsAtDesk_1.png

    The hook automated_testing_kit_preprocess_image now finds two files for the same name with this code $file = \Drupal::entityTypeManager()->getStorage('file')->loadByProperties(['filename' => $filename]); and so it doesn't set data-media-id.

    There may be more circumstances like the description of this issue says, my code fixes one source.

Production build 0.71.5 2024