163 lines
4.9 KiB
PHP
163 lines
4.9 KiB
PHP
<?php
|
|
|
|
namespace WPContentSync\Tests\Unit\Url;
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
use WPContentSync\Url\UrlMapping;
|
|
use WPContentSync\Url\UrlMappingCollection;
|
|
use WPContentSync\Url\UrlTransformer;
|
|
|
|
class UrlTransformerTest extends TestCase {
|
|
public function test_it_rewrites_plain_urls(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'Visit https://staging.example.test/about for details.',
|
|
$transformer->transformString( 'Visit https://example.test/about for details.', $mappings )
|
|
);
|
|
}
|
|
|
|
public function test_it_rewrites_html_attribute_urls(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'<a href="https://staging.example.test/about"><img src="https://staging.example.test/image.jpg"></a>',
|
|
$transformer->transformString(
|
|
'<a href="https://example.test/about"><img src="https://example.test/image.jpg"></a>',
|
|
$mappings
|
|
)
|
|
);
|
|
}
|
|
|
|
public function test_it_rewrites_escaped_urls(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test/path?a=1&b=2', 'https://staging.example.test/path?a=1&b=2' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'https://staging.example.test/path?a=1&b=2',
|
|
$transformer->transformString( 'https://example.test/path?a=1&b=2', $mappings )
|
|
);
|
|
}
|
|
|
|
public function test_it_rewrites_protocol_relative_urls(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'//staging.example.test/uploads/file.pdf',
|
|
$transformer->transformString( '//example.test/uploads/file.pdf', $mappings )
|
|
);
|
|
}
|
|
|
|
public function test_it_supports_multiple_mappings(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
|
|
new UrlMapping( 'https://cdn.example.test', 'https://cdn.staging.example.test' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'https://staging.example.test https://cdn.staging.example.test/image.jpg',
|
|
$transformer->transformString( 'https://example.test https://cdn.example.test/image.jpg', $mappings )
|
|
);
|
|
}
|
|
|
|
public function test_it_does_not_cascade_mapping_destinations_into_other_sources(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://a.example.test', 'https://b.example.test' ),
|
|
new UrlMapping( 'https://b.example.test', 'https://c.example.test' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'https://b.example.test/page https://c.example.test/page',
|
|
$transformer->transformString( 'https://a.example.test/page https://b.example.test/page', $mappings )
|
|
);
|
|
}
|
|
|
|
public function test_it_does_not_rewrite_partial_host_matches(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'https://example.test.evil/path https://staging.example.test/path',
|
|
$transformer->transformString(
|
|
'https://example.test.evil/path https://example.test/path',
|
|
$mappings
|
|
)
|
|
);
|
|
}
|
|
|
|
public function test_it_does_not_rewrite_userinfo_host_lookalikes(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'https://example.test@evil.test/path https://staging.example.test/path',
|
|
$transformer->transformString(
|
|
'https://example.test@evil.test/path https://example.test/path',
|
|
$mappings
|
|
)
|
|
);
|
|
}
|
|
|
|
public function test_it_prefers_more_specific_overlapping_mappings(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test', 'https://staging.example.test' ),
|
|
new UrlMapping( 'https://example.test/uploads', 'https://media.staging.example.test/uploads' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'https://media.staging.example.test/uploads/image.jpg',
|
|
$transformer->transformString( 'https://example.test/uploads/image.jpg', $mappings )
|
|
);
|
|
}
|
|
|
|
public function test_it_rewrites_escaped_protocol_relative_urls(): void {
|
|
$transformer = new UrlTransformer();
|
|
$mappings = new UrlMappingCollection(
|
|
array(
|
|
new UrlMapping( 'https://example.test/path?a=1&b=2', 'https://staging.example.test/path?a=1&b=2' ),
|
|
)
|
|
);
|
|
|
|
self::assertSame(
|
|
'//staging.example.test/path?a=1&b=2',
|
|
$transformer->transformString( '//example.test/path?a=1&b=2', $mappings )
|
|
);
|
|
}
|
|
}
|