Add effective end and start date, extend unit test
[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
5d345864 39 public function setUp(): void {
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 testClassToKey() {
6a488035
TO
49 $this->assertEquals("test.foo.bar", $this->mapper->classToKey('test_foo_bar'));
50 }
51
00be9182 52 public function testClassToPath() {
6a488035
TO
53 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test_foo_bar'));
54 }
55
00be9182 56 public function testIsExtensionClass() {
6a488035
TO
57 $this->assertTrue($this->mapper->isExtensionClass('test_foo_bar'));
58 $this->assertFalse($this->mapper->isExtensionClass('test.foo.bar'));
59 $this->assertFalse($this->mapper->isExtensionClass('CRM_Core_DAO'));
60 }
61
00be9182 62 public function testIsExtensionKey() {
6a488035
TO
63 $this->assertFalse($this->mapper->isExtensionKey('test_foo_bar'));
64 $this->assertTrue($this->mapper->isExtensionKey('test.foo.bar'));
65 $this->assertFalse($this->mapper->isExtensionKey('CRM_Core_DAO'));
66 }
67
00be9182 68 public function testGetTemplateName() {
6a488035
TO
69 $this->assertEquals("oddball.tpl", $this->mapper->getTemplateName('test_foo_bar'));
70 }
71
00be9182 72 public function testGetTemplatePath() {
6a488035
TO
73 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper->getTemplatePath('test_foo_bar'));
74 }
75
00be9182 76 public function testKeyToClass() {
6a488035
TO
77 $this->assertEquals("test_foo_bar", $this->mapper->keyToClass('test.foo.bar'));
78 }
79
00be9182 80 public function testKeyToPath() {
6a488035
TO
81 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test.foo.bar'));
82 $this->assertEquals("{$this->basedir2}/weird/foobar/oddball.php", $this->mapperWithSlash->classToPath('test.foo.bar'));
83 }
84
00be9182 85 public function testKeyToBasePath() {
6a488035
TO
86 $this->assertEquals("{$this->basedir}/weird/foobar", $this->mapper->keyToBasePath('test.foo.bar'));
87 $this->assertEquals("{$this->basedir2}/weird/foobar", $this->mapperWithSlash->keyToBasePath('test.foo.bar'));
88
89 global $civicrm_root;
90 $this->assertEquals(rtrim($civicrm_root, '/'), $this->mapper->keyToBasePath('civicrm'));
91 }
92
00be9182 93 public function testKeyToUrl() {
6a488035
TO
94 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapper->keyToUrl('test.foo.bar'));
95 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapperWithSlash->keyToUrl('test.foo.bar'));
96
97 $config = CRM_Core_Config::singleton();
98 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapper->keyToUrl('civicrm'));
99 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapperWithSlash->keyToUrl('civicrm'));
100 }
101
3b3f6d23 102 public function testGetKeysByPath() {
9099cab3 103 $mappers = [
3b3f6d23
TO
104 $this->basedir => $this->mapper,
105 $this->basedir2 => $this->mapperWithSlash,
9099cab3 106 ];
3b3f6d23
TO
107 foreach ($mappers as $basedir => $mapper) {
108 /** @var CRM_Extension_Mapper $mapper */
9099cab3
CW
109 $this->assertEquals([], $mapper->getKeysByPath($basedir));
110 $this->assertEquals([], $mapper->getKeysByPath($basedir . '/weird'));
111 $this->assertEquals([], $mapper->getKeysByPath($basedir . '/weird/'));
112 $this->assertEquals([], $mapper->getKeysByPath($basedir . '/weird//'));
113 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/*'));
114 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '//*'));
115 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/*'));
116 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar'));
117 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar/'));
118 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar//'));
119 $this->assertEquals(['test.foo.bar'], $mapper->getKeysByPath($basedir . '/weird/foobar/*'));
3b3f6d23
TO
120 }
121 }
122
9bb46670
TO
123 public function testGetKeysByTag() {
124 $this->assertEquals([], $this->mapper->getKeysByTag('big-rock-candy-mountain'));
125 $this->assertEquals(['test.foo.bar'], $this->mapper->getKeysByTag('wakka'));
126 }
127
4cbe18b8
EM
128 /**
129 * @param CRM_Utils_Cache_Interface $cache
130 * @param null $cacheKey
131 * @param string $appendPathGarbage
132 *
133 * @return array
134 */
00be9182 135 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
6a488035
TO
136 /*
137 $container = new CRM_Extension_Container_Static(array(
e70a7fc0
TO
138 'test.foo.bar' => array(
139 'path' => '/path/to/test.foo.bar',
140 'resUrl' => 'http://resources/test.foo.bar',
141 ),
6a488035 142 ));
e70a7fc0 143 */
6a488035
TO
144 $basedir = rtrim($this->createTempDir('ext-'), '/');
145 mkdir("$basedir/weird");
146 mkdir("$basedir/weird/foobar");
9bb46670 147 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
148 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
149 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
9099cab3 150 return [$basedir, $c];
6a488035 151 }
96025800 152
6a488035 153}