Merge pull request #8634 from konadave/CRM-19007-master
[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 public function setUp() {
9 parent::setUp();
10 list ($this->basedir, $this->container) = $this->_createContainer();
11 $this->mapper = new CRM_Extension_Mapper($this->container);
12
13 list ($this->basedir2, $this->containerWithSlash) = $this->_createContainer(NULL, NULL, '/');
14 $this->mapperWithSlash = new CRM_Extension_Mapper($this->containerWithSlash);
15 }
16
17 public function tearDown() {
18 parent::tearDown();
19 }
20
21 public function testClassToKey() {
22 $this->assertEquals("test.foo.bar", $this->mapper->classToKey('test_foo_bar'));
23 }
24
25 public function testClassToPath() {
26 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test_foo_bar'));
27 }
28
29 public function testIsExtensionClass() {
30 $this->assertTrue($this->mapper->isExtensionClass('test_foo_bar'));
31 $this->assertFalse($this->mapper->isExtensionClass('test.foo.bar'));
32 $this->assertFalse($this->mapper->isExtensionClass('CRM_Core_DAO'));
33 }
34
35 public function testIsExtensionKey() {
36 $this->assertFalse($this->mapper->isExtensionKey('test_foo_bar'));
37 $this->assertTrue($this->mapper->isExtensionKey('test.foo.bar'));
38 $this->assertFalse($this->mapper->isExtensionKey('CRM_Core_DAO'));
39 }
40
41 public function testGetTemplateName() {
42 $this->assertEquals("oddball.tpl", $this->mapper->getTemplateName('test_foo_bar'));
43 }
44
45 public function testGetTemplatePath() {
46 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper->getTemplatePath('test_foo_bar'));
47 }
48
49 public function testKeyToClass() {
50 $this->assertEquals("test_foo_bar", $this->mapper->keyToClass('test.foo.bar'));
51 }
52
53 public function testKeyToPath() {
54 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test.foo.bar'));
55 $this->assertEquals("{$this->basedir2}/weird/foobar/oddball.php", $this->mapperWithSlash->classToPath('test.foo.bar'));
56 }
57
58 public function testKeyToBasePath() {
59 $this->assertEquals("{$this->basedir}/weird/foobar", $this->mapper->keyToBasePath('test.foo.bar'));
60 $this->assertEquals("{$this->basedir2}/weird/foobar", $this->mapperWithSlash->keyToBasePath('test.foo.bar'));
61
62 global $civicrm_root;
63 $this->assertEquals(rtrim($civicrm_root, '/'), $this->mapper->keyToBasePath('civicrm'));
64 }
65
66 public function testKeyToUrl() {
67 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapper->keyToUrl('test.foo.bar'));
68 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapperWithSlash->keyToUrl('test.foo.bar'));
69
70 $config = CRM_Core_Config::singleton();
71 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapper->keyToUrl('civicrm'));
72 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapperWithSlash->keyToUrl('civicrm'));
73 }
74
75 /**
76 * @param CRM_Utils_Cache_Interface $cache
77 * @param null $cacheKey
78 * @param string $appendPathGarbage
79 *
80 * @return array
81 */
82 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
83 /*
84 $container = new CRM_Extension_Container_Static(array(
85 'test.foo.bar' => array(
86 'path' => '/path/to/test.foo.bar',
87 'resUrl' => 'http://resources/test.foo.bar',
88 ),
89 ));
90 */
91 $basedir = rtrim($this->createTempDir('ext-'), '/');
92 mkdir("$basedir/weird");
93 mkdir("$basedir/weird/foobar");
94 file_put_contents("$basedir/weird/foobar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file></extension>");
95 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
96 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
97 return array($basedir, $c);
98 }
99
100 }