Merge pull request #15884 from kainuk/issue-lab-1365
[civicrm-core.git] / tests / phpunit / CRM / Extension / MapperTest.php
CommitLineData
6a488035
TO
1<?php
2
aba1cd8b
EM
3/**
4 * Class CRM_Extension_MapperTest
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035 7class CRM_Extension_MapperTest extends CiviUnitTestCase {
3b3f6d23
TO
8
9 /**
10 * @var string
11 */
39b959db
SL
12 protected $basedir;
13
14 /**
15 * @var string
16 */
17 protected $basedir2;
18
19 /**
20 * @var CRM_Extension_Container_Interface
21 */
22 protected $container;
3b3f6d23
TO
23
24 /**
25 * @var CRM_Extension_Container_Interface
26 */
39b959db
SL
27 protected $containerWithSlash;
28
29 /**
30 * @var CRM_Extension_Mapper
31 */
32 protected $mapper;
3b3f6d23
TO
33
34 /**
35 * @var CRM_Extension_Mapper
36 */
39b959db 37 protected $mapperWithSlash;
3b3f6d23 38
00be9182 39 public function setUp() {
6a488035
TO
40 parent::setUp();
41 list ($this->basedir, $this->container) = $this->_createContainer();
42 $this->mapper = new CRM_Extension_Mapper($this->container);
43
44 list ($this->basedir2, $this->containerWithSlash) = $this->_createContainer(NULL, NULL, '/');
45 $this->mapperWithSlash = new CRM_Extension_Mapper($this->containerWithSlash);
46 }
47
00be9182 48 public function tearDown() {
6a488035
TO
49 parent::tearDown();
50 }
51
00be9182 52 public function testClassToKey() {
6a488035
TO
53 $this->assertEquals("test.foo.bar", $this->mapper->classToKey('test_foo_bar'));
54 }
55
00be9182 56 public function testClassToPath() {
6a488035
TO
57 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test_foo_bar'));
58 }
59
00be9182 60 public function testIsExtensionClass() {
6a488035
TO
61 $this->assertTrue($this->mapper->isExtensionClass('test_foo_bar'));
62 $this->assertFalse($this->mapper->isExtensionClass('test.foo.bar'));
63 $this->assertFalse($this->mapper->isExtensionClass('CRM_Core_DAO'));
64 }
65
00be9182 66 public function testIsExtensionKey() {
6a488035
TO
67 $this->assertFalse($this->mapper->isExtensionKey('test_foo_bar'));
68 $this->assertTrue($this->mapper->isExtensionKey('test.foo.bar'));
69 $this->assertFalse($this->mapper->isExtensionKey('CRM_Core_DAO'));
70 }
71
00be9182 72 public function testGetTemplateName() {
6a488035
TO
73 $this->assertEquals("oddball.tpl", $this->mapper->getTemplateName('test_foo_bar'));
74 }
75
00be9182 76 public function testGetTemplatePath() {
6a488035
TO
77 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper->getTemplatePath('test_foo_bar'));
78 }
79
00be9182 80 public function testKeyToClass() {
6a488035
TO
81 $this->assertEquals("test_foo_bar", $this->mapper->keyToClass('test.foo.bar'));
82 }
83
00be9182 84 public function testKeyToPath() {
6a488035
TO
85 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test.foo.bar'));
86 $this->assertEquals("{$this->basedir2}/weird/foobar/oddball.php", $this->mapperWithSlash->classToPath('test.foo.bar'));
87 }
88
00be9182 89 public function testKeyToBasePath() {
6a488035
TO
90 $this->assertEquals("{$this->basedir}/weird/foobar", $this->mapper->keyToBasePath('test.foo.bar'));
91 $this->assertEquals("{$this->basedir2}/weird/foobar", $this->mapperWithSlash->keyToBasePath('test.foo.bar'));
92
93 global $civicrm_root;
94 $this->assertEquals(rtrim($civicrm_root, '/'), $this->mapper->keyToBasePath('civicrm'));
95 }
96
00be9182 97 public function testKeyToUrl() {
6a488035
TO
98 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapper->keyToUrl('test.foo.bar'));
99 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapperWithSlash->keyToUrl('test.foo.bar'));
100
101 $config = CRM_Core_Config::singleton();
102 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapper->keyToUrl('civicrm'));
103 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapperWithSlash->keyToUrl('civicrm'));
104 }
105
3b3f6d23 106 public function testGetKeysByPath() {
9099cab3 107 $mappers = [
3b3f6d23
TO
108 $this->basedir => $this->mapper,
109 $this->basedir2 => $this->mapperWithSlash,
9099cab3 110 ];
3b3f6d23
TO
111 foreach ($mappers as $basedir => $mapper) {
112 /** @var CRM_Extension_Mapper $mapper */
9099cab3
CW
113 $this->assertEquals([], $mapper->getKeysByPath($basedir));
114 $this->assertEquals([], $mapper->getKeysByPath($basedir . '/weird'));
115 $this->assertEquals([], $mapper->getKeysByPath($basedir . '/weird/'));
116 $this->assertEquals([], $mapper->getKeysByPath($basedir . '/weird//'));
117 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/*'));
118 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '//*'));
119 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/*'));
120 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar'));
121 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar/'));
122 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar//'));
123 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar/*'));
3b3f6d23
TO
124 }
125 }
126
9bb46670
TO
127 public function testGetKeysByTag() {
128 $this->assertEquals([], $this->mapper->getKeysByTag('big-rock-candy-mountain'));
129 $this->assertEquals(['test.foo.bar'], $this->mapper->getKeysByTag('wakka'));
130 }
131
4cbe18b8
EM
132 /**
133 * @param CRM_Utils_Cache_Interface $cache
134 * @param null $cacheKey
135 * @param string $appendPathGarbage
136 *
137 * @return array
138 */
00be9182 139 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
6a488035
TO
140 /*
141 $container = new CRM_Extension_Container_Static(array(
e70a7fc0
TO
142 'test.foo.bar' => array(
143 'path' => '/path/to/test.foo.bar',
144 'resUrl' => 'http://resources/test.foo.bar',
145 ),
6a488035 146 ));
e70a7fc0 147 */
6a488035
TO
148 $basedir = rtrim($this->createTempDir('ext-'), '/');
149 mkdir("$basedir/weird");
150 mkdir("$basedir/weird/foobar");
9bb46670 151 file_put_contents("$basedir/weird/foobar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file><tags><tag>wakka</tag></tags></extension>");
6a488035
TO
152 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
153 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
9099cab3 154 return [$basedir, $c];
6a488035 155 }
96025800 156
6a488035 157}