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