Update Unit test styling to cover the future coder version
[civicrm-core.git] / tests / phpunit / CRM / Extension / MapperTest.php
CommitLineData
6a488035
TO
1<?php
2
aba1cd8b
EM
3/**
4 * Class CRM_Extension_MapperTest
acb109b7 5 * @group headless
aba1cd8b 6 */
6a488035 7class CRM_Extension_MapperTest extends CiviUnitTestCase {
3b3f6d23
TO
8
9 /**
10 * @var string
11 */
39b959db
SL
12 protected $basedir;
13
14 /**
15 * @var string
16 */
17 protected $basedir2;
18
19 /**
20 * @var CRM_Extension_Container_Interface
21 */
22 protected $container;
3b3f6d23
TO
23
24 /**
25 * @var CRM_Extension_Container_Interface
26 */
39b959db
SL
27 protected $containerWithSlash;
28
29 /**
30 * @var CRM_Extension_Mapper
31 */
32 protected $mapper;
3b3f6d23
TO
33
34 /**
35 * @var CRM_Extension_Mapper
36 */
39b959db 37 protected $mapperWithSlash;
3b3f6d23 38
00be9182 39 public function setUp() {
6a488035
TO
40 parent::setUp();
41 list ($this->basedir, $this->container) = $this->_createContainer();
42 $this->mapper = new CRM_Extension_Mapper($this->container);
43
44 list ($this->basedir2, $this->containerWithSlash) = $this->_createContainer(NULL, NULL, '/');
45 $this->mapperWithSlash = new CRM_Extension_Mapper($this->containerWithSlash);
46 }
47
00be9182 48 public function tearDown() {
6a488035
TO
49 parent::tearDown();
50 }
51
00be9182 52 public function testClassToKey() {
6a488035
TO
53 $this->assertEquals("test.foo.bar", $this->mapper->classToKey('test_foo_bar'));
54 }
55
00be9182 56 public function testClassToPath() {
6a488035
TO
57 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test_foo_bar'));
58 }
59
00be9182 60 public function testIsExtensionClass() {
6a488035
TO
61 $this->assertTrue($this->mapper->isExtensionClass('test_foo_bar'));
62 $this->assertFalse($this->mapper->isExtensionClass('test.foo.bar'));
63 $this->assertFalse($this->mapper->isExtensionClass('CRM_Core_DAO'));
64 }
65
00be9182 66 public function testIsExtensionKey() {
6a488035
TO
67 $this->assertFalse($this->mapper->isExtensionKey('test_foo_bar'));
68 $this->assertTrue($this->mapper->isExtensionKey('test.foo.bar'));
69 $this->assertFalse($this->mapper->isExtensionKey('CRM_Core_DAO'));
70 }
71
00be9182 72 public function testGetTemplateName() {
6a488035
TO
73 $this->assertEquals("oddball.tpl", $this->mapper->getTemplateName('test_foo_bar'));
74 }
75
00be9182 76 public function testGetTemplatePath() {
6a488035
TO
77 $this->assertEquals("{$this->basedir}/weird/foobar/templates", $this->mapper->getTemplatePath('test_foo_bar'));
78 }
79
00be9182 80 public function testKeyToClass() {
6a488035
TO
81 $this->assertEquals("test_foo_bar", $this->mapper->keyToClass('test.foo.bar'));
82 }
83
00be9182 84 public function testKeyToPath() {
6a488035
TO
85 $this->assertEquals("{$this->basedir}/weird/foobar/oddball.php", $this->mapper->classToPath('test.foo.bar'));
86 $this->assertEquals("{$this->basedir2}/weird/foobar/oddball.php", $this->mapperWithSlash->classToPath('test.foo.bar'));
87 }
88
00be9182 89 public function testKeyToBasePath() {
6a488035
TO
90 $this->assertEquals("{$this->basedir}/weird/foobar", $this->mapper->keyToBasePath('test.foo.bar'));
91 $this->assertEquals("{$this->basedir2}/weird/foobar", $this->mapperWithSlash->keyToBasePath('test.foo.bar'));
92
93 global $civicrm_root;
94 $this->assertEquals(rtrim($civicrm_root, '/'), $this->mapper->keyToBasePath('civicrm'));
95 }
96
00be9182 97 public function testKeyToUrl() {
6a488035
TO
98 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapper->keyToUrl('test.foo.bar'));
99 $this->assertEquals("http://example/basedir/weird/foobar", $this->mapperWithSlash->keyToUrl('test.foo.bar'));
100
101 $config = CRM_Core_Config::singleton();
102 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapper->keyToUrl('civicrm'));
103 $this->assertEquals(rtrim($config->resourceBase, '/'), $this->mapperWithSlash->keyToUrl('civicrm'));
104 }
105
3b3f6d23
TO
106 public function testGetKeysByPath() {
107 $mappers = array(
108 $this->basedir => $this->mapper,
109 $this->basedir2 => $this->mapperWithSlash,
110 );
111 foreach ($mappers as $basedir => $mapper) {
112 /** @var CRM_Extension_Mapper $mapper */
113 $this->assertEquals(array(), $mapper->getKeysByPath($basedir));
114 $this->assertEquals(array(), $mapper->getKeysByPath($basedir . '/weird'));
115 $this->assertEquals(array(), $mapper->getKeysByPath($basedir . '/weird/'));
116 $this->assertEquals(array(), $mapper->getKeysByPath($basedir . '/weird//'));
117 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/*'));
118 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '//*'));
119 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/*'));
120 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar'));
121 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar/'));
122 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar//'));
123 $this->assertEquals(array('test.foo.bar'), $mapper->getKeysByPath($basedir . '/weird/foobar/*'));
124 }
125 }
126
4cbe18b8
EM
127 /**
128 * @param CRM_Utils_Cache_Interface $cache
129 * @param null $cacheKey
130 * @param string $appendPathGarbage
131 *
132 * @return array
133 */
00be9182 134 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
6a488035
TO
135 /*
136 $container = new CRM_Extension_Container_Static(array(
e70a7fc0
TO
137 'test.foo.bar' => array(
138 'path' => '/path/to/test.foo.bar',
139 'resUrl' => 'http://resources/test.foo.bar',
140 ),
6a488035 141 ));
e70a7fc0 142 */
6a488035
TO
143 $basedir = rtrim($this->createTempDir('ext-'), '/');
144 mkdir("$basedir/weird");
145 mkdir("$basedir/weird/foobar");
146 file_put_contents("$basedir/weird/foobar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file></extension>");
147 // not needed for now // file_put_contents("$basedir/weird/bar/oddball.php", "<?php\n");
148 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
149 return array($basedir, $c);
150 }
96025800 151
6a488035 152}