Move pear/mail from packages to composer.json.
[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 <icon>admin/small/foo.png</icon>
19 <weight>10</weight>
20 </item>
21 </menu>
22 ';
23 $xml = simplexml_load_string($xmlString);
24 $menu = array();
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']);
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>
45 <exception>gamma</exception>
46 </ids_arguments>
47 </item>
48 </menu>
49 ';
50 $xml = simplexml_load_string($xmlString);
51 $menu = array();
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']);
55 $this->assertEquals(array('alpha', 'beta'), $menu['civicrm/foo/bar']['ids_arguments']['json']);
56 $this->assertEquals(array('gamma'), $menu['civicrm/foo/bar']['ids_arguments']['exceptions']);
57 $this->assertEquals(array(), $menu['civicrm/foo/bar']['ids_arguments']['html']);
58
59 $idsConfig = CRM_Core_IDS::createRouteConfig($menu['civicrm/foo/bar']);
60 $this->assertTrue(in_array('alpha', $idsConfig['General']['json'])); // XML
61 $this->assertTrue(in_array('beta', $idsConfig['General']['json'])); // XML
62 $this->assertTrue(in_array('gamma', $idsConfig['General']['exceptions'])); // XML
63 $this->assertTrue(in_array('thankyou_text', $idsConfig['General']['exceptions'])); // Inherited
64 }
65
66 /**
67 * Check that novel data elements in the menu are correctly
68 * stored and loaded.
69 */
70 public function testModuleData() {
71 CRM_Core_Menu::store(TRUE);
72 $item = CRM_Core_Menu::get('civicrm/case');
73 $this->assertFalse(isset($item['ids_arguments']['exceptions']));
74 $this->assertFalse(isset($item['whimsy']));
75
76 CRM_Utils_Hook::singleton()->setHook('civicrm_alterMenu', function(&$items){
77 $items['civicrm/case']['ids_arguments']['exceptions'][] = 'foobar';
78 $items['civicrm/case']['whimsy'] = 'godliness';
79 });
80
81 CRM_Core_Menu::store(TRUE);
82 $item = CRM_Core_Menu::get('civicrm/case');
83 $this->assertTrue(in_array('foobar', $item['ids_arguments']['exceptions']));
84 $this->assertEquals('godliness', $item['whimsy']);
85 }
86
87 /**
88 * @return array
89 */
90 public function pathArguments() {
91 $cases = array(); // array(0 => string $input, 1 => array $expectedOutput)
92 //$cases[] = array(NULL, array());
93 //$cases[] = array('', array());
94 //$cases[] = array('freestanding', array('freestanding' => NULL));
95 $cases[] = array('addSequence=1', array('addSequence' => '1'));
96 $cases[] = array('attachUpload=1', array('attachUpload' => '1'));
97 $cases[] = array('mode=256', array('mode' => '256'));
98 $cases[] = array(
99 'mode=256,addSequence=1,attachUpload=1',
100 array('mode' => '256', 'addSequence' => '1', 'attachUpload' => 1),
101 );
102 $cases[] = array(
103 'mode=256,urlToSession=a:b:c:d',
104 array(
105 'mode' => '256',
106 'urlToSession' => array(
107 array('urlVar' => 'a', 'sessionVar' => 'b', 'type' => 'c', 'default' => 'd'),
108 ),
109 ),
110 );
111 $cases[] = array(
112 'mode=256,urlToSession=a:b:c:d;z:y:x:w',
113 array(
114 'mode' => '256',
115 'urlToSession' => array(
116 array('urlVar' => 'a', 'sessionVar' => 'b', 'type' => 'c', 'default' => 'd'),
117 array('urlVar' => 'z', 'sessionVar' => 'y', 'type' => 'x', 'default' => 'w'),
118 ),
119 ),
120 );
121 $cases[] = array('url=whiz!;.:#=%/|+bang?', array('url' => 'whiz!;.:#=%/|+bang?'));
122 return $cases;
123 }
124
125 /**
126 * @param $inputString
127 * @param $expectedArray
128 * @dataProvider pathArguments
129 */
130 public function testGetArrayForPathArgs($inputString, $expectedArray) {
131 $actual = CRM_Core_Menu::getArrayForPathArgs($inputString);
132 $this->assertEquals($expectedArray, $actual);
133 }
134
135 }