3 require_once 'CiviTest/CiviUnitTestCase.php';
5 class CRM_Extension_Container_BasicTest
extends CiviUnitTestCase
{
14 function testGetKeysEmpty() {
15 $basedir = $this->createTempDir('ext-empty-');
16 $c = new CRM_Extension_Container_Basic($basedir, 'http://example/basedir', NULL, NULL);
17 $this->assertEquals($c->getKeys(), array());
20 function testGetKeys() {
21 list($basedir, $c) = $this->_createContainer();
22 $this->assertEquals($c->getKeys(), array('test.foo', 'test.foo.bar'));
25 function testGetPath() {
26 list($basedir, $c) = $this->_createContainer();
28 $c->getPath('un.kno.wn');
29 } catch (CRM_Extension_Exception
$e) {
32 $this->assertTrue(is_object($exc), 'Expected exception');
34 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
35 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
38 function testGetPath_extraSlashFromConfig() {
39 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
41 $c->getPath('un.kno.wn');
42 } catch (CRM_Extension_Exception
$e) {
45 $this->assertTrue(is_object($exc), 'Expected exception');
47 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
48 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
51 function testGetResUrl() {
52 list($basedir, $c) = $this->_createContainer();
54 $c->getResUrl('un.kno.wn');
55 } catch (CRM_Extension_Exception
$e) {
58 $this->assertTrue(is_object($exc), 'Expected exception');
60 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
61 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
64 function testGetResUrl_extraSlashFromConfig() {
65 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
67 $c->getResUrl('un.kno.wn');
68 } catch (CRM_Extension_Exception
$e) {
71 $this->assertTrue(is_object($exc), 'Expected exception');
73 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
74 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
77 function testCaching() {
78 $cache = new CRM_Utils_Cache_Arraycache(array());
79 $this->assertTrue(!is_array($cache->get('basic-scan')));
80 list($basedir, $c) = $this->_createContainer($cache, 'basic-scan');
81 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
82 $this->assertTrue(is_array($cache->get('basic-scan')));
84 $cacheData = $cache->get('basic-scan');
85 $this->assertEquals('/foo/bar', $cacheData['test.foo.bar']);
88 function _createContainer(CRM_Utils_Cache_Interface
$cache = NULL, $cacheKey = NULL, $appendPathGarbage = '') {
89 $basedir = rtrim($this->createTempDir('ext-'), '/');
90 mkdir("$basedir/foo");
91 mkdir("$basedir/foo/bar");
92 file_put_contents("$basedir/foo/info.xml", "<extension key='test.foo' type='module'><file>foo</file></extension>");
93 // not needed for now // file_put_contents("$basedir/foo/foo.php", "<?php\n");
94 file_put_contents("$basedir/foo/bar/info.xml", "<extension key='test.foo.bar' type='report'><file>oddball</file></extension>");
95 // not needed for now // file_put_contents("$basedir/foo/bar/oddball.php", "<?php\n");
96 $c = new CRM_Extension_Container_Basic($basedir . $appendPathGarbage, 'http://example/basedir' . $appendPathGarbage, $cache, $cacheKey);
97 return array($basedir, $c);
100 function testConvertPathsToUrls() {
102 'foo.bar' => 'foo\bar',
103 'whiz.bang' => 'tests\extensions\whiz\bang'
105 $expectedRelUrls = array(
106 'foo.bar' => 'foo/bar',
107 'whiz.bang' => 'tests/extensions/whiz/bang',
109 $actualRelUrls = CRM_Extension_Container_Basic
::convertPathsToUrls('\\', $relPaths);
110 $this->assertEquals($expectedRelUrls, $actualRelUrls);