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