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