1+ # !/usr/bin/env perl
2+ use Mojo::Base ' -signatures' ;
3+ use BlogDB::Web::Test;
4+
5+ my $t = Test::Mojo::BlogDB-> new(' BlogDB::Web' );
6+
7+
8+ # Create a tag, it should have no votes at first.
9+ $t -> create_user-> create_tag( ' first' )-> code_block( sub {
10+ my $t = shift ;
11+ $t -> _ss( $t -> app-> db-> resultset(' PendingTag' )-> search({name => ' first' })-> first);
12+ ok $t -> _sg, " Found tag" ;
13+ is $t -> _sg-> name, ' first' , " Tag named correctly." ;
14+ is $t -> _sg-> vote_score, 0, " Vote score = 0" ;
15+ });
16+
17+ # Upvote them!
18+ $t -> post_ok( ' /tags/vote' , form => {
19+ tag => ' first'
20+ })-> code_block( sub {
21+ my $t = shift ;
22+ $t -> _ss( $t -> app-> db-> resultset(' PendingTag' )-> search({name => ' first' })-> first);
23+ ok $t -> _sg, " Found tag" ;
24+ is $t -> _sg-> name, ' first' , " Tag named correctly." ;
25+ is $t -> _sg-> vote_score, 1, " Vote score = 1" ;
26+ });
27+
28+ # Upvote them again to undo it!
29+ $t -> post_ok( ' /tags/vote' , form => {
30+ tag => ' first'
31+ })-> code_block( sub {
32+ my $t = shift ;
33+ $t -> _ss( $t -> app-> db-> resultset(' PendingTag' )-> search({name => ' first' })-> first);
34+ ok $t -> _sg, " Found tag" ;
35+ is $t -> _sg-> name, ' first' , " Tag named correctly." ;
36+ is $t -> _sg-> vote_score, 0, " Vote score = 0" ;
37+ });
38+
39+ # Make three new users and upvote it, then check the vote count.
40+ $t = Test::Mojo::BlogDB-> new(' BlogDB::Web' )
41+ -> create_user
42+ -> post_ok( ' /tags/vote' , form => { tag => ' first' });
43+
44+ $t = Test::Mojo::BlogDB-> new(' BlogDB::Web' )
45+ -> create_user
46+ -> post_ok( ' /tags/vote' , form => { tag => ' first' });
47+
48+ $t = Test::Mojo::BlogDB-> new(' BlogDB::Web' )
49+ -> create_user
50+ -> post_ok( ' /tags/vote' , form => { tag => ' first' })
51+ -> code_block( sub {
52+ my $t = shift ;
53+ $t -> _ss( $t -> app-> db-> resultset(' PendingTag' )-> search({name => ' first' })-> first);
54+ ok $t -> _sg, " Found tag" ;
55+ is $t -> _sg-> name, ' first' , " Tag named correctly." ;
56+ is $t -> _sg-> vote_score, 3, " Vote score = 3" ;
57+ });
58+
59+ # Voting on a tag without a user account will not work.
60+ $t = Test::Mojo::BlogDB-> new(' BlogDB::Web' )
61+ -> post_ok( ' /tags/vote' , form => { tag => ' first' })
62+ -> stash_has( { errors => [ ' Login required.' ] }, ' Need user account to vote.' )
63+ -> code_block( sub {
64+ my $t = shift ;
65+ $t -> _ss( $t -> app-> db-> resultset(' PendingTag' )-> search({name => ' first' })-> first);
66+ ok $t -> _sg, " Found tag" ;
67+ is $t -> _sg-> name, ' first' , " Tag named correctly." ;
68+ is $t -> _sg-> vote_score, 3, " Same vote count after unauthed vote attempt." ;
69+ });
70+
71+
72+ done_testing;
0 commit comments