CRM-20926 - CRM_Core_MenuTest - Add basic test for XML parsing
[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 }
34
35 /**
36 * Check that novel data elements in the menu are correctly
37 * stored and loaded.
38 */
39 public function testModuleData() {
40 CRM_Core_Menu::store(TRUE);
41 $item = CRM_Core_Menu::get('civicrm/case');
42 $this->assertFalse(isset($item['ids_arguments']['exception']));
43 $this->assertFalse(isset($item['whimsy']));
44
45 CRM_Utils_Hook::singleton()->setHook('civicrm_alterMenu', function(&$items){
46 $items['civicrm/case']['ids_arguments']['exception'][] = 'foobar';
47 $items['civicrm/case']['whimsy'] = 'godliness';
48 });
49
50 CRM_Core_Menu::store(TRUE);
51 $item = CRM_Core_Menu::get('civicrm/case');
52 $this->assertTrue(in_array('foobar', $item['ids_arguments']['exception']));
53 $this->assertEquals('godliness', $item['whimsy']);
54 }
55
56 /**
57 * @return array
58 */
59 public function pathArguments() {
60 $cases = array(); // array(0 => string $input, 1 => array $expectedOutput)
61 //$cases[] = array(NULL, array());
62 //$cases[] = array('', array());
63 //$cases[] = array('freestanding', array('freestanding' => NULL));
64 $cases[] = array('addSequence=1', array('addSequence' => '1'));
65 $cases[] = array('attachUpload=1', array('attachUpload' => '1'));
66 $cases[] = array('mode=256', array('mode' => '256'));
67 $cases[] = array(
68 'mode=256,addSequence=1,attachUpload=1',
69 array('mode' => '256', 'addSequence' => '1', 'attachUpload' => 1),
70 );
71 $cases[] = array(
72 'mode=256,urlToSession=a:b:c:d',
73 array(
74 'mode' => '256',
75 'urlToSession' => array(
76 array('urlVar' => 'a', 'sessionVar' => 'b', 'type' => 'c', 'default' => 'd'),
77 ),
78 ),
79 );
80 $cases[] = array(
81 'mode=256,urlToSession=a:b:c:d;z:y:x:w',
82 array(
83 'mode' => '256',
84 'urlToSession' => array(
85 array('urlVar' => 'a', 'sessionVar' => 'b', 'type' => 'c', 'default' => 'd'),
86 array('urlVar' => 'z', 'sessionVar' => 'y', 'type' => 'x', 'default' => 'w'),
87 ),
88 ),
89 );
90 $cases[] = array('url=whiz!;.:#=%/|+bang?', array('url' => 'whiz!;.:#=%/|+bang?'));
91 return $cases;
92 }
93
94 /**
95 * @param $inputString
96 * @param $expectedArray
97 * @dataProvider pathArguments
98 */
99 public function testGetArrayForPathArgs($inputString, $expectedArray) {
100 $actual = CRM_Core_Menu::getArrayForPathArgs($inputString);
101 $this->assertEquals($expectedArray, $actual);
102 }
103
104 }