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

Last change on this file since 473 was 473, checked in by kake, 17 years ago

Changes since

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