Wrong preg_replace in ImageViewBuilderTest removes relevant attributes

Created on 9 May 2023, over 1 year ago
Updated 19 August 2023, over 1 year ago

Problem/Motivation

Just lost 2 hours of my life wondering why the **** attributes in test class ImageViewBuilderTest <img> results were not created. In the end I found

/**
   * Renders a render array.
   */
  private function renderPlain(array $build): string {
    $html = $this->container->get('renderer')->renderPlain($build);
    $html = preg_replace('#src=".+/files/#s', 'src="/files/', $html);
    $html = preg_replace('#\?itok=.+"#', '?itok=abc"', $html);
    $html = preg_replace(['#\s{2,}#', '#\n#'], '', $html);
    return rtrim($html);
  }

and found out that the line:

$html = preg_replace('#\?itok=.+"#', '?itok=abc"', $html);

Removes all attributes after the itok.......

-'<img alt="Image Test Do" src="/files/styles/test/public/image-test-do.jpg?itok=abc" />'
+'<img alt="Image Test Do" src="/files/styles/test/public/image-test-do.jpg?itok=abc" width="10" height="10" loading="lazy" />'

wouza how I hate regex...

No offense, the same could have happened to me ...

Steps to reproduce

See above

Proposed resolution

Fix the regex to only replace the itok value and nothing else
Document the renderPlain method and add comments what each regex is expected to do.

Remaining tasks

User interface changes

API changes

Data model changes

🐛 Bug report
Status

Fixed

Version

3.0

Component

Code

Created by

🇩🇪Germany Anybody Porta Westfalica

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

Comments & Activities

Production build 0.71.5 2024