Bug 21846: Regression tests for add_tag_approval
[koha.git] / t / db_dependent / Tags.t
1 #!/usr/bin/perl
2
3 # This file is part of Koha.
4 #
5 # Koha is free software; you can redistribute it and/or modify it
6 # under the terms of the GNU General Public License as published by
7 # the Free Software Foundation; either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # Koha is distributed in the hope that it will be useful, but
11 # WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18 use Modern::Perl;
19
20 use utf8;
21
22 use Test::More tests => 34;
23
24 use t::lib::TestBuilder;
25
26 use List::MoreUtils qw(any);
27
28 use Koha::Database;
29 use Koha::Tags;
30 use Koha::Tags::Approvals;
31 use Koha::Tags::Indexes;
32
33 use C4::Tags;
34
35 # So any output is readable :-D
36 binmode STDOUT, ':encoding(utf8)';
37
38 my $schema = Koha::Database->schema;
39 my $builder = t::lib::TestBuilder->new;
40
41 subtest 'add_tag_approval() tests' => sub {
42
43     plan tests => 7;
44
45     $schema->storage->txn_begin;
46
47     # Make sure there's no pollution on the DB
48     Koha::Tags::Approvals->search->delete;
49
50     my $terms = {
51       # term => count
52         '🐋a' => 3, # added an ASCII char to make it differ from just emojis
53         '🌮'  => 2,
54         '👍'  => 1,
55     };
56
57     for my $term ( keys %{ $terms } ) {
58         for (my $i=1; $i <= $terms->{$term}; $i++) {
59             C4::Tags::add_tag_approval( $term );
60         }
61     }
62
63     my $approvals = Koha::Tags::Approvals->search;
64     is( $approvals->count, scalar keys %{ $terms }, 'All terms got their approval row' );
65
66     while ( my $approval = $approvals->next ) {
67         ok( exists $terms->{$approval->term}, 'The returned term is in our list' );
68         is( $approval->weight_total, $terms->{$approval->term} );
69     }
70
71     $schema->storage->txn_rollback;
72 };
73
74 subtest 'add_tag_index() tests' => sub {
75
76     plan tests => 7;
77
78     $schema->storage->txn_begin;
79
80     # Make sure there's no pollution on the DB
81     Koha::Tags::Indexes->search->delete;
82
83     my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
84
85     my $terms = {
86       # term => count
87         '🐋a' => 3, # added an ASCII char to make it differ from just emojis
88         '🌮'  => 2,
89         '👍'  => 1,
90     };
91
92     for my $term ( keys %{ $terms } ) {
93         for (my $i=1; $i <= $terms->{$term}; $i++) {
94             C4::Tags::add_tag_approval( $term );
95             C4::Tags::add_tag_index( $term, $biblio->biblionumber );
96         }
97     }
98
99     my $indexes = Koha::Tags::Indexes->search({ biblionumber => $biblio->biblionumber });
100     is( $indexes->count, scalar keys %{ $terms }, 'All terms got their index row' );
101
102     while ( my $index = $indexes->next ) {
103         ok( exists $terms->{$index->term}, 'The returned term is in our list' );
104         is( $index->weight, $terms->{$index->term}  );
105     }
106
107     $schema->storage->txn_rollback;
108 };
109
110 subtest 'get_tag_rows() tests' => sub {
111
112     plan tests => 7;
113
114     $schema->storage->txn_begin;
115
116     # Make sure there's no pollution on the DB
117     Koha::Tags->search->delete;
118
119     my $patron = $builder->build_object({ class => 'Koha::Patrons' });
120     my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
121
122     my @terms = ( '🐋a', '🌮', '👍' );
123
124     for my $term ( @terms ) {
125         $builder->build_object({ class => 'Koha::Tags', value => {
126             borrowernumber => $patron->id,
127             biblionumber   => $biblio->id,
128             term           => $term,
129         } });
130     }
131
132     my $tags = Koha::Tags->search({ borrowernumber => $patron->id });
133     is( $tags->count, scalar @terms, 'All terms got their row' );
134
135     while ( my $tag = $tags->next ) {
136         ok( any { $_ eq $tag->term } @terms , 'The returned term is in our list' );
137     }
138
139     for my $term ( @terms ) {
140         my @result = @{ C4::Tags::get_tag_rows({ term => $term }) };
141
142         is( scalar @result, 1, 'Only one row matches each' );
143     }
144
145     $schema->storage->txn_rollback;
146 };
147
148 # Check no tags case.
149 my @tagsarray;
150 my $tags = \@tagsarray;
151 my ($min, $max) = C4::Tags::stratify_tags(0, $tags);
152 is($min, 0, 'Empty array min');
153 is($max, 0, 'Empty array max');
154
155 # Simple 'sequential 5' test
156 $tags = make_tags(1,2,3,4,5);
157 my @strata = (0,1,2,3,4);
158 ($min, $max) = C4::Tags::stratify_tags(5, $tags);
159 check_tag_strata($tags, \@strata, 'Sequential 5');
160 is($min, 0, 'Sequential 5 min');
161 is($max, 4, 'Sequential 5 max');
162
163 # Reverse test - should have the same results as previous
164 $tags = make_tags(5,4,3,2,1);
165 @strata = (4,3,2,1,0);
166 ($min, $max) = C4::Tags::stratify_tags(5, $tags);
167 check_tag_strata($tags, \@strata, 'Reverse Sequential 5');
168 is($min, 0, 'Sequential 5 min');
169 is($max, 4, 'Sequential 5 max');
170
171 # All the same test - should all have the same results
172 $tags = make_tags(4,4,4,4,4);
173 @strata = (0,0,0,0,0);
174 ($min, $max) = C4::Tags::stratify_tags(5, $tags);
175 check_tag_strata($tags, \@strata, 'All The Same');
176 is($min, 0, 'Sequential 5 min');
177 is($max, 0, 'Sequential 5 max');
178
179 # Some the same, some different
180 $tags = make_tags(1,2,2,3,3,8);
181 @strata = (0,0,0,1,1,4);
182 ($min, $max) = C4::Tags::stratify_tags(5, $tags);
183 check_tag_strata($tags, \@strata, 'All The Same');
184 is($min, 0, 'Sequential 5 min');
185 is($max, 7, 'Sequential 5 max');
186
187 # Runs tests against the results
188 sub check_tag_strata {
189     my ($tags, $expected, $name) = @_;
190
191     foreach my $t (@$tags) {
192         my $w = $t->{weight_total};
193         my $s = $t->{stratum};
194         is($s, shift @$expected, $name . " - $w ($s)");
195     }
196 }
197
198 # Makes some tags with just enough info to test
199 sub make_tags {
200     my @res;
201     while (@_) {
202         push @res, { weight_total => shift @_ };
203     }
204     return \@res;
205 }