root/trunk/t/14_recent_changes_cookies.t

Revision 958, 4.2 kB (checked in by earle, 20 months ago)

Add detailed DBD::SQLite error reporting to a couple of places where it was missing.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
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.
53my $have_sqlite = 1;
54my $sqlite_error;   
55
56eval { require DBD::SQLite; };
57if ( $@ ) {
58    ($sqlite_error) = $@ =~ /^(.*?)\n/;
59    $have_sqlite = 0;
60}
61
62SKIP: {
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}
Note: See TracBrowser for help on using the browser.