Author: kake Date: 2012-03-14 17:14:30 +0000 (Wed, 14 Mar 2012) New Revision: 1312
Added: trunk/t/91_username_in_templates.t Modified: trunk/Changes trunk/MANIFEST trunk/lib/OpenGuides/Template.pm trunk/preferences.cgi Log: Made the 'username' TT variable accessible to all templates.
Modified: trunk/Changes =================================================================== --- trunk/Changes 2012-03-13 17:24:05 UTC (rev 1311) +++ trunk/Changes 2012-03-14 17:14:30 UTC (rev 1312) @@ -4,6 +4,7 @@ http://dev.openguides.org/log/trunk.
0.66 ? + All templates now have access to the "username" TT variable. Add CSS classes for each category and locale to the "content" div (and hence we now require version 2.24 of Template). Switch to using Geo::Coordinates::OSGB/ITM instead of
Modified: trunk/MANIFEST =================================================================== --- trunk/MANIFEST 2012-03-13 17:24:05 UTC (rev 1311) +++ trunk/MANIFEST 2012-03-14 17:14:30 UTC (rev 1312) @@ -155,6 +155,7 @@ t/85_universal_edit_link.t t/86_recent_changes.t t/90_css_category_locale_classes.t +t/91_username_in_templates.t t/templates/15_test.tt wiki.cgi META.json
Modified: trunk/lib/OpenGuides/Template.pm =================================================================== --- trunk/lib/OpenGuides/Template.pm 2012-03-13 17:24:05 UTC (rev 1311) +++ trunk/lib/OpenGuides/Template.pm 2012-03-14 17:14:30 UTC (rev 1312) @@ -190,10 +190,20 @@ $tt_vars->{node_param} = CGI->escape($args{wiki}->formatter->node_name_to_node_param($args{node})); }
- # Now set further TT variables if explicitly supplied - do this last - # as these override auto-set ones. + # Now set further TT variables if explicitly supplied - do this after the + # above auto-setting as these override auto-set ones. $tt_vars = { %$tt_vars, %{ $args{vars} || {} } };
+ # Finally, dig out the username from the cookie if we haven't already + # been sent it in vars. + if ( !$tt_vars->{username} ) { + my %prefs = OpenGuides::CGI->get_prefs_from_cookie(config => $config); + # If there's nothing in there, it defaults to "Anonymous". + if ( $prefs{username} ne "Anonymous" ) { + $tt_vars->{username} = $prefs{username}; + } + } + my $header = "";
unless ( $args{noheaders} ) {
Modified: trunk/preferences.cgi =================================================================== --- trunk/preferences.cgi 2012-03-13 17:24:05 UTC (rev 1311) +++ trunk/preferences.cgi 2012-03-14 17:14:30 UTC (rev 1312) @@ -38,6 +38,8 @@ ); push @cookies, $rc_cookie; } + # We have to send the username to OpenGuides::Template because they might + # have changed it, in which case it won't be in the cookie yet. print OpenGuides::Template->output( wiki => $wiki, config => $config, @@ -46,6 +48,7 @@ vars => { not_editable => 1, not_deletable => 1, + username => $prefs{username}, } ); }
Added: trunk/t/91_username_in_templates.t =================================================================== --- trunk/t/91_username_in_templates.t (rev 0) +++ trunk/t/91_username_in_templates.t 2012-03-14 17:14:30 UTC (rev 1312) @@ -0,0 +1,64 @@ +use strict; +use Cwd; +use OpenGuides; +use OpenGuides::CGI; +use OpenGuides::Test; +use Test::More; + +eval { require DBD::SQLite; }; +if ( $@ ) { + my ($error) = $@ =~ /^(.*?)\n/; + plan skip_all => + "DBD::SQLite could not be used - no database to test with. ($error)"; +} + +plan tests => 2; + +my $config = OpenGuides::Test->make_basic_config; +$config->custom_template_path( cwd . "/t/templates/tmp/" ); +my $guide = OpenGuides->new( config => $config ); + +# Clear out the database from any previous runs. +OpenGuides::Test::refresh_db(); + +# Write a node. +OpenGuides::Test->write_data( + guide => $guide, + node => "Ship Of Fools", + return_output => 1, + ); + +# Write a custom banner template that includes the username. +eval { + unlink cwd . "/t/templates/tmp/custom_banner.tt"; +}; + +open( my $fh, ">", cwd . "/t/templates/tmp/custom_banner.tt" ) or die $!; +print $fh <<EOF; +<div class="banner_username"> + [% IF username %] + You are logged in as [% username %]. + [% ELSE %] + You are not logged in. + [% END %] +</div> +EOF +close $fh or die $!; + +# Set a username in the cookie. +my $cookie = OpenGuides::CGI->make_prefs_cookie( + config => $config, + username => "Kake", +); +$ENV{HTTP_COOKIE} = $cookie; + +# Check that username appears if cookie is set. +my $output = $guide->display_node( id => "Ship Of Fools", return_output => 1 ); +like( $output, qr/You are logged in as Kake./, + "username sent to templates if set in prefs cookie" ); + +# Check that username doesn't appear if cookie not set. +delete $ENV{HTTP_COOKIE}; +$output = $guide->display_node( id => "Ship Of Fools", return_output => 1 ); +like( $output, qr/You are not logged in./, + "...but not if no username is set." );
openguides-commits@lists.openguides.org