4 * Class CRM_Extension_MapperTest
7 class CRM_Extension_MapperTest
extends CiviUnitTestCase
{
12 protected $basedir, $basedir2;
15 * @var CRM_Extension_Container_Interface
17 protected $container, $containerWithSlash;
20 * @var CRM_Extension_Mapper
22 protected $mapper, $mapperWithSlash;
24 public function setUp() {
26 list ($this->basedir
, $this->container
) = $this->_createContainer();
27 $this->mapper
= new CRM_Extension_Mapper($this->container
);
29 list ($this->basedir2
, $this->containerWithSlash
) = $this->_createContainer(NULL, NULL, '/');
30 $this->mapperWithSlash
= new CRM_Extension_Mapper($this->containerWithSlash
);
33 public function tearDown() {
37 public function testClassToKey() {
38 $this->assertEquals("test.foo.bar", $this->mapper
->classToKey('test_foo_bar'));
41 public function testClassToPath() {
42 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper
->classToPath('test_foo_bar'));
45 public function testIsExtensionClass() {
46 $this->assertTrue($this->mapper
->isExtensionClass('test_foo_bar'));
47 $this->assertFalse($this->mapper
->isExtensionClass('test.foo.bar'));
48 $this->assertFalse($this->mapper
->isExtensionClass('CRM_Core_DAO'));
51 public function testIsExtensionKey() {
52 $this->assertFalse($this->mapper
->isExtensionKey('test_foo_bar'));
53 $this->assertTrue($this->mapper
->isExtensionKey('test.foo.bar'));
54 $this->assertFalse($this->mapper
->isExtensionKey('CRM_Core_DAO'));
57 public function testGetTemplateName() {
58 $this->assertEquals("oddball.tpl", $this->mapper
->getTemplateName('test_foo_bar'));
61 public function testGetTemplatePath() {
62 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper
->getTemplatePath('test_foo_bar'));
65 public function testKeyToClass() {
66 $this->assertEquals("test_foo_bar", $this->mapper
->keyToClass('test.foo.bar'));
69 public function testKeyToPath() {
70 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper
->classToPath('test.foo.bar'));
71 $this->assertEquals("{$this->basedir2}/weird/foobar/oddball.php", $this->mapperWithSlash
->classToPath('test.foo.bar'));
74 public function testKeyToBasePath() {
75 $this->assertEquals("{$this->basedir}/weird/foobar", $this->mapper
->keyToBasePath('test.foo.bar'));
76 $this->assertEquals("{$this->basedir2}/weird/foobar", $this->mapperWithSlash
->keyToBasePath('test.foo.bar'));
79 $this->assertEquals(rtrim($civicrm_root, '/'), $this->mapper
->keyToBasePath('civicrm'));
82 public function testKeyToUrl() {
83 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapper
->keyToUrl('test.foo.bar'));
84 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapperWithSlash
->keyToUrl('test.foo.bar'));
86 $config = CRM_Core_Config
::singleton();
87 $this->assertEquals(rtrim($config->resourceBase
, '/'), $this->mapper
->keyToUrl('civicrm'));
88 $this->assertEquals(rtrim($config->resourceBase
, '/'), $this->mapperWithSlash
->keyToUrl('civicrm'));
91 public function testGetKeysByPath() {
93 $this->basedir
=> $this->mapper
,
94 $this->basedir2
=> $this->mapperWithSlash
,
96 foreach ($mappers as $basedir => $mapper) {
97 /** @var CRM_Extension_Mapper $mapper */
98 $this->assertEquals(array(), $mapper->getKeysByPath($basedir));
99 $this->assertEquals(array(), $mapper->getKeysByPath($basedir . '/weird'));
100 $this->assertEquals(array(), $mapper->getKeysByPath($basedir . '/weird/'));
101 $this->assertEquals(array(), $mapper->getKeysByPath($basedir . '/weird//'));
102 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/*'));
103 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '//*'));
104 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/*'));
105 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar'));
106 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar/'));
107 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar//'));
108 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar/*'));
113 * @param CRM_Utils_Cache_Interface $cache
114 * @param null $cacheKey
115 * @param string $appendPathGarbage
119 public function _createContainer(CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
121 $container = new CRM_Extension_Container_Static(array(
122 'test.foo.bar' => array(
123 'path' => '/path/to/test.foo.bar',
124 'resUrl' => 'http://resources/test.foo.bar',
128 $basedir = rtrim($this->createTempDir('ext-'), '/');
129 mkdir("$basedir/weird");
130 mkdir("$basedir/weird/foobar");
131 file_put_contents("$basedir/weird/foobar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file></extension>");
132 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
133 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
134 return array($basedir, $c);