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