4 use Test::More tests => 84;
12 ok($Maypole::VERSION, 'defines $VERSION');
13 ok($INC{'Maypole/Config.pm'}, 'loads Maypole::Config');
14 ok($INC{'UNIVERSAL/require.pm'}, 'loads UNIVERSAL::require');
15 ok($INC{'Maypole/Constants.pm'}, 'loads Maypole::Constants');
16 ok($INC{'Maypole/Headers.pm'}, 'loads Maypole::Headers');
17 ok($INC{'Class/Accessor/Fast.pm'}, 'loads Class::Accessor::Fast');
18 ok($INC{'Class/Data/Inheritable.pm'}, 'loads Class::Data::Inheritable');
21 my $OK = Maypole::Constants::OK();
22 my $DECLINED = Maypole::Constants::DECLINED();
23 my $ERROR = Maypole::Constants::ERROR();
26 my @API = qw/ config init_done view_object params query param objects model_class
27 template_args output path args action template error document_encoding
28 content_type table headers_in headers_out
29 is_model_applicable setup setup_model init handler handler_guts
30 call_authenticate call_exception additional_data
31 authenticate exception parse_path make_path
32 make_uri get_template_root get_request
33 parse_location send_output
38 can_ok(Maypole => @API);
40 ok( ! UNIVERSAL::can(Maypole => 'is_applicable'), 'no is_applicable() method' );
42 ok(Maypole->config->isa('Maypole::Config'), 'config is a Maypole::Config object');
43 ok(! Maypole->init_done, '... which is false by default');
44 is(Maypole->view_object, undef, '... which is undefined');
46 # simple test class that inherits from Maypole
49 @MyDriver::ISA = 'Maypole';
50 @MyDriver::VERSION = 1;
53 # back to package main;
54 my $driver_class = 'MyDriver';
56 # subclass inherits API
57 can_ok($driver_class => @API);
59 # Mock the model class
60 my (%required, @db_args, @adopted);
61 my $model_class = 'Maypole::Model::CDBI';
62 my $table_class = $driver_class . '::One';
64 my $mock_model = Test::MockModule->new($model_class);
66 require => sub {$required{+shift} = 1},
67 setup_database => sub {
69 $_[1]->{classes} = ["$model_class\::One", "$model_class\::Two"];
70 $_[1]->{tables} = [qw(one two)];
72 adopt => sub {push @adopted, \@_},
78 # 2.11 - removed tests to check the installed handler was a different ref after setup().
79 # The handler tests were testing Maypole's old (pre 2.11) method of importing handler()
80 # into the subclass - it works via standard inheritance now, by setting the 'method'
81 # attribute on Maypole::handler(). The reason the handlers were different
82 # was because setup() would create a new anonymous ref to Maypole::handler(), and install
83 # that - i.e. it installed the same code, but in a different ref, so they tested unequal
84 # although they referred to the same code
86 $driver_class->setup('dbi:foo');
88 ok($required{$model_class}, '... requires model class');
89 is($driver_class->config->model(),
90 'Maypole::Model::CDBI', '... default model is CDBI');
91 is(@db_args, 1, '... calls model->setup_database');
92 like(join (' ', @{$db_args[0]}),
93 qr/$model_class Maypole::Config=\S* $driver_class dbi:foo/,
94 '... setup_database passed setup() args');
95 is(@adopted, 2, '... calls model->adopt foreach class in the model');
96 ok($adopted[0][0]->isa($model_class),
97 '... sets up model subclasses to inherit from model');
98 $driver_class->config->model('NonExistant::Model');
99 eval {$driver_class->setup};
100 like($@, qr/Couldn't load the model class/,
101 '... dies if unable to load model class');
105 $driver_class->config->model($model_class);
108 # Mock the view class
109 my $view_class = 'Maypole::View::TT';
110 my $mock_view = Test::MockModule->new($view_class);
112 new => sub {bless{}, shift},
113 require => sub {$required{+shift} = 1},
118 $driver_class->init();
119 ok($required{$view_class}, '... requires the view class');
120 is($driver_class->config->view, $view_class, '... the default view class is TT');
121 is(join(' ', @{$driver_class->config->display_tables}), 'one two',
122 '... config->display_tables defaults to all tables');
123 ok($driver_class->view_object->isa($view_class),
124 '... creates an instance of the view object');
125 ok($driver_class->init_done, '... sets init_done');
126 $driver_class->config->view('NonExistant::View');
127 eval {$driver_class->init};
128 like($@, qr/Couldn't load the view class/,
129 '... dies if unable to load view class');
133 $driver_class->config->view($view_class);
136 my ($r, $req); # request objects
144 my $mock_driver = Test::MockModule->new($driver_class, no_auto => 1);
146 init => sub {$init++; shift->init_done(1)},
147 get_request => sub {($r, $req) = @_; $called{get_request}++},
148 parse_location => sub {$called{parse_location}++},
149 handler_guts => sub {$called{handler_guts}++; $status},
150 send_output => sub {$called{send_output}++},
153 my $rv = $driver_class->handler();
155 ok($r && $r->isa($driver_class), '... created $r');
156 ok($called{get_request}, '... calls get_request()');
157 ok($called{parse_location}, '... calls parse_location');
158 ok($called{handler_guts}, '... calls handler_guts()');
159 ok($called{send_output}, '... call send_output');
160 is($rv, 0, '... return status (should be ok?)');
161 ok(!$init, "... doesn't call init() if init_done()");
163 ok($r->headers_out && $r->headers_out->isa('Maypole::Headers'),
164 '... populates headers_out() with a Maypole::Headers object');
166 # call again, testing other branches
167 $driver_class->init_done(0);
169 $rv = $driver_class->handler();
170 ok($called{handler_guts} == 2 && $called{send_output} == 1,
171 '... returns early if handler_guts failed');
172 is($rv, -1, '... returning the error code from handler_guts');
174 $driver_class->handler();
175 ok($init && $driver_class->init_done, "... init() called if !init_done()");
182 @{$table_class . "::ISA"} = $model_class;
185 my ($applicable, %called);
187 my $mock_driver = new Test::MockModule($driver_class, no_auto => 1);
188 my $mock_table = new Test::MockModule($table_class, no_auto => 1);
191 #is_applicable => sub {push @{$called{applicable}},\@_; $applicable},
192 is_model_applicable =>
193 sub {push @{$called{applicable}},\@_; $applicable},
194 get_request => sub {($r, $req) = @_},
195 additional_data => sub {$called{additional_data}++},
199 table_process => sub {push @{$called{process}},\@_},
203 class_of => sub {push @{$called{class_of}},\@_; $table_class},
204 process => sub {push @{$called{model_process}}, \@_},
208 process => sub {push @{$called{view_process}}, \@_; $OK}
214 $r->{path} = '/table/action';
217 my $status = $r->handler_guts();
219 is($r->model_class, $table_class, '... sets model_class from table()');
220 ok($called{additional_data}, '... call additional_data()');
221 is($status, $OK, '... return status = OK');
222 ok($called{model_process},
223 '... if_applicable, call model_class->process');
230 $r->{path} = '/table/action';
233 $status = $r->handler_guts();
235 is($r->template, $r->path,
236 '... if ! is_applicable set template() to path()');
237 ok(!$called{model_process},
238 '... !if_applicable, call model_class->process');
239 is_deeply($called{view_process}[0][1], $r,
240 ' ... view_object->process called');
241 is($status, $OK, '... return status = OK');
243 # pre-load some output
247 $r->{output} = 'test';
249 $status = $r->handler_guts();
251 ok(!$called{view_process},
252 '... unless output, call view_object->process to get output');
254 # fail authentication
255 $mock_driver->mock(call_authenticate => sub {$DECLINED});
256 $status = $r->handler_guts();
257 is($status, $DECLINED,
258 '... return DECLINED unless call_authenticate == OK');
260 # ... TODO authentication error handling
261 # ... TODO model error handling
262 # ... TODO view processing error handling
265 # is_model_applicable()
267 $r->config->display_tables([qw(one two)]);
268 $r->config->ok_tables(undef);
269 $r->model_class($table_class);
271 $r->action('unittest');
273 $mock_model->mock('is_public', sub {0});
274 my $true_false = $r->is_model_applicable;
276 '... returns 0 unless model_class->is_public(action)');
277 $mock_model->mock('is_public', sub {$is_public = \@_; 1});
278 $true_false = $r->is_model_applicable;
279 is($true_false, 1, '... returns 1 if table is in ok_tables');
280 is_deeply($is_public, [$r->model_class, 'unittest'],
281 '... calls model_class->is_public with request action');
282 is_deeply($r->config->ok_tables, {one => 1, two => 1},
283 '... config->ok_tables defaults to config->display_tables');
284 delete $r->config->ok_tables->{one};
285 $true_false = $r->is_model_applicable;
286 is($true_false, 0, '... returns 0 unless $r->table is in ok_tables');
289 my $mock_driver = new Test::MockModule($driver_class, no_auto => 1);
290 my $mock_table = new Test::MockModule($table_class, no_auto => 1);
292 # call_authenticate()
296 authenticate => sub {$auth_calls{model_auth} = \@_; $OK}
298 my $status = $r->call_authenticate;
299 is_deeply($auth_calls{model_auth}, [$table_class, $r],
300 '... calls model_class->authenticate if it exists');
301 is($status, $OK, '... and returns its status (OK)');
302 $mock_table->mock(authenticate => sub {$DECLINED});
303 $status = $r->call_authenticate;
304 is($status, $DECLINED, '... or DECLINED, as appropriate');
306 $mock_table->unmock('authenticate');
307 $mock_driver->mock(authenticate => sub {return $DECLINED});
308 $status = $r->call_authenticate;
309 is($status, $DECLINED, '... otherwise it calls authenticte()');
310 $mock_driver->unmock('authenticate');
311 $status = $r->call_authenticate;
312 is($status, $OK, '... the default authenticate is OK');
319 exception => sub {$ex_calls{model_exception} = \@_; $OK}
322 exception => sub {$ex_calls{driver_exception} = \@_; 'X'}
324 my $status = $r->call_exception('ERR');
325 is_deeply($ex_calls{model_exception}, [$table_class, $r, 'ERR'],
326 '... calls model_class->exception if it exists');
327 is($status, $OK, '... and returns its status (OK)');
328 $mock_table->mock(exception => sub {$DECLINED});
329 $status = $r->call_exception('ERR');
330 is_deeply($ex_calls{driver_exception}, [$r, 'ERR'],
331 '... or calls driver->exception if model returns !OK');
332 is($status, 'X', '... and returns the drivers status');
334 $mock_table->unmock('exception');
335 $mock_driver->unmock('exception');
336 $status = $r->call_exception('ERR');
337 is($status, $ERROR, '... the default exception is ERROR');
342 is(Maypole->authenticate(), $OK, '... returns OK');
347 is(Maypole->exception(), $ERROR, '... returns ERROR');
355 is($r->path, 'frontpage', '... path() defaults to "frontpage"');
359 is($r->table, 'table', '... parses "table" from the first part of path');
360 ok(@{$r->args} == 0, '... "args" default to empty list');
362 $r->path('/table/action');
364 ok($r->table eq 'table' && $r->action eq 'action',
365 '... action is parsed from second part of path');
367 $r->path('/table/action/arg1/arg2');
369 is_deeply($r->args, [qw(arg1 arg2)],
370 '... "args" are populated from remaning components');
372 # ... action defaults to index
375 is($r->action, 'index', '... action defaults to index');
378 # make_uri() and make_path() - see pathtools.t
381 # get_template_root()
383 is(Maypole->get_template_root(), '.', '... returns "."');
388 eval {Maypole->parse_location()};
389 like($@, qr/Do not use Maypole directly/, '... croaks - must be overriden');
394 eval {Maypole->send_output};
395 like($@, qr/Do not use Maypole directly/, '... croaks - must be overriden');
400 my $p = { foo => 'bar',
401 quux => [ qw/one two three/ ],
409 is_deeply( [keys %$p], [$r->param] );
411 cmp_ok( $r->param('foo'), eq => 'bar' );
412 cmp_ok( $r->param('num'), '==' => 3 );
413 cmp_ok( $r->param('zero'), '==' => 0 );
415 ok( ! defined $r->param('buz') );
417 # scalar context returns the 1st value, not a ref
418 cmp_ok( scalar $r->param('quux'), eq => 'one' );
419 is_deeply( [$r->param('quux')], [ qw/one two three/ ] );
421 $r->param(foo => 'booze');
422 cmp_ok( $r->param('foo'), 'eq', 'booze' );
424 $r->param(foo => undef);
425 ok( ! defined $r->param('foo') );
427 # cannot introduce new keys
428 $r->param(new => 'sox');
429 ok( ! defined $r->param('new') );