Merge pull request #1216 from davecivicrm/CRM-13062
[civicrm-core.git] / tests / phpunit / CRM / Extension / Container / CollectionTest.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.3 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27 require_once 'CiviTest/CiviUnitTestCase.php';
28
29 class CRM_Extension_Container_CollectionTest extends CiviUnitTestCase {
30 //@todo make BAO enotice compliant & remove the line below
31 // WARNING - NEVER COPY & PASTE $_eNoticeCompliant = FALSE
32 // new test classes should be compliant.
33 public $_eNoticeCompliant = FALSE;
34 function setUp() {
35 parent::setUp();
36 }
37
38 function tearDown() {
39 parent::tearDown();
40 }
41
42 function testGetKeysEmpty() {
43 $c = new CRM_Extension_Container_Collection(array());
44 $this->assertEquals($c->getKeys(), array());
45 }
46
47 function testGetKeys() {
48 $c = $this->_createContainer();
49 $this->assertEquals(array('test.conflict', 'test.whiz', 'test.whizbang', 'test.foo', 'test.foo.bar'), $c->getKeys());
50 }
51
52 function testGetPath() {
53 $c = $this->_createContainer();
54 try {
55 $c->getPath('un.kno.wn');
56 } catch (CRM_Extension_Exception $e) {
57 $exc = $e;
58 }
59 $this->assertTrue(is_object($exc), 'Expected exception');
60
61 $this->assertEquals("/path/to/foo", $c->getPath('test.foo'));
62 $this->assertEquals("/path/to/bar", $c->getPath('test.foo.bar'));
63 $this->assertEquals("/path/to/whiz", $c->getPath('test.whiz'));
64 $this->assertEquals("/path/to/whizbang", $c->getPath('test.whizbang'));
65 $this->assertEquals("/path/to/conflict-b", $c->getPath('test.conflict'));
66 }
67
68 function testGetResUrl() {
69 $c = $this->_createContainer();
70 try {
71 $c->getResUrl('un.kno.wn');
72 } catch (CRM_Extension_Exception $e) {
73 $exc = $e;
74 }
75 $this->assertTrue(is_object($exc), 'Expected exception');
76
77 $this->assertEquals('http://foo', $c->getResUrl('test.foo'));
78 $this->assertEquals('http://foobar', $c->getResUrl('test.foo.bar'));
79 $this->assertEquals('http://whiz', $c->getResUrl('test.whiz'));
80 $this->assertEquals('http://whizbang', $c->getResUrl('test.whizbang'));
81 $this->assertEquals('http://conflict-b', $c->getResUrl('test.conflict'));
82 }
83
84 function testCaching() {
85 $cache = new CRM_Utils_Cache_Arraycache(array());
86 $this->assertTrue(!is_array($cache->get('ext-collection')));
87 $c = $this->_createContainer($cache, 'ext-collection');
88 $this->assertEquals('http://foo', $c->getResUrl('test.foo'));
89 $this->assertTrue(is_array($cache->get('ext-collection')));
90
91 $cacheData = $cache->get('ext-collection');
92 $this->assertEquals('a', $cacheData['test.foo']); // 'test.foo' was defined in the 'a' container
93 $this->assertEquals('b', $cacheData['test.whiz']); // 'test.whiz' was defined in the 'b' container
94 }
95
96 function _createContainer(CRM_Utils_Cache_Interface $cache = NULL, $cacheKey = NULL) {
97 $containers = array();
98 $containers['a'] = new CRM_Extension_Container_Static(array(
99 'test.foo' => array(
100 'path' => '/path/to/foo',
101 'resUrl' => 'http://foo',
102 ),
103 'test.foo.bar' => array(
104 'path' => '/path/to/bar',
105 'resUrl' => 'http://foobar',
106 ),
107 ));
108 $containers['b'] = new CRM_Extension_Container_Static(array(
109 'test.whiz' => array(
110 'path' => '/path/to/whiz',
111 'resUrl' => 'http://whiz',
112 ),
113 'test.whizbang' => array(
114 'path' => '/path/to/whizbang',
115 'resUrl' => 'http://whizbang',
116 ),
117 'test.conflict' => array(
118 'path' => '/path/to/conflict-b',
119 'resUrl' => 'http://conflict-b',
120 ),
121 ));
122 $containers['c'] = new CRM_Extension_Container_Static(array(
123 'test.conflict' => array(
124 'path' => '/path/to/conflict-c',
125 'resUrl' => 'http://conflict-c',
126 ),
127 ));
128 $c = new CRM_Extension_Container_Collection($containers, $cache, $cacheKey);
129 return $c;
130 }
131 }