Import from SVN (r45945, r596)
[civicrm-core.git] / tests / phpunit / CRM / Extension / MapperTest.php
CommitLineData
6a488035
TO
1<?php
2
3require_once 'CiviTest/CiviUnitTestCase.php';
4
5class CRM_Extension_MapperTest extends CiviUnitTestCase {
6 function setUp() {
7 parent::setUp();
8 list ($this->basedir, $this->container) = $this->_createContainer();
9 $this->mapper = new CRM_Extension_Mapper($this->container);
10
11 list ($this->basedir2, $this->containerWithSlash) = $this->_createContainer(NULL, NULL, '/');
12 $this->mapperWithSlash = new CRM_Extension_Mapper($this->containerWithSlash);
13 }
14
15 function tearDown() {
16 parent::tearDown();
17 }
18
19 function testClassToKey() {
20 $this->assertEquals("test.foo.bar", $this->mapper->classToKey('test_foo_bar'));
21 }
22
23 function testClassToPath() {
24 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test_foo_bar'));
25 }
26
27 function testIsExtensionClass() {
28 $this->assertTrue($this->mapper->isExtensionClass('test_foo_bar'));
29 $this->assertFalse($this->mapper->isExtensionClass('test.foo.bar'));
30 $this->assertFalse($this->mapper->isExtensionClass('CRM_Core_DAO'));
31 }
32
33 function testIsExtensionKey() {
34 $this->assertFalse($this->mapper->isExtensionKey('test_foo_bar'));
35 $this->assertTrue($this->mapper->isExtensionKey('test.foo.bar'));
36 $this->assertFalse($this->mapper->isExtensionKey('CRM_Core_DAO'));
37 }
38
39 function testGetTemplateName() {
40 $this->assertEquals("oddball.tpl", $this->mapper->getTemplateName('test_foo_bar'));
41 }
42
43 function testGetTemplatePath() {
44 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper->getTemplatePath('test_foo_bar'));
45 }
46
47 function testKeyToClass() {
48 $this->assertEquals("test_foo_bar", $this->mapper->keyToClass('test.foo.bar'));
49 }
50
51 function testKeyToPath() {
52 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test.foo.bar'));
53 $this->assertEquals("{$this->basedir2}/weird/foobar/oddball.php", $this->mapperWithSlash->classToPath('test.foo.bar'));
54 }
55
56 function testKeyToBasePath() {
57 $this->assertEquals("{$this->basedir}/weird/foobar", $this->mapper->keyToBasePath('test.foo.bar'));
58 $this->assertEquals("{$this->basedir2}/weird/foobar", $this->mapperWithSlash->keyToBasePath('test.foo.bar'));
59
60 global $civicrm_root;
61 $this->assertEquals(rtrim($civicrm_root, '/'), $this->mapper->keyToBasePath('civicrm'));
62 }
63
64 function testKeyToUrl() {
65 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapper->keyToUrl('test.foo.bar'));
66 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapperWithSlash->keyToUrl('test.foo.bar'));
67
68 $config = CRM_Core_Config::singleton();
69 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapper->keyToUrl('civicrm'));
70 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapperWithSlash->keyToUrl('civicrm'));
71 }
72
73 function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
74 /*
75 $container = new CRM_Extension_Container_Static(array(
76 'test.foo.bar' => array(
77 'path' => '/path/to/test.foo.bar',
78 'resUrl' => 'http://resources/test.foo.bar',
79 ),
80 ));
81 */
82 $basedir = rtrim($this->createTempDir('ext-'), '/');
83 mkdir("$basedir/weird");
84 mkdir("$basedir/weird/foobar");
85 file_put_contents("$basedir/weird/foobar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file></extension>");
86 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
87 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
88 return array($basedir, $c);
89 }
90}