Merge pull request #6936 from eileenmcnaughton/CRM-14720
[civicrm-core.git] / tests / phpunit / CRM / Core / BAO / NavigationTest.php
CommitLineData
31cb1898
AN
1<?php
2require_once 'CiviTest/CiviUnitTestCase.php';
3
4/**
5 * Class CRM_Core_BAO_NavigationTest.
6 */
7class CRM_Core_BAO_NavigationTest extends CiviUnitTestCase {
8
9 /**
10 * Set up data for the test run.
11 *
12 * Here we ensure we are starting from a default report navigation.
13 */
14 public function setUp() {
15 parent::setUp();
412b762a 16 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
31cb1898
AN
17 }
18
19 /**
20 * Test that a missing report menu link is added by rebuildReportsNavigation.
21 */
22 public function testCreateMissingReportMenuItemLink() {
23 $reportCount = $this->getCountReportInstances();
24 CRM_Core_DAO::executeQuery("DELETE FROM civicrm_navigation WHERE url = 'civicrm/report/instance/1?reset=1'");
25 $this->assertEquals($reportCount - 1, $this->getCountReportInstances());
412b762a 26 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
31cb1898
AN
27
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);
34 }
35
36 /**
0087242f 37 * Test that an existing report link is rebuilt under it's parent.
31cb1898
AN
38 *
39 * Function tests CRM_Core_BAO_Navigation::rebuildReportsNavigation.
40 */
41 public function testUpdateExistingReportMenuLink() {
42 $url = 'civicrm/report/instance/1';
43 $url_params = 'reset=1';
44 $existing_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($url, $url_params);
45 $this->assertNotEquals(FALSE, $existing_nav);
46 $existing_nav->parent_id = 1;
47 $existing_nav->save();
412b762a 48 CRM_Core_BAO_Navigation::rebuildReportsNavigation(CRM_Core_Config::domainID());
31cb1898
AN
49 $parent_url = 'civicrm/report/list';
50 $parent_url_params = 'compid=99&reset=1';
0087242f 51 $reportsMenu = CRM_Core_BAO_Navigation::createOrUpdateTopLevelReportsNavItem(CRM_Core_Config::domainID());
52 $parent_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($parent_url, $parent_url_params, $reportsMenu->id);
31cb1898
AN
53 $this->assertNotEquals($parent_nav->id, 1);
54 $changed_existing_nav = new CRM_Core_BAO_Navigation();
55 $changed_existing_nav->id = $existing_nav->id;
56 $changed_existing_nav->find(TRUE);
57 $this->assertEquals($changed_existing_nav->parent_id, $parent_nav->id);
58 }
59
60
61 /**
62 * Test that a navigation item can be retrieved by it's url.
63 */
64 public function testGetNavItemByUrl() {
65 $random_string = substr(sha1(rand()), 0, 7);
66 $name = "Test Menu Link {$random_string}";
67 $url = "civicrm/test/{$random_string}";
68 $url_params = "reset=1";
69 $params = array(
70 'name' => $name,
71 'label' => ts($name),
72 'url' => "{$url}?{$url_params}",
73 'parent_id' => NULL,
74 'is_active' => TRUE,
75 'permission' => array(
76 'access CiviCRM',
77 ),
78 );
79 CRM_Core_BAO_Navigation::add($params);
80 $new_nav = CRM_Core_BAO_Navigation::getNavItemByUrl($url, $url_params);
81 $this->assertObjectHasAttribute('id', $new_nav);
82 $this->assertNotNull($new_nav->id);
83 $new_nav->delete();
84 }
85
86 /**
87 * Get a count of report instances.
88 *
89 * @return int
90 */
91 protected function getCountReportInstances() {
92 return CRM_Core_DAO::singleValueQuery(
93 "SELECT count(*) FROM civicrm_navigation WHERE url LIKE 'civicrm/report/instance/%'");
94 }
95
96}