Revision 9866d032
Von Sven Schöling vor 8 Monaten hinzugefügt
t/fxtransaction/ap_transactions.t | ||
---|---|---|
25 | 25 |
|
26 | 26 |
use HTML::Query; |
27 | 27 |
use Support::TestSetup; |
28 |
use Support::Integration; |
|
28 | 29 |
|
29 | 30 |
my $part; |
30 | 31 |
my $vendor; |
... | ... | |
69 | 70 |
|
70 | 71 |
} |
71 | 72 |
|
72 |
package MockDispatcher { |
|
73 |
sub end_request { die "END_OF_REQUEST" } |
|
74 |
}; |
|
75 |
$::dispatcher = bless { }, "MockDispatcher"; |
|
76 |
|
|
77 |
# make a pseudo request to an old bin/mozilla style entry point |
|
78 |
# captures STDOUT, STDERR and the return values of the called action |
|
79 |
# |
|
80 |
# the given form should be set up like it would be in Dispatcher after read_cgi_input |
|
81 |
sub make_request { |
|
82 |
my ($script, $form, $action) = @_; |
|
83 |
|
|
84 |
my ($out, $err, @ret); |
|
85 |
|
|
86 |
package main { |
|
87 |
local $SIG{__WARN__} = sub { |
|
88 |
# ignore spurious warnings, TAP::Harness calls this warnings enabled |
|
89 |
}; |
|
90 |
|
|
91 |
require "bin/mozilla/$script.pl"; |
|
92 |
|
|
93 |
open(my $out_fh, '>', \$out) or die; |
|
94 |
open(my $err_fh, '>', \$err) or die; |
|
95 |
|
|
96 |
local *STDOUT = $out_fh; |
|
97 |
local *STDERR = $err_fh; |
|
98 |
|
|
99 |
local $::form = Form->new; |
|
100 |
$::form->{$_} = $form->{$_} for keys %$form; |
|
101 |
$::form->{script} = $script.'.pl'; # usually set by dispatcher, needed for checks in update_exchangerate |
|
102 |
local $ENV{REQUEST_URI} = "http://localhost/$script.pl"; # needed for Form::redirect_header |
|
103 |
|
|
104 |
no strict "refs"; |
|
105 |
eval { |
|
106 |
no warnings; |
|
107 |
@ret = &{ "::$action" }(); |
|
108 |
1; |
|
109 |
} or do { my $err = $@; |
|
110 |
die unless $err =~ /^END_OF_REQUEST/; |
|
111 |
@ret = (1); |
|
112 |
} |
|
113 |
} |
|
114 |
return ($out, $err, @ret); |
|
115 |
} |
|
116 |
|
|
117 |
sub form_from_html { |
|
118 |
my ($html) = @_; |
|
119 |
my $q = HTML::Query->new(text => $html); |
|
120 |
|
|
121 |
my %form; |
|
122 |
for my $input ($q->query('#form input')->get_elements()) { |
|
123 |
next if !$input->attr('name') || $input->attr('disabled'); |
|
124 |
$form{ $input->attr('name') } = $input->attr('value') // ""; |
|
125 |
} |
|
126 |
for my $select ($q->query('#form select')->get_elements()) { |
|
127 |
my $name = $select->attr('name'); |
|
128 |
my ($selected_option) = ( |
|
129 |
grep({ $_->tag eq 'option' && $_->attr('selected') } $select->content_list), |
|
130 |
grep({ $_->tag eq 'option' } $select->content_list) |
|
131 |
); |
|
132 |
|
|
133 |
$form{ $name } = $selected_option->attr('value') // $selected_option->as_text |
|
134 |
if $selected_option; |
|
135 |
} |
|
136 |
|
|
137 |
%form; |
|
138 |
} |
|
139 |
|
|
140 | 73 |
######## main test code ####### |
141 | 74 |
|
142 | 75 |
Support::TestSetup::login(); |
76 |
Support::Integration::setup(); |
|
143 | 77 |
init_db(); |
144 | 78 |
|
145 | 79 |
{ |
... | ... | |
153 | 87 |
my %form; |
154 | 88 |
|
155 | 89 |
# make new invoice |
156 |
my ($out, $err, @ret) = make_request('ir', { type => 'invoice' }, 'add');
|
|
90 |
my ($out, $err, @ret) = make_request('ir', 'add', type => 'invoice');
|
|
157 | 91 |
is $ret[0], 1, "new purchase invoice"; |
158 | 92 |
%form = form_from_html($out); |
159 | 93 |
|
... | ... | |
162 | 96 |
$form{currency} = $currency; |
163 | 97 |
|
164 | 98 |
# update |
165 |
($out, $err, @ret) = make_request('ir', \%form, 'update');
|
|
99 |
($out, $err, @ret) = make_request('ir', 'update', %form);
|
|
166 | 100 |
is $ret[0], 1, "update purchase invoice with currency"; |
167 | 101 |
%form = form_from_html($out); |
168 | 102 |
|
... | ... | |
171 | 105 |
$form{partnumber_1} = $part->partnumber; |
172 | 106 |
|
173 | 107 |
# update |
174 |
($out, $err, @ret) = make_request('ir', \%form, 'update');
|
|
108 |
($out, $err, @ret) = make_request('ir', 'update', %form);
|
|
175 | 109 |
is $ret[0], 1, "update purchase invoice with part and exchangerate"; |
176 | 110 |
%form = form_from_html($out); |
177 | 111 |
|
... | ... | |
179 | 113 |
$form{paid_1} = _format_number($part->lastcost / $exchangerate, -2); # lastcost = 5€ = 2$ |
180 | 114 |
$form{exchangerate_1} = _format_number($payment_exchangerate, -2); |
181 | 115 |
|
182 |
($out, $err, @ret) = make_request('ir', \%form, 'post');
|
|
116 |
($out, $err, @ret) = make_request('ir', 'post', %form);
|
|
183 | 117 |
is $ret[0], 1, "posting '$description' does not generate error"; |
184 | 118 |
warn $err if $err; |
185 | 119 |
ok $out =~ /ir\.pl\?action=edit&id=(\d+)/, "posting '$description' returns redirect to id"; |
186 | 120 |
my $id = $1; |
187 | 121 |
|
188 |
($out, $err, @ret) = make_request('ir', { id => $id }, 'edit');
|
|
122 |
($out, $err, @ret) = make_request('ir', 'edit', id => $id );
|
|
189 | 123 |
is $ret[0], 1, "'$description' did not cause an error"; |
190 | 124 |
warn $err if $err; |
191 | 125 |
|
... | ... | |
209 | 143 |
my %form; |
210 | 144 |
|
211 | 145 |
# make new ap transaction |
212 |
my ($out, $err, @ret) = make_request('ap', { }, 'add');
|
|
146 |
my ($out, $err, @ret) = make_request('ap', 'add'); |
|
213 | 147 |
is $ret[0], 1, "new ap transaction"; |
214 | 148 |
%form = form_from_html($out); |
215 | 149 |
|
... | ... | |
220 | 154 |
$form{invnumber} = $description; |
221 | 155 |
|
222 | 156 |
# make new ap transaction |
223 |
($out, $err, @ret) = make_request('ap', \%form, 'update');
|
|
157 |
($out, $err, @ret) = make_request('ap', 'update', %form);
|
|
224 | 158 |
is $ret[0], 1, "update ap transaction with currency"; |
225 | 159 |
%form = form_from_html($out); |
226 | 160 |
|
... | ... | |
229 | 163 |
$form{exchangerate} = _format_number($exchangerate, -2); |
230 | 164 |
$form{exchangerate_1} = _format_number($payment_exchangerate, -2); |
231 | 165 |
|
232 |
($out, $err, @ret) = make_request('ap', \%form, 'post');
|
|
166 |
($out, $err, @ret) = make_request('ap', 'post', %form);
|
|
233 | 167 |
is $ret[0], 1, "posting '$description' did not cause an error"; |
234 | 168 |
|
235 | 169 |
my $invoice = SL::DB::Manager::PurchaseInvoice->find_by(invnumber => $description); |
236 | 170 |
ok $invoice, "posting '$description' can be found in the database"; |
237 | 171 |
|
238 |
($out, $err, @ret) = make_request('ap', { id => $invoice->id }, 'edit');
|
|
172 |
($out, $err, @ret) = make_request('ap', 'edit', id => $invoice->id);
|
|
239 | 173 |
is $ret[0], 1, "loading '$description' did not cause an error"; |
240 | 174 |
warn $err if $err; |
241 | 175 |
|
... | ... | |
331 | 265 |
$invoice = SL::DB::Manager::PurchaseInvoice->find_by(invnumber => $testname); |
332 | 266 |
|
333 | 267 |
# now load with old code |
334 |
my ($out, $err, @ret) = make_request('ap', { id => $invoice->id }, 'edit');
|
|
268 |
my ($out, $err, @ret) = make_request('ap', 'edit', id => $invoice->id);
|
|
335 | 269 |
is $ret[0], 1, "loading '$testname' did not cause an error"; |
336 | 270 |
warn $err if $err; |
337 | 271 |
|
Auch abrufbar als: Unified diff
tests: t/fx/aptransactions.t auf t/Support/Integration.pm umgeschrieben