summaryrefslogtreecommitdiff
path: root/t/12-menu.t
blob: c6d775d61fe9b290a565bfeb1afce388043c7ebf (plain)
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. $ENV{TMPDIR} = 't/var';
  5. use Test::More tests => 37;
  6. my $notrap = 1;
  7. if ($^O ne 'MSWin32') { #Test::Trap known to crash Win32
  8. eval 'use Test::Trap';
  9. if ($@) {
  10. $notrap = 1;
  11. } else {
  12. use Test::Trap qw(trap $trap);
  13. $notrap = 0;
  14. }
  15. }
  16. use LedgerSMB::Form;
  17. use LedgerSMB::Menu;
  18. my $ini;
  19. my @r;
  20. my $form;
  21. my $myconfig;
  22. # general new and add_file checks
  23. $ini = new Menu;
  24. ok(defined $ini, 'new: File not provided');
  25. isa_ok($ini, 'Menu', 'new: File not provided gives correct type');
  26. $ini->add_file('t/data/12-menu1.ini');
  27. is_deeply($ini->{'AR--Add Transaction'},
  28. {'action' => 'add', 'module' => 'ar.pl'},
  29. 'add_file: First file added, leaf correct');
  30. is_deeply($ini->{'ORDER'},
  31. ['AR', 'AR--Add Transaction'],
  32. 'add_file: First file added, order correct');
  33. $ini->add_file('t/data/12-menu2.ini');
  34. is_deeply($ini->{'AR--Add Transaction'},
  35. {'action' => 'add', 'module' => 'ar.pl'},
  36. 'add_file: Second file added, old leaf correct');
  37. is_deeply($ini->{'AR--foo'},
  38. {'module' => 'am.pl'},
  39. 'add_file: Second file added, new leaf correct');
  40. is_deeply($ini->{'ORDER'},
  41. ['AR', 'AR--Add Transaction', 'section', 'AR--foo'],
  42. 'add_file: Second file added, order correct');
  43. $ini = Menu->new('t/data/12-menu2.ini');
  44. ok(defined $ini, 'new: File provided');
  45. isa_ok($ini, 'Menu', 'new: File provided gives correct type');
  46. is_deeply($ini->{'AR--foo'},
  47. {'module' => 'am.pl'},
  48. 'new: File provided, leaf correct');
  49. is_deeply($ini->{'ORDER'},
  50. ['section', 'AR--foo'],
  51. 'new: File provided, order correct');
  52. $ini->add_file('t/data/12-menu3.ini');
  53. is_deeply($ini->{'AR--foo'},
  54. {'module' => 'ax.pl'},
  55. 'add_file: Data re-added, leaf correct, not duplicated');
  56. is_deeply($ini->{'ORDER'},
  57. ['section', 'AR--foo'],
  58. 'add_file: Data re-added, order correct, not duplicated');
  59. # menuitem checks
  60. $form = new Form;
  61. $form->{script} = 'tt.pl';
  62. $form->{tag} = '1';
  63. $myconfig = {'login' => 'testuser', 'numberformat' => '1000.00'};
  64. $ini = new Menu('t/data/12-menu4.ini');
  65. is_deeply($ini->{'AR--foo'},
  66. {'module' => 'ax.pl', 'action' => 'add', 'type' => 'foo'},
  67. 'new: Data for menu item test 1 correct');
  68. is_deeply($ini->{'New Window'}, {'target' => '_blank'},
  69. 'new: Data for menu item test 2 correct');
  70. is_deeply($ini->{'Website'}, {'href' => 'ledgersmb.org'},
  71. 'new: Data for menu item test 3 correct');
  72. is_deeply($ini->{'AR--test'},
  73. {'module' => 'test', 'apples' => 'login=', 'pears' => 'numberformat=2'},
  74. 'new: Data for menu item test 4 correct');
  75. is_deeply($ini->{'AR'}, {'target' => 'acc_menu', 'type' => 'test'},
  76. 'new: Data for menu item test 5 correct');
  77. is($ini->menuitem($myconfig, $form, 'AR--foo'), '<a style="display:block;"href="ax.pl?path=bin/mozilla&amp;action=add&amp;level=AR--foo&amp;login=&amp;timeout=&amp;sessionid=&amp;js=&amp;type=foo">',
  78. 'menuitem: Menu item test 1, base');
  79. ok(!defined $ini->{'AR--foo'}->{'module'}, 'menuitem: Deleted module');
  80. ok(!defined $ini->{'AR--foo'}->{'action'}, 'menuitem: Deleted action');
  81. is($ini->menuitem($myconfig, $form, 'New Window'), '<a style="display:block;"href="tt.pl?path=bin/mozilla&amp;action=section_menu&amp;level=New%20Window&amp;login=&amp;timeout=&amp;sessionid=&amp;js=" target="_blank">',
  82. 'menuitem: Menu item test 2, target');
  83. is($ini->menuitem($myconfig, $form, 'Website'), '<a href="ledgersmb.org">',
  84. 'menuitem: Menu item test 3, href');
  85. ok(!defined $ini->{'Website'}->{'href'}, 'menuitem: Deleted href');
  86. $form->{menubar} = 1;
  87. is($ini->menuitem($myconfig, $form, 'AR--test'), '<a style=""href="test?path=bin/mozilla&amp;action=section_menu&amp;level=AR--test&amp;login=&amp;timeout=&amp;sessionid=&amp;js=&amp;apples=testuser&amp;pears=1000.002">',
  88. 'menuitem: Menu item test 4, myconfig substitutions');
  89. is($ini->menuitem($myconfig, $form, 'AR'), '<a style=""href="tt.pl?path=bin/mozilla&amp;action=section_menu&amp;level=AR&amp;login=&amp;timeout=&amp;sessionid=&amp;js=&amp;type=test#id1" target="acc_menu">',
  90. 'menuitem: Menu item test 5, acc_menu');
  91. ok(!defined $ini->{'AR'}->{'target'}, 'menuitem: Deleted target');
  92. # access_control check
  93. $myconfig = {'acs' => 'AR--test'};
  94. $ini = new Menu('t/data/12-menu4.ini');
  95. is_deeply([$ini->access_control($myconfig)], ['AR', 'New Window', 'Website'],
  96. 'access_control: Single item, not top exclusion, top');
  97. is_deeply([$ini->access_control($myconfig, 'AR')], ['AR--foo'],
  98. 'access_control: Single item, not top exclusion, submenu');
  99. $myconfig = {'acs' => 'Website'};
  100. is_deeply([$ini->access_control($myconfig)], ['AR', 'New Window'],
  101. 'access_control: Single item, top exclusion, top');
  102. is_deeply([$ini->access_control($myconfig, 'AR')], ['AR--foo', 'AR--test'],
  103. 'access_control: Single item, top exclusion, sub menu');
  104. $myconfig = {'acs' => 'AR--test;AR--foo;New Window'};
  105. is_deeply([$ini->access_control($myconfig)], ['AR', 'Website'],
  106. 'access_control: Multiple items, top');
  107. is_deeply([$ini->access_control($myconfig, 'AR')], [],
  108. 'access_control: Multiple items, sub menu');
  109. $myconfig = {'acs' => 'AR'};
  110. is_deeply([$ini->access_control($myconfig)], ['New Window', 'Website'],
  111. 'access_control: Top menu exclusion, top');
  112. is_deeply([$ini->access_control($myconfig, 'AR')], ['AR--foo', 'AR--test'],
  113. 'access_control: Top menu exclusion, sub menu');
  114. # file not found check
  115. SKIP: {
  116. skip 'file not found test, Test::Trap not usable', 2 if !$notrap;
  117. $ini = new Menu;
  118. @r = trap{$ini->add_file('t/data/12-not-a-file')};
  119. is_deeply($ini->{'ORDER'}, [],
  120. 'add_file: Non-existent file added, order correct');
  121. like($trap->die, qr|12-not-a-file :|,
  122. 'add_file: Non-existent file causes error display');
  123. }
  124. ### Gratuitous testing to increase coverage rating
  125. ##$ini = undef;
  126. ##@r = trap{$ini = Menu::new};
  127. ##isa_ok($ini, 'main', 'new: No type passed gives main type');
  128. ##
  129. ##my $pkg = 'foo';
  130. ##$ini = undef;
  131. ##@r = trap{$ini = Menu::new(\$pkg)};
  132. ##like($trap->{warn}[0], qr|has no copy constructor! creating a new object|,
  133. ## 'new: Type passed scalar reference');
  134. ##isa_ok($ini, 'SCALAR', 'new: Type passed scalar reference gives SCALAR type');
  135. ##Menu::add_file($ini, 't/data/12-menu1.ini');
  136. ##is_deeply($ini->{'AR--Add Transaction'},
  137. ## {'action' => 'add', 'module' => 'ar.pl'},
  138. ## 'add_file: File added to SCALAR, leaf correct');
  139. ##is_deeply($ini->{'ORDER'},
  140. ## ['AR', 'AR--Add Transaction'],
  141. ## 'add_file: File added to SCALAR, order correct');