tests/phpunit/** - Remove unnecessary "require_once" statements
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / NavigationTest.php
CommitLineData
31cb1898 1<?php
31cb1898
AN
2
3/**
4 * Class CRM_Core_BAO_NavigationTest.
5 */
6class CRM_Core_BAO_NavigationTest extends CiviUnitTestCase {
7
8 /**
9 * Set up data for the test run.
10 *
11 * Here we ensure we are starting from a default report navigation.
12 */
13 public function setUp() {
14 parent::setUp();
412b762a 15 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
31cb1898
AN
16 }
17
18 /**
19 * Test that a missing report menu link is added by rebuildReportsNavigation.
20 */
21 public function testCreateMissingReportMenuItemLink() {
22 $reportCount = $this->getCountReportInstances();
51704c4d 23 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_navigation WHERE url LIKE 'civicrm/report/instance/1?reset=1%'");
31cb1898 24 $this->assertEquals($reportCount - 1, $this->getCountReportInstances());
412b762a 25 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
31cb1898
AN
26
27 $this->assertEquals($reportCount, $this->getCountReportInstances());
28 $url = 'civicrm/report/instance/1';
29 $url_params = 'reset=1';
30 $new_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($url, $url_params);
31 $this->assertObjectHasAttribute('id', $new_nav);
32 $this->assertNotNull($new_nav->id);
33 }
34
51704c4d 35 /**
36 * Test that a link with output=criteria at the end is not duplicated.
37 */
38 public function testNoDuplicateReportMenuItemLink() {
39 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
40 $reportCount = $this->getCountReportInstances();
41 CRM_Core_DAO::executeQuery("
42 UPDATE civicrm_navigation
43 SET url = CONCAT(url, '&output=critieria')
44 WHERE url LIKE 'civicrm/report/instance/%?reset=1'");
45 $this->assertEquals($reportCount, $this->getCountReportInstances());
46 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
47
48 $this->assertEquals($reportCount, $this->getCountReportInstances());
49 }
50
dfbeefd8 51 /**
52 * Test that All reports link is not stolen.
53 *
54 * There are 2 All reports links by default. What we DON'T want to see is them
55 * both winding up under the Reports menu - since they already exist they should be unchanged
56 * by rebuilding reports.
57 */
58 public function testNoDuplicateAllReportsLink() {
59 $existing_links = $this->callAPISuccess('Navigation', 'get', array('label' => 'All Reports', 'sequential' => 1));
60 $this->assertNotEquals($existing_links['values'][0]['parent_id'], $existing_links['values'][1]['parent_id']);
61 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
62 $new_links = $this->callAPISuccess('Navigation', 'get', array('label' => 'All Reports', 'sequential' => 1));
63 $this->assertEquals($existing_links['values'][0]['parent_id'], $new_links['values'][0]['parent_id']);
64 $this->assertEquals($existing_links['values'][1]['parent_id'], $new_links['values'][1]['parent_id']);
65 }
66
31cb1898 67 /**
0087242f 68 * Test that an existing report link is rebuilt under it's parent.
31cb1898
AN
69 *
70 * Function tests CRM_Core_BAO_Navigation::rebuildReportsNavigation.
71 */
72 public function testUpdateExistingReportMenuLink() {
73 $url = 'civicrm/report/instance/1';
74 $url_params = 'reset=1';
75 $existing_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($url, $url_params);
51704c4d 76
31cb1898
AN
77 $this->assertNotEquals(FALSE, $existing_nav);
78 $existing_nav->parent_id = 1;
79 $existing_nav->save();
412b762a 80 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
31cb1898
AN
81 $parent_url = 'civicrm/report/list';
82 $parent_url_params = 'compid=99&reset=1';
0087242f 83 $reportsMenu = CRM_Core_BAO_Navigation::createOrUpdateTopLevelReportsNavItem(CRM_Core_Config::domainID());
84 $parent_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($parent_url, $parent_url_params, $reportsMenu->id);
31cb1898
AN
85 $this->assertNotEquals($parent_nav->id, 1);
86 $changed_existing_nav = new CRM_Core_BAO_Navigation();
87 $changed_existing_nav->id = $existing_nav->id;
88 $changed_existing_nav->find(TRUE);
89 $this->assertEquals($changed_existing_nav->parent_id, $parent_nav->id);
90 }
91
31cb1898
AN
92 /**
93 * Test that a navigation item can be retrieved by it's url.
94 */
95 public function testGetNavItemByUrl() {
96 $random_string = substr(sha1(rand()), 0, 7);
97 $name = "Test Menu Link {$random_string}";
98 $url = "civicrm/test/{$random_string}";
99 $url_params = "reset=1";
100 $params = array(
101 'name' => $name,
102 'label' => ts($name),
103 'url' => "{$url}?{$url_params}",
104 'parent_id' => NULL,
105 'is_active' => TRUE,
106 'permission' => array(
107 'access CiviCRM',
108 ),
109 );
110 CRM_Core_BAO_Navigation::add($params);
111 $new_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($url, $url_params);
112 $this->assertObjectHasAttribute('id', $new_nav);
113 $this->assertNotNull($new_nav->id);
114 $new_nav->delete();
115 }
116
51704c4d 117 /**
118 * Test that a navigation item can be retrieved by it's url with a wildcard.
119 *
120 * We want to be able to get a report url with OR without the output=criteria since
121 * that is part of the navigation but not the instance.
122 */
123 public function testGetNavItemByUrlWildcard() {
124 $random_string = substr(sha1(rand()), 0, 7);
125 $name = "Test Menu Link {$random_string}";
126 $url = "civicrm/test/{$random_string}";
127 $url_params = "reset=1&output=criteria";
128 $params = array(
129 'name' => $name,
130 'label' => ts($name),
131 'url' => "{$url}?{$url_params}",
132 'parent_id' => NULL,
133 'is_active' => TRUE,
134 'permission' => array(
135 'access CiviCRM',
136 ),
137 );
138 CRM_Core_BAO_Navigation::add($params);
139 $new_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($url, 'reset=1%');
140 $this->assertObjectHasAttribute('id', $new_nav);
141 $this->assertNotNull($new_nav->id);
142 $new_nav->delete();
143 }
144
31cb1898
AN
145 /**
146 * Get a count of report instances.
147 *
148 * @return int
149 */
150 protected function getCountReportInstances() {
151 return CRM_Core_DAO::singleValueQuery(
152 "SELECT count(*) FROM civicrm_navigation WHERE url LIKE 'civicrm/report/instance/%'");
153 }
154
dfbeefd8 155 /**
156 * Get a count of navigation items that match the url.
157 * @param string $url
158 *
159 * @return int
160 */
161 protected function getCountURL($url) {
162 return CRM_Core_DAO::singleValueQuery(
163 "SELECT count(*) FROM civicrm_navigation WHERE url ='{$url}'");
164 }
165
b774b065
TO
166 /**
167 * Run fixNavigationMenu() on a menu which already has navIDs
168 * everywhere. They should be unchanged.
169 */
170 public function testFixNavigationMenu_preserveIDs() {
171 $input[10] = array(
172 'attributes' => array(
173 'label' => 'Custom Menu Entry',
174 'parentID' => NULL,
175 'navID' => 10,
176 'active' => 1,
177 ),
178 'child' => array(
179 '11' => array(
180 'attributes' => array(
181 'label' => 'Custom Child Menu',
182 'parentID' => 10,
183 'navID' => 11,
184 ),
185 'child' => NULL,
186 ),
187 ),
188 );
189
190 $output = $input;
191 CRM_Core_BAO_Navigation::fixNavigationMenu($output);
192
193 $this->assertEquals(NULL, $output[10]['attributes']['parentID']);
194 $this->assertEquals(10, $output[10]['attributes']['navID']);
195 $this->assertEquals(10, $output[10]['child'][11]['attributes']['parentID']);
196 $this->assertEquals(11, $output[10]['child'][11]['attributes']['navID']);
197 }
198
199 /**
200 * Run fixNavigationMenu() on a menu which is missing some navIDs. They
201 * should be filled in, and others should be preserved.
202 */
203 public function testFixNavigationMenu_inferIDs() {
204 $input[10] = array(
205 'attributes' => array(
206 'label' => 'Custom Menu Entry',
207 'parentID' => NULL,
208 'navID' => 10,
209 'active' => 1,
210 ),
211 'child' => array(
212 '0' => array(
213 'attributes' => array(
214 'label' => 'Custom Child Menu',
215 ),
216 'child' => NULL,
217 ),
218 '100' => array(
219 'attributes' => array(
220 'label' => 'Custom Child Menu 2',
221 'navID' => 100,
222 ),
223 'child' => NULL,
224 ),
225 ),
226 );
227
228 $output = $input;
229 CRM_Core_BAO_Navigation::fixNavigationMenu($output);
230
231 $this->assertEquals('Custom Menu Entry', $output[10]['attributes']['label']);
232 $this->assertEquals(NULL, $output[10]['attributes']['parentID']);
233 $this->assertEquals(10, $output[10]['attributes']['navID']);
234
235 $this->assertEquals('Custom Child Menu', $output[10]['child'][101]['attributes']['label']);
236 $this->assertEquals(10, $output[10]['child'][101]['attributes']['parentID']);
237 $this->assertEquals(101, $output[10]['child'][101]['attributes']['navID']);
238
239 $this->assertEquals('Custom Child Menu 2', $output[10]['child'][100]['attributes']['label']);
240 $this->assertEquals(10, $output[10]['child'][100]['attributes']['parentID']);
241 $this->assertEquals(100, $output[10]['child'][100]['attributes']['navID']);
242 }
243
244 public function testFixNavigationMenu_inferIDs_deep() {
245 $input[10] = array(
246 'attributes' => array(
247 'label' => 'Custom Menu Entry',
248 'parentID' => NULL,
249 'navID' => 10,
250 'active' => 1,
251 ),
252 'child' => array(
253 '0' => array(
254 'attributes' => array(
255 'label' => 'Custom Child Menu',
256 ),
257 'child' => array(
258 '100' => array(
259 'attributes' => array(
260 'label' => 'Custom Child Menu 2',
261 'navID' => 100,
262 ),
263 'child' => NULL,
264 ),
265 ),
266 ),
267 ),
268 );
269
270 $output = $input;
271 CRM_Core_BAO_Navigation::fixNavigationMenu($output);
272
273 $this->assertEquals('Custom Menu Entry', $output[10]['attributes']['label']);
274 $this->assertEquals(NULL, $output[10]['attributes']['parentID']);
275 $this->assertEquals(10, $output[10]['attributes']['navID']);
276
277 $this->assertEquals('Custom Child Menu', $output[10]['child'][101]['attributes']['label']);
278 $this->assertEquals(10, $output[10]['child'][101]['attributes']['parentID']);
279 $this->assertEquals(101, $output[10]['child'][101]['attributes']['navID']);
280
281 $this->assertEquals('Custom Child Menu 2', $output[10]['child'][101]['child'][100]['attributes']['label']);
282 $this->assertEquals(101, $output[10]['child'][101]['child'][100]['attributes']['parentID']);
283 $this->assertEquals(100, $output[10]['child'][101]['child'][100]['attributes']['navID']);
284 }
285
31cb1898 286}