1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198
|
use strict;
use Test::More 'no_plan';
$ENV{'CGI_APP_RETURN_ONLY'} = 1;
use CGI::Session;
use CGI::Session::Driver::file;
use File::Spec;
$CGI::Session::Driver::file::FileName = 'session.dat';
my $Session_ID;
my $Storage_Name = 'some_storage_name';
my $Other_Storage_Name = 'other_storage_name';
my $Storage_Hash;
{
package WebApp1;
use CGI::Application;
use vars qw(@ISA);
BEGIN { @ISA = qw(CGI::Application); }
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::FormState;
use Test::More;
sub setup {
my $self = shift;
$self->run_modes(['start']);
$self->session_config(
CGI_SESSION_OPTIONS => [ "driver:File", undef, { Directory => 't' } ],
);
$Session_ID = $self->session->id;
$self->session->param('foo', 42);
is($self->session->param('foo'), 42, '[webapp1] new session initialized');
$self->form_state;
}
sub start {
my $self = shift;
eval {
$self->form_state->init($Storage_Name, 'bongo' => 'bubba');
};
ok($@, "prevented from calling init with bad option");
my $exists = $self->form_state->init($Storage_Name);
ok(!$exists, '[webapp1] form state did not already exist');
my @keys = sort $self->form_state->param;
ok(eq_array(\@keys, []), '[webapp2] form_state keys (1)');
# Store some parameters
$self->form_state->param('name' => 'Road Runner');
is($self->form_state->param('name'), 'Road Runner', '[webapp1] form_state: name');
@keys = sort $self->form_state->param;
ok(eq_array(\@keys, ['name']), '[webapp2] form_state keys (2)');
$self->form_state->clear_params;
is($self->form_state->param('name'), undef, '[webapp1] form_state: name (cleared)');
@keys = sort $self->form_state->param;
ok(eq_array(\@keys, []), '[webapp2] form_state keys (3)');
$self->form_state->param('name' => 'Bugs Bunny');
$self->form_state->param('occupation' => 'Having Fun');
# Store some other parameters via hashref
$self->form_state->param({
'name2' => 'Wile E. Coyote',
'occupation' => 'Cartoon Character',
});
@keys = sort $self->form_state->param;
ok(eq_array(\@keys, ['name', 'name2', 'occupation']), '[webapp2] form_state keys (4)');
is($self->form_state->param('name'), 'Bugs Bunny', '[webapp1] form_state: name');
is($self->form_state->param('name2'), 'Wile E. Coyote', '[webapp1] form_state: name2');
is($self->form_state->param('occupation'), 'Cartoon Character', '[webapp1] form_state: occupation');
my $t = $self->load_tmpl('t/tmpl/some_storage_name.html');
my $output = $t->output;
$Storage_Hash = $self->form_state->id;
is($output, "$Storage_Name:$Storage_Hash", 'form_state_id added to output');
}
}
{
package WebApp2;
use CGI::Application;
use vars qw(@ISA);
BEGIN { @ISA = qw(CGI::Application); }
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::FormState;
use Test::More;
sub setup {
my $self = shift;
$self->run_modes(['start']);
# init session and verify that it's got content
$self->session_config(
CGI_SESSION_OPTIONS => [ "driver:File", $Session_ID, { Directory => 't' } ],
);
is($self->session->param('foo'), 42, '[webapp2] previous session initialized');
$self->start_mode('start');
$self->run_modes([qw/start/]);
}
sub start {
my $self = shift;
my $exists = $self->form_state->init($Storage_Name);
ok($exists, '[webapp2] form state already existed');
# Retrieve some parameters
my @keys = sort $self->form_state->param;
ok(eq_array(\@keys, ['name', 'name2', 'occupation']), '[webapp2] form_state keys');
# Retrieve some parameters
is($self->form_state->param('name'), 'Bugs Bunny', '[webapp2] form_state: name');
is($self->form_state->param('name2'), 'Wile E. Coyote', '[webapp2] form_state: name2');
is($self->form_state->param('occupation'), 'Cartoon Character', '[webapp2] form_state: occupation');
}
}
{
package WebApp3;
use CGI::Application;
use vars qw(@ISA);
BEGIN { @ISA = qw(CGI::Application); }
use CGI::Application::Plugin::Session;
use CGI::Application::Plugin::FormState;
use Test::More;
sub setup {
my $self = shift;
$self->run_modes(['start']);
# init session and verify that it's got content
$self->session_config(
CGI_SESSION_OPTIONS => [ "driver:File", $Session_ID, { Directory => 't' } ],
);
is($self->session->param('foo'), 42, '[webapp3] previous session initialized');
$self->start_mode('start');
$self->run_modes([qw/start/]);
}
sub start {
my $self = shift;
my $exists = $self->form_state->init($Other_Storage_Name);
ok(!$exists, '[webapp3] form state did not already exist');
my @keys = sort $self->form_state->param;
ok(eq_array(\@keys, []), '[webapp3] form_state keys empty');
}
}
WebApp1->new->run;
my $query = CGI->new;
$query->param($Storage_Name, $Storage_Hash);
WebApp2->new(QUERY => $query)->run;
$query = CGI->new;
$query->param($Other_Storage_Name, $Storage_Hash);
WebApp3->new(QUERY => $query)->run;
unlink File::Spec->catfile('t', $CGI::Session::Driver::file::FileName);
|