Skip to content

Commit 6537e6f

Browse files
committed
Add a utility method to test base_url handling
1 parent 45a097c commit 6537e6f

File tree

2 files changed

+111
-0
lines changed

2 files changed

+111
-0
lines changed

src/Services/CloudflarePurgeService.php

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,56 @@ function(&$value, $key) {
193193
return $urls;
194194
}
195195

196+
/**
197+
* Replace the scheme/host with the configured base URL if it exists
198+
* This is gnerally used by tests to validate base_url functionality
199+
*/
200+
public static function replaceWithBaseUrl(array $urls) : array {
201+
202+
$baseURL = self::config()->get('base_url');
203+
if(!$baseURL) {
204+
return $urls;
205+
}
206+
207+
$scheme = parse_url($baseURL, PHP_URL_SCHEME);
208+
$host = parse_url($baseURL, PHP_URL_HOST);
209+
210+
if(!$scheme) {
211+
throw new \Exception("base_url needs to have a scheme");
212+
}
213+
if(!$host) {
214+
throw new \Exception("base_url needs to have a host");
215+
}
216+
217+
// Replace base_url
218+
$updatedUrls = [];
219+
foreach($urls as $url) {
220+
221+
// gather parts from URL provide
222+
$path = parse_url($url, PHP_URL_PATH);
223+
$port = parse_url($url, PHP_URL_PORT);
224+
$query = parse_url($url, PHP_URL_QUERY);
225+
226+
// use base URL parts for these components
227+
$newUrl = $scheme . "://";
228+
$newUrl .= $host;
229+
if($port) {
230+
$newUrl .= ":" . $port;
231+
}
232+
if($path) {
233+
$newUrl .= $path;
234+
}
235+
if($query) {
236+
$newUrl .= "?" . $query;
237+
}
238+
239+
$updatedUrls[] = $newUrl;
240+
241+
}
242+
243+
return $updatedUrls;
244+
}
245+
196246
/**
197247
* Purge all from zone by creating a cache purge job in the future (which handles the purging)
198248
* The idea here is that job will be created in the future with a configured delay (hrs)

tests/BaseURLTest.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace NSWDPC\Utilities\Cloudflare\Tests;
4+
5+
use NSWDPC\Utilities\Cloudflare\CloudflarePurgeService;
6+
use SilverStripe\Core\Config\Config;
7+
8+
require_once(dirname(__FILE__) . '/CloudflarePurgeTest.php');
9+
10+
/**
11+
* Test base_url configuration
12+
* @author James
13+
*/
14+
class BaseURLTest extends CloudflarePurgeTest
15+
{
16+
17+
protected $usesDatabase = false;
18+
19+
public function testBaseURL() {
20+
21+
$scheme = "https";
22+
$host = "alt.example.com";
23+
$baseUrl = $scheme . "://" . $host;
24+
Config::modify()->update(CloudflarePurgeService::class, 'base_url', $baseUrl);
25+
26+
// these URLS should all have their scheme + host updated
27+
$urls = [
28+
"https://example.com/testversionedrecord.html?stage=Stage&alternateformat=1"
29+
=> "{$baseUrl}/testversionedrecord.html?stage=Stage&alternateformat=1",
30+
31+
"/testversionedrecord.html?stage=Stage&alternateformat=1"
32+
=> "{$baseUrl}/testversionedrecord.html?stage=Stage&alternateformat=1",
33+
34+
"https://example.com/testversionedrecord.html?stage=Stage"
35+
=> "{$baseUrl}/testversionedrecord.html?stage=Stage",
36+
37+
// this URL will produce a host as below
38+
"example.com/testversionedrecord.html?stage=Stage"
39+
=> "{$baseUrl}example.com/testversionedrecord.html?stage=Stage",
40+
41+
"https://example.org/testversionedrecord.html?alternateformat=1"
42+
=> "{$baseUrl}/testversionedrecord.html?alternateformat=1",
43+
44+
"https://example.org/testversionedrecord/"
45+
=> "{$baseUrl}/testversionedrecord/",
46+
47+
"https://example.org/"
48+
=> "{$baseUrl}/",
49+
50+
"https://example.org"
51+
=> "{$baseUrl}",
52+
];
53+
54+
foreach($urls as $inUrl => $expectedUrl) {
55+
$outUrls = CloudflarePurgeService::replaceWithBaseUrl( [$inUrl] );
56+
$this->assertEquals($expectedUrl, $outUrls[0], "Returned URL does not match expected");
57+
}
58+
59+
}
60+
61+
}

0 commit comments

Comments
 (0)