Galileo/Command/setup.pm untested

filter: Statementstmt 0.0% 0 / 89 Branchbran 0.0% 0 / 16 Conditioncond 0.0% 0 / 6 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 0.0% 0 / 5 Podpod 0.0% 0 / 1 totaltotal 0.0% 0 / 119 CC 9 SCAR 41.4
CC
9
Cov
0%
run
90.0
BEGIN
2.0
BEGIN
2.0
CRAP 62.9
1 package Galileo::Command::setup;
2 use Mojo::Base 'Mojolicious::Command';
3  
4 use Mojolicious::Command::daemon;
5  
6 use Mojolicious::Routes;
7 use Mojo::JSON 'j';
8  
9 has description => "Configure your Galileo CMS via a web interface\n";
10  
11 sub run {
12 my ($self, @args) = @_;
13  
14 my $app = $self->app;
15 $app->plugin('Galileo::Plugin::Deploy'); # provides dh helper
16  
17 my $r = Mojolicious::Routes->new;
18 $app->routes($r); # remove all routes
19  
20 $app->helper( 'control_group' => sub {
21 my $self = shift;
22 my $contents = pop;
23 my %args = @_;
24  
25 $self->render_to_string(
26 template => 'setup/control_group',
27 'control_group.contents' => ref $contents ? $contents->() : $contents,
28 'control_group.label' => $args{label} || '',
29 'control_group.for' => $args{for} || '',
30 );
31 });
32  
33 $r->any( '/' => 'setup/welcome' );
34 $r->any( '/configure' => 'setup/configure' );
35 $r->any( '/store_config' => sub {
36 my $self = shift;
37 my $names = $self->req->params->names;
38  
39 # map JSON keys to Perl data
40 my %params = map { $_ => scalar $self->param($_) } @$names;
41 foreach my $key ( qw/extra_css extra_js extra_static_paths secrets db_options pagedown_extra_options/ ) {
42 $params{$key} = j($params{$key});
43 }
44  
45 $self->app->config_file->spurt($self->dumper(\%params));
46  
47 $self->app->load_config;
48 $self->humane_flash( 'Configuration saved' );
49 $self->redirect_to('/');
50 });
51  
52 $r->any( '/database' => sub {
53 my $self = shift;
54  
55 my $dh = $self->dh;
56  
57 # Nothing installed
58 my $installed = $dh->installed_version;
59 unless ( $installed ) {
60 return $self->render( 'setup/database' );
61 }
62  
63 # Something is installed, check for upgrades
64 my $available = $dh->schema->schema_version;
65  
66 # Do nothing if version is current
67 if ( $installed == $available ) {
68 $self->flash( 'galileo.message' => 'Database schema is current.' );
69 } else {
70 $dh->do_upgrade;
71 $self->flash( 'galileo.message' => "Upgrade database $installed -> $available." );
72 }
73  
74 $self->redirect_to('finish');
75 });
76  
77 $r->any( '/database_install' => sub {
78 my $self = shift;
79 my $pw1 = $self->param('pw1');
80 my $pw2 = $self->param('pw2');
81 unless ( $pw1 eq $pw2 ) {
82 $self->humane_flash( q{Passwords don't match!} );
83 return $self->redirect_to('database');
84 }
85  
86 my $dh = $self->dh;
87  
88 eval {
89 my $user = $self->param('user') || die "Admin Username is required\n";
90 my $full = $self->param('full');
91 $dh->do_install;
92 $dh->inject_sample_data($user, $pw1, $full);
93 };
94  
95 if ($@) {
96 my $error = "$@";
97 chomp $error;
98 $self->humane_flash( $error );
99 return $self->redirect_to('database');
100 }
101  
102 $self->flash( 'galileo.message' => 'Database has been setup.' );
103 $self->redirect_to('finish');
104 });
105  
106 $r->any('/finish' => sub {
107 my $self = shift;
108 my $message = $self->flash( 'galileo.message' );
109  
110 my $dh = $self->dh;
111 my $installed = $dh->installed_version;
112 my $available = $dh->schema->schema_version;
113 my $has_admin = $dh->has_admin_user;
114  
115 if ($installed) {
116 unless ($has_admin) {
117 $message .= ' No administration user was created.';
118 }
119 unless ($installed == $available) {
120 $message .= " Installed database version ($installed) is older than the newest available ($available).";
121 }
122 $self->stash( 'galileo.success' => 1 );
123 $self->stash( 'galileo.message' => $message );
124 } else {
125 $self->stash( 'galileo.success' => 0 );
126 $self->stash(
127 'galileo.message' =>
128 'It does not appear that your database is setup, please rerun the setup utility'
129 );
130 }
131  
132 $self->humane_stash( 'Goodbye' );
133 $self->render('setup/finish');
134 $self->tx->on( finish => sub { exit } );
135 });
136  
137 $self->Mojolicious::Command::daemon::run(@args);
138 }
139  
140 1;
141