feat: add metadata url transformer

This commit is contained in:
Keith Solomon
2026-04-26 14:51:21 -05:00
parent e744754389
commit 3b09c3f410
3 changed files with 187 additions and 0 deletions
@@ -0,0 +1,82 @@
<?php
namespace WPContentSync\Tests\Unit\Url;
use PHPUnit\Framework\TestCase;
use WPContentSync\Url\MetadataUrlTransformer;
use WPContentSync\Url\UrlMapping;
use WPContentSync\Url\UrlMappingCollection;
use WPContentSync\Url\UrlTransformer;
class MetadataUrlTransformerTest extends TestCase {
private function mappings(): UrlMappingCollection {
return new UrlMappingCollection(
array(
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
)
);
}
public function test_it_recursively_transforms_array_metadata(): void {
$transformer = new MetadataUrlTransformer( new UrlTransformer() );
self::assertSame(
array(
'hero' => array(
'url' => 'https://staging.example.test/uploads/hero.jpg',
),
'count' => 3,
'flag' => true,
),
$transformer->transformValue(
array(
'hero' => array(
'url' => 'https://example.test/uploads/hero.jpg',
),
'count' => 3,
'flag' => true,
),
$this->mappings()
)
);
}
public function test_it_transforms_json_strings(): void {
$transformer = new MetadataUrlTransformer( new UrlTransformer() );
$result = $transformer->transformValue(
'{"url":"https:\/\/example.test\/uploads\/hero.jpg"}',
$this->mappings()
);
self::assertSame( '{"url":"https:\/\/staging.example.test\/uploads\/hero.jpg"}', $result );
}
public function test_it_preserves_serialized_data_validity(): void {
$transformer = new MetadataUrlTransformer( new UrlTransformer() );
// phpcs:ignore -- Test fixture requires native serialized metadata.
$serialized = serialize(
array(
'url' => 'https://example.test/uploads/hero.jpg',
)
);
$result = $transformer->transformValue( $serialized, $this->mappings() );
self::assertSame(
array(
'url' => 'https://staging.example.test/uploads/hero.jpg',
),
// phpcs:ignore -- Assertion verifies the transformed serialized metadata remains valid.
unserialize( $result )
);
}
public function test_it_transforms_plain_string_metadata(): void {
$transformer = new MetadataUrlTransformer( new UrlTransformer() );
self::assertSame(
'https://staging.example.test/contact',
$transformer->transformValue( 'https://example.test/contact', $this->mappings() )
);
}
}
+13
View File
@@ -138,6 +138,19 @@ if ( ! function_exists( 'wp_parse_url' ) ) {
}
}
if ( ! function_exists( 'wp_json_encode' ) ) {
/**
* Minimal JSON encoder for unit tests.
*
* @param mixed $value Value to encode.
* @return string|false
*/
function wp_json_encode( $value ) {
// phpcs:ignore -- Test stub for WordPress' wp_json_encode().
return json_encode( $value );
}
}
if ( ! function_exists( 'get_option' ) ) {
/**
* Minimal WordPress option reader for unit tests.