Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 9 additions & 4 deletions assets/src/blocks/godam-image/render.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,16 @@
*/
do_action( 'rtgodam_before_attachment_lookup' );

// Fall back to the attachment's own URL / alt / dimensions when not stored on
// the block (e.g. a copied block that carries only the id).
// Use the attachment's current URL, not the one saved on the block. The saved URL
// is whatever the editor had when the image was picked: the local uploads file for
// an image later served from the GoDAM CDN, or an older version of an image that
// was replaced since. The saved URL stays as the fallback for a deleted attachment.
// Alt and dimensions still come from the attachment only when not stored on the
// block (e.g. a copied block that carries only the id).
if ( $godam_attachment_id ) {
if ( '' === $godam_src ) {
$godam_src = (string) wp_get_attachment_image_url( $godam_attachment_id, 'full' );
$godam_current_src = (string) wp_get_attachment_image_url( $godam_attachment_id, 'full' );
if ( '' !== $godam_current_src ) {
$godam_src = $godam_current_src;
}
if ( '' === $godam_alt ) {
$godam_alt = (string) get_post_meta( $godam_attachment_id, '_wp_attachment_image_alt', true );
Expand Down
107 changes: 107 additions & 0 deletions tests/php/GodamImageCurrentUrlTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
/**
* Unit tests for the image URL the GoDAM Image block prints (#2401).
*
* The block saves the image URL when it is picked in the editor. That saved URL goes
* stale: it can be the local uploads file of an image GoDAM now serves from its CDN,
* or an older version of an image that was replaced since. The block prints the
* attachment's current URL and keeps the saved one only for a deleted attachment.
*
* Each test runs in its own process: the stubs below stand in for WordPress functions
* that other tests define differently.
*
* @package GoDAM
*/

namespace RTGODAM\Tests;

use PHPUnit\Framework\TestCase;

/**
* @runTestsInSeparateProcesses
* @preserveGlobalState disabled
*
* @coversNothing
*/
class GodamImageCurrentUrlTest extends TestCase {

private const SAVED_URL = 'https://example.test/wp-content/uploads/2026/11/photo.png';

protected function setUp(): void {
parent::setUp();

require_once dirname( __DIR__ ) . '/stubs/hooks.php';
require_once dirname( __DIR__ ) . '/stubs/godam-image-render-functions.php';
require_once dirname( __DIR__ ) . '/stubs/layer-registry.php';

$GLOBALS['rtgodam_hooks'] = array();
$GLOBALS['rtgodam_stub'] = array( 'post_meta' => array() );
rtgodam_test_register_core_layer_types();
}

protected function tearDown(): void {
unset( $GLOBALS['rtgodam_stub'], $GLOBALS['rtgodam_hooks'] );
parent::tearDown();
}

/**
* Render the block and return the `src` of its image.
*
* @param array $attributes Block attributes.
* @return string
*/
private function rendered_src( array $attributes ): string {
ob_start();
include dirname( __DIR__, 2 ) . '/assets/src/blocks/godam-image/render.php';
$markup = (string) ob_get_clean();

$this->assertSame( 1, preg_match( '/<img[^>]*\bsrc="([^"]*)"/', $markup, $matches ), 'The block printed no image.' );

return html_entity_decode( $matches[1], ENT_QUOTES, 'UTF-8' );
}

public function test_prints_the_cdn_url_when_the_block_saved_the_local_file(): void {
$GLOBALS['rtgodam_stub']['attachment_image_url'] = 'https://abc.gdcdn.us/xyz/photo.webp?v=v1';

$src = $this->rendered_src(
array(
'id' => 42,
'url' => self::SAVED_URL,
)
);

$this->assertSame( 'https://abc.gdcdn.us/xyz/photo.webp?v=v1', $src );
}

public function test_prints_the_replaced_version_when_the_block_saved_an_older_one(): void {
$GLOBALS['rtgodam_stub']['attachment_image_url'] = 'https://abc.gdcdn.us/xyz/photo.webp?v=v3';

$src = $this->rendered_src(
array(
'id' => 42,
'url' => 'https://abc.gdcdn.us/xyz/photo.webp?v=v2',
)
);

$this->assertSame( 'https://abc.gdcdn.us/xyz/photo.webp?v=v3', $src );
}

public function test_keeps_the_saved_url_when_the_attachment_is_gone(): void {
$GLOBALS['rtgodam_stub']['attachment_image_url'] = false;

$src = $this->rendered_src(
array(
'id' => 42,
'url' => self::SAVED_URL,
)
);

$this->assertSame( self::SAVED_URL, $src );
}

public function test_uses_the_attachment_url_when_the_block_saved_none(): void {
$GLOBALS['rtgodam_stub']['attachment_image_url'] = 'https://abc.gdcdn.us/xyz/photo.webp';

$this->assertSame( 'https://abc.gdcdn.us/xyz/photo.webp', $this->rendered_src( array( 'id' => 42 ) ) );
}
}
9 changes: 5 additions & 4 deletions tests/stubs/godam-image-render-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
/**
* Narrow stubs for rendering the GoDAM Image block template in unit tests.
*
* Used by HotspotLinkSanitizeTest, which includes the real render.php and reads the
* layers it hands to the front end. Per-test state lives in
* Used by HotspotLinkSanitizeTest and GodamImageCurrentUrlTest, which include the
* real render.php and read the markup it prints. Per-test state lives in
* `$GLOBALS['rtgodam_stub']`, as elsewhere in the suite: `post_meta` is read by the
* bootstrap's get_post_meta() stub.
*
Expand Down Expand Up @@ -87,10 +87,11 @@ function wp_enqueue_script() { // phpcs:ignore WordPress.NamingConventions.Prefi
if ( ! function_exists( 'wp_get_attachment_image_url' ) ) {

/**
* @return string The block carries its own URL in these tests.
* @return string|false `$GLOBALS['rtgodam_stub']['attachment_image_url']`, or '' when
* not set (the block then uses the URL saved on it).
*/
function wp_get_attachment_image_url() { // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedFunctionFound -- stub mirroring the WP function.
return '';
return $GLOBALS['rtgodam_stub']['attachment_image_url'] ?? '';
}
}

Expand Down
Loading