]> git.decadent.org.uk Git - maypole.git/blob - t/maypole.t
Implemented daveh's fixes for is_applicable() -
[maypole.git] / t / maypole.t
1 #!/usr/bin/perl
2 use strict;
3 use warnings;
4 use Test::More tests => 74;
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 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 init handler handler_guts
30               call_authenticate call_exception additional_data
31               authenticate exception parse_path get_template_root get_request
32               parse_location send_output
33               /;
34                 
35 can_ok(Maypole => @API);
36
37 ok( ! UNIVERSAL::can(Maypole => 'is_applicable'), 'no is_applicable() method' );
38
39 ok(Maypole->config->isa('Maypole::Config'), 'config is a Maypole::Config object');
40 ok(! Maypole->init_done, '... which is false by default');
41 is(Maypole->view_object, undef, '... which is undefined');
42
43 # simple test class that inherits from Maypole
44 {
45     package MyDriver;
46     @MyDriver::ISA = 'Maypole';
47     @MyDriver::VERSION = 1;
48 }
49
50 # back to package main;
51 my $driver_class = 'MyDriver';
52
53 # subclass inherits API
54 can_ok($driver_class => @API);
55
56 # Mock the model class
57 my (%required, @db_args, @adopted);
58 my $model_class = 'Maypole::Model::CDBI';
59 my $table_class = $driver_class . '::One';
60
61 my $mock_model = Test::MockModule->new($model_class);
62 $mock_model->mock(
63     require        => sub {$required{+shift} = 1},
64     setup_database => sub {
65         push @db_args, \@_;
66         $_[1]->{classes} = ["$model_class\::One", "$model_class\::Two"];
67         $_[1]->{tables}  = [qw(one two)];
68     },
69     adopt          => sub {push @adopted, \@_},
70 );
71
72
73 # setup
74 {
75     # 2.11 - removed tests to check the installed handler was a different ref after setup().
76     # The handler tests were testing Maypole's old (pre 2.11) method of importing handler() 
77     # into the subclass - it works via standard inheritance now, by setting the 'method' 
78     # attribute on Maypole::handler(). The reason the handlers were different 
79     # was because setup() would create a new anonymous ref to Maypole::handler(), and install 
80     # that - i.e. it installed the same code, but in a different ref, so they tested unequal
81     # although they referred to the same code
82
83     $driver_class->setup('dbi:foo'); 
84     
85     ok($required{$model_class}, '... requires model class');
86     is($driver_class->config->model(),
87         'Maypole::Model::CDBI', '... default model is CDBI');
88     is(@db_args, 1, '... calls model->setup_database');
89     like(join (' ', @{$db_args[0]}),
90         qr/$model_class Maypole::Config=\S* $driver_class dbi:foo/,
91         '... setup_database passed setup() args');
92     is(@adopted, 2, '... calls model->adopt foreach class in the model');
93     ok($adopted[0][0]->isa($model_class),
94     '... sets up model subclasses to inherit from model');
95     $driver_class->config->model('NonExistant::Model');
96     eval {$driver_class->setup};
97     like($@, qr/Couldn't load the model class/,
98         '... dies if unable to load model class');
99     
100     # cleanup
101     $@ = undef; 
102     $driver_class->config->model($model_class);
103 }
104
105 # Mock the view class
106 my $view_class = 'Maypole::View::TT';
107 my $mock_view = Test::MockModule->new($view_class);
108 $mock_view->mock(
109     new     => sub {bless{}, shift},
110     require => sub {$required{+shift} = 1},
111 );
112
113 # init()
114 {
115     $driver_class->init();
116     ok($required{$view_class}, '... requires the view class');
117     is($driver_class->config->view, $view_class, '... the default view class is TT');
118     is(join(' ', @{$driver_class->config->display_tables}), 'one two',
119         '... config->display_tables defaults to all tables');
120     ok($driver_class->view_object->isa($view_class),
121         '... creates an instance of the view object');
122     ok($driver_class->init_done, '... sets init_done');
123     $driver_class->config->view('NonExistant::View');
124     eval {$driver_class->init};
125     like($@, qr/Couldn't load the view class/,
126         '... dies if unable to load view class');
127         
128     # cleanup
129     $@ = undef; 
130     $driver_class->config->view($view_class);
131 }
132
133 my ($r, $req); # request objects
134
135 # handler()
136 {
137     my $init = 0;
138     my $status = 0;
139     my %called;
140     
141     my $mock_driver = Test::MockModule->new($driver_class, no_auto => 1);
142     $mock_driver->mock(
143         init           => sub {$init++; shift->init_done(1)},
144         get_request    => sub {($r, $req) = @_; $called{get_request}++},
145         parse_location => sub {$called{parse_location}++},
146         handler_guts   => sub {$called{handler_guts}++; $status},
147         send_output    => sub {$called{send_output}++},
148     );
149
150     my $rv = $driver_class->handler();
151     
152     ok($r && $r->isa($driver_class), '... created $r');
153     ok($called{get_request}, '... calls get_request()');
154     ok($called{parse_location}, '... calls parse_location');
155     ok($called{handler_guts}, '... calls handler_guts()');
156     ok($called{send_output}, '... call send_output');
157     is($rv, 0, '... return status (should be ok?)');
158     ok(!$init, "... doesn't call init() if init_done()");
159     
160     ok($r->headers_out && $r->headers_out->isa('Maypole::Headers'),
161        '... populates headers_out() with a Maypole::Headers object');
162        
163     # call again, testing other branches
164     $driver_class->init_done(0);
165     $status = -1;
166     $rv = $driver_class->handler();
167     ok($called{handler_guts} == 2 && $called{send_output} == 1,
168        '... returns early if handler_guts failed');
169     is($rv, -1, '... returning the error code from handler_guts');
170     
171     $driver_class->handler();
172     ok($init && $driver_class->init_done, "... init() called if !init_done()");
173 }
174
175 {
176     # handler_guts()
177     {
178         no strict 'refs';
179         @{$table_class . "::ISA"} = $model_class;
180     }
181
182     my ($applicable, %called);
183     
184     my $mock_driver = new Test::MockModule($driver_class, no_auto => 1);
185     my $mock_table  = new Test::MockModule($table_class, no_auto => 1);
186     
187     $mock_driver->mock(
188         #is_applicable   => sub {push @{$called{applicable}},\@_; $applicable},
189         is_model_applicable   => 
190             sub {push @{$called{applicable}},\@_; $applicable},
191         get_request     => sub {($r, $req) = @_},
192         additional_data => sub {$called{additional_data}++},
193     );
194     
195     $mock_table->mock(
196         table_process   => sub {push @{$called{process}},\@_},
197     );
198     
199     $mock_model->mock(
200         class_of        => sub {push @{$called{class_of}},\@_; $table_class},
201         process         => sub {push @{$called{model_process}}, \@_},
202     );
203     
204     $mock_view->mock(
205         process         => sub {push @{$called{view_process}}, \@_; $OK}
206     );
207     
208     # allow request
209     $applicable = 1;
210     
211     $r->{path} = '/table/action';    
212     $r->parse_path;
213     
214     my $status = $r->handler_guts();
215
216     is($r->model_class, $table_class, '... sets model_class from table()');
217     ok($called{additional_data}, '... call additional_data()');
218     is($status, $OK, '... return status = OK');
219     ok($called{model_process},
220        '... if_applicable, call model_class->process');
221
222     # decline request
223     %called = ();
224     
225     $applicable = 0;
226     
227     $r->{path} = '/table/action';
228     $r->parse_path;
229     
230     $status = $r->handler_guts();
231     
232     is($r->template, $r->path,
233        '... if ! is_applicable set template() to path()');
234     ok(!$called{model_process},
235        '... !if_applicable, call model_class->process');
236     is_deeply($called{view_process}[0][1], $r,
237               ' ... view_object->process called');
238     is($status, $OK, '... return status = OK');
239
240     # pre-load some output
241     %called = ();
242     
243     $r->parse_path;
244     $r->{output} = 'test';
245     
246     $status = $r->handler_guts();
247     
248     ok(!$called{view_process},
249        '... unless output, call view_object->process to get output');
250
251     # fail authentication
252     $mock_driver->mock(call_authenticate => sub {$DECLINED});
253     $status = $r->handler_guts();
254     is($status, $DECLINED,
255        '... return DECLINED unless call_authenticate == OK');
256
257     # ... TODO authentication error handling
258     # ... TODO model error handling
259     # ... TODO view processing error handling
260 }
261
262 # is_model_applicable()
263 {
264     $r->config->display_tables([qw(one two)]);
265     $r->config->ok_tables(undef);
266     $r->model_class($table_class);
267     $r->table('one');
268     $r->action('unittest');
269     my $is_public;
270     $mock_model->mock('is_public', sub {0});
271     my $true_false = $r->is_model_applicable;
272     is($true_false, 0,
273     '... returns 0 unless model_class->is_public(action)');
274     $mock_model->mock('is_public', sub {$is_public = \@_; 1});
275     $true_false = $r->is_model_applicable;
276     is($true_false, 1, '... returns 1 if table is in ok_tables');
277     is_deeply($is_public, [$r->model_class, 'unittest'],
278             '... calls model_class->is_public with request action');
279     is_deeply($r->config->ok_tables, {one => 1, two => 1},
280             '... config->ok_tables defaults to config->display_tables');
281     delete $r->config->ok_tables->{one};
282     $true_false = $r->is_model_applicable;
283     is($true_false, 0, '... returns 0 unless $r->table is in ok_tables');
284 }
285
286 my $mock_driver = new Test::MockModule($driver_class, no_auto => 1);
287 my $mock_table  = new Test::MockModule($table_class, no_auto => 1);
288
289 # call_authenticate()
290 {
291     my %auth_calls;
292     $mock_table->mock(
293         authenticate => sub {$auth_calls{model_auth} = \@_; $OK}
294     );
295     my $status = $r->call_authenticate;
296     is_deeply($auth_calls{model_auth}, [$table_class, $r],
297             '... calls model_class->authenticate if it exists');
298     is($status, $OK, '... and returns its status (OK)');
299     $mock_table->mock(authenticate => sub {$DECLINED});
300     $status = $r->call_authenticate;
301     is($status, $DECLINED, '... or DECLINED, as appropriate');
302     
303     $mock_table->unmock('authenticate');
304     $mock_driver->mock(authenticate => sub {return $DECLINED});
305     $status = $r->call_authenticate;
306     is($status, $DECLINED, '... otherwise it calls authenticte()');
307     $mock_driver->unmock('authenticate');
308     $status = $r->call_authenticate;
309     is($status, $OK, '... the default authenticate is OK');
310 }
311
312 # call_exception()
313 {
314     my %ex_calls;
315     $mock_table->mock(
316         exception => sub {$ex_calls{model_exception} = \@_; $OK}
317     );
318     $mock_driver->mock(
319         exception => sub {$ex_calls{driver_exception} = \@_; 'X'}
320     );
321     my $status = $r->call_exception('ERR');
322     is_deeply($ex_calls{model_exception}, [$table_class, $r, 'ERR'],
323             '... calls model_class->exception if it exists');
324     is($status, $OK, '... and returns its status (OK)');
325     $mock_table->mock(exception => sub {$DECLINED});
326     $status = $r->call_exception('ERR');
327     is_deeply($ex_calls{driver_exception}, [$r, 'ERR'],
328             '... or calls driver->exception if model returns !OK');
329     is($status, 'X', '... and returns the drivers status');
330     
331     $mock_table->unmock('exception');
332     $mock_driver->unmock('exception');
333     $status = $r->call_exception('ERR');
334     is($status, $ERROR, '... the default exception is ERROR');
335 }
336
337 # authenticate()
338 {
339     is(Maypole->authenticate(), $OK, '... returns OK');
340 }
341
342 # exception()
343 {
344     is(Maypole->exception(), $ERROR, '... returns ERROR');
345 }
346
347 # parse_path()
348 {
349     $r->path(undef);
350     $r->parse_path;
351     is($r->path, 'frontpage', '... path() defaults to "frontpage"');
352     
353     $r->path('/table');
354     $r->parse_path;
355     is($r->table, 'table', '... parses "table" from the first part of path');
356     ok(@{$r->args} == 0, '... "args" default to empty list');
357     
358     $r->path('/table/action');
359     $r->parse_path;
360     ok($r->table eq 'table' && $r->action eq 'action',
361     '... action is parsed from second part of path');
362     
363     $r->path('/table/action/arg1/arg2');
364     $r->parse_path;
365     is_deeply($r->args, [qw(arg1 arg2)],
366     '... "args" are populated from remaning components');
367     
368     # ... action defaults to index
369     $r->path('/table');
370     $r->parse_path;
371     is($r->action, 'index', '... action defaults to index');
372 }
373
374 # get_template_root()
375 {
376     is(Maypole->get_template_root(), '.', '... returns "."');
377 }
378
379 # parse_location()
380 {
381     eval {Maypole->parse_location()};
382     like($@, qr/Do not use Maypole directly/, '... croaks - must be overriden');
383 }
384
385 # send_output()
386 {
387     eval {Maypole->send_output};
388     like($@, qr/Do not use Maypole directly/, '... croaks - must be overriden');
389 }