Merge pull request #17284 from agh1/admin-console-only
[civicrm-core.git] / tests / phpunit / CRM / Core / MenuTest.php
1 <?php
2
3 /**
4 * Class CRM_Core_MenuTest
5 * @group headless
6 */
7 class CRM_Core_MenuTest extends CiviUnitTestCase {
8
9 public function testReadXML() {
10 $xmlString = '<?xml version="1.0" encoding="iso-8859-1" ?>
11 <menu>
12 <item>
13 <path>civicrm/foo/bar</path>
14 <title>Foo Bar</title>
15 <desc>The foo is one with the bar.</desc>
16 <page_callback>CRM_Foo_Page_Bar</page_callback>
17 <adminGroup>Customize Data and Screens</adminGroup>
18 <weight>10</weight>
19 </item>
20 </menu>
21 ';
22 $xml = simplexml_load_string($xmlString);
23 $menu = [];
24 CRM_Core_Menu::readXML($xml, $menu);
25 $this->assertTrue(isset($menu['civicrm/foo/bar']));
26 $this->assertEquals('Foo Bar', $menu['civicrm/foo/bar']['title']);
27 $this->assertEquals('The foo is one with the bar.', $menu['civicrm/foo/bar']['desc']);
28 $this->assertEquals('CRM_Foo_Page_Bar', $menu['civicrm/foo/bar']['page_callback']);
29 $this->assertEquals('Customize Data and Screens', $menu['civicrm/foo/bar']['adminGroup']);
30 $this->assertEquals('10', $menu['civicrm/foo/bar']['weight']);
31 $this->assertTrue(!isset($menu['civicrm/foo/bar']['ids_arguments']));
32 }
33
34 public function testReadXML_IDS() {
35 $xmlString = '<?xml version="1.0" encoding="iso-8859-1" ?>
36 <menu>
37 <item>
38 <path>civicrm/foo/bar</path>
39 <title>Foo Bar</title>
40 <ids_arguments>
41 <json>alpha</json>
42 <json>beta</json>
43 <exception>gamma</exception>
44 </ids_arguments>
45 </item>
46 </menu>
47 ';
48 $xml = simplexml_load_string($xmlString);
49 $menu = [];
50 CRM_Core_Menu::readXML($xml, $menu);
51 $this->assertTrue(isset($menu['civicrm/foo/bar']));
52 $this->assertEquals('Foo Bar', $menu['civicrm/foo/bar']['title']);
53 $this->assertEquals(['alpha', 'beta'], $menu['civicrm/foo/bar']['ids_arguments']['json']);
54 $this->assertEquals(['gamma'], $menu['civicrm/foo/bar']['ids_arguments']['exceptions']);
55 $this->assertEquals([], $menu['civicrm/foo/bar']['ids_arguments']['html']);
56
57 $idsConfig = CRM_Core_IDS::createRouteConfig($menu['civicrm/foo/bar']);
58 // XML
59 $this->assertTrue(in_array('alpha', $idsConfig['General']['json']));
60 // XML
61 $this->assertTrue(in_array('beta', $idsConfig['General']['json']));
62 // XML
63 $this->assertTrue(in_array('gamma', $idsConfig['General']['exceptions']));
64 // Inherited
65 $this->assertTrue(in_array('thankyou_text', $idsConfig['General']['exceptions']));
66 }
67
68 /**
69 * Check that novel data elements in the menu are correctly
70 * stored and loaded.
71 */
72 public function testModuleData() {
73 CRM_Core_Menu::store(TRUE);
74 $item = CRM_Core_Menu::get('civicrm/case');
75 $this->assertFalse(isset($item['ids_arguments']['exceptions']));
76 $this->assertFalse(isset($item['whimsy']));
77
78 CRM_Utils_Hook::singleton()->setHook('civicrm_alterMenu', function(&$items) {
79 $items['civicrm/case']['ids_arguments']['exceptions'][] = 'foobar';
80 $items['civicrm/case']['whimsy'] = 'godliness';
81 });
82
83 CRM_Core_Menu::store(TRUE);
84 $item = CRM_Core_Menu::get('civicrm/case');
85 $this->assertTrue(in_array('foobar', $item['ids_arguments']['exceptions']));
86 $this->assertEquals('godliness', $item['whimsy']);
87 }
88
89 /**
90 * @return array
91 */
92 public function pathArguments() {
93 // array(0 => string $input, 1 => array $expectedOutput)
94 $cases = [];
95 //$cases[] = array(NULL, array());
96 //$cases[] = array('', array());
97 //$cases[] = array('freestanding', array('freestanding' => NULL));
98 $cases[] = ['addSequence=1', ['addSequence' => '1']];
99 $cases[] = ['attachUpload=1', ['attachUpload' => '1']];
100 $cases[] = ['mode=256', ['mode' => '256']];
101 $cases[] = [
102 'mode=256,addSequence=1,attachUpload=1',
103 ['mode' => '256', 'addSequence' => '1', 'attachUpload' => 1],
104 ];
105 $cases[] = [
106 'mode=256,urlToSession=a:b:c:d',
107 [
108 'mode' => '256',
109 'urlToSession' => [
110 ['urlVar' => 'a', 'sessionVar' => 'b', 'type' => 'c', 'default' => 'd'],
111 ],
112 ],
113 ];
114 $cases[] = [
115 'mode=256,urlToSession=a:b:c:d;z:y:x:w',
116 [
117 'mode' => '256',
118 'urlToSession' => [
119 ['urlVar' => 'a', 'sessionVar' => 'b', 'type' => 'c', 'default' => 'd'],
120 ['urlVar' => 'z', 'sessionVar' => 'y', 'type' => 'x', 'default' => 'w'],
121 ],
122 ],
123 ];
124 $cases[] = ['url=whiz!;.:#=%/|+bang?', ['url' => 'whiz!;.:#=%/|+bang?']];
125 return $cases;
126 }
127
128 /**
129 * @param $inputString
130 * @param $expectedArray
131 * @dataProvider pathArguments
132 */
133 public function testGetArrayForPathArgs($inputString, $expectedArray) {
134 $actual = CRM_Core_Menu::getArrayForPathArgs($inputString);
135 $this->assertEquals($expectedArray, $actual);
136 }
137
138 }