Commit | Line | Data |
---|---|---|
aee09efd TO |
1 | <?php |
2 | ||
3 | namespace Civi\Core; | |
4 | ||
5 | /** | |
6 | * Class PathsTest | |
7 | * @package Civi\Core | |
8 | * @group headless | |
9 | */ | |
10 | class PathsTest extends \CiviUnitTestCase { | |
11 | ||
12 | public function getExamples() { | |
13 | $exs = []; | |
14 | ||
15 | // Ensure that various permutations of `$civicrm_paths`, `Civi::paths()->getPath()` | |
16 | // and `Civi::paths()->getUrl()` work as expected. | |
17 | ||
715cee35 | 18 | // Trailing-slash configurations |
aee09efd | 19 | |
715cee35 TO |
20 | $exs['ap1'] = ['te.st', 'path', '/var/www/files/', '[te.st]/foo/bar', '/var/www/files/foo/bar']; |
21 | $exs['ap2'] = ['te.st', 'path', '/var/www/files/', '[te.st]/foo/', '/var/www/files/foo/']; | |
22 | $exs['ap3'] = ['te.st', 'path', '/var/www/files/', '[te.st]/foo', '/var/www/files/foo']; | |
021f851c TO |
23 | $exs['ap4'] = ['te.st', 'path', '/var/www/files/', '[te.st]/.', '/var/www/files']; |
24 | $exs['ap5'] = ['te.st', 'path', '/var/www/files/', '[te.st]/0', '/var/www/files/0']; | |
25 | $exs['ap6'] = ['te.st', 'path', '/var/www/files/', '[te.st]/', '/var/www/files/']; | |
aee09efd | 26 | |
715cee35 TO |
27 | $exs['au1'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/foo/bar', 'http://example.com/files/foo/bar']; |
28 | $exs['au2'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/foo/', 'http://example.com/files/foo/']; | |
29 | $exs['au3'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/foo', 'http://example.com/files/foo']; | |
021f851c TO |
30 | $exs['au4'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/.', 'http://example.com/files']; |
31 | $exs['au5'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/0', 'http://example.com/files/0']; | |
32 | $exs['au6'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/', 'http://example.com/files/']; | |
aee09efd | 33 | |
715cee35 TO |
34 | $exs['au18'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/foo/bar', 'http://example.com:8080/foo/bar']; |
35 | $exs['au28'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/foo/', 'http://example.com:8080/foo/']; | |
36 | $exs['au38'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/foo', 'http://example.com:8080/foo']; | |
021f851c TO |
37 | $exs['au48'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/.', 'http://example.com:8080']; |
38 | $exs['au58'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/0', 'http://example.com:8080/0']; | |
39 | $exs['au68'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/', 'http://example.com:8080/']; | |
aee09efd | 40 | |
715cee35 | 41 | // Trimmed-slash configurations |
aee09efd | 42 | |
715cee35 TO |
43 | $exs['bp1'] = ['te.st', 'path', '/var/www/files', '[te.st]/foo/bar', '/var/www/files/foo/bar']; |
44 | $exs['bp2'] = ['te.st', 'path', '/var/www/files', '[te.st]/foo/', '/var/www/files/foo/']; | |
45 | $exs['bp3'] = ['te.st', 'path', '/var/www/files', '[te.st]/foo', '/var/www/files/foo']; | |
021f851c TO |
46 | $exs['bp4'] = ['te.st', 'path', '/var/www/files', '[te.st]/.', '/var/www/files']; |
47 | $exs['bp5'] = ['te.st', 'path', '/var/www/files', '[te.st]/0', '/var/www/files/0']; | |
48 | $exs['bp6'] = ['te.st', 'path', '/var/www/files', '[te.st]/', '/var/www/files/']; | |
aee09efd | 49 | |
715cee35 TO |
50 | $exs['bu1'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/foo/bar', 'http://example.com/files/foo/bar']; |
51 | $exs['bu2'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/foo/', 'http://example.com/files/foo/']; | |
52 | $exs['bu3'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/foo', 'http://example.com/files/foo']; | |
021f851c TO |
53 | $exs['bu4'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/.', 'http://example.com/files']; |
54 | $exs['bu5'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/0', 'http://example.com/files/0']; | |
55 | $exs['bu6'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/', 'http://example.com/files/']; | |
aee09efd | 56 | |
715cee35 TO |
57 | $exs['bu18'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/foo/bar', 'http://example.com:8080/foo/bar']; |
58 | $exs['bu28'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/foo/', 'http://example.com:8080/foo/']; | |
59 | $exs['bu38'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/foo', 'http://example.com:8080/foo']; | |
021f851c TO |
60 | $exs['bu48'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/.', 'http://example.com:8080']; |
61 | $exs['bu58'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/0', 'http://example.com:8080/0']; | |
62 | $exs['bu68'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/', 'http://example.com:8080/']; | |
63 | ||
64 | // Oddballs | |
65 | $exs['wp1'] = ['wp.ex1', 'url', 'http://example.com/wp-admin/admin.php', '[wp.ex1]/.', 'http://example.com/wp-admin/admin.php']; | |
66 | $exs['http'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/httpIsBetterThanGopher', 'http://example.com/files/httpIsBetterThanGopher']; | |
aee09efd TO |
67 | |
68 | return $exs; | |
69 | } | |
70 | ||
71 | /** | |
72 | * @param $varName | |
73 | * @param $varType | |
74 | * @param $varValue | |
75 | * @param $inputExpr | |
76 | * @param $expectValue | |
77 | * @dataProvider getExamples | |
78 | */ | |
79 | public function testExamples($varName, $varType, $varValue, $inputExpr, $expectValue) { | |
80 | global $civicrm_paths; | |
81 | $civicrm_paths[$varName][$varType] = $varValue; | |
82 | $func = ($varType === 'url') ? 'getUrl' : 'getPath'; | |
83 | ||
84 | $paths = new Paths(); | |
85 | $paths->register($varName, function() { | |
86 | return ['path' => 'FIXME-PATH', 'url' => 'FIXME-URL']; | |
87 | }); | |
88 | ||
89 | $actualValue = call_user_func([$paths, $func], $inputExpr); | |
715cee35 | 90 | $this->assertEquals($expectValue, $actualValue, "Evaluate $func(\"$inputExpr\") given ([$varName] = \"$varValue\")"); |
aee09efd TO |
91 | |
92 | unset($civicrm_paths[$varName][$varType]); | |
93 | } | |
94 | ||
95 | public function testGetUrl_ImplicitBase() { | |
96 | $p = \Civi::paths(); | |
97 | $cmsRoot = rtrim($p->getVariable('cms.root', 'url'), '/'); | |
98 | ||
99 | $this->assertEquals("$cmsRoot/foo/bar", $p->getUrl('foo/bar')); | |
100 | $this->assertEquals("$cmsRoot/foo/", $p->getUrl('foo/')); | |
101 | $this->assertEquals("$cmsRoot/foo", $p->getUrl('foo')); | |
102 | } | |
103 | ||
d0cda11f TO |
104 | /** |
105 | * This test demonstrates how to (and how not to) compute a derivative path variable. | |
106 | */ | |
107 | public function testAbsoluteRelativeConversions() { | |
108 | $gstack = \CRM_Utils_GlobalStack::singleton(); | |
109 | $gstack->push(['_SERVER' => ['HTTP_HOST' => 'example.com']]); | |
110 | $cleanup = \CRM_Utils_AutoClean::with([$gstack, 'pop']); | |
111 | ||
112 | $paths = new Paths(); | |
113 | $paths->register('test.base', function () { | |
114 | return [ | |
115 | 'path' => '/var/foo/', | |
116 | 'url' => 'http://example.com/foo/', | |
117 | ]; | |
118 | }); | |
119 | $paths->register('test.goodsub', function () use ($paths) { | |
961b2006 | 120 | // This is a stand-in for how [civicrm.bower], [civicrm.packages], [civicrm.vendor] currently work. |
d0cda11f TO |
121 | return [ |
122 | 'path' => $paths->getPath('[test.base]/good/'), | |
123 | 'url' => $paths->getUrl('[test.base]/good/', 'absolute'), | |
124 | ]; | |
125 | }); | |
126 | $paths->register('test.badsub', function () use ($paths) { | |
961b2006 | 127 | // This is a stand-in for how [civicrm.bower], [civicrm.packages], [civicrm.vendor] used to work (incorrectly). |
d0cda11f TO |
128 | return [ |
129 | 'path' => $paths->getPath('[test.base]/bad/'), | |
961b2006 TO |
130 | // The following *looks* OK, but it's not. Note that `getUrl()` by default uses `$preferFormat==relative`. |
131 | // Both registered URLs (`register()`, `$civicrm_paths`) and outputted URLs (`getUrl()`) | |
132 | // can be in relative form. However, they are relative to different bases: registrations are | |
133 | // relative to CMS root, and outputted URLs are relative to HTTP root. They are often the same, but... | |
134 | // on deployments where they differ, this example will misbehave. | |
d0cda11f TO |
135 | 'url' => $paths->getUrl('[test.base]/bad/'), |
136 | ]; | |
137 | }); | |
138 | ||
139 | // The test.base works as explicitly defined... | |
140 | $this->assertEquals('/var/foo', $paths->getPath('[test.base]/.')); | |
141 | $this->assertEquals('http://example.com/foo', $paths->getUrl('[test.base]/.', 'absolute')); | |
142 | $this->assertEquals('/foo', $paths->getUrl('[test.base]/.', 'relative')); | |
143 | ||
144 | // The test.goodsub works as expected... | |
145 | $this->assertEquals('/var/foo/good', $paths->getPath('[test.goodsub]/.')); | |
146 | $this->assertEquals('http://example.com/foo/good', $paths->getUrl('[test.goodsub]/.', 'absolute')); | |
147 | $this->assertEquals('/foo/good', $paths->getUrl('[test.goodsub]/.', 'relative')); | |
148 | ||
149 | // The test.badsub doesn't work as expected. | |
150 | $this->assertEquals('/var/foo/bad', $paths->getPath('[test.badsub]/.')); | |
151 | $this->assertNotEquals('http://example.com/foo/bad', $paths->getUrl('[test.badsub]/.', 'absolute')); | |
152 | $this->assertNotEquals('/foo/bad', $paths->getUrl('[test.badsub]/.', 'relative')); | |
153 | } | |
154 | ||
aee09efd | 155 | } |