Merge pull request #5903 from scardinius/master
[civicrm-core.git] / tests / phpunit / CRM / Extension / Container / BasicTest.php
1 <?php
2
3 /**
4 * Class CRM_Extension_Container_BasicTest
5 * @group headless
6 */
7 class CRM_Extension_Container_BasicTest extends CiviUnitTestCase {
8 public function setUp() {
9 parent::setUp();
10 }
11
12 public function tearDown() {
13 parent::tearDown();
14 }
15
16 public function testGetKeysEmpty() {
17 $basedir = $this->createTempDir('ext-empty-');
18 $c = new CRM_Extension_Container_Basic($basedir, 'http://example/basedir', NULL, NULL);
19 $this->assertEquals($c->getKeys(), array());
20 }
21
22 public function testGetKeys() {
23 list($basedir, $c) = $this->_createContainer();
24 $this->assertEquals($c->getKeys(), array('test.foo', 'test.foo.bar'));
25 }
26
27 public function testGetPath() {
28 list($basedir, $c) = $this->_createContainer();
29 try {
30 $c->getPath('un.kno.wn');
31 }
32 catch (CRM_Extension_Exception $e) {
33 $exc = $e;
34 }
35 $this->assertTrue(is_object($exc), 'Expected exception');
36
37 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
38 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
39 }
40
41 public function testGetPath_extraSlashFromConfig() {
42 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
43 try {
44 $c->getPath('un.kno.wn');
45 }
46 catch (CRM_Extension_Exception $e) {
47 $exc = $e;
48 }
49 $this->assertTrue(is_object($exc), 'Expected exception');
50
51 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
52 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
53 }
54
55 public function testGetResUrl() {
56 list($basedir, $c) = $this->_createContainer();
57 try {
58 $c->getResUrl('un.kno.wn');
59 }
60 catch (CRM_Extension_Exception $e) {
61 $exc = $e;
62 }
63 $this->assertTrue(is_object($exc), 'Expected exception');
64
65 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
66 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
67 }
68
69 public function testGetResUrl_extraSlashFromConfig() {
70 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
71 try {
72 $c->getResUrl('un.kno.wn');
73 }
74 catch (CRM_Extension_Exception $e) {
75 $exc = $e;
76 }
77 $this->assertTrue(is_object($exc), 'Expected exception');
78
79 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
80 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
81 }
82
83 public function testCaching() {
84 $cache = new CRM_Utils_Cache_Arraycache(array());
85 $this->assertTrue(!is_array($cache->get('basic-scan')));
86 list($basedir, $c) = $this->_createContainer($cache, 'basic-scan');
87 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
88 $this->assertTrue(is_array($cache->get('basic-scan')));
89
90 $cacheData = $cache->get('basic-scan');
91 $this->assertEquals('/foo/bar', $cacheData['test.foo.bar']);
92 }
93
94 /**
95 * @param CRM_Utils_Cache_Interface $cache
96 * @param null $cacheKey
97 * @param string $appendPathGarbage
98 *
99 * @return array
100 */
101 public function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
102 $basedir = rtrim($this->createTempDir('ext-'), '/');
103 mkdir("$basedir/foo");
104 mkdir("$basedir/foo/bar");
105 file_put_contents("$basedir/foo/info.xml", "<extension key='test.foo' type='module'><file>foo</file></extension>");
106 // not needed for now // file_put_contents("$basedir/foo/foo.php", "<?php\n");
107 file_put_contents("$basedir/foo/bar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file></extension>");
108 // not needed for now // file_put_contents("$basedir/foo/bar/oddball.php", "<?php\n");
109 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
110 return array($basedir, $c);
111 }
112
113 public function testConvertPathsToUrls() {
114 $relPaths = array(
115 'foo.bar' => 'foo\bar',
116 'whiz.bang' => 'tests\extensions\whiz\bang',
117 );
118 $expectedRelUrls = array(
119 'foo.bar' => 'foo/bar',
120 'whiz.bang' => 'tests/extensions/whiz/bang',
121 );
122 $actualRelUrls = CRM_Extension_Container_Basic::convertPathsToUrls('\\', $relPaths);
123 $this->assertEquals($expectedRelUrls, $actualRelUrls);
124 }
125
126 }