Author: kake Date: 2012-05-04 13:21:17 +0100 (Fri, 04 May 2012) New Revision: 1379
Modified: trunk/lib/OpenGuides.pm trunk/t/47_navbar_placement.t trunk/t/49_custom_header.t trunk/t/53_show_index.t trunk/t/55_node_history.t trunk/t/82_stylesheet.t trunk/t/83_show_delete_to_admins_only.t trunk/t/84_navbar_admin.t trunk/t/90_css_category_locale_classes.t trunk/t/95_multiple_index.t Log: Omit HTTP headers in various tests in order to avoid HTML parser warnings from Test::HTML::Content (fixes #284).
Modified: trunk/lib/OpenGuides.pm =================================================================== --- trunk/lib/OpenGuides.pm 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/lib/OpenGuides.pm 2012-05-04 12:21:17 UTC (rev 1379) @@ -156,6 +156,13 @@ return_output => 1, );
+ # Return output as a string with HTTP headers omitted (for tests). + $guide->display_node( + id => "Calthorpe Arms", + return_output => 1, + noheaders => 1, + ); + # Or return the hash of variables that will be passed to the template # (not including those set additionally by OpenGuides::Template). $guide->display_node( @@ -176,6 +183,9 @@ (At the moment, C<return_tt_vars> acts as if the C<intercept_redirect> parameter was passed.)
+The C<noheaders> parameter only takes effect if C<return_output> is true +and C<intercept_redirect> is false or omitted. + If you have specified the C<host_checker_module> option in your C<wiki.conf>, this method will attempt to call the <blacklisted_host> method of that module to determine whether the host requesting the node @@ -191,6 +201,8 @@ my ($self, %args) = @_; my $return_output = $args{return_output} || 0; my $intercept_redirect = $args{intercept_redirect}; + my $noheaders = ( $return_output && !$intercept_redirect + && $args{noheaders} ); my $version = $args{version}; my $id = $args{id} || $self->config->home_name; my $wiki = $self->wiki; @@ -212,12 +224,13 @@
if ( $is_blacklisted ) { my $output = OpenGuides::Template->output( - wiki => $self->wiki, - config => $config, - template => "blacklisted_host.tt", - vars => { - not_editable => 1, - }, + wiki => $self->wiki, + config => $config, + template => "blacklisted_host.tt", + vars => { + not_editable => 1, + }, + noheaders => $noheaders, ); return $output if $return_output; print $output; @@ -262,7 +275,7 @@ latitude => $metadata{latitude}[0], config => $config); if ($args{format} && $args{format} eq 'raw') { - print "Content-Type: text/plain\n\n"; + print "Content-Type: text/plain\n\n" unless $noheaders; print $node_data{content}; return 0; } @@ -364,20 +377,22 @@ } return %tt_vars if $args{return_tt_vars}; my $output = $self->process_template( - id => $id, - template => "home_node.tt", - tt_vars => %tt_vars, - http_status => $http_status + id => $id, + template => "home_node.tt", + tt_vars => %tt_vars, + http_status => $http_status, + noheaders => $noheaders, ); return $output if $return_output; print $output; } else { return %tt_vars if $args{return_tt_vars}; my $output = $self->process_template( - id => $id, - template => "node.tt", - tt_vars => %tt_vars, - http_status => $http_status + id => $id, + template => "node.tt", + tt_vars => %tt_vars, + http_status => $http_status, + noheaders => $noheaders, ); return $output if $return_output; print $output; @@ -644,6 +659,8 @@ Displays a form that lets the user view and set their preferences. The C<return_output> and C<return_tt_vars> parameters can be used to return the output or template variables, instead of printing the output to STDOUT. +The C<noheaders> parameter can also be used in conjunction with +C<return_output>, if you wish to omit all HTTP headers.
=cut
@@ -931,6 +948,13 @@ return_output => 1, );
+ # Return output as a string with HTTP headers omitted (for tests). + $guide->show_index( + cat => "pubs", + return_output => 1, + noheaders => 1, + ); + # Or return the template variables (again, useful for writing tests). $guide->show_index( cat => "pubs", @@ -948,10 +972,14 @@ redirect is required, this method will fake the redirect and return the output/variables that will actually end up being viewed by the user. If instead you want to see the HTTP headers that will be printed in order to -perform the redirect, pass the C<intercept_redirect> parameter as well. The -C<intercept_redirect> parameter has no effect if no redirect is required, or -if the C<return_output>/C<return_tt_vars> parameter is omitted. +perform the redirect, pass the C<intercept_redirect> parameter as well.
+The C<intercept_redirect> parameter has no effect if no redirect is required, +or if the C<return_output>/C<return_tt_vars> parameter is omitted. + +The C<noheaders> parameter only takes effect if C<return_output> is true +and C<intercept_redirect> is false or omitted. + =cut
sub show_index { @@ -1181,6 +1209,10 @@ tt_vars => %tt_vars, );
+ if ( $args{return_output} && !$args{intercept_redirect} ) { + $conf{noheaders} = $args{noheaders}; + } + my $output = $self->process_template( %conf ); return $output if $args{return_output}; print $output; @@ -2464,7 +2496,8 @@ template => $args{template}, vars => $args{tt_vars}, cookies => $args{cookies}, - http_status => $args{http_status} + http_status => $args{http_status}, + noheaders => $args{noheaders}, ); if ( $args{content_type} ) { $output_conf{content_type} = $args{content_type};
Modified: trunk/t/47_navbar_placement.t =================================================================== --- trunk/t/47_navbar_placement.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/47_navbar_placement.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -24,7 +24,7 @@ my ( $config, $guide, $wiki, $cookie, $output );
# Clear out the database from any previous runs. - OpenGuides::Test::refresh_db(); +OpenGuides::Test::refresh_db();
# Make a guide. $config = OpenGuides::Test->make_basic_config; @@ -37,29 +37,17 @@ );
# Make sure navbar shows up on node display. -$output = $guide->display_node( - id => "Red Lion", - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( "Red Lion" ); Test::HTML::Content::tag_ok( $output, "div", { id => "navbar" }, "navbar included on node display" );
$config->content_above_navbar_in_html( 0 ); -$output = $guide->display_node( - id => "Red Lion", - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( "Red Lion" ); Test::HTML::Content::tag_ok( $output, "div", { id => "navbar" }, "...ditto if content_above_navbar_in_html set to 0" );
$config->content_above_navbar_in_html( 1 ); -$output = $guide->display_node( - id => "Red Lion", - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( "Red Lion" ); Test::HTML::Content::tag_ok( $output, "div", { id => "navbar" }, "...ditto if content_above_navbar_in_html set to 1" );
@@ -67,29 +55,17 @@ $config = OpenGuides::Test->make_basic_config; # get a fresh config $guide = OpenGuides->new( config => $config ); # make sure the guide sees it $config->navbar_on_home_page( 1 ); -$output = $guide->display_node( - id => $config->home_name, - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( $config->home_name ); Test::HTML::Content::tag_ok( $output, "div", { id => "navbar" }, "navbar included on home node when navbar_on_home_page switched on" );
$config->content_above_navbar_in_html( 0 ); -$output = $guide->display_node( - id => $config->home_name, - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( $config->home_name ); Test::HTML::Content::tag_ok( $output, "div", { id => "navbar" }, "...ditto if content_above_navbar_in_html set to 0" );
$config->content_above_navbar_in_html( 1 ); -$output = $guide->display_node( - id => $config->home_name, - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( $config->home_name ); Test::HTML::Content::tag_ok( $output, "div", { id => "navbar" }, "...ditto if content_above_navbar_in_html set to 1" );
@@ -97,29 +73,17 @@ $config = OpenGuides::Test->make_basic_config; # get a fresh config $guide = OpenGuides->new( config => $config ); # make sure the guide sees it $config->navbar_on_home_page( 0 ); -$output = $guide->display_node( - id => $config->home_name, - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( $config->home_name ); Test::HTML::Content::no_tag( $output, "div", { id => "navbar" }, "navbar excluded from home node when navbar_on_home_page switched off" );
$config->content_above_navbar_in_html( 0 ); -$output = $guide->display_node( - id => $config->home_name, - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( $config->home_name ); Test::HTML::Content::no_tag( $output, "div", { id => "navbar" }, "...ditto if content_above_navbar_in_html set to 0" );
$config->content_above_navbar_in_html( 1 ); -$output = $guide->display_node( - id => $config->home_name, - return_output => 1, - ); -$output =~ s/^Content-Type.*[\r\n]+//m; +$output = display_node( $config->home_name ); Test::HTML::Content::no_tag( $output, "div", { id => "navbar" }, "...ditto if content_above_navbar_in_html set to 1" );
@@ -149,3 +113,9 @@ Test::HTML::Content::tag_ok( $output, "div", { id => "navbar" }, "...ditto if content_above_navbar_in_html set to 1" );
+sub display_node { + return $guide->display_node( id => shift, + return_output => 1, + noheaders => 1, + ); +}
Modified: trunk/t/49_custom_header.t =================================================================== --- trunk/t/49_custom_header.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/49_custom_header.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -52,6 +52,7 @@ my $output = $guide->display_node( id => $config->home_name, return_output => 1, + noheaders => 1, ); $output =~ s/^Content-Type.*[\r\n]+//m; Test::HTML::Content::tag_ok( $output, "meta", { name => "foo" },
Modified: trunk/t/53_show_index.t =================================================================== --- trunk/t/53_show_index.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/53_show_index.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -59,6 +59,7 @@ $guide->show_index( cat => "Alpha", return_output => 1, + noheaders => 1, ); }; is( $@, "", "->show_index doesn't die" ); @@ -205,6 +206,13 @@
SKIP: { skip "Test::HTML::Content not available", 1 unless $thc; + # Do this again to get a version without headers, so T::H::C doesn't whine. + $output = $guide->show_index( + return_output => 1, + loc => "assam", + format => "map", + noheaders => 1, + ); Test::HTML::Content::link_ok( $output, "http://example.com/wiki.cgi?action=index;loc=assam", "We have a link to the non-map version" );
Modified: trunk/t/55_node_history.t =================================================================== --- trunk/t/55_node_history.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/55_node_history.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -31,6 +31,7 @@ id => "South Croydon Station", version => 1, return_output => 1, + noheaders => 1, ); like( $output, qr'South_Croydon_Station', "node param escaped properly in links in historic view" );
Modified: trunk/t/82_stylesheet.t =================================================================== --- trunk/t/82_stylesheet.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/82_stylesheet.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -31,7 +31,8 @@
# Display it, and make sure we get the openguides-base.css in there. $config->static_url( "http://example.org/static/" ); -my $output = $guide->display_node( id => "Red Lion", return_output => 1 ); +my $output = $guide->display_node( id => "Red Lion", return_output => 1, + noheaders => 1 ); Test::HTML::Content::tag_ok( $output, "link", { rel => "stylesheet", href => "http://example.org/static/openguides-base.css" }, @@ -39,7 +40,8 @@
# Make sure the guide's own stylesheet overrides this though. $config->stylesheet_url( "http://example.com/styles.css" ); -$output = $guide->display_node( id => "Red Lion", return_output => 1 ); +$output = $guide->display_node( id => "Red Lion", return_output => 1, + noheaders => 1 ); Test::HTML::Content::no_tag( $output, "link", { rel => "stylesheet", href => "openguides-base.css" }, "...but not when one is provided" );
Modified: trunk/t/83_show_delete_to_admins_only.t =================================================================== --- trunk/t/83_show_delete_to_admins_only.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/83_show_delete_to_admins_only.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -6,7 +6,6 @@ use OpenGuides::Config; use OpenGuides::CGI;
- eval { require DBD::SQLite; }; if ( $@ ) { plan skip_all => "DBD::SQLite not installed - no database to test with"; @@ -43,20 +42,19 @@ ); }
- plan tests => 4;
my ( $config, $guide, $wiki, $output );
# Clear out the database from any previous runs. - OpenGuides::Test::refresh_db(); +OpenGuides::Test::refresh_db();
+# Make a guide +$config = OpenGuides::Test->make_basic_config; +$config->enable_page_deletion( 1 ); +$guide = OpenGuides->new( config => $config );
- # Make a guide - $config = OpenGuides::Test->make_basic_config; -$config->enable_page_deletion( 1 ); - $guide = OpenGuides->new( config => $config ); - # set is_admin to 1 +# set is_admin to 1 my $cookie = OpenGuides::CGI->make_prefs_cookie( config => $config, username => "bob", @@ -75,23 +73,25 @@ node => "Test Page", );
- $output = $guide->display_node( - return_output => 1, - id => "Test Page", - ); +$output = $guide->display_node( + return_output => 1, + noheaders => 1, + id => "Test Page", + ); + # check delete link is shown in footer - Test::HTML::Content::tag_ok( $output, "div", { id => "footer_delete_link" }, - "delete link in footer for admin" ); +Test::HTML::Content::tag_ok( $output, "div", { id => "footer_delete_link" }, + "delete link in footer for admin" );
- $output = $guide->list_all_versions( - return_output => 1, - id => "Test Page", - ); - like( $output, qr/version=1;action=delete/, - "delete links on history page"); +$output = $guide->list_all_versions( + return_output => 1, + id => "Test Page", + ); +like( $output, qr/version=1;action=delete/, + "delete links on history page" );
# set is_admin to 0 - $cookie = OpenGuides::CGI->make_prefs_cookie( +$cookie = OpenGuides::CGI->make_prefs_cookie( config => $config, username => "bob", include_geocache_link => 1, @@ -105,16 +105,18 @@ ); $ENV{HTTP_COOKIE} = $cookie;
- $output = $guide->display_node( - return_output => 1, - id => "Test Page", - ); +$output = $guide->display_node( + return_output => 1, + noheaders => 1, + id => "Test Page", + ); + # check that the delete link in footer isnt shown - Test::HTML::Content::no_tag( $output, "div", { id => "footer_delete_link" }, - "delete link in footer not shown"); - $output = $guide->list_all_versions( - return_output => 1, - id => "Test Page", - ); - unlike( $output, qr/version=1;action=delete/, - "no delete links on history page"); +Test::HTML::Content::no_tag( $output, "div", { id => "footer_delete_link" }, + "delete link in footer not shown"); +$output = $guide->list_all_versions( + return_output => 1, + id => "Test Page", + ); +unlike( $output, qr/version=1;action=delete/, + "no delete links on history page");
Modified: trunk/t/84_navbar_admin.t =================================================================== --- trunk/t/84_navbar_admin.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/84_navbar_admin.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -6,7 +6,6 @@ use OpenGuides::Config; use OpenGuides::CGI;
- eval { require DBD::SQLite; }; if ( $@ ) { plan skip_all => "DBD::SQLite not installed - no database to test with"; @@ -43,19 +42,18 @@ ); }
- plan tests => 2;
my ( $config, $guide, $wiki, $output );
# Clear out the database from any previous runs. - OpenGuides::Test::refresh_db(); +OpenGuides::Test::refresh_db();
+# Make a guide +$config = OpenGuides::Test->make_basic_config; +$guide = OpenGuides->new( config => $config );
- # Make a guide - $config = OpenGuides::Test->make_basic_config; - $guide = OpenGuides->new( config => $config ); - # set is_admin to 1 +# set is_admin to 1 my $cookie = OpenGuides::CGI->make_prefs_cookie( config => $config, username => "bob", @@ -74,15 +72,18 @@ node => "Test Page", );
- $output = $guide->display_node( - return_output => 1, - id => "Test Page", - ); +$output = $guide->display_node( + return_output => 1, + noheaders => 1, + id => "Test Page", + ); + # check navbar_admin div is shown. - Test::HTML::Content::tag_ok( $output, "div", { id => "navbar_admin" }, - "admin section displayed in navbar" ); +Test::HTML::Content::tag_ok( $output, "div", { id => "navbar_admin" }, + "admin section displayed in navbar" ); + # set is_admin to 0 - $cookie = OpenGuides::CGI->make_prefs_cookie( +$cookie = OpenGuides::CGI->make_prefs_cookie( config => $config, username => "bob", include_geocache_link => 1, @@ -96,10 +97,12 @@ ); $ENV{HTTP_COOKIE} = $cookie;
- $output = $guide->display_node( - return_output => 1, - id => "Test Page", - ); +$output = $guide->display_node( + return_output => 1, + noheaders => 1, + id => "Test Page", + ); + # check that the navbar_admin div isnt shown - Test::HTML::Content::no_tag( $output, "div", { id => "navbar_admin" }, - "navbar not shown"); +Test::HTML::Content::no_tag( $output, "div", { id => "navbar_admin" }, + "navbar not shown" );
Modified: trunk/t/90_css_category_locale_classes.t =================================================================== --- trunk/t/90_css_category_locale_classes.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/90_css_category_locale_classes.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -34,7 +34,8 @@ return_output => 1, );
-my $output = $guide->display_node( id => "Crown", return_output => 1 ); +my $output = $guide->display_node( id => "Crown", return_output => 1, + noheaders => 1 ); Test::HTML::Content::tag_ok( $output, "div", { id => "content", class => "cat_pubs loc_cornmarket" }, "Node in one locale and one category has CSS classes for both." ); @@ -47,7 +48,8 @@ locales => "Magdalen Street", return_output => 1, ); -$output = $guide->display_node( id => "Debenhams", return_output => 1 ); +$output = $guide->display_node( id => "Debenhams", return_output => 1, + noheaders => 1 ); Test::HTML::Content::tag_ok( $output, "div", { id => "content", class => "cat_baby_changing loc_magdalen_street" }, "...and spaces in locale/category names are replaced by underscores." );
Modified: trunk/t/95_multiple_index.t =================================================================== --- trunk/t/95_multiple_index.t 2012-05-04 10:31:33 UTC (rev 1378) +++ trunk/t/95_multiple_index.t 2012-05-04 12:21:17 UTC (rev 1379) @@ -54,7 +54,8 @@ my %tt_vars = $guide->show_index( cat => "pubs", return_tt_vars => 1 ); is( scalar @{$tt_vars{nodes}}, 2, "Right number of nodes returned in pure category search" ); -my $output = $guide->show_index( cat => "pubs", return_output => 1 ); +my $output = $guide->show_index( cat => "pubs", return_output => 1, + noheaders => 1 ); Test::HTML::Content::title_ok( $output, "Index of Category Pubs - Test", "...and page title is correct" ); Test::HTML::Content::link_ok( $output, $config->script_name . "?Category_Pubs", @@ -63,7 +64,8 @@ %tt_vars = $guide->show_index( loc => "waddon", return_tt_vars => 1 ); is( scalar @{$tt_vars{nodes}}, 2, "Right number of nodes returned in pure locale search" ); -$output = $guide->show_index( loc => "waddon", return_output => 1 ); +$output = $guide->show_index( loc => "waddon", return_output => 1, + noheaders => 1 ); Test::HTML::Content::title_ok( $output, "Index of Locale Waddon - Test", "...and page title is correct" ); Test::HTML::Content::link_ok( $output, $config->script_name . "?Locale_Waddon", @@ -74,7 +76,7 @@ is( scalar @{$tt_vars{nodes}}, 1, "Right number of nodes returned in category+locale search" ); $output = $guide->show_index( cat => "pubs", loc => "waddon", - return_output => 1 ); + return_output => 1, noheaders => 1 ); Test::HTML::Content::title_ok( $output, "Index of Category Pubs and Locale Waddon - Test", "...and page title is correct" ); @@ -90,7 +92,7 @@ is( scalar @{$tt_vars{nodes}}, 1, "Right number of nodes returned in category+locale search with map" ); $output = $guide->show_index( cat => "pubs", loc => "waddon", format => "map", - return_output => 1 ); + return_output => 1, noheaders => 1 ); Test::HTML::Content::title_ok( $output, "Map of Category Pubs and Locale Waddon - Test", "...and page title is correct" ); @@ -108,11 +110,9 @@
# Test the JSON version. $output = $guide->show_index( cat => "pubs", loc => "waddon", format => "json", - return_output => 1 ); + return_output => 1, noheaders => 1 ); unlike( $output, qr/error/i, "JSON format invocation doesn't cause error." );
-# Need to strip out the Content-Type: header or the decoder gets confused. -$output =~ s/^Content-Type:.*\n//s; my $parsed = eval { local $SIG{__WARN__} = sub { die $_[0]; }; decode_json( $output );
openguides-commits@lists.openguides.org