]> git.decadent.org.uk Git - maypole.git/blob - t/maypole.t
4a0cd0234fec89e718f0ed2c0ad24eff1d885383
[maypole.git] / t / maypole.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Test::More tests => 84;
5 use Test::MockModule;
6
7 # module compilation
8 require_ok('Maypole');
9
10 # loaded modules
11 {
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');
19 }
20
21 my $OK       = Maypole::Constants::OK();
22 my $DECLINED = Maypole::Constants::DECLINED();
23 my $ERROR    = Maypole::Constants::ERROR();
24
25 # Maypole API
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
34               start_request_hook
35               session get_session
36               /;
37                 
38 can_ok(Maypole => @API);
39
40 ok( ! UNIVERSAL::can(Maypole => 'is_applicable'), 'no is_applicable() method' );
41
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');
45
46 # simple test class that inherits from Maypole
47 {
48     package MyDriver;
49     @MyDriver::ISA = 'Maypole';
50     @MyDriver::VERSION = 1;
51 }
52
53 # back to package main;
54 my $driver_class = 'MyDriver';
55
56 # subclass inherits API
57 can_ok($driver_class => @API);
58
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';
63
64 my $mock_model = Test::MockModule->new($model_class);
65 $mock_model->mock(
66     require        => sub {$required{+shift} = 1},
67     setup_database => sub {
68         push @db_args, \@_;
69         $_[1]->{classes} = ["$model_class\::One", "$model_class\::Two"];
70         $_[1]->{tables}  = [qw(one two)];
71     },
72     adopt          => sub {push @adopted, \@_},
73 );
74
75
76 # setup
77 {
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
85
86     $driver_class->setup('dbi:foo'); 
87     
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');
102     
103     # cleanup
104     $@ = undef; 
105     $driver_class->config->model($model_class);
106 }
107
108 # Mock the view class
109 my $view_class = 'Maypole::View::TT';
110 my $mock_view = Test::MockModule->new($view_class);
111 $mock_view->mock(
112     new     => sub {bless{}, shift},
113     require => sub {$required{+shift} = 1},
114 );
115
116 # init()
117 {
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');
130         
131     # cleanup
132     $@ = undef; 
133     $driver_class->config->view($view_class);
134 }
135
136 my ($r, $req); # request objects
137
138 # handler()
139 {
140     my $init = 0;
141     my $status = 0;
142     my %called;
143     
144     my $mock_driver = Test::MockModule->new($driver_class, no_auto => 1);
145     $mock_driver->mock(
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}++},
151     );
152
153     my $rv = $driver_class->handler();
154     
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()");
162     
163     ok($r->headers_out && $r->headers_out->isa('Maypole::Headers'),
164        '... populates headers_out() with a Maypole::Headers object');
165        
166     # call again, testing other branches
167     $driver_class->init_done(0);
168     $status = -1;
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');
173     
174     $driver_class->handler();
175     ok($init && $driver_class->init_done, "... init() called if !init_done()");
176 }
177
178 {
179     # handler_guts()
180     {
181         no strict 'refs';
182         @{$table_class . "::ISA"} = $model_class;
183     }
184
185     my ($applicable, %called);
186     
187     my $mock_driver = new Test::MockModule($driver_class, no_auto => 1);
188     my $mock_table  = new Test::MockModule($table_class, no_auto => 1);
189     
190     $mock_driver->mock(
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}++},
196     );
197     
198     $mock_table->mock(
199         table_process   => sub {push @{$called{process}},\@_},
200     );
201     
202     $mock_model->mock(
203         class_of        => sub {push @{$called{class_of}},\@_; $table_class},
204         process         => sub {push @{$called{model_process}}, \@_},
205     );
206     
207     $mock_view->mock(
208         process         => sub {push @{$called{view_process}}, \@_; $OK}
209     );
210     
211     # allow request
212     $applicable = 1;
213     
214     $r->{path} = '/table/action';    
215     $r->parse_path;
216     
217     my $status = $r->handler_guts();
218
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');
224
225     # decline request
226     %called = ();
227     
228     $applicable = 0;
229     
230     $r->{path} = '/table/action';
231     $r->parse_path;
232     
233     $status = $r->handler_guts();
234     
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');
242
243     # pre-load some output
244     %called = ();
245     
246     $r->parse_path;
247     $r->{output} = 'test';
248     
249     $status = $r->handler_guts();
250     
251     ok(!$called{view_process},
252        '... unless output, call view_object->process to get output');
253
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');
259
260     # ... TODO authentication error handling
261     # ... TODO model error handling
262     # ... TODO view processing error handling
263 }
264
265 # is_model_applicable()
266 {
267     $r->config->display_tables([qw(one two)]);
268     $r->config->ok_tables(undef);
269     $r->model_class($table_class);
270     $r->table('one');
271     $r->action('unittest');
272     my $is_public;
273     $mock_model->mock('is_public', sub {0});
274     my $true_false = $r->is_model_applicable;
275     is($true_false, 0,
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');
287 }
288
289 my $mock_driver = new Test::MockModule($driver_class, no_auto => 1);
290 my $mock_table  = new Test::MockModule($table_class, no_auto => 1);
291
292 # call_authenticate()
293 {
294     my %auth_calls;
295     $mock_table->mock(
296         authenticate => sub {$auth_calls{model_auth} = \@_; $OK}
297     );
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');
305     
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');
313 }
314
315 # call_exception()
316 {
317     my %ex_calls;
318     $mock_table->mock(
319         exception => sub {$ex_calls{model_exception} = \@_; $OK}
320     );
321     $mock_driver->mock(
322         exception => sub {$ex_calls{driver_exception} = \@_; 'X'}
323     );
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');
333     
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');
338 }
339
340 # authenticate()
341 {
342     is(Maypole->authenticate(), $OK, '... returns OK');
343 }
344
345 # exception()
346 {
347     is(Maypole->exception(), $ERROR, '... returns ERROR');
348 }
349
350 # parse_path()
351 {
352     $r->path(undef);
353     
354     $r->parse_path;
355     is($r->path, 'frontpage', '... path() defaults to "frontpage"');
356     
357     $r->path('/table');
358     $r->parse_path;
359     is($r->table, 'table', '... parses "table" from the first part of path');
360     ok(@{$r->args} == 0, '... "args" default to empty list');
361     
362     $r->path('/table/action');
363     $r->parse_path;
364     ok($r->table eq 'table' && $r->action eq 'action',
365     '... action is parsed from second part of path');
366     
367     $r->path('/table/action/arg1/arg2');
368     $r->parse_path;
369     is_deeply($r->args, [qw(arg1 arg2)],
370     '... "args" are populated from remaning components');
371     
372     # ... action defaults to index
373     $r->path('/table');
374     $r->parse_path;
375     is($r->action, 'index', '... action defaults to index');
376 }
377
378 # make_uri() and make_path() - see pathtools.t
379
380
381 # get_template_root()
382 {
383     is(Maypole->get_template_root(), '.', '... returns "."');
384 }
385
386 # parse_location()
387 {
388     eval {Maypole->parse_location()};
389     like($@, qr/Do not use Maypole directly/, '... croaks - must be overriden');
390 }
391
392 # send_output()
393 {
394     eval {Maypole->send_output};
395     like($@, qr/Do not use Maypole directly/, '... croaks - must be overriden');
396 }
397
398 # param()
399 {
400         my $p = { foo => 'bar', 
401                   quux => [ qw/one two three/ ],
402                   buz => undef,
403                   num => 3,
404                   zero => 0,
405                   };
406                   
407         $r->{params} = $p;
408         
409         is_deeply( [keys %$p], [$r->param] );
410         
411         cmp_ok( $r->param('foo'), eq => 'bar' );
412         cmp_ok( $r->param('num'), '==' => 3 );
413         cmp_ok( $r->param('zero'), '==' => 0 );
414         
415         ok( ! defined $r->param('buz') );
416         
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/ ] );
420         
421         $r->param(foo => 'booze');
422         cmp_ok( $r->param('foo'), 'eq', 'booze' );
423         
424         $r->param(foo => undef);
425         ok( ! defined $r->param('foo') );
426         
427         # cannot introduce new keys
428         $r->param(new => 'sox');
429         ok( ! defined $r->param('new') );
430 }
431