Merge pull request #6392 from joannechester/CRM-16954-change-filters
[civicrm-core.git] / tests / phpunit / CRM / Extension / MapperTest.php
CommitLineData
6a488035
TO
1<?php
2
3require_once 'CiviTest/CiviUnitTestCase.php';
4
aba1cd8b
EM
5/**
6 * Class CRM_Extension_MapperTest
7 */
6a488035 8class CRM_Extension_MapperTest extends CiviUnitTestCase {
00be9182 9 public function setUp() {
6a488035
TO
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
00be9182 18 public function tearDown() {
6a488035
TO
19 parent::tearDown();
20 }
21
00be9182 22 public function testClassToKey() {
6a488035
TO
23 $this->assertEquals("test.foo.bar", $this->mapper->classToKey('test_foo_bar'));
24 }
25
00be9182 26 public function testClassToPath() {
6a488035
TO
27 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test_foo_bar'));
28 }
29
00be9182 30 public function testIsExtensionClass() {
6a488035
TO
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
00be9182 36 public function testIsExtensionKey() {
6a488035
TO
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
00be9182 42 public function testGetTemplateName() {
6a488035
TO
43 $this->assertEquals("oddball.tpl", $this->mapper->getTemplateName('test_foo_bar'));
44 }
45
00be9182 46 public function testGetTemplatePath() {
6a488035
TO
47 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper->getTemplatePath('test_foo_bar'));
48 }
49
00be9182 50 public function testKeyToClass() {
6a488035
TO
51 $this->assertEquals("test_foo_bar", $this->mapper->keyToClass('test.foo.bar'));
52 }
53
00be9182 54 public function testKeyToPath() {
6a488035
TO
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
00be9182 59 public function testKeyToBasePath() {
6a488035
TO
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
00be9182 67 public function testKeyToUrl() {
6a488035
TO
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
4cbe18b8
EM
76 /**
77 * @param CRM_Utils_Cache_Interface $cache
78 * @param null $cacheKey
79 * @param string $appendPathGarbage
80 *
81 * @return array
82 */
00be9182 83 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
6a488035
TO
84 /*
85 $container = new CRM_Extension_Container_Static(array(
e70a7fc0
TO
86 'test.foo.bar' => array(
87 'path' => '/path/to/test.foo.bar',
88 'resUrl' => 'http://resources/test.foo.bar',
89 ),
6a488035 90 ));
e70a7fc0 91 */
6a488035
TO
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 }
96025800 100
6a488035 101}