| 1 | use strict; |
|---|
| 2 | use CGI::Wiki::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 | eval { require DBD::SQLite; }; |
|---|
| 54 | my $have_sqlite = $@ ? 0 : 1; |
|---|
| 55 | |
|---|
| 56 | SKIP: { |
|---|
| 57 | skip "DBD::SQLite not installed - no database to test with", 2 |
|---|
| 58 | unless $have_sqlite; |
|---|
| 59 | |
|---|
| 60 | CGI::Wiki::Setup::SQLite::setup( { dbname => "t/node.db" } ); |
|---|
| 61 | my $config = OpenGuides::Config->new( |
|---|
| 62 | vars => { |
|---|
| 63 | dbtype => "sqlite", |
|---|
| 64 | dbname => "t/node.db", |
|---|
| 65 | indexing_directory => "t/indexes", |
|---|
| 66 | script_name => "wiki.cgi", |
|---|
| 67 | script_url => "http://example.com/", |
|---|
| 68 | site_name => "Test Site", |
|---|
| 69 | template_path => "./templates", |
|---|
| 70 | home_name => "Home", |
|---|
| 71 | } |
|---|
| 72 | ); |
|---|
| 73 | eval { require CGI::Wiki::Search::Plucene; }; |
|---|
| 74 | if ( $@ ) { $config->use_plucene ( 0 ) }; |
|---|
| 75 | |
|---|
| 76 | my $guide = OpenGuides->new( config => $config ); |
|---|
| 77 | |
|---|
| 78 | my $prefs_cookie = OpenGuides::CGI->make_prefs_cookie( |
|---|
| 79 | config => $config, |
|---|
| 80 | track_recent_changes_views => 1, |
|---|
| 81 | ); |
|---|
| 82 | my $rc_cookie = OpenGuides::CGI->make_recent_changes_cookie( |
|---|
| 83 | config => $config |
|---|
| 84 | ); |
|---|
| 85 | $ENV{HTTP_COOKIE} = $prefs_cookie . "; " . $rc_cookie; |
|---|
| 86 | my $output = $guide->display_node( |
|---|
| 87 | id => "RecentChanges", |
|---|
| 88 | return_output => 1, |
|---|
| 89 | ); |
|---|
| 90 | like( $output, qr/Set-Cookie:/, "recent changes cookie set when asked" ); |
|---|
| 91 | |
|---|
| 92 | $prefs_cookie = OpenGuides::CGI->make_prefs_cookie( |
|---|
| 93 | config => $config, |
|---|
| 94 | track_recent_changes_views => 0, |
|---|
| 95 | ); |
|---|
| 96 | $ENV{HTTP_COOKIE} = $prefs_cookie . "; " . $rc_cookie; |
|---|
| 97 | $output = $guide->display_node( |
|---|
| 98 | id => "RecentChanges", |
|---|
| 99 | return_output => 1, |
|---|
| 100 | ); |
|---|
| 101 | unlike( $output, qr/Set-Cookie:/, "...and not when not" ); |
|---|
| 102 | } |
|---|