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