From a430c80f3524df4db5f556fb2dd0ddfa2ffb3eca Mon Sep 17 00:00:00 2001 From: Alex Sassmannshausen Date: Fri, 18 Mar 2016 19:07:12 +0100 Subject: [PATCH] Bug 16195: Add Koha::Categories object. * Koha/Categories.pm: New file. * Koha/Category.pm: New file. * t/Category.t: New file. --- Koha/Categories.pm | 33 +++++++++++++ Koha/Category.pm | 28 +++++++++++ t/Category.t | 134 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 195 insertions(+) create mode 100644 Koha/Categories.pm create mode 100644 Koha/Category.pm create mode 100644 t/Category.t diff --git a/Koha/Categories.pm b/Koha/Categories.pm new file mode 100644 index 0000000..ce6f2d6 --- /dev/null +++ b/Koha/Categories.pm @@ -0,0 +1,33 @@ +package Koha::Categories; + +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; +use Koha::Category; + +use base qw(Koha::Objects); + +sub _type { + return 'Category'; +} + +sub object_class { + return 'Koha::Category'; +} + +1; diff --git a/Koha/Category.pm b/Koha/Category.pm new file mode 100644 index 0000000..1f508e6 --- /dev/null +++ b/Koha/Category.pm @@ -0,0 +1,28 @@ +package Koha::Category; + +# 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, write to the Free Software Foundation, Inc., +# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. + +use Modern::Perl; + +use Koha::Database; + +use base qw(Koha::Object); + +sub _type { + return 'Category'; +} + +1; diff --git a/t/Category.t b/t/Category.t new file mode 100644 index 0000000..f6b93df --- /dev/null +++ b/t/Category.t @@ -0,0 +1,134 @@ +#!/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 . + +use Modern::Perl; + +use Test::More tests => 108; +use t::lib::Mocks; + +BEGIN { + t::lib::Mocks::mock_dbh; + use_ok('Koha::Object'); + use_ok('Koha::Category'); + use_ok('Koha::Categories'); +} + +# We want to test: +# - Creating categories +# - Accessors +# - Setters +# - Second Accessors +# - Searching for categories [no, because db_dependent] +# - Finding categories [no, because db_dependent] + +# Preliminaries: +# The Category Accessor/Field names. +my @xsors = qw/categorycode description enrolmentperiod enrolmentperioddate + upperagelimit dateofbirthrequired finetype bulk enrolmentfee + overduenoticerequired issuelimit reservefee hidelostitems + category_type BlockExpiredPatronOpacActions default_privacy/; + +# Some Category creation data. +my @cat_data = ( + { id => 'cat1', num => 21, bool => 1 }, + { id => 'cat2', num => 16, bool => 0 }, + { id => 'cat3', num => 35, bool => 1 }, +); + +# Tests: Creating categories +my @cats = map { + my @data = ( + $_->{id}, "Test category created (" . $_ . ")", + 365 + $_->{num}, '2016-01-01', $_->{num}, $_->{bool}, + 'BLA', $_->{bool}, $_->{num} + 0.53, $_->{bool}, + $_->{num} - 5, $_->{num} - 5.06, $_->{bool}, 'A', + -1, 'default', + ); + my $props = {}; + foreach my $xsor (@xsors) { + $props->{$xsor} = shift @data; + }; + { id => $_->{id}, obj => Koha::Category->new($props), data => $props } + if (ok(Koha::Category->new($props), "Creating categories ['$_->{id}']")); +} @cat_data; + +# Tests: Is a Category +map { isa_ok($_->{obj}, 'Koha::Category', $_->{id}) } @cats; + +# Tests: Accessors +map { + my $cat = $_; + map { + is( + $cat->{obj}->$_, $cat->{data}->{$_}, + "Accessors ['$cat->{id}': '$_']" + ); + } @xsors; +} @cats; + +# Some new Category data. +my @cat_data_new = ( + { id => 'cat1new', num => 31, bool => 0 }, + { id => 'cat2new', num => 76, bool => 1 }, + { id => 'cat3new', num => 55, bool => 0 }, +); + +# Tests: Setters +my @cats_new = map { + my $dt = shift @cat_data_new; + my @data = ( + $dt->{id}, "Test category created (" . $dt . ")", + 365 + $dt->{num}, '2014-01-01', $dt->{num}, $dt->{bool}, + 'FOO', $dt->{bool}, $dt->{num} + 0.53, $dt->{bool}, + $dt->{num} - 5, $dt->{num} - 5.06, $dt->{bool}, 'A', + -1, 'default', + ); + my $props = {}; + foreach my $xsor (@xsors) { + $props->{$xsor} = shift @data; + }; + { id => $_->{id}, obj => $_->{obj}, data => $props } + if (ok($_->{obj}->set($props), "Setters ['$_->{id}']")); +} @cats; + +# Tests: Second Accessors +map { + my $cat = $_; + map { + is( + $cat->{obj}->$_, $cat->{data}->{$_}, + "Second Accessors ['$cat->{id}': '$_']" + ); + } @xsors; +} @cats_new; + +# These are db_dependent, and hence are not performed here. +# # Search Tests +# is( +# Koha::Categories->search({ dateofbirthrequired => 1})->count, 2, +# 'Search works' +# ); + +# # Find Tests +# map { +# is( +# Koha::Categories->find($_->{id})->enrolmentperiod, +# $_->{data}->{enrolmentperiod}, $_->{id} . ' Found' +# ); +# } @cats; + +1; -- 2.7.3