Unverified Commit b82673c3 authored by Seamus Lee's avatar Seamus Lee Committed by GitHub

Merge pull request #16887 from totten/5.24-dbl-path

joomla#26 - Fix path derivation when CMS is rooted in a subdir
parents 72f72900 961b2006
......@@ -38,19 +38,19 @@ class Paths {
->register('civicrm.packages', function () {
return [
'path' => \Civi::paths()->getPath('[civicrm.root]/packages/'),
'url' => \Civi::paths()->getUrl('[civicrm.root]/packages/'),
'url' => \Civi::paths()->getUrl('[civicrm.root]/packages/', 'absolute'),
];
})
->register('civicrm.vendor', function () {
return [
'path' => \Civi::paths()->getPath('[civicrm.root]/vendor/'),
'url' => \Civi::paths()->getUrl('[civicrm.root]/vendor/'),
'url' => \Civi::paths()->getUrl('[civicrm.root]/vendor/', 'absolute'),
];
})
->register('civicrm.bower', function () {
return [
'path' => \Civi::paths()->getPath('[civicrm.root]/bower_components/'),
'url' => \Civi::paths()->getUrl('[civicrm.root]/bower_components/'),
'url' => \Civi::paths()->getUrl('[civicrm.root]/bower_components/', 'absolute'),
];
})
->register('civicrm.files', function () {
......
......@@ -101,4 +101,55 @@ class PathsTest extends \CiviUnitTestCase {
$this->assertEquals("$cmsRoot/foo", $p->getUrl('foo'));
}
/**
* This test demonstrates how to (and how not to) compute a derivative path variable.
*/
public function testAbsoluteRelativeConversions() {
$gstack = \CRM_Utils_GlobalStack::singleton();
$gstack->push(['_SERVER' => ['HTTP_HOST' => 'example.com']]);
$cleanup = \CRM_Utils_AutoClean::with([$gstack, 'pop']);
$paths = new Paths();
$paths->register('test.base', function () {
return [
'path' => '/var/foo/',
'url' => 'http://example.com/foo/',
];
});
$paths->register('test.goodsub', function () use ($paths) {
// This is a stand-in for how [civicrm.bower], [civicrm.packages], [civicrm.vendor] currently work.
return [
'path' => $paths->getPath('[test.base]/good/'),
'url' => $paths->getUrl('[test.base]/good/', 'absolute'),
];
});
$paths->register('test.badsub', function () use ($paths) {
// This is a stand-in for how [civicrm.bower], [civicrm.packages], [civicrm.vendor] used to work (incorrectly).
return [
'path' => $paths->getPath('[test.base]/bad/'),
// The following *looks* OK, but it's not. Note that `getUrl()` by default uses `$preferFormat==relative`.
// Both registered URLs (`register()`, `$civicrm_paths`) and outputted URLs (`getUrl()`)
// can be in relative form. However, they are relative to different bases: registrations are
// relative to CMS root, and outputted URLs are relative to HTTP root. They are often the same, but...
// on deployments where they differ, this example will misbehave.
'url' => $paths->getUrl('[test.base]/bad/'),
];
});
// The test.base works as explicitly defined...
$this->assertEquals('/var/foo', $paths->getPath('[test.base]/.'));
$this->assertEquals('http://example.com/foo', $paths->getUrl('[test.base]/.', 'absolute'));
$this->assertEquals('/foo', $paths->getUrl('[test.base]/.', 'relative'));
// The test.goodsub works as expected...
$this->assertEquals('/var/foo/good', $paths->getPath('[test.goodsub]/.'));
$this->assertEquals('http://example.com/foo/good', $paths->getUrl('[test.goodsub]/.', 'absolute'));
$this->assertEquals('/foo/good', $paths->getUrl('[test.goodsub]/.', 'relative'));
// The test.badsub doesn't work as expected.
$this->assertEquals('/var/foo/bad', $paths->getPath('[test.badsub]/.'));
$this->assertNotEquals('http://example.com/foo/bad', $paths->getUrl('[test.badsub]/.', 'absolute'));
$this->assertNotEquals('/foo/bad', $paths->getUrl('[test.badsub]/.', 'relative'));
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment