| 1 | use strict; |
|---|
| 2 | use OpenGuides::Config; |
|---|
| 3 | use OpenGuides::CGI; |
|---|
| 4 | use Time::Piece; |
|---|
| 5 | use Time::Seconds; |
|---|
| 6 | use Test::More tests => 20; |
|---|
| 7 | |
|---|
| 8 | eval { OpenGuides::CGI->make_prefs_cookie; }; |
|---|
| 9 | ok( $@, "->make_prefs_cookie dies if no config object supplied" ); |
|---|
| 10 | |
|---|
| 11 | eval { OpenGuides::CGI->make_prefs_cookie( config => "foo" ); }; |
|---|
| 12 | ok( $@, "...or if config isn't an OpenGuides::Config" ); |
|---|
| 13 | |
|---|
| 14 | my $config = OpenGuides::Config->new( vars => { site_name => "Test Site" } ); |
|---|
| 15 | |
|---|
| 16 | eval { OpenGuides::CGI->make_prefs_cookie( config => $config ); }; |
|---|
| 17 | is( $@, "", "...but not if it is" ); |
|---|
| 18 | |
|---|
| 19 | my $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 | ); |
|---|
| 32 | isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" ); |
|---|
| 33 | |
|---|
| 34 | my $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)$//; |
|---|
| 38 | print "# (String hacked to $expiry_string)\n"; |
|---|
| 39 | my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T"); |
|---|
| 40 | print "# Expires: " . $cookie->expires . ", ie $expiry\n"; |
|---|
| 41 | my $now = localtime; |
|---|
| 42 | print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n"; |
|---|
| 43 | ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" ); |
|---|
| 44 | |
|---|
| 45 | $ENV{HTTP_COOKIE} = $cookie; |
|---|
| 46 | |
|---|
| 47 | eval { OpenGuides::CGI->get_prefs_from_cookie; }; |
|---|
| 48 | ok( $@, "->get_prefs_from_cookie dies if no config object supplied" ); |
|---|
| 49 | |
|---|
| 50 | eval { OpenGuides::CGI->get_prefs_from_cookie( config => "foo" ); }; |
|---|
| 51 | ok( $@, "...or if config isn't an OpenGuides::Config" ); |
|---|
| 52 | |
|---|
| 53 | eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); }; |
|---|
| 54 | is( $@, "", "...but not if it is" ); |
|---|
| 55 | |
|---|
| 56 | my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config ); |
|---|
| 57 | is( $prefs{username}, "Kake", |
|---|
| 58 | "get_prefs_from_cookie can find username" ); |
|---|
| 59 | is( $prefs{include_geocache_link}, 1, "...and geocache prefs" ); |
|---|
| 60 | is( $prefs{preview_above_edit_box}, 1, "...and preview prefs" ); |
|---|
| 61 | is( $prefs{latlong_traditional}, 1, "...and latlong prefs" ); |
|---|
| 62 | is( $prefs{omit_help_links}, 1, "...and help link prefs" ); |
|---|
| 63 | is( $prefs{show_minor_edits_in_rc}, 1, "...and minor edits prefs" ); |
|---|
| 64 | is( $prefs{default_edit_type}, "tidying", "...and default edit prefs" ); |
|---|
| 65 | is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" ); |
|---|
| 66 | ok( $prefs{track_recent_changes_views}, "...and recent changes tracking" ); |
|---|
| 67 | is( $prefs{display_google_maps}, 1, "...and Google Maps display preference" ); |
|---|
| 68 | |
|---|
| 69 | # Check that cookie parsing fails nicely if no cookie set. |
|---|
| 70 | delete $ENV{HTTP_COOKIE}; |
|---|
| 71 | %prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); }; |
|---|
| 72 | is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" ); |
|---|
| 73 | is( keys %prefs, 10, "...and returns ten default values" ); |
|---|