4 * Class CRM_Core_BAO_NavigationTest.
7 class CRM_Core_BAO_NavigationTest
extends CiviUnitTestCase
{
10 * Set up data for the test run.
12 * Here we ensure we are starting from a default report navigation.
14 public function setUp() {
16 CRM_Core_BAO_Navigation
::rebuildReportsNavigation(CRM_Core_Config
::domainID());
20 * Test that a missing report menu link is added by rebuildReportsNavigation.
22 public function testCreateMissingReportMenuItemLink() {
23 $reportCount = $this->getCountReportInstances();
24 CRM_Core_DAO
::executeQuery("DELETE FROM civicrm_navigation WHERE url LIKE 'civicrm/report/instance/1?reset=1%'");
25 $this->assertEquals($reportCount - 1, $this->getCountReportInstances());
26 CRM_Core_BAO_Navigation
::rebuildReportsNavigation(CRM_Core_Config
::domainID());
28 $this->assertEquals($reportCount, $this->getCountReportInstances());
29 $url = 'civicrm/report/instance/1';
30 $url_params = 'reset=1';
31 $new_nav = CRM_Core_BAO_Navigation
::getNavItemByUrl($url, $url_params);
32 $this->assertObjectHasAttribute('id', $new_nav);
33 $this->assertNotNull($new_nav->id
);
37 * Test that a link with output=criteria at the end is not duplicated.
39 public function testNoDuplicateReportMenuItemLink() {
40 CRM_Core_BAO_Navigation
::rebuildReportsNavigation(CRM_Core_Config
::domainID());
41 $reportCount = $this->getCountReportInstances();
42 CRM_Core_DAO
::executeQuery("
43 UPDATE civicrm_navigation
44 SET url = CONCAT(url, '&output=critieria')
45 WHERE url LIKE 'civicrm/report/instance/%?reset=1'");
46 $this->assertEquals($reportCount, $this->getCountReportInstances());
47 CRM_Core_BAO_Navigation
::rebuildReportsNavigation(CRM_Core_Config
::domainID());
49 $this->assertEquals($reportCount, $this->getCountReportInstances());
53 * Test that All reports link is not stolen.
55 * There are 2 All reports links by default. What we DON'T want to see is them
56 * both winding up under the Reports menu - since they already exist they should be unchanged
57 * by rebuilding reports.
59 public function testNoDuplicateAllReportsLink() {
60 $existing_links = $this->callAPISuccess('Navigation', 'get', ['label' => 'All Reports', 'sequential' => 1]);
61 $this->assertNotEquals($existing_links['values'][0]['parent_id'], $existing_links['values'][1]['parent_id']);
62 CRM_Core_BAO_Navigation
::rebuildReportsNavigation(CRM_Core_Config
::domainID());
63 $new_links = $this->callAPISuccess('Navigation', 'get', ['label' => 'All Reports', 'sequential' => 1]);
64 $this->assertEquals($existing_links['values'][0]['parent_id'], $new_links['values'][0]['parent_id']);
65 $this->assertEquals($existing_links['values'][1]['parent_id'], $new_links['values'][1]['parent_id']);
69 * Test that an existing report link is rebuilt under it's parent.
71 * Function tests CRM_Core_BAO_Navigation::rebuildReportsNavigation.
73 public function testUpdateExistingReportMenuLink() {
74 $url = 'civicrm/report/instance/1';
75 $url_params = 'reset=1';
76 $existing_nav = CRM_Core_BAO_Navigation
::getNavItemByUrl($url, $url_params);
78 $this->assertNotEquals(FALSE, $existing_nav);
79 $existing_nav->parent_id
= 1;
80 $existing_nav->save();
81 CRM_Core_BAO_Navigation
::rebuildReportsNavigation(CRM_Core_Config
::domainID());
82 $parent_url = 'civicrm/report/list';
83 $parent_url_params = 'compid=99&reset=1';
84 $reportsMenu = CRM_Core_BAO_Navigation
::createOrUpdateTopLevelReportsNavItem(CRM_Core_Config
::domainID());
85 $parent_nav = CRM_Core_BAO_Navigation
::getNavItemByUrl($parent_url, $parent_url_params, $reportsMenu->id
);
86 $this->assertNotEquals($parent_nav->id
, 1);
87 $changed_existing_nav = new CRM_Core_BAO_Navigation();
88 $changed_existing_nav->id
= $existing_nav->id
;
89 $changed_existing_nav->find(TRUE);
90 $this->assertEquals($changed_existing_nav->parent_id
, $parent_nav->id
);
94 * Test that a navigation item can be retrieved by it's url.
96 public function testGetNavItemByUrl() {
97 $random_string = substr(sha1(rand()), 0, 7);
98 $name = "Test Menu Link {$random_string}";
99 $url = "civicrm/test/{$random_string}";
100 $url_params = "reset=1";
103 'label' => ts($name),
104 'url' => "{$url}?{$url_params}",
111 CRM_Core_BAO_Navigation
::add($params);
112 $new_nav = CRM_Core_BAO_Navigation
::getNavItemByUrl($url, $url_params);
113 $this->assertObjectHasAttribute('id', $new_nav);
114 $this->assertNotNull($new_nav->id
);
119 * Test that a navigation item can be retrieved by it's url with a wildcard.
121 * We want to be able to get a report url with OR without the output=criteria since
122 * that is part of the navigation but not the instance.
124 public function testGetNavItemByUrlWildcard() {
125 $random_string = substr(sha1(rand()), 0, 7);
126 $name = "Test Menu Link {$random_string}";
127 $url = "civicrm/test/{$random_string}";
128 $url_params = "reset=1&output=criteria";
131 'label' => ts($name),
132 'url' => "{$url}?{$url_params}",
139 CRM_Core_BAO_Navigation
::add($params);
140 $new_nav = CRM_Core_BAO_Navigation
::getNavItemByUrl($url, 'reset=1%');
141 $this->assertObjectHasAttribute('id', $new_nav);
142 $this->assertNotNull($new_nav->id
);
147 * Get a count of report instances.
151 protected function getCountReportInstances() {
152 return CRM_Core_DAO
::singleValueQuery(
153 "SELECT count(*) FROM civicrm_navigation WHERE url LIKE 'civicrm/report/instance/%'");
157 * Get a count of navigation items that match the url.
162 protected function getCountURL($url) {
163 return CRM_Core_DAO
::singleValueQuery(
164 "SELECT count(*) FROM civicrm_navigation WHERE url ='{$url}'");
168 * Run fixNavigationMenu() on a menu which already has navIDs
169 * everywhere. They should be unchanged.
171 public function testFixNavigationMenu_preserveIDs() {
174 'label' => 'Custom Menu Entry',
182 'label' => 'Custom Child Menu',
192 CRM_Core_BAO_Navigation
::fixNavigationMenu($output);
194 $this->assertEquals(NULL, $output[10]['attributes']['parentID']);
195 $this->assertEquals(10, $output[10]['attributes']['navID']);
196 $this->assertEquals(10, $output[10]['child'][11]['attributes']['parentID']);
197 $this->assertEquals(11, $output[10]['child'][11]['attributes']['navID']);
201 * Run fixNavigationMenu() on a menu which is missing some navIDs. They
202 * should be filled in, and others should be preserved.
204 public function testFixNavigationMenu_inferIDs() {
207 'label' => 'Custom Menu Entry',
215 'label' => 'Custom Child Menu',
221 'label' => 'Custom Child Menu 2',
230 CRM_Core_BAO_Navigation
::fixNavigationMenu($output);
232 $this->assertEquals('Custom Menu Entry', $output[10]['attributes']['label']);
233 $this->assertEquals(NULL, $output[10]['attributes']['parentID']);
234 $this->assertEquals(10, $output[10]['attributes']['navID']);
236 $this->assertEquals('Custom Child Menu', $output[10]['child'][101]['attributes']['label']);
237 $this->assertEquals(10, $output[10]['child'][101]['attributes']['parentID']);
238 $this->assertEquals(101, $output[10]['child'][101]['attributes']['navID']);
240 $this->assertEquals('Custom Child Menu 2', $output[10]['child'][100]['attributes']['label']);
241 $this->assertEquals(10, $output[10]['child'][100]['attributes']['parentID']);
242 $this->assertEquals(100, $output[10]['child'][100]['attributes']['navID']);
245 public function testFixNavigationMenu_inferIDs_deep() {
248 'label' => 'Custom Menu Entry',
256 'label' => 'Custom Child Menu',
261 'label' => 'Custom Child Menu 2',
272 CRM_Core_BAO_Navigation
::fixNavigationMenu($output);
274 $this->assertEquals('Custom Menu Entry', $output[10]['attributes']['label']);
275 $this->assertEquals(NULL, $output[10]['attributes']['parentID']);
276 $this->assertEquals(10, $output[10]['attributes']['navID']);
278 $this->assertEquals('Custom Child Menu', $output[10]['child'][101]['attributes']['label']);
279 $this->assertEquals(10, $output[10]['child'][101]['attributes']['parentID']);
280 $this->assertEquals(101, $output[10]['child'][101]['attributes']['navID']);
282 $this->assertEquals('Custom Child Menu 2', $output[10]['child'][101]['child'][100]['attributes']['label']);
283 $this->assertEquals(101, $output[10]['child'][101]['child'][100]['attributes']['parentID']);
284 $this->assertEquals(100, $output[10]['child'][101]['child'][100]['attributes']['navID']);
288 * Tests that permissions and component status are checked with the correct operator.
290 public function testCheckPermissions() {
292 'permission' => 'access CiviCRM, access CiviContribute',
295 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviContribute');
296 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM', 'access CiviContribute'];
297 $this->assertTrue(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
299 CRM_Core_BAO_ConfigSetting
::disableComponent('CiviContribute');
300 $this->assertFalse(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
302 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviContribute');
303 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviContribute'];
304 $this->assertFalse(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
306 $menuItem['operator'] = 'OR';
307 $this->assertTrue(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
309 CRM_Core_BAO_ConfigSetting
::disableComponent('CiviContribute');
310 $this->assertFalse(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
312 CRM_Core_BAO_ConfigSetting
::enableComponent('CiviMail');
313 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviMail', 'delete in CiviMail'];
315 'permission' => 'access CiviMail, delete in CiviMail',
318 $this->assertTrue(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
319 $menuItem['operator'] = 'OR';
320 $this->assertTrue(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
321 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['delete in CiviMail'];
322 $this->assertTrue(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
323 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviCRM'];
324 $this->assertFalse(CRM_Core_BAO_Navigation
::checkPermission($menuItem));
325 CRM_Core_Config
::singleton()->userPermissionClass
->permissions
= ['access CiviMail', 'delete in CiviMail'];
326 CRM_Core_BAO_ConfigSetting
::disableComponent('CiviMail');
327 $this->assertFalse(CRM_Core_BAO_Navigation
::checkPermission($menuItem));