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