Author: kake Date: 2012-04-28 16:31:20 +0100 (Sat, 28 Apr 2012) New Revision: 1371
Modified: trunk/Changes trunk/lib/OpenGuides/JSON.pm trunk/preferences.cgi trunk/t/59_preferences.t Log: Added JSON output for the preferences page (fixes #293).
Modified: trunk/Changes =================================================================== --- trunk/Changes 2012-04-27 18:04:12 UTC (rev 1370) +++ trunk/Changes 2012-04-28 15:31:20 UTC (rev 1371) @@ -24,6 +24,7 @@ Added a basic stylesheet, installed into the static_path directory. Fixed the JSON output for action=index (using a new dependency on Template::Plugin::JSON::Escape). + Added JSON output for the preferences page (#293). IP addresses no longer show on recent changes for people without the admin preference set.
Modified: trunk/lib/OpenGuides/JSON.pm =================================================================== --- trunk/lib/OpenGuides/JSON.pm 2012-04-27 18:04:12 UTC (rev 1370) +++ trunk/lib/OpenGuides/JSON.pm 2012-04-28 15:31:20 UTC (rev 1371) @@ -9,6 +9,8 @@ use Time::Piece; use URI::Escape; use Carp 'croak'; +use JSON; +use OpenGuides::CGI;
sub new { my ( $class, @args ) = @_; @@ -143,6 +145,13 @@ return $self->{json_maker}; }
+sub make_prefs_json { + my $self = shift; + my %prefs = OpenGuides::CGI->get_prefs_from_cookie( + config => $self->{config} ); + return encode_json( %prefs ); +} + sub make_recentchanges_json { my ( $self, %args ) = @_;
@@ -245,6 +254,15 @@ Returns a raw LWiki::Toolkit::Plugin::JSON object created with the values you invoked this module with.
+=item B<make_prefs_json> + + my $json_writer = OpenGuides::JSON->new( wiki => $wiki, + config => $config ); + print $json_writer->make_prefs_json(); + +Retrieves the preferences from any stored preferences cookie, supplies +defaults for any preferences not set, returns the result as JSON. + =item B<make_recentchanges_json>
# Ten most recent changes.
Modified: trunk/preferences.cgi =================================================================== --- trunk/preferences.cgi 2012-04-27 18:04:12 UTC (rev 1370) +++ trunk/preferences.cgi 2012-04-28 15:31:20 UTC (rev 1371) @@ -6,6 +6,7 @@ use CGI; use OpenGuides::Config; use OpenGuides::CGI; +use OpenGuides::JSON; use OpenGuides::Utils; use OpenGuides::Template;
@@ -14,9 +15,15 @@ my $wiki = OpenGuides::Utils->make_wiki_object( config => $config ); my $cgi = CGI->new(); my $action = $cgi->param('action') || ''; +my $format = $cgi->param('format') || '';
if ( $action eq "set_preferences" ) { set_preferences(); +} elsif ( $action eq "show" && $format eq "json" ) { + my $json_writer = OpenGuides::JSON->new( wiki => $wiki, + config => $config ); + print "Content-type: text/javascript\n\n"; + print $json_writer->make_prefs_json(); } else { show_form(); }
Modified: trunk/t/59_preferences.t =================================================================== --- trunk/t/59_preferences.t 2012-04-27 18:04:12 UTC (rev 1370) +++ trunk/t/59_preferences.t 2012-04-28 15:31:20 UTC (rev 1371) @@ -1,31 +1,25 @@ use strict; -use Wiki::Toolkit::Setup::SQLite; +use JSON; use OpenGuides; +use OpenGuides::JSON; 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)"; }
-eval { require DBD::SQLite; }; -if ( $@ ) { - plan skip_all => "DBD::SQLite not installed - no database to test with"; - exit 0; -} - eval { require Test::HTML::Content; }; if ( $@ ) { plan skip_all => "Test::HTML::Content not installed"; exit 0; }
-plan tests => 4; +plan tests => 12;
- OpenGuides::Test::refresh_db(); +OpenGuides::Test::refresh_db();
my $config = OpenGuides::Test->make_basic_config; my $guide = OpenGuides->new( config => $config ); @@ -70,6 +64,59 @@ "input", { type => "checkbox", name => "display_google_maps" }, "...but not when node maps are globally disabled." );
+# Test JSON version of prefs page. +my $json_writer = OpenGuides::JSON->new( wiki => $wiki, + config => $config ); +delete $ENV{HTTP_COOKIE}; +my $output = eval { + $json_writer->make_prefs_json(); +}; +ok( !$@, "->make_prefs_json() doesn't die when no cookie set." ); +if ( $@ ) { warn "# Error was: $@"; } +# 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 ); +}; +ok( !$@, "...and its output looks like JSON." ); +if ( $@ ) { warn "# Warning was: $@"; } +ok( $parsed->{username}, "...and a username is included in the output" ); +#use Data::Dumper; print Dumper $parsed; exit 0; + +$ENV{HTTP_COOKIE} = OpenGuides::CGI->make_prefs_cookie( config => $config ); +$output = eval { + $json_writer->make_prefs_json(); +}; +ok( !$@, "->make_prefs_json() doesn't die when cookie set with all defaults."); +if ( $@ ) { warn "# Error was: $@"; } +$output =~ s/^Content-Type:.*\n//s; +$parsed = eval { + local $SIG{__WARN__} = sub { die $_[0]; }; + decode_json( $output ); +}; +ok( !$@, "...and its output looks like JSON." ); +if ( $@ ) { warn "# Warning was: $@"; } +# We don't get a username set in this case. + +$ENV{HTTP_COOKIE} = OpenGuides::CGI->make_prefs_cookie( config => $config, + username => "Kake" ); +$output = eval { + $json_writer->make_prefs_json(); +}; +ok( !$@, + "->make_prefs_json() doesn't die when cookie set with given username."); +if ( $@ ) { warn "# Error was: $@"; } +$output =~ s/^Content-Type:.*\n//s; +$parsed = eval { + local $SIG{__WARN__} = sub { die $_[0]; }; + decode_json( $output ); +}; +ok( !$@, "...and its output looks like JSON." ); +if ( $@ ) { warn "# Warning was: $@"; } +is( $parsed->{username}, "Kake", + "...and the correct username is included in the output" ); + sub get_output { my ($wiki, $config) = @_;