3 # This file is part of Koha.
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.
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.
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>.
22 use Test
::More tests
=> 34;
24 use t
::lib
::TestBuilder
;
26 use List
::MoreUtils
qw(any);
30 use Koha
::Tags
::Approvals
;
31 use Koha
::Tags
::Indexes
;
35 # So any output is readable :-D
36 binmode STDOUT
, ':encoding(utf8)';
38 my $schema = Koha
::Database
->schema;
39 my $builder = t
::lib
::TestBuilder
->new;
41 subtest
'add_tag_approval() tests' => sub {
45 $schema->storage->txn_begin;
47 # Make sure there's no pollution on the DB
48 Koha
::Tags
::Approvals
->search->delete;
52 '🐋a' => 3, # added an ASCII char to make it differ from just emojis
57 for my $term ( keys %{ $terms } ) {
58 for (my $i=1; $i <= $terms->{$term}; $i++) {
59 C4
::Tags
::add_tag_approval
( $term );
63 my $approvals = Koha
::Tags
::Approvals
->search;
64 is
( $approvals->count, scalar keys %{ $terms }, 'All terms got their approval row' );
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} );
71 $schema->storage->txn_rollback;
74 subtest
'add_tag_index() tests' => sub {
78 $schema->storage->txn_begin;
80 # Make sure there's no pollution on the DB
81 Koha
::Tags
::Indexes
->search->delete;
83 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
87 '🐋a' => 3, # added an ASCII char to make it differ from just emojis
92 while ( my ( $term, $count ) = each %$terms ) {
94 C4
::Tags
::add_tag_approval
( $term );
95 C4
::Tags
::add_tag_index
( $term, $biblio->biblionumber );
99 my $indexes = Koha
::Tags
::Indexes
->search({ biblionumber
=> $biblio->biblionumber });
100 is
( $indexes->count, scalar keys %{ $terms }, 'All terms got their index row' );
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} );
107 $schema->storage->txn_rollback;
110 subtest
'get_tag_rows() tests' => sub {
114 $schema->storage->txn_begin;
116 # Make sure there's no pollution on the DB
117 Koha
::Tags
->search->delete;
119 my $patron = $builder->build_object({ class => 'Koha::Patrons' });
120 my $biblio = $builder->build_object({ class => 'Koha::Biblios' });
122 my @terms = ( '🐋a', '🌮', '👍' );
124 for my $term ( @terms ) {
125 $builder->build_object({ class => 'Koha::Tags', value
=> {
126 borrowernumber
=> $patron->id,
127 biblionumber
=> $biblio->id,
132 my $tags = Koha
::Tags
->search({ borrowernumber
=> $patron->id });
133 is
( $tags->count, scalar @terms, 'All terms got their row' );
135 while ( my $tag = $tags->next ) {
136 ok
( any
{ $_ eq $tag->term } @terms , 'The returned term is in our list' );
139 for my $term ( @terms ) {
140 my @result = @
{ C4
::Tags
::get_tag_rows
({ term
=> $term }) };
142 is
( scalar @result, 1, 'Only one row matches each' );
145 $schema->storage->txn_rollback;
148 # Check no tags case.
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');
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');
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');
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');
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');
187 # Runs tests against the results
188 sub check_tag_strata
{
189 my ($tags, $expected, $name) = @_;
191 foreach my $t (@
$tags) {
192 my $w = $t->{weight_total
};
193 my $s = $t->{stratum
};
194 is
($s, shift @
$expected, $name . " - $w ($s)");
198 # Makes some tags with just enough info to test
202 push @res, { weight_total
=> shift @_ };