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

Last change on this file since 1236 was 1236, checked in by Dominic Hargreaves, 13 years ago

Add an admin navbar, to be displayed if the user requests (fixes #261)

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.6 KB
Line 
1use strict;
2use OpenGuides::Config;
3use OpenGuides::CGI;
4use Time::Piece;
5use Time::Seconds;
6use Test::More tests => 29;
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    is_admin                   => "admin_pref",
34);
35isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" );
36
37my $expiry_string = $cookie->expires;
38# Hack off the timezone bit since strptime can't parse it portably.
39# Timezones taken from RFC 822.
40$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)$//;
41print "# (String hacked to $expiry_string)\n";
42my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T");
43print "# Expires: " . $cookie->expires . ", ie $expiry\n";
44my $now = localtime;
45print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n";
46ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" );
47
48$ENV{HTTP_COOKIE} = $cookie;
49
50eval { OpenGuides::CGI->get_prefs_from_cookie; };
51ok( $@, "->get_prefs_from_cookie dies if no config object supplied" );
52
53eval { OpenGuides::CGI->get_prefs_from_cookie( config => "foo" ); };
54ok( $@, "...or if config isn't an OpenGuides::Config" );
55
56eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
57is( $@, "", "...but not if it is" );
58
59my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config );
60is( $prefs{username}, "un_pref", "get_prefs_from_cookie can find username" );
61is( $prefs{include_geocache_link}, "gc_pref", "...and geocache prefs" );
62is( $prefs{preview_above_edit_box}, "pv_pref", "...and preview prefs" );
63is( $prefs{latlong_traditional}, "ll_pref", "...and latlong prefs" );
64is( $prefs{omit_help_links}, "hl_pref", "...and help link prefs" );
65is( $prefs{show_minor_edits_in_rc}, "me_pref", "...and minor edits prefs" );
66is( $prefs{default_edit_type}, "et_pref", "...and default edit prefs" );
67is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" );
68is( $prefs{track_recent_changes_views}, "rc_pref",
69                                     "...and recent changes tracking" );
70is( $prefs{display_google_maps}, "gm_pref",
71                                     "...and Google Maps display preference" );
72is( $prefs{is_admin}, "admin_pref",
73                                     "...and admin preference" );
74# Now make sure that true/false preferences are taken account of when
75# they're false.
76$cookie = OpenGuides::CGI->make_prefs_cookie(
77    config                     => $config,
78    include_geocache_link      => 0,
79    preview_above_edit_box     => 0,
80    latlong_traditional        => 0,
81    omit_help_links            => 0,
82    show_minor_edits_in_rc     => 0,
83    track_recent_changes_views => 0,
84    display_google_maps        => 0,
85    is_admin                   => 0,
86);
87
88$ENV{HTTP_COOKIE} = $cookie;
89
90%prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config );
91ok( !$prefs{include_geocache_link}, "geocache prefs taken note of when false");
92ok( !$prefs{preview_above_edit_box}, "...and preview prefs" );
93ok( !$prefs{latlong_traditional}, "...and latlong prefs" );
94ok( !$prefs{omit_help_links}, "...and help link prefs" );
95ok( !$prefs{show_minor_edits_in_rc}, "...and minor edits prefs" );
96ok( !$prefs{track_recent_changes_views}, "...and recent changes prefs" );
97ok( !$prefs{display_google_maps}, "...and Google Maps prefs" );
98ok( !$prefs{is_admin}, "...and admin prefs" );
99
100# Check that cookie parsing fails nicely if no cookie set.
101delete $ENV{HTTP_COOKIE};
102%prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
103is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" );
104is( keys %prefs, 11, "...and returns ten default values" );
Note: See TracBrowser for help on using the repository browser.