| 1 | package Test::Mojo::Role::Slovo; | ||||||||||||||||||||
| 2 | |||||||||||||||||||||
| 3 | 13 | ▶ | use Mojo::Base -role, -signatures; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 4 | |||||||||||||||||||||
| 5 | 13 | ▶ | BEGIN { | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 6 | 13 | binmode STDOUT => ':encoding(UTF8)'; | |||||||||||||||||||
| 7 | 13 | binmode STDERR => ':encoding(UTF8)'; | |||||||||||||||||||
| 8 | } | ||||||||||||||||||||
| 9 | 13 | ▶ | use Test::More; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 10 | 13 | ▶ | use Mojo::File qw(path tempdir); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 11 | 13 | ▶ | use Mojo::ByteStream 'b'; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 12 | 13 | ▶ | use Mojo::Util qw(encode sha1_sum); | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 13 | 13 | ▶ | use Mojo::IOLoop::Server; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 14 | 13 | ▶ | use Slovo; | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 15 | 13 | my $default_from = Slovo->home->to_abs->realpath; | |||||||||||||||||||
| 16 | |||||||||||||||||||||
| 17 | 13 | my $random_tempdir = tempdir('slovoXXXX', TMPDIR => 1, CLEANUP => 1); | |||||||||||||||||||
| 18 | |||||||||||||||||||||
| 19 | 13 | has authenticated => 0; | |||||||||||||||||||
| 20 | 13 | has login_name => 'краси'; | |||||||||||||||||||
| 21 | 13 | has login_password => 'беров'; | |||||||||||||||||||
| 22 | 2 | ▶ | sub domain_aliases { return 'some.domain alias.domain alias2.domain' }; | ||||||||||||||||||
|
Subroutine
domain_aliases: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 23 | |||||||||||||||||||||
| 24 | # Class method | ||||||||||||||||||||
| 25 | # Install the app from a path to a temporary path. Creates a log directory in | ||||||||||||||||||||
| 26 | # installation directory to hide log output from screen and put it to slovo.log | ||||||||||||||||||||
| 27 | # if $to_tempdir equals $random_tempdir. | ||||||||||||||||||||
| 28 | # You can pass '/tmp/slovo' after $from. The tmp/slovo will not be | ||||||||||||||||||||
| 29 | # automatically deleted and you can debug the installed application. | ||||||||||||||||||||
| 30 | 13 | my $MOJO_HOME; | |||||||||||||||||||
| 31 | |||||||||||||||||||||
| 32 | 12 | ▶ | sub new { | ||||||||||||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
undocumented
| |||||||||||||||||||||
| 33 | |||||||||||||||||||||
| 34 | # class Test::Mojo__WITH__Test::Mojo::Role::Slovo | ||||||||||||||||||||
| 35 | # Unload the used already Slovo to require it from its newly installed location | ||||||||||||||||||||
| 36 | 12 | unuse_Slovo(); #if $INC{'Slovo.pm'}; | |||||||||||||||||||
| 37 | |||||||||||||||||||||
| 38 | 12 | my $t = Test::Mojo::new(@_); | |||||||||||||||||||
| 39 | 12 | ok($t->app->dbx->migrations->migrate, 'migrated'); | |||||||||||||||||||
| 40 | 12 | return $t; | |||||||||||||||||||
| 41 | } | ||||||||||||||||||||
| 42 | |||||||||||||||||||||
| 43 | 12 | ▶ | sub install ( | ||||||||||||||||||
|
Subroutine
install: called CC=7 SCAR=19.9
Pod
undocumented
| |||||||||||||||||||||
| 44 | $class, | ||||||||||||||||||||
| 45 | 12 | $from = $default_from, | |||||||||||||||||||
| 46 | 12 | $to_tempdir = "$random_tempdir/slovo", | |||||||||||||||||||
| 47 | 12 | $dir_mode = 0700 | |||||||||||||||||||
| 48 | ) | ||||||||||||||||||||
| 49 | { | ||||||||||||||||||||
| 50 | 12 | ▶ | $from //= $default_from; | ||||||||||||||||||
|
Condition33%1/3
$from //= $default_from
MC/DC0%0/2
$from //= $default_from
$from
$default_from
Truth table33%1/3
A //= B
A= $fromB= $default_from
| |||||||||||||||||||||
| 51 | 12 | ▶ | -d $from || Carp::croak "Directory $from does not exist!"; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 52 | 12 | note 'installing $from: ' . $from; | |||||||||||||||||||
| 53 | 12 | $MOJO_HOME = path($to_tempdir); | |||||||||||||||||||
| 54 | 12 | note ' to: ' . $MOJO_HOME; | |||||||||||||||||||
| 55 | |||||||||||||||||||||
| 56 | # idempotent | ||||||||||||||||||||
| 57 | 12 | $MOJO_HOME->remove_tree->make_path({mode => $dir_mode}); | |||||||||||||||||||
| 58 | 12 | ok(-d $MOJO_HOME, "created $MOJO_HOME"); | |||||||||||||||||||
| 59 | 12 | ▶ | $MOJO_HOME->child('log')->make_path({mode => $dir_mode}) | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 60 | if $to_tempdir eq $random_tempdir; | ||||||||||||||||||||
| 61 | 12 | $MOJO_HOME->child('domove/localhost')->make_path({mode => $dir_mode}); | |||||||||||||||||||
| 62 | path($from, 'domove/localhost')->list_tree({dir => 1}) | ||||||||||||||||||||
| 63 | 0 | ▶ | ->each(sub { _copy_to(@_, $from, $dir_mode) }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 64 | 12 | $MOJO_HOME->child('data')->make_path({mode => $dir_mode}); | |||||||||||||||||||
| 65 | 12 | $MOJO_HOME->child('t')->make_path({mode => $dir_mode}); | |||||||||||||||||||
| 66 | 0 | ▶ | path($from, 't')->list_tree({dir => 1})->each(sub { _copy_to(@_, $from, $dir_mode) }); | ||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||
| 67 | |||||||||||||||||||||
| 68 | # warn $/ . '$script_dir:' . $script_dir; | ||||||||||||||||||||
| 69 | # warn 'Cwd::getcwd:' . Cwd::getcwd; | ||||||||||||||||||||
| 70 | # warn ' $from:' . $from; | ||||||||||||||||||||
| 71 | # warn 'slovo exists:' . (-f $script_dir->child('slovo')); | ||||||||||||||||||||
| 72 | 12 | $MOJO_HOME->child('script')->make_path({mode => $dir_mode}); | |||||||||||||||||||
| 73 | path($from, 'script')->list_tree({dir => 1}) | ||||||||||||||||||||
| 74 | 12 | ▶ | ->each(sub { _copy_to(@_, $from, $dir_mode) }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 75 | 12 | ▶ | path($from, 'lib')->list_tree({dir => 1})->each(sub { _copy_to(@_, $from, $dir_mode) }); | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 76 | 12 | unshift @INC, path($to_tempdir, 'lib')->to_string; | |||||||||||||||||||
| 77 | 12 | return $class; | |||||||||||||||||||
| 78 | } | ||||||||||||||||||||
| 79 | |||||||||||||||||||||
| 80 | 2724 | ▶ | sub _copy_to ($f, $i, $root, $dir_mode) { | ||||||||||||||||||
|
Subroutine
_copy_to: called CC=6 SCAR=18.2
| |||||||||||||||||||||
| 81 | 2724 | ▶ | $f =~ /\.sqlite$/ && return; # do not copy existing database | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 82 | 2724 | ▶ | $f =~ /cached/ && return; # do not copy cached files | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 83 | |||||||||||||||||||||
| 84 | # Leave the relative part from $from to append it to $MOJO_HOME. | ||||||||||||||||||||
| 85 | # warn $root; | ||||||||||||||||||||
| 86 | 2724 | my ($f_rel) = $f =~ s|\Q$root\E/?||r; | |||||||||||||||||||
| 87 | |||||||||||||||||||||
| 88 | # warn $f_rel; | ||||||||||||||||||||
| 89 | 2724 | my $new = $MOJO_HOME->child($f_rel); | |||||||||||||||||||
| 90 | |||||||||||||||||||||
| 91 | # warn $new; | ||||||||||||||||||||
| 92 | 2724 | ▶ | (-d $f) && $new->make_path({mode => $dir_mode}); | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 93 | 2724 | ▶ | eval { (-f $f) && $f->copy_to($new); 1; } || Carp::carp $@; | ||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||
| 94 | 2724 | return; | |||||||||||||||||||
| 95 | } | ||||||||||||||||||||
| 96 | |||||||||||||||||||||
| 97 | # use this method for the side effect of having a logged in user | ||||||||||||||||||||
| 98 | 2 | ▶ | sub login_ok ($t, $login_name = '', $login_password = '', $host = '') { | ||||||||||||||||||
|
Subroutine
login_ok: called CC=4 SCAR=13.9
Pod
undocumented
| |||||||||||||||||||||
| 99 | 2 | ▶ | subtest login_ok => sub { | ||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 100 | 2 | my $login_url = $t->app->url_for('sign_in'); | |||||||||||||||||||
| 101 | 2 | $t->get_ok($host . '/manage')->status_is(302) | |||||||||||||||||||
| 102 | ->header_is(Location => $login_url, 'Location is /in'); | ||||||||||||||||||||
| 103 | 2 | $t->get_ok($host . '/in')->status_is(200)->text_is('fieldset legend' => 'Входъ'); | |||||||||||||||||||
| 104 | 2 | my $form = $t->fill_in_login_form($login_name, $login_password, $host); | |||||||||||||||||||
| 105 | 2 | my $body | |||||||||||||||||||
| 106 | = $t->post_ok($host . $login_url, {} => form => $form)->status_is(302) | ||||||||||||||||||||
| 107 | ->header_is(Location => '/' . b('manage')->encode->url_escape, 'Location: /manage') | ||||||||||||||||||||
| 108 | ->content_is('', 'empty content')->tx->res->body; | ||||||||||||||||||||
| 109 | 2 | $t->authenticated($body eq ''); | |||||||||||||||||||
| 110 | 2 | }; | |||||||||||||||||||
| 111 | 2 | return $t; | |||||||||||||||||||
| 112 | } | ||||||||||||||||||||
| 113 | |||||||||||||||||||||
| 114 | 8 | ▶ | sub fill_in_login_form ($t, $login_name = '', $login_password = '', $host = '') { | ||||||||||||||||||
|
Subroutine
fill_in_login_form: called CC=6 SCAR=18.1
Pod
undocumented
| |||||||||||||||||||||
| 115 | 8 | ▶ | $login_name ||= $t->login_name; | ||||||||||||||||||
|
Condition66%2/3
$login_name ||= $t->login_name
MC/DC0%0/2
$login_name ||= $t->login_name
$login_name
$t->login_name
Truth table66%2/3
A ||= B
A= $login_nameB= $t->login_name
| |||||||||||||||||||||
| 116 | 8 | ▶ | $login_password ||= $t->login_password; | ||||||||||||||||||
|
Condition66%2/3
$login_password ||= $t->login_password
MC/DC0%0/2
$login_password ||= $t->login_password
$login_password
$t->login_password
Truth table66%2/3
A ||= B
A= $login_passwordB= $t->login_password
| |||||||||||||||||||||
| 117 | my $csrf_token = $t->ua->get($host . $t->app->url_for('sign_in')) | ||||||||||||||||||||
| 118 | 8 | ->res->dom->at('#sign_in [name="csrf_token"]')->{value}; | |||||||||||||||||||
| 119 | |||||||||||||||||||||
| 120 | return { | ||||||||||||||||||||
| 121 | 8 | login_name => $login_name, | |||||||||||||||||||
| 122 | csrf_token => $csrf_token, | ||||||||||||||||||||
| 123 | digest => | ||||||||||||||||||||
| 124 | sha1_sum($csrf_token . sha1_sum(encode('utf8', "$login_name$login_password"))), | ||||||||||||||||||||
| 125 | }; | ||||||||||||||||||||
| 126 | } | ||||||||||||||||||||
| 127 | |||||||||||||||||||||
| 128 | 6 | ▶ | sub login ($t, $login_name = '', $login_password = '') { | ||||||||||||||||||
|
Subroutine
login: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 129 | 6 | my $form = $t->fill_in_login_form($login_name, $login_password); | |||||||||||||||||||
| 130 | 6 | my $body | |||||||||||||||||||
| 131 | = $t->post_ok($t->app->url_for('sign_in') => {} => form => $form)->tx->res->body; | ||||||||||||||||||||
| 132 | 6 | return $t->authenticated($body eq '')->authenticated; | |||||||||||||||||||
| 133 | } | ||||||||||||||||||||
| 134 | |||||||||||||||||||||
| 135 | # Tests creation of a domove record and returns the URL for GET, PUT, DELETE | ||||||||||||||||||||
| 136 | ## no critic (ValuesAndExpressions::ProhibitQuotesAsQuotelikeOperatorDelimiters) | ||||||||||||||||||||
| 137 | 0 | ▶ | sub create_edit_domain_ok ($t) { | ||||||||||||||||||
|
Subroutine
create_edit_domain_ok: not called CC=3 SCAR=24.8
Pod
undocumented
| |||||||||||||||||||||
| 138 | |||||||||||||||||||||
| 139 | #authenticate user if not authenticated | ||||||||||||||||||||
| 140 | 0 | ▶ | if (!$t->authenticated) { | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 141 | 0 | ok($t->login(), 'logged in'); | |||||||||||||||||||
| 142 | } | ||||||||||||||||||||
| 143 | 0 | $t->get_ok($t->app->url_for('create_domove'))->status_is(200); | |||||||||||||||||||
| 144 | 0 | my $store_url = $t->app->url_for('store_domove'); | |||||||||||||||||||
| 145 | 0 | ▶ | my $TEST_DOMAIN = $ENV{TEST_DOMAIN} || $t->domain_aliases; | ||||||||||||||||||
|
Condition0%0/3
$ENV{'TEST_DOMAIN'} || $t->domain_aliases
MC/DC0%0/2
$ENV{'TEST_DOMAIN'} || $t->domain_aliases
$ENV{'TEST_DOMAIN'}
$t->domain_aliases
Truth table0%0/3
A || B
A= $ENV{'TEST_DOMAIN'}B= $t->domain_aliases
| |||||||||||||||||||||
| 146 | 0 | my $domain = [split /\s+/, $TEST_DOMAIN]; | |||||||||||||||||||
| 147 | 0 | my $form = { | |||||||||||||||||||
| 148 | domain => $domain->[0], | ||||||||||||||||||||
| 149 | aliases => $TEST_DOMAIN, | ||||||||||||||||||||
| 150 | site_name => 'У дома', | ||||||||||||||||||||
| 151 | description => 'Съвсем у дома', | ||||||||||||||||||||
| 152 | owner_id => 5, | ||||||||||||||||||||
| 153 | group_id => 5, | ||||||||||||||||||||
| 154 | published => 2, | ||||||||||||||||||||
| 155 | templates => 'themes/malka' | ||||||||||||||||||||
| 156 | }; | ||||||||||||||||||||
| 157 | 0 | my $edit_url = $t->post_ok($store_url => form => $form)->status_is(302) | |||||||||||||||||||
| 158 | ->tx->res->headers->location; | ||||||||||||||||||||
| 159 | 0 | $t->get_ok($edit_url)->text_is('h2' => "1:$form->{domain}"); | |||||||||||||||||||
| 160 | |||||||||||||||||||||
| 161 | 0 | $form->{aliases} .= ' alias2.domain'; | |||||||||||||||||||
| 162 | 0 | $t->put_ok($edit_url => form => $form)->status_is(302) | |||||||||||||||||||
| 163 | ->header_is(Location => $edit_url); | ||||||||||||||||||||
| 164 | 0 | my $body = $t->get_ok($edit_url)->tx->res->body; | |||||||||||||||||||
| 165 | 0 | like($body => qr/$form->{aliases}/ => 'aliases changed'); | |||||||||||||||||||
| 166 | 0 | like($body => qr/$form->{templates}/ => 'templates changed'); | |||||||||||||||||||
| 167 | 0 | return $edit_url; | |||||||||||||||||||
| 168 | } | ||||||||||||||||||||
| 169 | |||||||||||||||||||||
| 170 | 2 | ▶ | sub meta_names_ok ($t) { | ||||||||||||||||||
|
Subroutine
meta_names_ok: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 171 | 2 | for (qw(author description keywords generator viewport )) { | |||||||||||||||||||
| 172 | 10 | my $selector = qq'head meta[name="$_"]'; | |||||||||||||||||||
| 173 | 10 | $t->element_exists($selector, $selector . ' exists') | |||||||||||||||||||
| 174 | ->attr_like($selector => 'content', qr/.+/ => $selector . ' has content'); | ||||||||||||||||||||
| 175 | } | ||||||||||||||||||||
| 176 | |||||||||||||||||||||
| 177 | # OpenGraph | ||||||||||||||||||||
| 178 | 2 | for (qw(og:type og:site_name og:title og:url og:type og:article:author og:description | |||||||||||||||||||
| 179 | og:locale og:article:published_time og:article:modified_time)) | ||||||||||||||||||||
| 180 | { | ||||||||||||||||||||
| 181 | 20 | my $selector = qq'head meta[property="$_"]'; | |||||||||||||||||||
| 182 | 20 | $t->element_exists($selector, $selector . ' exists') | |||||||||||||||||||
| 183 | ->attr_like($selector => 'content', qr/.+/ => $selector . ' has content'); | ||||||||||||||||||||
| 184 | |||||||||||||||||||||
| 185 | } | ||||||||||||||||||||
| 186 | 2 | return $t; | |||||||||||||||||||
| 187 | } | ||||||||||||||||||||
| 188 | |||||||||||||||||||||
| 189 | # Stollen from Class::Unload and modified. Thanks! | ||||||||||||||||||||
| 190 | ## no critic (Subroutines::ProhibitUnusedPrivateSubroutines,TestingAndDebugging::ProhibitNoStrict) | ||||||||||||||||||||
| 191 | 12 | ▶ | sub unuse_Slovo { | ||||||||||||||||||
|
Subroutine
unuse_Slovo: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||||||||||||
| 192 | 12 | my $class = 'Slovo'; | |||||||||||||||||||
| 193 | 12 | my $symtab = $class . '::'; | |||||||||||||||||||
| 194 | { | ||||||||||||||||||||
| 195 | 13 | ▶ | no strict 'refs'; # we're fiddling with the symbol table | ||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||
| 196 | 12 | @{$class . '::ISA'} = (); | |||||||||||||||||||
| 197 | |||||||||||||||||||||
| 198 | # Delete all symbols except other namespaces | ||||||||||||||||||||
| 199 | 12 | for my $symbol (keys %$symtab) { | |||||||||||||||||||
| 200 | 363 | ▶ | next if $symbol =~ /\A[^:]+::\z/; | ||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||
| 201 | 324 | delete $symtab->{$symbol}; | |||||||||||||||||||
| 202 | } | ||||||||||||||||||||
| 203 | } | ||||||||||||||||||||
| 204 | |||||||||||||||||||||
| 205 | 12 | my $inc_file = join('/', split /(?:'|::)/, $class) . '.pm'; | |||||||||||||||||||
| 206 | 12 | delete $INC{$inc_file}; | |||||||||||||||||||
| 207 | |||||||||||||||||||||
| 208 | 12 | return 1; | |||||||||||||||||||
| 209 | } | ||||||||||||||||||||
| 210 | 13 | 1; | |||||||||||||||||||