Merge pull request #17976 from civicrm/5.28
[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>
5bfe764c
TO
18 <weight>10</weight>
19 </item>
20 </menu>
21 ';
22 $xml = simplexml_load_string($xmlString);
9099cab3 23 $menu = [];
5bfe764c
TO
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']);
5bfe764c 30 $this->assertEquals('10', $menu['civicrm/foo/bar']['weight']);
4535c1f5
TO
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>
36d4fa1b 43 <exception>gamma</exception>
4535c1f5
TO
44 </ids_arguments>
45 </item>
46 </menu>
47 ';
48 $xml = simplexml_load_string($xmlString);
9099cab3 49 $menu = [];
4535c1f5
TO
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']);
9099cab3
CW
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']);
36d4fa1b
TO
56
57 $idsConfig = CRM_Core_IDS::createRouteConfig($menu['civicrm/foo/bar']);
39b959db
SL
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']));
5bfe764c
TO
66 }
67
0e502e45
TO
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');
36d4fa1b 75 $this->assertFalse(isset($item['ids_arguments']['exceptions']));
0e502e45
TO
76 $this->assertFalse(isset($item['whimsy']));
77
39b959db 78 CRM_Utils_Hook::singleton()->setHook('civicrm_alterMenu', function(&$items) {
36d4fa1b 79 $items['civicrm/case']['ids_arguments']['exceptions'][] = 'foobar';
0e502e45
TO
80 $items['civicrm/case']['whimsy'] = 'godliness';
81 });
82
83 CRM_Core_Menu::store(TRUE);
84 $item = CRM_Core_Menu::get('civicrm/case');
36d4fa1b 85 $this->assertTrue(in_array('foobar', $item['ids_arguments']['exceptions']));
0e502e45
TO
86 $this->assertEquals('godliness', $item['whimsy']);
87 }
88
7fe37828
EM
89 /**
90 * @return array
91 */
00be9182 92 public function pathArguments() {
39b959db 93 // array(0 => string $input, 1 => array $expectedOutput)
9099cab3 94 $cases = [];
3b4339fd 95 //$cases[] = array(NULL, array());
96 //$cases[] = array('', array());
97 //$cases[] = array('freestanding', array('freestanding' => NULL));
9099cab3
CW
98 $cases[] = ['addSequence=1', ['addSequence' => '1']];
99 $cases[] = ['attachUpload=1', ['attachUpload' => '1']];
100 $cases[] = ['mode=256', ['mode' => '256']];
101 $cases[] = [
92915c55 102 'mode=256,addSequence=1,attachUpload=1',
9099cab3
CW
103 ['mode' => '256', 'addSequence' => '1', 'attachUpload' => 1],
104 ];
105 $cases[] = [
92915c55 106 'mode=256,urlToSession=a:b:c:d',
9099cab3 107 [
92915c55 108 'mode' => '256',
9099cab3
CW
109 'urlToSession' => [
110 ['urlVar' => 'a', 'sessionVar' => 'b', 'type' => 'c', 'default' => 'd'],
111 ],
112 ],
113 ];
114 $cases[] = [
92915c55 115 'mode=256,urlToSession=a:b:c:d;z:y:x:w',
9099cab3 116 [
92915c55 117 'mode' => '256',
9099cab3
CW
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?']];
3b4339fd 125 return $cases;
126 }
127
128 /**
129 * @param $inputString
130 * @param $expectedArray
131 * @dataProvider pathArguments
132 */
00be9182 133 public function testGetArrayForPathArgs($inputString, $expectedArray) {
3b4339fd 134 $actual = CRM_Core_Menu::getArrayForPathArgs($inputString);
135 $this->assertEquals($expectedArray, $actual);
136 }
96025800 137
ef10e0b5 138}