4 * Class CRM_Extension_MapperTest
7 class CRM_Extension_MapperTest
extends CiviUnitTestCase
{
20 * @var CRM_Extension_Container_Interface
25 * @var CRM_Extension_Container_Interface
27 protected $containerWithSlash;
30 * @var CRM_Extension_Mapper
35 * @var CRM_Extension_Mapper
37 protected $mapperWithSlash;
39 public function setUp() {
41 list ($this->basedir
, $this->container
) = $this->_createContainer();
42 $this->mapper
= new CRM_Extension_Mapper($this->container
);
44 list ($this->basedir2
, $this->containerWithSlash
) = $this->_createContainer(NULL, NULL, '/');
45 $this->mapperWithSlash
= new CRM_Extension_Mapper($this->containerWithSlash
);
48 public function tearDown() {
52 public function testClassToKey() {
53 $this->assertEquals("test.foo.bar", $this->mapper
->classToKey('test_foo_bar'));
56 public function testClassToPath() {
57 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper
->classToPath('test_foo_bar'));
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'));
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'));
72 public function testGetTemplateName() {
73 $this->assertEquals("oddball.tpl", $this->mapper
->getTemplateName('test_foo_bar'));
76 public function testGetTemplatePath() {
77 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper
->getTemplatePath('test_foo_bar'));
80 public function testKeyToClass() {
81 $this->assertEquals("test_foo_bar", $this->mapper
->keyToClass('test.foo.bar'));
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'));
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'));
94 $this->assertEquals(rtrim($civicrm_root, '/'), $this->mapper
->keyToBasePath('civicrm'));
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'));
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'));
106 public function testGetKeysByPath() {
108 $this->basedir
=> $this->mapper
,
109 $this->basedir2
=> $this->mapperWithSlash
,
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/*'));
127 public function testGetKeysByTag() {
128 $this->assertEquals([], $this->mapper
->getKeysByTag('big-rock-candy-mountain'));
129 $this->assertEquals(['test.foo.bar'], $this->mapper
->getKeysByTag('wakka'));
133 * @param CRM_Utils_Cache_Interface $cache
134 * @param null $cacheKey
135 * @param string $appendPathGarbage
139 public function _createContainer(CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
141 $container = new CRM_Extension_Container_Static(array(
142 'test.foo.bar' => array(
143 'path' => '/path/to/test.foo.bar',
144 'resUrl' => 'http://resources/test.foo.bar',
148 $basedir = rtrim($this->createTempDir('ext-'), '/');
149 mkdir("$basedir/weird");
150 mkdir("$basedir/weird/foobar");
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>");
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);
154 return [$basedir, $c];