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

Last change on this file since 878 was 731, checked in by Dominic Hargreaves, 16 years ago

Couple of things missing from previous commit.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 3.1 KB
Line 
1use strict;
2use OpenGuides::Config;
3use OpenGuides::CGI;
4use Time::Piece;
5use Time::Seconds;
6use Test::More tests => 20;
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
19my $cookie = OpenGuides::CGI->make_prefs_cookie(
20    config                     => $config,
21    username                   => "Kake",
22    include_geocache_link      => 1,
23    preview_above_edit_box     => 1,
24    latlong_traditional        => 1,
25    omit_help_links            => 1,
26    show_minor_edits_in_rc     => 1,
27    default_edit_type          => "tidying",
28    cookie_expires             => "never",
29    track_recent_changes_views => 1,
30    display_google_maps => 1
31);
32isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" );
33
34my $expiry_string = $cookie->expires;
35# Hack off the timezone bit since strptime can't parse it portably.
36# Timezones taken from RFC 822.
37$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)$//;
38print "# (String hacked to $expiry_string)\n";
39my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T");
40print "# Expires: " . $cookie->expires . ", ie $expiry\n";
41my $now = localtime;
42print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n";
43ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" );
44
45$ENV{HTTP_COOKIE} = $cookie;
46
47eval { OpenGuides::CGI->get_prefs_from_cookie; };
48ok( $@, "->get_prefs_from_cookie dies if no config object supplied" );
49
50eval { OpenGuides::CGI->get_prefs_from_cookie( config => "foo" ); };
51ok( $@, "...or if config isn't an OpenGuides::Config" );
52
53eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
54is( $@, "", "...but not if it is" );
55
56my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config );
57is( $prefs{username}, "Kake",
58    "get_prefs_from_cookie can find username" );
59is( $prefs{include_geocache_link}, 1, "...and geocache prefs" );
60is( $prefs{preview_above_edit_box}, 1, "...and preview prefs" );
61is( $prefs{latlong_traditional}, 1, "...and latlong prefs" );
62is( $prefs{omit_help_links}, 1, "...and help link prefs" );
63is( $prefs{show_minor_edits_in_rc}, 1, "...and minor edits prefs" );
64is( $prefs{default_edit_type}, "tidying", "...and default edit prefs" );
65is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" );
66ok( $prefs{track_recent_changes_views}, "...and recent changes tracking" );
67is( $prefs{display_google_maps}, 1, "...and Google Maps display preference" );
68
69# Check that cookie parsing fails nicely if no cookie set.
70delete $ENV{HTTP_COOKIE};
71%prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); };
72is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" );
73is( keys %prefs, 10, "...and returns ten default values" );
Note: See TracBrowser for help on using the repository browser.