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