| 1 |
<?php |
| 2 |
|
| 3 |
require_once __DIR__ . '/../dependency-path-mapping.php'; |
| 4 |
|
| 5 |
class Test_URL_To_File_Mapping extends PHPUnit\Framework\TestCase { |
| 6 |
// TODO: URL without host is based on ABSPATH |
| 7 |
// TODO: Separate plugin URL with site URL host. Exist/non-exist |
| 8 |
// TODO: Separate content URL with site URL host. Exist/non-exist |
| 9 |
// TODO: Site root URL. Exist/non-exist |
| 10 |
// TODO: Plugin URL under content URL dir but separate from site URL. Same host as site URL. |
| 11 |
// TODO: Plugin URL under content URL which is under site URL. Same host as site URL. |
| 12 |
// TODO: Content URL descended from site URL |
| 13 |
// TODO: Plugin URL with different host than site URL |
| 14 |
// TODO: Content URL with different host than site URL |
| 15 |
// TODO: Relative URLs |
| 16 |
|
| 17 |
function test_abspath_resolution() { |
| 18 |
$site_url = 'https://example.com/site'; |
| 19 |
$site_dir = __DIR__ . '/data/url-to-file-mapping/site'; |
| 20 |
$content_url = 'https://example.com/site/wp-content'; |
| 21 |
$content_dir = "$site_dir/content"; |
| 22 |
$plugin_url = 'https://example.com/site/wp-content/plugins'; |
| 23 |
$plugin_dir = "$content_dir/plugin"; |
| 24 |
|
| 25 |
$dpm = new Page_Optimize_Dependency_Path_Mapping( |
| 26 |
$site_url, |
| 27 |
$site_dir, |
| 28 |
$content_url, |
| 29 |
$content_dir, |
| 30 |
$plugin_url, |
| 31 |
$plugin_dir |
| 32 |
); |
| 33 |
|
| 34 |
// TODO: Remove this call to realpath() when we fix dependency_src_to_fs_path() to stop doubling path separators |
| 35 |
$this->assertEquals( "$site_dir/exists", realpath( $dpm->dependency_src_to_fs_path( '/exists' ) ) ); |
| 36 |
$this->assertFalse( $dpm->dependency_src_to_fs_path( '/nonexistent' ) ); |
| 37 |
} |
| 38 |
} |
| 39 |
|
| 40 |
class Test_URI_Path_To_File_Mapping extends PHPUnit\Framework\TestCase { |
| 41 |
|
| 42 |
// TODO: Test URI and FS paths with and without trailing slashes |
| 43 |
|
| 44 |
/** |
| 45 |
* @dataProvider provide_test_data |
| 46 |
* @test |
| 47 |
*/ |
| 48 |
function run_test( |
| 49 |
$label, |
| 50 |
$site_host, |
| 51 |
$site_uri_path, |
| 52 |
$site_dir, |
| 53 |
$content_host, |
| 54 |
$content_uri_path, |
| 55 |
$content_dir, |
| 56 |
$plugin_host, |
| 57 |
$plugin_uri_path, |
| 58 |
$plugin_dir |
| 59 |
) { |
| 60 |
$site_url = "{$site_host}{$site_uri_path}"; |
| 61 |
$content_url = "{$content_host}{$content_uri_path}"; |
| 62 |
$plugin_url = "{$plugin_host}{$plugin_uri_path}"; |
| 63 |
|
| 64 |
$root = __DIR__ . '/data/url-to-file-mapping'; |
| 65 |
$site_dir = "{$root}{$site_dir}"; |
| 66 |
$content_dir = "{$root}{$content_dir}"; |
| 67 |
$plugin_dir = "{$root}{$plugin_dir}"; |
| 68 |
|
| 69 |
$dpm = new Page_Optimize_Dependency_Path_Mapping( |
| 70 |
$site_url, |
| 71 |
$site_dir, |
| 72 |
$content_url, |
| 73 |
$content_dir, |
| 74 |
$plugin_url, |
| 75 |
$plugin_dir |
| 76 |
); |
| 77 |
|
| 78 |
$this->assertEquals( "$site_dir/exists", $dpm->uri_path_to_fs_path( "$site_uri_path/exists" ), "$label: Cannot find file based on site URI path" ); |
| 79 |
$this->assertFalse( $dpm->uri_path_to_fs_path( "$site_uri_path/nonexistent" ), "$label: Should have failed for nonexistent file based on site URI path" ); |
| 80 |
|
| 81 |
$actual_content_path = $dpm->uri_path_to_fs_path( "$content_uri_path/exists" ); |
| 82 |
if ( 0 === strpos( $content_url, $site_url ) ) { |
| 83 |
// Content is under site URL. We expect this path to resolve. |
| 84 |
$this->assertEquals( "$content_dir/exists", $actual_content_path, "$label: Cannot find file based on content URI path" ); |
| 85 |
} else { |
| 86 |
// Content is not under site URL. We expect a resolution failure. |
| 87 |
$this->assertFalse( $actual_content_path, "$label: Should have failed for content URI path outside of site URL" ); |
| 88 |
} |
| 89 |
$this->assertFalse( $dpm->uri_path_to_fs_path( "$content_uri_path/nonexistent" ), "$label: Should have failed for nonexistent file based on content URI path" ); |
| 90 |
|
| 91 |
$actual_plugin_path = $dpm->uri_path_to_fs_path( "$plugin_uri_path/exists" ); |
| 92 |
if ( 0 === strpos( $plugin_url, $site_url ) ) { |
| 93 |
// Plugins are under site URL. We expect this path to resolve. |
| 94 |
$this->assertEquals( "$plugin_dir/exists", $actual_plugin_path, "$label: Cannot find file based on plugin URI path" ); |
| 95 |
} else { |
| 96 |
// Plugins are not under site URL. We expect a resolution failure. |
| 97 |
$this->assertFalse( $actual_plugin_path, "$label: Should have failed for plugin URI path outside of site URL" ); |
| 98 |
} |
| 99 |
$this->assertFalse( $dpm->uri_path_to_fs_path( "$plugin_uri_path/nonexistent" ), "$label: Should have failed for nonexistent file based on plugin URI path" ); |
| 100 |
} |
| 101 |
|
| 102 |
function provide_test_data() { |
| 103 |
return array( |
| 104 |
array( |
| 105 |
'Nested site->content->plugin dirs', |
| 106 |
'https://example.com', |
| 107 |
'/subdir', |
| 108 |
'/site', |
| 109 |
'https://example.com', |
| 110 |
'/subdir/wp-content', |
| 111 |
'/site/content', |
| 112 |
'https://example.com', |
| 113 |
'/subdir/wp-content/plugins', |
| 114 |
'/site/content/plugins', |
| 115 |
), |
| 116 |
array( |
| 117 |
'Nested content->plugin dirs, separate from ABSPATH', |
| 118 |
'https://example.com', |
| 119 |
'/subdir', |
| 120 |
'/site', |
| 121 |
'https://example.com', |
| 122 |
'/subdir/wp-content', |
| 123 |
'/content', |
| 124 |
'https://example.com', |
| 125 |
'/subdir/wp-content/plugins', |
| 126 |
'/content/plugins' |
| 127 |
), |
| 128 |
array( |
| 129 |
'Content and plugin dirs separate from ABSPATH and each other', |
| 130 |
'https://example.com', |
| 131 |
'/subdir', |
| 132 |
'/site', |
| 133 |
'https://example.com', |
| 134 |
'/subdir/wp-content', |
| 135 |
'/content', |
| 136 |
'https://example.com', |
| 137 |
'/subdir/wp-content/plugins', |
| 138 |
'/plugins' |
| 139 |
), |
| 140 |
array( |
| 141 |
'Content and plugin URLs have same host but are not under the site URL', |
| 142 |
'https://example.com', |
| 143 |
'/subdir', |
| 144 |
'/site', |
| 145 |
'https://example.com', |
| 146 |
'/wp-content', // Not descended from site URL path |
| 147 |
'/site/content', |
| 148 |
'https://example.com', |
| 149 |
'/wp-content/plugins', // Not descended from site URL path |
| 150 |
'/site/content/plugins' |
| 151 |
), |
| 152 |
array( |
| 153 |
'Content and plugin URLs have different host from site URL', |
| 154 |
'https://example.com', |
| 155 |
'/subdir', |
| 156 |
'/site', |
| 157 |
'https://example.com:1234', |
| 158 |
'/subdir/wp-content', |
| 159 |
'/site/content', |
| 160 |
'https://other1.com', |
| 161 |
'/subdir/wp-content/plugins', |
| 162 |
'/site/content/plugins', |
| 163 |
), |
| 164 |
); |
| 165 |
} |
| 166 |
} |
| 167 |
|