| 1 | use strict; |
|---|
| 2 | use Wiki::Toolkit::Setup::SQLite; |
|---|
| 3 | use OpenGuides::Config; |
|---|
| 4 | use OpenGuides; |
|---|
| 5 | use OpenGuides::CGI; |
|---|
| 6 | use Time::Piece; |
|---|
| 7 | use Time::Seconds; |
|---|
| 8 | use Test::More tests => 11; |
|---|
| 9 | |
|---|
| 10 | eval { OpenGuides::CGI->make_recent_changes_cookie; }; |
|---|
| 11 | ok( $@, "->make_recent_changes_cookie dies if no config object supplied" ); |
|---|
| 12 | |
|---|
| 13 | eval { OpenGuides::CGI->make_recent_changes_cookie( config => "foo" ); }; |
|---|
| 14 | ok( $@, "...or if config isn't an OpenGuides::Config" ); |
|---|
| 15 | |
|---|
| 16 | my $config = OpenGuides::Config->new( vars => { site_name => "Test Site" } ); |
|---|
| 17 | |
|---|
| 18 | eval { OpenGuides::CGI->make_recent_changes_cookie( config => $config ); }; |
|---|
| 19 | is( $@, "", "...but not if it is" ); |
|---|
| 20 | |
|---|
| 21 | my $cookie = OpenGuides::CGI->make_recent_changes_cookie( config => $config ); |
|---|
| 22 | isa_ok( $cookie, "CGI::Cookie", |
|---|
| 23 | "->make_recent_changes_cookie returns a cookie" ); |
|---|
| 24 | |
|---|
| 25 | my $expiry_string = $cookie->expires; |
|---|
| 26 | # Hack off the timezone bit since strptime can't parse it portably. |
|---|
| 27 | # Timezones taken from RFC 822. |
|---|
| 28 | $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)$//; |
|---|
| 29 | print "# (String hacked to $expiry_string)\n"; |
|---|
| 30 | my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T"); |
|---|
| 31 | print "# Expires: " . $cookie->expires . ", ie $expiry\n"; |
|---|
| 32 | my $now = localtime; |
|---|
| 33 | print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n"; |
|---|
| 34 | ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" ); |
|---|
| 35 | |
|---|
| 36 | $ENV{HTTP_COOKIE} = $cookie; |
|---|
| 37 | |
|---|
| 38 | eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie; }; |
|---|
| 39 | ok( $@, "->get_last_recent_changes_visit_from_cookie dies if no config object supplied" ); |
|---|
| 40 | |
|---|
| 41 | eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie( config => "foo" ); }; |
|---|
| 42 | ok( $@, "...or if config isn't an OpenGuides::Config" ); |
|---|
| 43 | |
|---|
| 44 | eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie( config => $config ); }; |
|---|
| 45 | is( $@, "", "...but not if it is" ); |
|---|
| 46 | |
|---|
| 47 | # Check that cookie parsing fails nicely if no cookie set. |
|---|
| 48 | delete $ENV{HTTP_COOKIE}; |
|---|
| 49 | eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie( config => $config ); }; |
|---|
| 50 | is( $@, "", "->get_last_recent_changes_visit_from_cookie doesn't die if no cookie set" ); |
|---|
| 51 | |
|---|
| 52 | # Now test that the prefs option is taken note of. |
|---|
| 53 | my $have_sqlite = 1; |
|---|
| 54 | my $sqlite_error; |
|---|
| 55 | |
|---|
| 56 | eval { require DBD::SQLite; }; |
|---|
| 57 | if ( $@ ) { |
|---|
| 58 | ($sqlite_error) = $@ =~ /^(.*?)\n/; |
|---|
| 59 | $have_sqlite = 0; |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | SKIP: { |
|---|
| 63 | skip "DBD::SQLite could not be used - no database to test with. ($sqlite_error)", 2 |
|---|
| 64 | unless $have_sqlite; |
|---|
| 65 | |
|---|
| 66 | Wiki::Toolkit::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 67 | my $config = OpenGuides::Config->new( |
|---|
| 68 | vars => { |
|---|
| 69 | dbtype => "sqlite", |
|---|
| 70 | dbname => "t/node.db", |
|---|
| 71 | indexing_directory => "t/indexes", |
|---|
| 72 | script_name => "wiki.cgi", |
|---|
| 73 | script_url => "http://example.com/", |
|---|
| 74 | site_name => "Test Site", |
|---|
| 75 | template_path => "./templates", |
|---|
| 76 | home_name => "Home", |
|---|
| 77 | } |
|---|
| 78 | ); |
|---|
| 79 | eval { require Wiki::Toolkit::Search::Plucene; }; |
|---|
| 80 | if ( $@ ) { $config->use_plucene ( 0 ) }; |
|---|
| 81 | |
|---|
| 82 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 83 | |
|---|
| 84 | my $prefs_cookie = OpenGuides::CGI->make_prefs_cookie( |
|---|
| 85 | config => $config, |
|---|
| 86 | track_recent_changes_views => 1, |
|---|
| 87 | ); |
|---|
| 88 | my $rc_cookie = OpenGuides::CGI->make_recent_changes_cookie( |
|---|
| 89 | config => $config |
|---|
| 90 | ); |
|---|
| 91 | $ENV{HTTP_COOKIE} = $prefs_cookie . "; " . $rc_cookie; |
|---|
| 92 | my $output = $guide->display_node( |
|---|
| 93 | id => "RecentChanges", |
|---|
| 94 | return_output => 1, |
|---|
| 95 | ); |
|---|
| 96 | like( $output, qr/Set-Cookie:/, "recent changes cookie set when asked" ); |
|---|
| 97 | |
|---|
| 98 | $prefs_cookie = OpenGuides::CGI->make_prefs_cookie( |
|---|
| 99 | config => $config, |
|---|
| 100 | track_recent_changes_views => 0, |
|---|
| 101 | ); |
|---|
| 102 | $ENV{HTTP_COOKIE} = $prefs_cookie . "; " . $rc_cookie; |
|---|
| 103 | $output = $guide->display_node( |
|---|
| 104 | id => "RecentChanges", |
|---|
| 105 | return_output => 1, |
|---|
| 106 | ); |
|---|
| 107 | unlike( $output, qr/Set-Cookie:/, "...and not when not" ); |
|---|
| 108 | } |
|---|