Merge pull request #15837 from totten/master-prtmpl
[civicrm-core.git] / tests / phpunit / CRM / Extension / MapperTest.php
1 <?php
2
3 /**
4 * Class CRM_Extension_MapperTest
5 * @group headless
6 */
7 class CRM_Extension_MapperTest extends CiviUnitTestCase {
8
9 /**
10 * @var string
11 */
12 protected $basedir;
13
14 /**
15 * @var string
16 */
17 protected $basedir2;
18
19 /**
20 * @var CRM_Extension_Container_Interface
21 */
22 protected $container;
23
24 /**
25 * @var CRM_Extension_Container_Interface
26 */
27 protected $containerWithSlash;
28
29 /**
30 * @var CRM_Extension_Mapper
31 */
32 protected $mapper;
33
34 /**
35 * @var CRM_Extension_Mapper
36 */
37 protected $mapperWithSlash;
38
39 public function setUp() {
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
48 public function tearDown() {
49 parent::tearDown();
50 }
51
52 public function testClassToKey() {
53 $this->assertEquals("test.foo.bar", $this->mapper->classToKey('test_foo_bar'));
54 }
55
56 public function testClassToPath() {
57 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test_foo_bar'));
58 }
59
60 public function testIsExtensionClass() {
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
66 public function testIsExtensionKey() {
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
72 public function testGetTemplateName() {
73 $this->assertEquals("oddball.tpl", $this->mapper->getTemplateName('test_foo_bar'));
74 }
75
76 public function testGetTemplatePath() {
77 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper->getTemplatePath('test_foo_bar'));
78 }
79
80 public function testKeyToClass() {
81 $this->assertEquals("test_foo_bar", $this->mapper->keyToClass('test.foo.bar'));
82 }
83
84 public function testKeyToPath() {
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
89 public function testKeyToBasePath() {
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
97 public function testKeyToUrl() {
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
106 public function testGetKeysByPath() {
107 $mappers = [
108 $this->basedir => $this->mapper,
109 $this->basedir2 => $this->mapperWithSlash,
110 ];
111 foreach ($mappers as $basedir => $mapper) {
112 /** @var CRM_Extension_Mapper $mapper */
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/*'));
124 }
125 }
126
127 /**
128 * @param CRM_Utils_Cache_Interface $cache
129 * @param null $cacheKey
130 * @param string $appendPathGarbage
131 *
132 * @return array
133 */
134 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
135 /*
136 $container = new CRM_Extension_Container_Static(array(
137 'test.foo.bar' => array(
138 'path' => '/path/to/test.foo.bar',
139 'resUrl' => 'http://resources/test.foo.bar',
140 ),
141 ));
142 */
143 $basedir = rtrim($this->createTempDir('ext-'), '/');
144 mkdir("$basedir/weird");
145 mkdir("$basedir/weird/foobar");
146 file_put_contents("$basedir/weird/foobar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file></extension>");
147 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
148 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
149 return [$basedir, $c];
150 }
151
152 }