blib/lib/HTTP/WebTest/Plugin/TagAttTest.pm

filter: Statementstmt 71.0% 54 / 76 Branchbran 36.1% 13 / 36 Conditioncond 13.3% 2 / 15 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 90.0% 9 / 10 Podpod 16.6% 1 / 6 totaltotal 51.9% 79 / 152 CC 26 SCAR 37.7
CC
26
Cov
54%
search_tag
73.4
find_attributes
56.0
check_response
4.0
CRAP 43.5
↑ Summary
1
2 package HTTP::WebTest::Plugin::TagAttTest;
3
4 1 ▶use vars qw($VERSION);
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 $VERSION = '1.00';
6 =head1 NAME
7
8 HTTP::WebTest::Plugin::TagAttTest - Test by tag and attribute existence
9
10 =head1 SYNOPSIS
11
12 Not Applicable
13
14 =head1 DESCRIPTION
15
16 This plugin allows to forbid or require tags and/or attributes in a web page.
17
18 =cut
19
20 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
21 1 ▶use base qw(HTTP::WebTest::Plugin);
Subroutine BEGIN: called CC=1 SCAR=0.0
22
23
24 #use HTTP::Status;
25
26 =head1 TEST PARAMETERS
27
28 =for pod_merge copy params
29
30 =head2 ignore_case
31
32 Determines if case is important.
33
34 =head3 Allowed values
35
36 C<yes>,C<no>
37
38 =head3 Default value
39
40 C<no>
41
42 =head2 tag_require
43
44 A required tag. This is an array of hashs such as C<< require_tag => [{tag=>"script", tag_text=>"spam", attr=>"language",attr_text=>"javascript"}] >>
45
46 See also the L</TAG HASH> for a more detailed explaination.
47
48
49 =head3 Allowed values
50
51 list of hashes
52
53 =head3 Default value
54
55 None (will generate a failed test)
56
57 =head2 forbid_tag
58
59
60 A forbidden tag. This is an array of hashs such as C<< forbid_tag => [{tag=>"script",attr=>"language",attr_text=>"javascript"}] >>
61
62 See also the L</TAG HASH> for a more detailed explaination.
63
64
65 =head3 Allowed values
66
67 list of hashes
68
69 =head3 Default value
70
71 None (will generate a failed test)
72
73 =head1 TAG HASH
74
75
76 =head2 tag
77
78 tag to forbid
79
80 =head2 attr
81
82 attribute to forbid
83
84
85 =head2 attr_text
86
87 regular expression or text. If text, will do a substring search.
88
89
90 =head2 tag_text
91
92 regular expression or text. If text, will do a substring search.
93
94 Note that if an element is missing, it will not be considered. So something like
95 C<require_tag => [{tag=>"title"}]> will assure that a page has a title tag, but the
96 title tag could be blank.
97
98
99 =cut
100
101
102 7 ▶sub param_types {
Subroutine param_types: called CC=1 SCAR=0.0
Pod documented
103 7 return q(use_case yesno
104 tag_forbid list
105 tag_require list);
106 }
107
108 sub get_tag($ $)
109 7 ▶{
Subroutine get_tag: called CC=2 SCAR=6.9
Pod undocumented
110 7 my ($page, $tag_name) = @_;
111 7 ▶ if (!defined($tag_name))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $tag_name) { } 3 4 7
112 {
113 3 return ($page->get_tag) ;
114 }
115 else
116 {
117 4 my $res = $page->get_tag($tag_name);
118 4 return ($res);
119 }
120 }
121 sub find_attributes
122 0 ▶{
Subroutine find_attributes: not called CC=7 SCAR=40.3
Pod undocumented
123
124 0 my( $ok, @attrarr, $attr_search, %attrhash, $attr_text_search, $case_re) = @_;
125 0 for my $attribute (@attrarr)
126 {
127 #this isn't as simple as for tags, because we can't get just certain attributes.
128 #the code can be shortened by combining the two if's, but it was giving me a
129 #headache figuring out all of the possibilities, so I left it for readability.
130 #case 6
131 0 ▶ if (!defined($attr_search))
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $attr_search) 0 0 0
132 {
133 0 my $attr_content = $attrhash{$attribute};
134 0 ▶ $ok = 0 if ($attr_content =~ /$case_re\Q$attr_text_search\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $attr_content =~ /$case_re\Q$attr_text_search\E/ 0 0 0
135 }
136
137 #case 4
138 0 ▶ if (!(defined ($attr_text_search)) and ($attribute eq $attr_search))
Branch: 1 branch
BranchTrueFalseTotal
if (not defined $attr_text_search and $attribute eq $attr_search) { } 0 0 0
Condition0%0/3
not defined $attr_text_search and $attribute eq $attr_search
!ll&&!rl&&r
000
MC/DC0%0/2
not defined $attr_text_search and $attribute eq $attr_search
not defined $attr_text_search $attribute eq $attr_search
Truth table0%0/3
not(A) and B
A= not defined $attr_text_searchB= $attribute eq $attr_search
ABresult
0X0
100
111
139 {
140 0 $ok = 0;
141 }
142 #case 5
143 else
144 {
145 0 my $attr_content = $attrhash{$attribute};
146 0 ▶ $ok = 0 if ($attr_content =~ /$case_re\Q$attr_text_search\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $attr_content =~ /$case_re\Q$attr_text_search\E/ 0 0 0
147 }
148 }
149 }
150
151 sub search_tag
152 7 ▶{
Subroutine search_tag: called CC=15 SCAR=43.0
Pod undocumented
153 7 my $ok = 1;
154 7 my ($page,$case_re, %tag_search_struct) = @_;
155 7 chomp (%tag_search_struct);
156 7 my $tag_search = $tag_search_struct{"tag"};
157 7 ▶ undef $tag_search if ($tag_search eq ''); #an undefined tag causes it to loop through all tags.
Branch: 1 branch
BranchTrueFalseTotal
if $tag_search eq "" 3 4 7
158 7 my $tag_text_search = $tag_search_struct{"tag_text"};
159 7 my $attr_search = $tag_search_struct{"attr"};
160 7 ▶ undef $attr_search if ($attr_search eq '');
Branch: 1 branch
BranchTrueFalseTotal
if $attr_search eq "" 3 4 7
161 7 my $attr_text_search = $tag_search_struct{"attr_text"};
162 7 my @results=();
163
164 7 ▶ return(0, "No values for tag searched") unless (defined ($tag_search) or defined ($attr_search));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tag_search or defined $attr_search 0 7 7
Condition66%2/3
defined $tag_search or defined $attr_search
l!l&&r!l&&!r
430
MC/DC0%0/2
defined $tag_search or defined $attr_search
defined $tag_search defined $attr_search
Truth table66%2/3
A or B
A= defined $tag_searchB= defined $attr_search
ABresult
1X1
011
000
165 #at this point we start looking for tags
166 #there are 6 main cases
167 #1, looking for a tag
168 #2, looking for a specific tag containing some specific text
169 #3, looking for any tag containing some specific text
170 #4, looking for an attribute
171 #5, looking for an attribute containing some specifice text
172 #6, looking for any attribute containing some specific text
173 # these can be combined
174 7 ▶ while ( my $tagstruct = get_tag($page,$tag_search) )
Branch: 1 branch
BranchTrueFalseTotal
if (my $tagstruct = &get_tag($page, $tag_search)) 0 7 7
175 {
176 0 my $tag = $tagstruct->[0];
177 0 ▶ next if ($tag =~ m!/!);
Branch: 1 branch
BranchTrueFalseTotal
if $tag =~ m[/] 0 0 0
178 0 my %attrhash= %{$tagstruct->[1]};
179 0 my @attrarr = @{$tagstruct->[2]};
180 #the tag exists so we want to see if the contents match
181 #case 1
182 0 ▶ $ok = 0 if (defined ($tag_search) and !defined ($tag_text_search)); #if we didn't search a tag, we should continue with the success assumption
Branch: 1 branch
BranchTrueFalseTotal
if defined $tag_search and not defined $tag_text_search 0 0 0
Condition0%0/3
defined $tag_search and not defined $tag_text_search
!ll&&!rl&&r
000
MC/DC0%0/2
defined $tag_search and not defined $tag_text_search
defined $tag_search not defined $tag_text_search
Truth table0%0/3
A and not(B)
A= defined $tag_searchB= not defined $tag_text_search
ABresult
0X0
100
111
183
184 #case 2 or 3
185 0 ▶ if ( defined ($tag_text_search) )
Branch: 1 branch
BranchTrueFalseTotal
if (defined $tag_text_search) 0 0 0
186 {
187 0 my $tag_content = $page->get_text;
188 0 ▶ $ok = 0 if ($tag_content =~ /$case_re\Q$tag_text_search\E/);
Branch: 1 branch
BranchTrueFalseTotal
if $tag_content =~ /$case_re\Q$tag_text_search\E/ 0 0 0
189 }
190
191 #this quits if we hit case 1, 2 or 3 and we aren't looking for cases 4, 5 or 6
192 0 ▶ last if ( (!defined ($attr_search) && !defined ($attr_text_search)) && !($ok));
Branch: 1 branch
BranchTrueFalseTotal
unless defined $attr_search or defined $attr_text_search or $ok 0 0 0
Condition0%0/6
defined $attr_search or defined $attr_text_search or $ok
l!l&&r!l&&!r
000
defined $attr_search or defined $attr_text_search
l!l&&r!l&&!r
000
MC/DC0%0/3
defined $attr_search or defined $attr_text_search or $ok
defined $attr_search defined $attr_text_search $ok
Truth table0%0/4
A or B or C
A= defined $attr_searchB= defined $attr_text_searchC= $ok
ABCresult
1XX1
01X1
0011
0000
193
194 #look for cases 4, 5, 6
195 0 $ok = find_attributes( $ok, @attrarr, $attr_search, %attrhash, $attr_text_search, $case_re);
196
197 #if $ok is 0, one of cases 4,5 or 6 must have failed.
198 0 ▶ last if ($ok == 0);
Branch: 1 branch
BranchTrueFalseTotal
if $ok == 0 0 0 0
199 }
200 7 return ($ok, "tag: " . $tag_search . ", tag text: " . $tag_text_search . ", attribute: " . $attr_search . ", attribute text: " . $attr_text_search);
201 }
202
203 sub test_tags
204 14 ▶{
Subroutine test_tags: called CC=2 SCAR=6.9
Pod undocumented
205 14 my ($self, $tag_type, $content, $case_re) = @_;
206 1 ▶ use HTML::TokeParser;
Subroutine BEGIN: called CC=1 SCAR=0.0
207 14 my $page = HTML::TokeParser->new(\$content);
208
209 14 my @results;
210 14 for my $tag_struct (@{$self->test_param( $tag_type, [] )})
211 {
212 7 my ($ok, $result) = search_tag( $page, $case_re, %{ $tag_struct });
213 7 push @results, $self->test_result($ok, $result);
214 }
215
216 14 return @results;
217 }
218
219
220 7 ▶sub check_response {
Subroutine check_response: called CC=4 SCAR=13.9
Pod undocumented
221 7 my $self = shift;
222
223 # response content
224 7 my $content = $self->webtest->current_response->content;
225 7 $self->validate_params(qw(ignore_case
226 tag_forbid tag_require));
227
228 # ignore case or not?
229 7 my $ignore_case = $self->yesno_test_param('ignore_case');
230 7 ▶ my $case_re = $ignore_case ? '(?i)' : '';
Branch: 1 branch
BranchTrueFalseTotal
$ignore_case ? : 0 7 7
231
232 # clean test results
233 7 my @results = ();
234 7 my @ret = ();
235
236 # check for forbidden tag and attribute
237
238 7 my @forbid = test_tags($self, 'tag_forbid', $content, $case_re );
239 7 ▶ push @ret, ['Forbidden tag and attribute', @results] if @forbid;
Branch: 1 branch
BranchTrueFalseTotal
if @forbid 4 3 7
240
241 7 my @require = test_tags( $self,'tag_require', $content, $case_re );
242 7 ▶ push @ret, ['Required tag and attribute', @results] if @require;
Branch: 1 branch
BranchTrueFalseTotal
if @require 3 4 7
243 7 return @ret;
244 }
245
246
247
248
249
250 =head1 COPYRIGHT
251
252 Copyright (c) 2003-2004 Edward Fancher. All rights reserved.
253
254 This program is free software; you can redistribute it and/or modify
255 it under the same terms as Perl itself.
256
257 =head1 SEE ALSO
258
259 L<HTTP::WebTest|HTTP::WebTest>
260
261 L<HTTP::WebTest::API|HTTP::WebTest::API>
262
263 L<HTTP::WebTest::Plugin|HTTP::WebTest::Plugin>
264
265 L<HTTP::WebTest::Plugins|HTTP::WebTest::Plugins>
266
267
268 L<HTTP::WebTest::Plugins|HTTP::WebTest::Plugins::TextMatchTest>
269
270 =cut
271
272 1 1;
↑ Summary