CRM-12030 - Always use '/' in URLs (even for Windows)
[civicrm-core.git] / tests / phpunit / CRM / Extension / Container / BasicTest.php
1 <?php
2
3 require_once 'CiviTest/CiviUnitTestCase.php';
4
5 class CRM_Extension_Container_BasicTest extends CiviUnitTestCase {
6 function setUp() {
7 parent::setUp();
8 }
9
10 function tearDown() {
11 parent::tearDown();
12 }
13
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());
18 }
19
20 function testGetKeys() {
21 list($basedir, $c) = $this->_createContainer();
22 $this->assertEquals($c->getKeys(), array('test.foo', 'test.foo.bar'));
23 }
24
25 function testGetPath() {
26 list($basedir, $c) = $this->_createContainer();
27 try {
28 $c->getPath('un.kno.wn');
29 } catch (CRM_Extension_Exception $e) {
30 $exc = $e;
31 }
32 $this->assertTrue(is_object($exc), 'Expected exception');
33
34 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
35 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
36 }
37
38 function testGetPath_extraSlashFromConfig() {
39 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
40 try {
41 $c->getPath('un.kno.wn');
42 } catch (CRM_Extension_Exception $e) {
43 $exc = $e;
44 }
45 $this->assertTrue(is_object($exc), 'Expected exception');
46
47 $this->assertEquals("$basedir/foo", $c->getPath('test.foo'));
48 $this->assertEquals("$basedir/foo/bar", $c->getPath('test.foo.bar'));
49 }
50
51 function testGetResUrl() {
52 list($basedir, $c) = $this->_createContainer();
53 try {
54 $c->getResUrl('un.kno.wn');
55 } catch (CRM_Extension_Exception $e) {
56 $exc = $e;
57 }
58 $this->assertTrue(is_object($exc), 'Expected exception');
59
60 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
61 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
62 }
63
64 function testGetResUrl_extraSlashFromConfig() {
65 list($basedir, $c) = $this->_createContainer(NULL, NULL, '/');
66 try {
67 $c->getResUrl('un.kno.wn');
68 } catch (CRM_Extension_Exception $e) {
69 $exc = $e;
70 }
71 $this->assertTrue(is_object($exc), 'Expected exception');
72
73 $this->assertEquals('http://example/basedir/foo', $c->getResUrl('test.foo'));
74 $this->assertEquals('http://example/basedir/foo/bar', $c->getResUrl('test.foo.bar'));
75 }
76
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')));
83
84 $cacheData = $cache->get('basic-scan');
85 $this->assertEquals('/foo/bar', $cacheData['test.foo.bar']);
86 }
87
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);
98 }
99
100 function testConvertPathsToUrls() {
101 $relPaths = array(
102 'foo.bar' => 'foo\bar',
103 'whiz.bang' => 'tests\extensions\whiz\bang'
104 );
105 $expectedRelUrls = array(
106 'foo.bar' => 'foo/bar',
107 'whiz.bang' => 'tests/extensions/whiz/bang',
108 );
109 $actualRelUrls = CRM_Extension_Container_Basic::convertPathsToUrls('\\', $relPaths);
110 $this->assertEquals($expectedRelUrls, $actualRelUrls);
111 }
112 }