3 require_once 'CiviTest/CiviUnitTestCase.php';
6 * Class CRM_Extension_Container_BasicTest
8 class CRM_Extension_Container_BasicTest
extends CiviUnitTestCase
{
9 public function setUp() {
13 public function tearDown() {
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());
23 public function testGetKeys() {
24 list($basedir, $c) = $this->_createContainer();
25 $this->assertEquals($c->getKeys(), array('test.foo', 'test.foo.bar'));
28 public function testGetPath() {
29 list($basedir, $c) = $this->_createContainer();
31 $c->getPath('un.kno.wn');
33 catch (CRM_Extension_Exception
$e) {
36 $this->assertTrue(is_object($exc), 'Expected exception');
38 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
39 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
42 public function testGetPath_extraSlashFromConfig() {
43 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
45 $c->getPath('un.kno.wn');
47 catch (CRM_Extension_Exception
$e) {
50 $this->assertTrue(is_object($exc), 'Expected exception');
52 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
53 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
56 public function testGetResUrl() {
57 list($basedir, $c) = $this->_createContainer();
59 $c->getResUrl('un.kno.wn');
61 catch (CRM_Extension_Exception
$e) {
64 $this->assertTrue(is_object($exc), 'Expected exception');
66 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
67 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
70 public function testGetResUrl_extraSlashFromConfig() {
71 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
73 $c->getResUrl('un.kno.wn');
75 catch (CRM_Extension_Exception
$e) {
78 $this->assertTrue(is_object($exc), 'Expected exception');
80 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
81 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
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')));
91 $cacheData = $cache->get('basic-scan');
92 $this->assertEquals('/foo/bar', $cacheData['test.foo.bar']);
96 * @param CRM_Utils_Cache_Interface $cache
97 * @param null $cacheKey
98 * @param string $appendPathGarbage
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);
114 public function testConvertPathsToUrls() {
116 'foo.bar' => 'foo\bar',
117 'whiz.bang' => 'tests\extensions\whiz\bang',
119 $expectedRelUrls = array(
120 'foo.bar' => 'foo/bar',
121 'whiz.bang' => 'tests/extensions/whiz/bang',
123 $actualRelUrls = CRM_Extension_Container_Basic
::convertPathsToUrls('\\', $relPaths);
124 $this->assertEquals($expectedRelUrls, $actualRelUrls);