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