From f7002f4e3eeb8152b5cad8d4ff78a14533d863cc Mon Sep 17 00:00:00 2001
From: Emmi Takkinen <emmi.takkinen@koha-suomi.fi>
Date: Tue, 19 Jan 2021 11:46:52 +0200
Subject: [PATCH] Bug 17897: Fix failing tests and QA issues

Tests failed because
1) txn_begin and rollback were used inside
of subtests.
2) column search_field.type was filled with
unallowed value.

This patch also fixes QA issue with test file
exec rights and incorrect license statement.

To test prove t/db_dependent/Koha/SearchEngine/ElasticSearch.t
---
 Koha/SearchMappingManager.pm                  | 20 ++++----
 .../Koha/SearchEngine/ElasticSearch.t         | 51 +++++++++----------
 2 files changed, 35 insertions(+), 36 deletions(-)
 mode change 100644 => 100755 t/db_dependent/Koha/SearchEngine/ElasticSearch.t

diff --git a/Koha/SearchMappingManager.pm b/Koha/SearchMappingManager.pm
index e8f41bd926..bc7e231704 100644
--- a/Koha/SearchMappingManager.pm
+++ b/Koha/SearchMappingManager.pm
@@ -2,18 +2,18 @@ package Koha::SearchMappingManager;
 
 # This file is part of Koha.
 #
-# Koha is free software; you can redistribute it and/or modify it under the
-# terms of the GNU General Public License as published by the Free Software
-# Foundation; either version 3 of the License, or (at your option) any later
-# version.
+# Koha is free software; you can redistribute it and/or modify it
+# under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 3 of the License, or
+# (at your option) any later version.
 #
-# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
-# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
-# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
+# Koha is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
 #
-# You should have received a copy of the GNU General Public License along
-# with Koha; if not, write to the Free Software Foundation, Inc.,
-# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+# You should have received a copy of the GNU General Public License
+# along with Koha; if not, see <http://www.gnu.org/licenses>.
 
 use Modern::Perl;
 
diff --git a/t/db_dependent/Koha/SearchEngine/ElasticSearch.t b/t/db_dependent/Koha/SearchEngine/ElasticSearch.t
old mode 100644
new mode 100755
index 77c9237c13..f0d749f69c
--- a/t/db_dependent/Koha/SearchEngine/ElasticSearch.t
+++ b/t/db_dependent/Koha/SearchEngine/ElasticSearch.t
@@ -17,21 +17,23 @@
 # You should have received a copy of the GNU General Public License
 # along with Koha; if not, see <http://www.gnu.org/licenses>.
 
-use Modern::Perl qw(2014);
+use Modern::Perl;
 use utf8;
-use Test::More;
+use Test::More tests => 3;
+use Test::Exception;
 
 use Koha::Database;
 use Koha::SearchEngine::Elasticsearch;
 use Koha::SearchMappingManager;
 
 my $schema = Koha::Database->schema;
+$schema->storage->txn_begin;
 
-subtest "Reset Elasticsearch mappings", \&reset_elasticsearch_mappings;
-sub reset_elasticsearch_mappings {
-    my ($rv, $mappings, $count, $mapping);
+subtest 'reset_elasticsearch_mappings() tests' => sub {
+
+    plan tests => 8;
 
-    $schema->storage->txn_begin;
+    my ($rv, $mappings, $count, $mapping);
 
     ok(1, 'Scenario: Reset Elasticsearch mappings to an empty database');
     #There might or might not be any mappings. Whatever the initial status is, make sure we start from empty tables
@@ -49,12 +51,10 @@ sub reset_elasticsearch_mappings {
     $count = $mappings->count();
     ok($count, 'Then search mapping tables have been populated');
 
-
-
     ok(1, 'Scenario: Reset Elasticsearch mappings when custom mappings already exist');
     $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
                                                    label => 'original language',
-                                                   type => 'keyword',
+                                                   type => '',
                                                    index_name => 'biblios',
                                                    marc_type => 'marc21',
                                                    marc_field => '024a',
@@ -74,14 +74,13 @@ sub reset_elasticsearch_mappings {
     $count = $mappings->count();
     ok($count > 10, 'Then search mapping tables have been populated');
 
-    $schema->storage->txn_rollback;
-}
+};
 
-subtest "Get Elasticsearch mappings", \&get_search_mappings;
-sub get_search_mappings {
-    my ($mappings, $mapping);
+subtest 'get_search_mappings() tests' => sub {
 
-    $schema->storage->txn_begin;
+    plan tests => 13;
+
+    my ($mappings, $mapping);
 
     ok(1, 'Scenario: Get a single search mapping by name');
     $mappings = Koha::SearchMappingManager::get_search_mappings({index_name => 'biblios', name => 'ff7-00'});
@@ -91,7 +90,7 @@ sub get_search_mappings {
     is($mapping->get_column('type'),       '',       'And the search mapping "type" matches');
     is($mapping->get_column('facet'),      '0',      'And the search mapping "facet" matches');
     is($mapping->get_column('suggestible'), '0',     'And the search mapping "suggestible" matches');
-    is($mapping->get_column('sort'),        undef,   'And the search mapping "sort" matches');
+    is($mapping->get_column('sort'),        '1',   'And the search mapping "sort" matches');
     is($mapping->get_column('search'),     '1',   'And the search mapping "search" matches');
     is($mapping->get_column('marc_type'),  'marc21', 'And the search mapping "marc_type" matches');
     is($mapping->get_column('marc_field'), '007_/0', 'And the search mapping "marc_field" matches');
@@ -101,19 +100,18 @@ sub get_search_mappings {
     ok($mappings, 'When search mappings are fetched');
     ok($mappings->count() > 10, 'Then we have "'.$mappings->count().'" search mappings :)');
 
-    $schema->storage->txn_rollback;
-}
+};
 
-subtest "Add a search mapping", \&add_mapping;
-sub add_mapping {
-    my ($rv, $mappings, $mapping, $count);
+subtest 'add_mapping() tests' => sub {
 
-    $schema->storage->txn_begin;
+    plan tests => 3;
+
+    my ($rv, $mappings, $mapping, $count);
 
     ok(1, "Scenario: Add the same mapping twice and hope for no duplicate mappings");
     $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
                                                    label => 'original language',
-                                                   type => 'keyword',
+                                                   type => '',
                                                    index_name => 'biblios',
                                                    marc_type => 'marc21',
                                                    marc_field => '024a',
@@ -122,7 +120,7 @@ sub add_mapping {
                                                    sort => 1});
     $rv = Koha::SearchMappingManager::add_mapping({name => 'ln-test',
                                                    label => 'original language',
-                                                   type => 'keyword',
+                                                   type => '',
                                                    index_name => 'biblios',
                                                    marc_type => 'marc21',
                                                    marc_field => '024a',
@@ -135,7 +133,8 @@ sub add_mapping {
     $count = $mappings->count();
     is($count, 1, "Then we received only one mapping from the database");
 
-    $schema->storage->txn_rollback;
-}
+};
+
+$schema->storage->txn_rollback;
 
 done_testing;
-- 
2.34.1