From 9748a277648818ecb55cf59a5281a95f5d160163 Mon Sep 17 00:00:00 2001
From: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
Date: Thu, 18 Feb 2021 15:42:48 +0100
Subject: [PATCH] Bug 27715: Add tests

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
---
 t/db_dependent/Utils/Datatables.t | 61 +++++++++++++++++++++++++++++++
 1 file changed, 61 insertions(+)
 create mode 100644 t/db_dependent/Utils/Datatables.t

diff --git a/t/db_dependent/Utils/Datatables.t b/t/db_dependent/Utils/Datatables.t
new file mode 100644
index 0000000000..9f52ce3508
--- /dev/null
+++ b/t/db_dependent/Utils/Datatables.t
@@ -0,0 +1,61 @@
+#!/usr/bin/perl
+
+# 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 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, see <http://www.gnu.org/licenses>.
+
+use Modern::Perl;
+
+use Test::More tests => 1;
+
+use C4::Utils::DataTables;
+
+use t::lib::Mocks;
+use t::lib::TestBuilder;
+
+use Koha::Database;
+
+my $schema = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+
+my $builder = t::lib::TestBuilder->new;
+
+subtest 'dt_build_orderby' => sub {
+    plan tests => 2;
+
+    my $dt_params = {
+        iSortCol_0  => 5,
+        sSortDir_0  => "asc",
+        mDataProp_5 => "branch",
+        name_sorton => "borrowers.surname borrowers.firstname",
+
+        iSortCol_1    => 2,
+        sSortDir_1    => "desc",
+        mDataProp_2   => "name",
+        branch_sorton => "branches.branchname",
+    };
+
+    my $orderby = dt_build_orderby($dt_params);
+    is( $orderby, " ORDER BY branches.branchname asc,borrowers.surname desc,borrowers.firstname desc ", 'ORDER BY has been correctly built' );
+
+    $dt_params = {
+        %$dt_params,
+        iSortCol_2                    => 3,
+        sSortDir_2                    => "asc",
+        mDataProp_3                   => "branch,somethingelse",
+    };
+
+    $orderby = dt_build_orderby($dt_params);
+    is( $orderby, " ORDER BY branches.branchname asc,borrowers.surname desc,borrowers.firstname desc ", 'ORDER BY has been correctly built, even with invalid stuff');
+};
-- 
2.20.1