summaryrefslogtreecommitdiff
path: root/t/11-ledgersmb.t
blob: be112bef073bc3553270ab5b610863e38bc2e96f (plain)
  1. #!/usr/bin/perl
  2. use strict;
  3. use warnings;
  4. $ENV{TMPDIR} = 't/var';
  5. use Test::More 'no_plan';
  6. use Test::Exception;
  7. use Test::Trap qw(trap $trap);
  8. use Math::BigFloat;
  9. use LedgerSMB::Sysconfig;
  10. use LedgerSMB;
  11. my $lsmb;
  12. sub redirect {
  13. print "redirected\n";
  14. }
  15. sub lsmb_error_func {
  16. print $_[0];
  17. }
  18. ##line subroutine
  19. ##108 new
  20. ##235 redirect
  21. ##254 format_amount
  22. ##364 parse_amount
  23. ##408 round_amount
  24. ##423 call_procedure
  25. ##454 date_to_number
  26. ##490 db_init
  27. ##522 redo_rows
  28. ##547 merge
  29. $lsmb = LedgerSMB->new();
  30. my %myconfig;
  31. my $utfstr;
  32. my @r;
  33. ok(defined $lsmb);
  34. isa_ok($lsmb, 'LedgerSMB');
  35. # $lsmb->escape checks
  36. $lsmb = LedgerSMB->new();
  37. $utfstr = "\xd8\xad";
  38. utf8::decode($utfstr);
  39. ok(!$lsmb->escape, 'escape: (undef)');
  40. ok(!$lsmb->escape('foo' => 'bar'), 'escape: (invalid args)');
  41. cmp_ok($lsmb->escape('string' => ' '), 'eq', '%20',
  42. 'escape: \' \'');
  43. cmp_ok($lsmb->escape('string' => 'foo'), 'eq', 'foo',
  44. 'escape: foo');
  45. cmp_ok($lsmb->escape('string' => 'foo bar'), 'eq', 'foo%20bar',
  46. 'escape: foo bar');
  47. TODO: {
  48. local $TODO = 'Fun with Unicode';
  49. cmp_ok($lsmb->escape('string' => $utfstr), 'eq', '%d8%ad',
  50. 'escape: U+D8AD');
  51. }
  52. # $lsmb->is_blank checks
  53. $lsmb = LedgerSMB->new();
  54. $lsmb->{blank} = ' ';
  55. $lsmb->{notblank} = ' d ';
  56. TODO: {
  57. local $TODO = 'Errors should be thrown';
  58. throws_ok{$lsmb->is_blank} 'Error::Simple', 'is_blank: (undef)';
  59. throws_ok{$lsmb->is_blank('foo' => 'bar')} 'Error::Simple',
  60. 'is_blank: (invalid args)';
  61. }
  62. is($lsmb->is_blank('name' => 'notblank'), 0, 'is_blank: notblank');
  63. is($lsmb->is_blank('name' => 'blank'), 1, 'is_blank: blank');
  64. # $lsmb->is_run_mode checks
  65. $lsmb = LedgerSMB->new();
  66. $ENV{GATEWAY_INTERFACE} = 'foo';
  67. is($lsmb->is_run_mode('cgi'), 1, 'is_run_mode: CGI - CGI');
  68. is($lsmb->is_run_mode('cli'), 0, 'is_run_mode: CGI - CLI');
  69. is($lsmb->is_run_mode('mod_perl'), 0, 'is_run_mode: CGI - mod_perl');
  70. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: CGI - (bad mode)');
  71. is($lsmb->is_run_mode, 0, 'is_run_mode: CGI - (unknown mode)');
  72. $ENV{MOD_PERL} = 'foo';
  73. is($lsmb->is_run_mode('cgi'), 1, 'is_run_mode: CGI/mod_perl - CGI');
  74. is($lsmb->is_run_mode('cli'), 0, 'is_run_mode: CGI/mod_perl - CLI');
  75. is($lsmb->is_run_mode('mod_perl'), 1, 'is_run_mode: CGI/mod_perl - mod_perl');
  76. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: CGI/mod_perl - (bad mode)');
  77. is($lsmb->is_run_mode, 0, 'is_run_mode: CGI/mod_perl - (unknown mode)');
  78. delete $ENV{GATEWAY_INTERFACE};
  79. is($lsmb->is_run_mode('cgi'), 0, 'is_run_mode: mod_perl - CGI');
  80. is($lsmb->is_run_mode('cli'), 0, 'is_run_mode: mod_perl - CLI');
  81. is($lsmb->is_run_mode('mod_perl'), 1, 'is_run_mode: mod_perl - mod_perl');
  82. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: mod_perl - (bad mode)');
  83. is($lsmb->is_run_mode, 0, 'is_run_mode: mod_perl - (unknown mode)');
  84. delete $ENV{MOD_PERL};
  85. is($lsmb->is_run_mode('cgi'), 0, 'is_run_mode: CLI - CGI');
  86. is($lsmb->is_run_mode('cli'), 1, 'is_run_mode: CLI - CLI');
  87. is($lsmb->is_run_mode('mod_perl'), 0, 'is_run_mode: CLI - mod_perl');
  88. is($lsmb->is_run_mode('foo'), 0, 'is_run_mode: CLI - (bad mode)');
  89. is($lsmb->is_run_mode, 0, 'is_run_mode: CLI - (unknown mode)');
  90. # $lsmb->num_text_rows checks
  91. $lsmb = LedgerSMB->new();
  92. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10, 'max' => 5),
  93. 2, 'num_text_rows: 2 rows, no column breakage, max 5 rows');
  94. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10, 'max' => 1),
  95. 1, 'num_text_rows: 2 rows, no column breakage, max 1 row');
  96. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10, 'max' => 2),
  97. 2, 'num_text_rows: 2 rows, no column breakage, max 2 rows');
  98. is($lsmb->num_text_rows('string' => "apple\npear", 'cols' => 10),
  99. 2, 'num_text_rows: 2 rows, no column breakage, no max row count');
  100. is($lsmb->num_text_rows('string' => "01234567890123456789", 'cols' => 10),
  101. 2, 'num_text_rows: 2 rows, non-word column breakage, no max row count');
  102. is($lsmb->num_text_rows('string' => "012345 67890123 456789", 'cols' => 10),
  103. 3, 'num_text_rows: 3 rows, word column breakage, no max row count');
  104. is($lsmb->num_text_rows('string' => "0123456789", 'cols' => 10),
  105. 1, 'num_text_rows: 1 rows, no breakage, max cols, no max row count');
  106. is($lsmb->num_text_rows('string' => "01234567890", 'cols' => 10),
  107. 2, 'num_text_rows: 2 rows, no breakage, max cols+1, no max row count');
  108. is($lsmb->num_text_rows('string' => "1\n\n2", 'cols' => 10),
  109. 3, 'num_text_rows: 3 rows, no breakage, blank line, no max row count');
  110. is($lsmb->num_text_rows('string' => "012345 67890123456789", 'cols' => 10),
  111. 3, 'num_text_rows: 3 rows, word and non column breakage, no max row count');
  112. # $lsmb->debug checks
  113. $lsmb = LedgerSMB->new();
  114. @r = trap{$lsmb->debug()};
  115. #SKIP: {like($trap->stdout, qr|\n\$VAR1 = bless\( {[\n\s]+'action' => '',[\n\s]+'dbversion' => '\d+\.\d+\.\d+',[\n\s]+'path' => 'bin/mozilla',[\n\s]+'version' => '$lsmb->{version}'[\n\s]+}, 'LedgerSMB' \);|,
  116. # 'debug: $lsmb->debug');
  117. #}
  118. SKIP: {
  119. skip 'Environment for file test not clean' if -f "t/var/lsmb-11.$$";
  120. $lsmb->{file} = "t/var/lsmb-11.$$";
  121. $lsmb->debug({'file' => $lsmb->{file}});
  122. ok(-f "t/var/lsmb-11.$$", "debug: output file t/var/lsmb-11.$$ created");
  123. open(my $FH, '<', "t/var/lsmb-11.$$");
  124. my @str = <$FH>;
  125. close($FH);
  126. chomp(@str);
  127. cmp_ok(grep (/\s?\$VAR1\s=\sbless/, @str), '>', 0,
  128. 'Debug Contents, var1 type');
  129. cmp_ok(grep (/'action' => ''/, @str), '>', 0,
  130. 'Debug contents, blank action');
  131. cmp_ok(grep (/'dbversion' => '\d+\.\d+\.\d+'/, @str), '>', 0,
  132. 'Debug contents, dbversion format');
  133. cmp_ok(grep (/'path' => 'bin\/mozilla'/, @str), '>', 0,
  134. 'Debug contents, path');
  135. cmp_ok(grep (/'version' => '$lsmb->{version}'/, @str), '>', 0,
  136. 'Debug contents, version match');
  137. cmp_ok(grep (/'file' => 't\/var\/lsmb-11.$$'/, @str), '>', 0,
  138. 'Debug contents file attribute match');
  139. is(unlink("t/var/lsmb-11.$$"), 1, "debug: removing t/var/lsmb-11.$$");
  140. ok(!-e "t/var/lsmb-11.$$", "debug: t/var/lsmb-11.$$ removed");
  141. };
  142. $lsmb->{file} = 't/this is a bad directory, I do not exist/foo';
  143. @r = trap {$lsmb->debug({'file' => $lsmb->{file}, $lsmb})};
  144. like($trap->die, qr/No such file or directory/,
  145. "debug: open failure causes death");
  146. ok(!-e $lsmb->{file}, "debug: file creation failed");
  147. # $lsmb->new checks
  148. $lsmb = LedgerSMB->new();
  149. ok(defined $lsmb, 'new: blank, defined');
  150. isa_ok($lsmb, 'LedgerSMB', 'new: blank, correct type');
  151. ok(defined $lsmb->{action}, 'new: blank, action defined');
  152. ok(defined $lsmb->{dbversion}, 'new: blank, dbversion defined');
  153. ok(defined $lsmb->{path}, 'new: blank, path defined');
  154. ok(defined $lsmb->{version}, 'new: blank, version defined');
  155. $lsmb = LedgerSMB->new('path=bin/lynx');
  156. ok(defined $lsmb, 'new: lynx, defined');
  157. isa_ok($lsmb, 'LedgerSMB', 'new: lynx, correct type');
  158. ok(defined $lsmb->{action}, 'new: lynx, action defined');
  159. ok(defined $lsmb->{dbversion}, 'new: lynx, dbversion defined');
  160. ok(defined $lsmb->{path}, 'new: lynx, path defined');
  161. is($lsmb->{path}, 'bin/lynx', 'new: lynx, path carried through');
  162. ok(defined $lsmb->{lynx}, 'new: lynx, lynx defined');
  163. is($lsmb->{lynx}, 1, 'new: lynx, lynx enabled');
  164. ok(defined $lsmb->{menubar}, 'new: lynx, menubar defined (deprecated)');
  165. is($lsmb->{menubar}, 1, 'new: lynx, menubar enabled (deprecated)');
  166. ok(defined $lsmb->{version}, 'new: lynx, version defined');
  167. # $lsmb->redirect checks
  168. $lsmb = LedgerSMB->new();
  169. ok(!defined $lsmb->{callback}, 'redirect: No callback set');
  170. @r = trap{$lsmb->redirect};
  171. is($trap->stdout, "redirected\n", 'redirect: No message or callback redirect');
  172. #TODO: {
  173. #local $TODO = '$lsmb->info for LedgerSMB';
  174. #@r = trap{$lsmb->redirect('msg' => 'hello world')};
  175. #is($trap->stdout, "hello world\n",
  176. # 'redirect: message, no callback redirect');
  177. #}
  178. $lsmb->{callback} = 1;
  179. @r = trap{$lsmb->redirect};
  180. is($trap->stdout, "redirected\n", 'redirect: callback, no message redirect');
  181. @r = trap{$lsmb->redirect('msg' => "hello world\n")};
  182. is($trap->stdout, "redirected\n", 'redirect: callback and message redirect');
  183. # $lsmb->call_procedure checks
  184. SKIP: {
  185. skip 'Skipping call_procedure tests, no db specified'
  186. if !defined $ENV{PGDATABASE};
  187. $lsmb = LedgerSMB->new();
  188. $lsmb->{dbh} = DBI->connect("dbi:Pg:dbname=$ENV{PGDATABASE}",
  189. undef, undef, {AutoCommit => 0 });
  190. @r = $lsmb->call_procedure('procname' => 'character_length',
  191. 'args' => ['month']);
  192. is($#r, 0, 'call_procedure: correct return length (one row)');
  193. is($r[0]->{'character_length'}, 5,
  194. 'call_procedure: single arg, non-numeric return');
  195. @r = $lsmb->call_procedure('procname' => 'trunc', 'args' => [57.1, 0]);
  196. is($r[0]->{'trunc'}, Math::BigFloat->new('57'),
  197. 'call_procedure: two args, numeric return');
  198. @r = $lsmb->call_procedure('procname' => 'pi', 'args' => []);
  199. like($r[0]->{'pi'}, qr/^3.14/,
  200. 'call_procedure: empty arg list, non-numeric return');
  201. @r = $lsmb->call_procedure('procname' => 'pi');
  202. like($r[0]->{'pi'}, qr/^3.14/,
  203. 'call_procedure: no args, non-numeric return');
  204. }
  205. # $lsmb->merge checks
  206. $lsmb = LedgerSMB->new();
  207. $lsmb->merge({'apple' => 1, 'pear' => 2, 'peach' => 3}, 'keys' => ['apple', 'pear']);
  208. ok(!defined $lsmb->{peach}, 'merge: Did not add unselected key');
  209. is($lsmb->{apple}, 1, 'merge: Added unselected key apple');
  210. is($lsmb->{pear}, 2, 'merge: Added unselected key pear');
  211. like($lsmb->{path}, qr#bin/(lynx|mozilla)#, 'merge: left existing key');
  212. $lsmb = LedgerSMB->new();
  213. $lsmb->merge({'apple' => 1, 'pear' => 2, 'peach' => 3});
  214. is($lsmb->{apple}, 1, 'merge: No key, added apple');
  215. is($lsmb->{pear}, 2, 'merge: No key, added pear');
  216. is($lsmb->{peach}, 3, 'merge: No key, added peach');
  217. like($lsmb->{path}, qr#bin/(lynx|mozilla)#, 'merge: No key, left existing key');
  218. $lsmb = LedgerSMB->new();
  219. $lsmb->merge({'apple' => 1, 'pear' => 2, 'peach' => 3}, 'index' => 1);
  220. is($lsmb->{apple_1}, 1, 'merge: Index 1, added apple as apple_1');
  221. is($lsmb->{pear_1}, 2, 'merge: Index 1, added pear as pear_1');
  222. is($lsmb->{peach_1}, 3, 'merge: Index 1, added peach as peach_1');
  223. like($lsmb->{path}, qr#bin/(lynx|mozilla)#, 'merge: Index 1, left existing key');
  224. # $lsmb->is_allowed_role checks
  225. $lsmb = LedgerSMB->new();
  226. $lsmb->{_roles} = ['apple', 'pear'];
  227. is($lsmb->is_allowed_role({allowed_roles => ['pear']}), 1,
  228. 'is_allowed_role: allowed role');
  229. $lsmb->{_roles} = ['apple', 'pear'];
  230. is($lsmb->is_allowed_role({allowed_roles => ['peach']}), 0,
  231. 'is_allowed_role: disallowed role');
  232. is($lsmb->is_allowed_role({'allowed_roles' => []}), 0,
  233. 'is_allowed_role: no allowable roles');
  234. delete $lsmb->{_roles};
  235. is($lsmb->is_allowed_role({'allowed_roles' => ['apple']}), 0,
  236. 'is_allowed_role: no roles for user');