line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Test::WWW::Mechanize::MultiMech; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
297384
|
use 5.006; |
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
52
|
|
4
|
1
|
|
|
1
|
|
7
|
use strict; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
45
|
|
5
|
1
|
|
|
1
|
|
7
|
use warnings FATAL => 'all'; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
80
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
our $VERSION = '1.006001'; # VERSION |
8
|
|
|
|
|
|
|
|
9
|
1
|
|
|
1
|
|
6
|
use Test::WWW::Mechanize; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
42
|
|
10
|
1
|
|
|
1
|
|
6
|
use Test::Builder qw//; |
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
23
|
|
11
|
1
|
|
|
1
|
|
6
|
use Carp qw/croak/; |
|
1
|
|
|
|
|
3
|
|
|
1
|
|
|
|
|
1650
|
|
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
sub _diag { |
14
|
0
|
|
|
0
|
|
|
Test::Builder->new->diag(@_); |
15
|
|
|
|
|
|
|
} |
16
|
|
|
|
|
|
|
|
17
|
|
|
|
|
|
|
sub new { |
18
|
0
|
|
|
0
|
1
|
|
my ( $class, %args ) = @_; |
19
|
|
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
|
ref $args{users} eq 'ARRAY' |
21
|
|
|
|
|
|
|
or croak 'You must give ``users\'\' to new->new(); ' |
22
|
|
|
|
|
|
|
. 'and it needs to be an arrayref'; |
23
|
|
|
|
|
|
|
|
24
|
0
|
|
|
|
|
|
my @args_users = @{ delete $args{users} }; |
|
0
|
|
|
|
|
|
|
25
|
0
|
|
|
|
|
|
my ( %users, @users_order ); |
26
|
0
|
|
|
|
|
|
for ( grep !($_%2), 0 .. $#args_users ) { |
27
|
0
|
|
|
|
|
|
my $user_args = $args_users[ $_+1 ]; |
28
|
|
|
|
|
|
|
|
29
|
0
|
|
|
|
|
|
push @users_order, $args_users[ $_ ]; |
30
|
|
|
|
|
|
|
|
31
|
0
|
|
|
|
|
|
my $mech = Test::WWW::Mechanize->new( %args ); |
32
|
0
|
0
|
|
|
|
|
$users{ $args_users[$_] } = { |
33
|
|
|
|
|
|
|
login => ( |
34
|
|
|
|
|
|
|
defined $user_args->{login} |
35
|
|
|
|
|
|
|
? $user_args->{login} : $args_users[ $_ ] |
36
|
|
|
|
|
|
|
), |
37
|
|
|
|
|
|
|
pass => $user_args->{pass}, |
38
|
|
|
|
|
|
|
mech => $mech, |
39
|
|
|
|
|
|
|
}; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
0
|
|
|
|
|
|
my $self = bless {}, $class; |
43
|
0
|
|
|
|
|
|
$self->{USERS} = \%users; |
44
|
0
|
|
|
|
|
|
$self->{USERS_ORDER} = \@users_order; |
45
|
0
|
|
|
|
|
|
$self->{MECH_ARGS} = \%args; |
46
|
0
|
|
|
|
|
|
return $self; |
47
|
|
|
|
|
|
|
} |
48
|
|
|
|
|
|
|
|
49
|
|
|
|
|
|
|
sub _mech { |
50
|
0
|
|
|
0
|
|
|
my $self = shift; |
51
|
0
|
|
|
|
|
|
my ( $any_user ) = grep !$self->{IGNORED_USERS}{$_}, |
52
|
0
|
|
|
|
|
|
@{$self->{USERS_ORDER}}; |
53
|
|
|
|
|
|
|
|
54
|
0
|
0
|
|
|
|
|
$any_user |
55
|
|
|
|
|
|
|
or croak q{Didn't find any available users when getting any} |
56
|
|
|
|
|
|
|
. q{ user's mech object.}; |
57
|
|
|
|
|
|
|
|
58
|
0
|
|
|
|
|
|
return $self->{USERS}{ $any_user }{mech}; |
59
|
|
|
|
|
|
|
} |
60
|
|
|
|
|
|
|
|
61
|
|
|
|
|
|
|
sub login { |
62
|
0
|
|
|
0
|
1
|
|
my ( $self, %args ) = @_; |
63
|
|
|
|
|
|
|
|
64
|
0
|
|
|
|
|
|
my $page = delete $args{login_page}; |
65
|
0
|
|
|
|
|
|
eval { |
66
|
0
|
0
|
|
|
|
|
$page = $self->_mech->uri |
67
|
|
|
|
|
|
|
unless defined $page; |
68
|
|
|
|
|
|
|
}; |
69
|
0
|
0
|
|
|
|
|
if ( $@ ) { |
70
|
0
|
|
|
|
|
|
croak 'You did not give ->login() a page and mech did not yet' |
71
|
|
|
|
|
|
|
. ' access any pages. Cannot proceed further'; |
72
|
|
|
|
|
|
|
} |
73
|
|
|
|
|
|
|
|
74
|
0
|
|
|
|
|
|
my $users = $self->{USERS}; |
75
|
0
|
|
|
|
|
|
for my $alias ( |
|
0
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
grep !$self->{IGNORED_USERS}{$_}, @{$self->{USERS_ORDER}} |
77
|
|
|
|
|
|
|
) { |
78
|
0
|
|
|
|
|
|
my $mech = $users->{ $alias }{mech}; |
79
|
|
|
|
|
|
|
|
80
|
0
|
|
|
|
|
|
$mech->get_ok( |
81
|
|
|
|
|
|
|
$page, |
82
|
|
|
|
|
|
|
"[$alias] get_ok($page)", |
83
|
|
|
|
|
|
|
); |
84
|
|
|
|
|
|
|
|
85
|
0
|
|
|
|
|
|
my $user_args = { %args }; |
86
|
0
|
0
|
|
|
|
|
if ( $user_args->{fields} ) { |
87
|
0
|
|
|
|
|
|
$user_args->{fields} = {%{ $user_args->{fields} }}; |
|
0
|
|
|
|
|
|
|
88
|
|
|
|
|
|
|
} |
89
|
|
|
|
|
|
|
|
90
|
0
|
0
|
|
|
|
|
for ( values %{ $user_args->{fields} || {} } ) { |
|
0
|
|
|
|
|
|
|
91
|
0
|
0
|
|
|
|
|
next unless ref eq 'SCALAR'; |
92
|
0
|
0
|
|
|
|
|
if ( $$_ eq 'LOGIN' ) { $_ = $users->{ $alias }{login}; } |
|
0
|
0
|
|
|
|
|
|
93
|
0
|
|
|
|
|
|
elsif ( $$_ eq 'PASS' ) { $_ = $users->{ $alias }{pass}; } |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
|
96
|
|
|
|
|
|
|
$mech->submit_form_ok( |
97
|
0
|
|
|
|
|
|
$user_args, |
98
|
|
|
|
|
|
|
"[$alias] Submitting login form", |
99
|
|
|
|
|
|
|
); |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub AUTOLOAD { |
104
|
0
|
|
|
0
|
|
|
my ( $self, @args ) = @_; |
105
|
|
|
|
|
|
|
|
106
|
0
|
|
|
|
|
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
107
|
|
|
|
|
|
|
|
108
|
0
|
|
|
|
|
|
our $AUTOLOAD; |
109
|
0
|
|
|
|
|
|
my $method = (split /::/, $AUTOLOAD)[-1]; |
110
|
0
|
0
|
|
|
|
|
return if $method eq 'DESTROY'; |
111
|
|
|
|
|
|
|
|
112
|
0
|
0
|
|
|
|
|
if ( $self->_mech->can($method) ) { |
|
0
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
113
|
0
|
|
|
|
|
|
return $self->_call_mech_method_on_each_user( $method, \@args ); |
114
|
|
|
|
|
|
|
} |
115
|
|
|
|
|
|
|
elsif ( grep $_ eq $method, @{ $self->{USERS_ORDER} } ) { |
116
|
0
|
|
|
|
|
|
my $alias = $method; |
117
|
0
|
|
|
|
|
|
_diag "[$alias]-only call"; |
118
|
0
|
|
|
|
|
|
return $self->{USERS}{ $alias }{mech}; |
119
|
|
|
|
|
|
|
} |
120
|
|
|
|
|
|
|
elsif ( $method eq 'any' ) { |
121
|
0
|
|
|
|
|
|
_diag "[any] call"; |
122
|
0
|
|
|
|
|
|
return $self->_mech; |
123
|
|
|
|
|
|
|
} |
124
|
|
|
|
|
|
|
|
125
|
0
|
|
|
|
|
|
croak qq|Can't locate object method "$method" via package | |
126
|
|
|
|
|
|
|
. __PACKAGE__; |
127
|
|
|
|
|
|
|
} |
128
|
|
|
|
|
|
|
|
129
|
|
|
|
|
|
|
sub _call_mech_method_on_each_user { |
130
|
0
|
|
|
0
|
|
|
my ( $self, $method, $args ) = @_; |
131
|
|
|
|
|
|
|
|
132
|
0
|
|
|
|
|
|
local $Test::Builder::Level = $Test::Builder::Level + 1; |
133
|
|
|
|
|
|
|
|
134
|
0
|
|
|
|
|
|
my %returns; |
135
|
0
|
|
|
|
|
|
for my $alias ( |
|
0
|
|
|
|
|
|
|
136
|
|
|
|
|
|
|
grep !$self->{IGNORED_USERS}{$_}, @{$self->{USERS_ORDER}} |
137
|
|
|
|
|
|
|
) { |
138
|
0
|
|
|
|
|
|
_diag("\n[$alias] Calling ->$method()\n"); |
139
|
0
|
|
|
|
|
|
$returns{ $alias } |
140
|
|
|
|
|
|
|
= $self->{USERS}{ $alias }{mech}->$method( @$args ); |
141
|
|
|
|
|
|
|
} |
142
|
|
|
|
|
|
|
|
143
|
0
|
|
|
|
|
|
$returns{any} = (values %returns)[0]; |
144
|
0
|
|
|
|
|
|
return \%returns; |
145
|
|
|
|
|
|
|
} |
146
|
|
|
|
|
|
|
|
147
|
|
|
|
|
|
|
sub remove_user { |
148
|
0
|
|
|
0
|
1
|
|
my ( $self, $alias ) = @_; |
149
|
|
|
|
|
|
|
|
150
|
0
|
0
|
|
|
|
|
return unless exists $self->{USERS}{ $alias }; |
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
@{ $self->{USERS_ORDER} } |
|
0
|
|
|
|
|
|
|
153
|
0
|
|
|
|
|
|
= grep $_ ne $alias, @{ $self->{USERS_ORDER} }; |
154
|
|
|
|
|
|
|
|
155
|
0
|
|
|
|
|
|
my $args = delete $self->{USERS}{ $alias }; |
156
|
|
|
|
|
|
|
|
157
|
0
|
|
|
|
|
|
croak 'You must have at least one user and you ' |
158
|
|
|
|
|
|
|
. 'just removed the last one' |
159
|
0
|
0
|
|
|
|
|
unless @{ $self->{USERS_ORDER} }; |
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
return ( $alias, $args ); |
162
|
|
|
|
|
|
|
} |
163
|
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
sub add_user { |
165
|
0
|
|
|
0
|
1
|
|
my ( $self, $alias, $args ) = @_; |
166
|
|
|
|
|
|
|
|
167
|
0
|
|
|
|
|
|
my $mech = Test::WWW::Mechanize->new( %{ $self->{MECH_ARGS} } ); |
|
0
|
|
|
|
|
|
|
168
|
|
|
|
|
|
|
|
169
|
0
|
0
|
|
|
|
|
$self->{USERS}{ $alias } = { |
170
|
0
|
|
|
|
|
|
%{ $args || {} }, |
171
|
|
|
|
|
|
|
mech => $mech, |
172
|
|
|
|
|
|
|
}; |
173
|
|
|
|
|
|
|
|
174
|
0
|
|
|
|
|
|
@{ $self->{USERS_ORDER} } = ( |
|
0
|
|
|
|
|
|
|
175
|
0
|
|
|
|
|
|
( grep $_ ne $alias, @{ $self->{USERS_ORDER} } ), |
176
|
|
|
|
|
|
|
$alias, |
177
|
|
|
|
|
|
|
); |
178
|
|
|
|
|
|
|
|
179
|
0
|
|
|
|
|
|
return; |
180
|
|
|
|
|
|
|
} |
181
|
|
|
|
|
|
|
|
182
|
|
|
|
|
|
|
sub all_users { |
183
|
0
|
|
|
0
|
1
|
|
my $self = shift; |
184
|
0
|
|
|
|
|
|
my $is_include_ignored = shift; |
185
|
0
|
|
|
|
|
|
return $is_include_ignored |
186
|
0
|
|
|
|
|
|
? @{ $self->{USERS_ORDER} } |
187
|
0
|
0
|
|
|
|
|
: grep !$self->{IGNORED_USERS}{ $_ }, @{ $self->{USERS_ORDER} }; |
188
|
|
|
|
|
|
|
} |
189
|
|
|
|
|
|
|
|
190
|
|
|
|
|
|
|
sub ignore_user { |
191
|
0
|
|
|
0
|
1
|
|
my ( $self, $alias ) = @_; |
192
|
|
|
|
|
|
|
|
193
|
0
|
0
|
|
|
|
|
return unless exists $self->{USERS}{ $alias }; |
194
|
|
|
|
|
|
|
|
195
|
0
|
|
|
|
|
|
$self->{IGNORED_USERS}{ $alias } = 1; |
196
|
0
|
0
|
|
|
|
|
if ( keys %{$self->{IGNORED_USERS}} eq @{ $self->{USERS_ORDER} } ){ |
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
197
|
0
|
|
|
|
|
|
croak q{You ignored all your users. Can't function without at least |
198
|
|
|
|
|
|
|
one active user}; |
199
|
|
|
|
|
|
|
} |
200
|
|
|
|
|
|
|
} |
201
|
|
|
|
|
|
|
|
202
|
|
|
|
|
|
|
sub unignore_user { |
203
|
0
|
|
|
0
|
1
|
|
my ( $self, $alias ) = @_; |
204
|
|
|
|
|
|
|
|
205
|
0
|
|
|
|
|
|
delete $self->{IGNORED_USERS}{ $alias }; |
206
|
|
|
|
|
|
|
} |
207
|
|
|
|
|
|
|
|
208
|
|
|
|
|
|
|
q| |
209
|
|
|
|
|
|
|
Why programmers like UNIX: unzip, strip, touch, finger, grep, mount, fsck, |
210
|
|
|
|
|
|
|
more, yes, fsck, fsck, fsck, umount, sleep |
211
|
|
|
|
|
|
|
|; |
212
|
|
|
|
|
|
|
__END__ |