(NFC) PathsTest - Make it easier to identify/debug test cases
[civicrm-core.git] / tests / phpunit / Civi / Core / PathsTest.php
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
18 // Trailing-slash configurations
19
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'];
23 $exs['ap4'] = ['te.st', 'path', '/var/www/files/', '[te.st]/.', '/var/www/files/'];
24
25 $exs['au1'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/foo/bar', 'http://example.com/files/foo/bar'];
26 $exs['au2'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/foo/', 'http://example.com/files/foo/'];
27 $exs['au3'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/foo', 'http://example.com/files/foo'];
28 $exs['au4'] = ['te.st', 'url', 'http://example.com/files/', '[te.st]/.', 'http://example.com/files/'];
29
30 $exs['au18'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/foo/bar', 'http://example.com:8080/foo/bar'];
31 $exs['au28'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/foo/', 'http://example.com:8080/foo/'];
32 $exs['au38'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/foo', 'http://example.com:8080/foo'];
33 $exs['au48'] = ['te.st', 'url', 'http://example.com:8080/', '[te.st]/.', 'http://example.com:8080/'];
34
35 // Trimmed-slash configurations
36
37 $exs['bp1'] = ['te.st', 'path', '/var/www/files', '[te.st]/foo/bar', '/var/www/files/foo/bar'];
38 $exs['bp2'] = ['te.st', 'path', '/var/www/files', '[te.st]/foo/', '/var/www/files/foo/'];
39 $exs['bp3'] = ['te.st', 'path', '/var/www/files', '[te.st]/foo', '/var/www/files/foo'];
40 $exs['bp4'] = ['te.st', 'path', '/var/www/files', '[te.st]/.', '/var/www/files/'];
41
42 $exs['bu1'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/foo/bar', 'http://example.com/files/foo/bar'];
43 $exs['bu2'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/foo/', 'http://example.com/files/foo/'];
44 $exs['bu3'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/foo', 'http://example.com/files/foo'];
45 $exs['bu4'] = ['te.st', 'url', 'http://example.com/files', '[te.st]/.', 'http://example.com/files/'];
46
47 $exs['bu18'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/foo/bar', 'http://example.com:8080/foo/bar'];
48 $exs['bu28'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/foo/', 'http://example.com:8080/foo/'];
49 $exs['bu38'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/foo', 'http://example.com:8080/foo'];
50 $exs['bu48'] = ['te.st', 'url', 'http://example.com:8080', '[te.st]/.', 'http://example.com:8080/'];
51
52 return $exs;
53 }
54
55 /**
56 * @param $varName
57 * @param $varType
58 * @param $varValue
59 * @param $inputExpr
60 * @param $expectValue
61 * @dataProvider getExamples
62 */
63 public function testExamples($varName, $varType, $varValue, $inputExpr, $expectValue) {
64 global $civicrm_paths;
65 $civicrm_paths[$varName][$varType] = $varValue;
66 $func = ($varType === 'url') ? 'getUrl' : 'getPath';
67
68 $paths = new Paths();
69 $paths->register($varName, function() {
70 return ['path' => 'FIXME-PATH', 'url' => 'FIXME-URL'];
71 });
72
73 $actualValue = call_user_func([$paths, $func], $inputExpr);
74 $this->assertEquals($expectValue, $actualValue, "Evaluate $func(\"$inputExpr\") given ([$varName] = \"$varValue\")");
75
76 unset($civicrm_paths[$varName][$varType]);
77 }
78
79 public function testGetUrl_ImplicitBase() {
80 $p = \Civi::paths();
81 $cmsRoot = rtrim($p->getVariable('cms.root', 'url'), '/');
82
83 $this->assertEquals("$cmsRoot/foo/bar", $p->getUrl('foo/bar'));
84 $this->assertEquals("$cmsRoot/foo/", $p->getUrl('foo/'));
85 $this->assertEquals("$cmsRoot/foo", $p->getUrl('foo'));
86 }
87
88 }