View | Details | Raw Unified | Return to bug 21846
Collapse All | Expand All

(-)a/t/db_dependent/Tags.t (-9 / +141 lines)
Lines 1-16 Link Here
1
#!/usr/bin/perl
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.
2
#
14
#
3
# This Koha test module is a stub!  
15
# You should have received a copy of the GNU General Public License
4
# Add more tests here!!!
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
5
17
6
use strict;
18
use Modern::Perl;
7
use warnings;
8
19
9
use Test::More tests => 32;
20
use utf8;
10
21
11
BEGIN {
22
use Test::More tests => 34;
12
        use_ok('C4::Tags');
23
13
}
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
};
14
147
15
# Check no tags case.
148
# Check no tags case.
16
my @tagsarray;
149
my @tagsarray;
17
- 

Return to bug 21846