source: trunk/t/14_recent_changes_cookies.t @ 785

Last change on this file since 785 was 785, checked in by Dominic Hargreaves, 15 years ago

Use Wiki::Toolkit. Note that CGI::Wiki::Plugin::Diff hasn't been converted yet,
so tests will be broken for now.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.0 KB
Line 
1use strict;
2use Wiki::Toolkit::Setup::SQLite;
3use OpenGuides::Config;
4use OpenGuides;
5use OpenGuides::CGI;
6use Time::Piece;
7use Time::Seconds;
8use Test::More tests => 11;
9
10eval { OpenGuides::CGI->make_recent_changes_cookie; };
11ok( $@, "->make_recent_changes_cookie dies if no config object supplied" );
12
13eval { OpenGuides::CGI->make_recent_changes_cookie( config => "foo" ); };
14ok( $@, "...or if config isn't an OpenGuides::Config" );
15
16my $config = OpenGuides::Config->new( vars => { site_name => "Test Site" } );
17
18eval { OpenGuides::CGI->make_recent_changes_cookie( config => $config ); };
19is( $@, "", "...but not if it is" );
20
21my $cookie = OpenGuides::CGI->make_recent_changes_cookie( config => $config );
22isa_ok( $cookie, "CGI::Cookie",
23        "->make_recent_changes_cookie returns a cookie" );
24
25my $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)$//;
29print "# (String hacked to $expiry_string)\n";
30my $expiry = Time::Piece->strptime( $expiry_string, "%a, %d-%b-%Y %T");
31print "# Expires: " . $cookie->expires . ", ie $expiry\n";
32my $now = localtime;
33print "# cookie should still be valid in a year, ie " . ($now + ONE_YEAR) . "\n";
34ok( $expiry - ( $now + ONE_YEAR ) > 0, "cookie expiry date correct" );
35
36$ENV{HTTP_COOKIE} = $cookie;
37
38eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie; };
39ok( $@, "->get_last_recent_changes_visit_from_cookie dies if no config object supplied" );
40
41eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie( config => "foo" ); };
42ok( $@, "...or if config isn't an OpenGuides::Config" );
43
44eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie( config => $config ); };
45is( $@, "", "...but not if it is" );
46
47# Check that cookie parsing fails nicely if no cookie set.
48delete $ENV{HTTP_COOKIE};
49eval { OpenGuides::CGI->get_last_recent_changes_visit_from_cookie( config => $config ); };
50is( $@, "", "->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.
53eval { require DBD::SQLite; };
54my $have_sqlite = $@ ? 0 : 1;
55
56SKIP: {
57    skip "DBD::SQLite not installed - no database to test with", 2
58      unless $have_sqlite;
59
60    Wiki::Toolkit::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 Wiki::Toolkit::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}
Note: See TracBrowser for help on using the repository browser.