source: trunk/t/13_cookies.t @ 953

Last change on this file since 953 was 953, checked in by kake, 15 years ago

Add config option to suppress inline maps on geotagged nodes (#188). Fix preferences to take notice of users turning off inline Google maps (#189).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.4 KB
Line 
1use strict;
2use OpenGuides::Config;
3use OpenGuides::CGI;
4use Time::Piece;
5use Time::Seconds;
6use Test::More tests => 27;
7
8eval { OpenGuides::CGI->make_prefs_cookie; };
9ok( $@, "->make_prefs_cookie dies if no config object supplied" );
10
11eval { OpenGuides::CGI->make_prefs_cookie( config => "foo" ); };
12ok( $@, "...or if config isn't an OpenGuides::Config" );
13
14my $config = OpenGuides::Config->new( vars => { site_name => "Test Site" } );
15
16eval { OpenGuides::CGI->make_prefs_cookie( config => $config ); };
17is( $@, "", "...but not if it is" );
18
19# Use nonsense values here to make sure the test is a good one regardless
20# of defaults - can't do this for cookie_expires, unfortunately.
21my $cookie = OpenGuides::CGI->make_prefs_cookie(
22    config                     => $config,
23    username                   => "un_pref",
24    include_geocache_link      => "gc_pref",
25    preview_above_edit_box     => "pv_pref",
26    latlong_traditional        => "ll_pref",
27    omit_help_links            => "hl_pref",
28    show_minor_edits_in_rc     => "me_pref",
29    default_edit_type          => "et_pref",
30    cookie_expires             => "never",
31    track_recent_changes_views => "rc_pref",
32    display_google_maps        => "gm_pref",
33);
34isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" );
35
36my $expiry_string = $cookie->expires;
37# Hack off the timezone bit since strptime can't parse it portably.
38# Timezones taken from RFC 822.
39$expiry_string =~ s/ (UT|GMT|EST|EDT|CST|CDT|MST|MDT|PST|PDT|1[A-IK-Z]|\+\d\d\d\d|-\d\d\d\d)$//;
40print "# (String hacked to $expiry_string)\n";
41my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T");
42print "# Expires: " . $cookie->expires . ", ie $expiry\n";
43my $now = localtime;
44print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n";
45ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" );
46
47$ENV{HTTP_COOKIE} = $cookie;
48
49eval { OpenGuides::CGI->get_prefs_from_cookie; };
50ok( $@, "->get_prefs_from_cookie dies if no config object supplied" );
51
52eval { OpenGuides::CGI->get_prefs_from_cookie( config => "foo" ); };
53ok( $@, "...or if config isn't an OpenGuides::Config" );
54
55eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
56is( $@, "", "...but not if it is" );
57
58my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config );
59is( $prefs{username}, "un_pref", "get_prefs_from_cookie can find username" );
60is( $prefs{include_geocache_link}, "gc_pref", "...and geocache prefs" );
61is( $prefs{preview_above_edit_box}, "pv_pref", "...and preview prefs" );
62is( $prefs{latlong_traditional}, "ll_pref", "...and latlong prefs" );
63is( $prefs{omit_help_links}, "hl_pref", "...and help link prefs" );
64is( $prefs{show_minor_edits_in_rc}, "me_pref", "...and minor edits prefs" );
65is( $prefs{default_edit_type}, "et_pref", "...and default edit prefs" );
66is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" );
67is( $prefs{track_recent_changes_views}, "rc_pref",
68                                     "...and recent changes tracking" );
69is( $prefs{display_google_maps}, "gm_pref",
70                                     "...and Google Maps display preference" );
71
72# Now make sure that true/false preferences are taken account of when
73# they're false.
74$cookie = OpenGuides::CGI->make_prefs_cookie(
75    config                     => $config,
76    include_geocache_link      => 0,
77    preview_above_edit_box     => 0,
78    latlong_traditional        => 0,
79    omit_help_links            => 0,
80    show_minor_edits_in_rc     => 0,
81    track_recent_changes_views => 0,
82    display_google_maps        => 0,
83);
84
85$ENV{HTTP_COOKIE} = $cookie;
86
87%prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config );
88ok( !$prefs{include_geocache_link}, "geocache prefs taken note of when false");
89ok( !$prefs{preview_above_edit_box}, "...and preview prefs" );
90ok( !$prefs{latlong_traditional}, "...and latlong prefs" );
91ok( !$prefs{omit_help_links}, "...and help link prefs" );
92ok( !$prefs{show_minor_edits_in_rc}, "...and minor edits prefs" );
93ok( !$prefs{track_recent_changes_views}, "...and recent changes prefs" );
94ok( !$prefs{display_google_maps}, "...and Google Maps prefs" );
95
96# Check that cookie parsing fails nicely if no cookie set.
97delete $ENV{HTTP_COOKIE};
98%prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
99is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" );
100is( keys %prefs, 10, "...and returns ten default values" );
Note: See TracBrowser for help on using the repository browser.