| 1 | use strict; |
|---|
| 2 | use OpenGuides::Config; |
|---|
| 3 | use OpenGuides::CGI; |
|---|
| 4 | use Time::Piece; |
|---|
| 5 | use Time::Seconds; |
|---|
| 6 | use Test::More tests => 29; |
|---|
| 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 | # 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. |
|---|
| 21 | my $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 | ); |
|---|
| 35 | isa_ok( $cookie, "CGI::Cookie", "->make_prefs_cookie returns a cookie" ); |
|---|
| 36 | |
|---|
| 37 | my $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)$//; |
|---|
| 41 | print "# (String hacked to $expiry_string)\n"; |
|---|
| 42 | my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T"); |
|---|
| 43 | print "# Expires: " . $cookie->expires . ", ie $expiry\n"; |
|---|
| 44 | my $now = localtime; |
|---|
| 45 | print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n"; |
|---|
| 46 | ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" ); |
|---|
| 47 | |
|---|
| 48 | $ENV{HTTP_COOKIE} = $cookie; |
|---|
| 49 | |
|---|
| 50 | eval { OpenGuides::CGI->get_prefs_from_cookie; }; |
|---|
| 51 | ok( $@, "->get_prefs_from_cookie dies if no config object supplied" ); |
|---|
| 52 | |
|---|
| 53 | eval { OpenGuides::CGI->get_prefs_from_cookie( config => "foo" ); }; |
|---|
| 54 | ok( $@, "...or if config isn't an OpenGuides::Config" ); |
|---|
| 55 | |
|---|
| 56 | eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); }; |
|---|
| 57 | is( $@, "", "...but not if it is" ); |
|---|
| 58 | |
|---|
| 59 | my %prefs = OpenGuides::CGI->get_prefs_from_cookie( config => $config ); |
|---|
| 60 | is( $prefs{username}, "un_pref", "get_prefs_from_cookie can find username" ); |
|---|
| 61 | is( $prefs{include_geocache_link}, "gc_pref", "...and geocache prefs" ); |
|---|
| 62 | is( $prefs{preview_above_edit_box}, "pv_pref", "...and preview prefs" ); |
|---|
| 63 | is( $prefs{latlong_traditional}, "ll_pref", "...and latlong prefs" ); |
|---|
| 64 | is( $prefs{omit_help_links}, "hl_pref", "...and help link prefs" ); |
|---|
| 65 | is( $prefs{show_minor_edits_in_rc}, "me_pref", "...and minor edits prefs" ); |
|---|
| 66 | is( $prefs{default_edit_type}, "et_pref", "...and default edit prefs" ); |
|---|
| 67 | is( $prefs{cookie_expires}, "never", "...and requested cookie expiry" ); |
|---|
| 68 | is( $prefs{track_recent_changes_views}, "rc_pref", |
|---|
| 69 | "...and recent changes tracking" ); |
|---|
| 70 | is( $prefs{display_google_maps}, "gm_pref", |
|---|
| 71 | "...and Google Maps display preference" ); |
|---|
| 72 | is( $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 ); |
|---|
| 91 | ok( !$prefs{include_geocache_link}, "geocache prefs taken note of when false"); |
|---|
| 92 | ok( !$prefs{preview_above_edit_box}, "...and preview prefs" ); |
|---|
| 93 | ok( !$prefs{latlong_traditional}, "...and latlong prefs" ); |
|---|
| 94 | ok( !$prefs{omit_help_links}, "...and help link prefs" ); |
|---|
| 95 | ok( !$prefs{show_minor_edits_in_rc}, "...and minor edits prefs" ); |
|---|
| 96 | ok( !$prefs{track_recent_changes_views}, "...and recent changes prefs" ); |
|---|
| 97 | ok( !$prefs{display_google_maps}, "...and Google Maps prefs" ); |
|---|
| 98 | ok( !$prefs{is_admin}, "...and admin prefs" ); |
|---|
| 99 | |
|---|
| 100 | # Check that cookie parsing fails nicely if no cookie set. |
|---|
| 101 | delete $ENV{HTTP_COOKIE}; |
|---|
| 102 | %prefs = eval { OpenGuides::CGI->get_prefs_from_cookie( config => $config ); }; |
|---|
| 103 | is( $@, "", "->get_prefs_from_cookie doesn't die if no cookie set" ); |
|---|
| 104 | is( keys %prefs, 11, "...and returns ten default values" ); |
|---|