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

(-)a/t/db_dependent/TestObjects/Biblios/BiblioFactory.pm (+96 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Biblios::BiblioFactory;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use C4::Biblio;
25
26
use t::db_dependent::TestObjects::Biblios::ExampleBiblios;
27
use t::db_dependent::TestObjects::ObjectFactory;
28
29
=head t::db_dependent::TestObjects::Biblios::createTestGroup( $data [, $hashKey] )
30
Calls C4::Biblio::TransformKohaToMarc() to make a MARC::Record and add it to
31
the DB. Returns a HASH of MARC::Records.
32
The HASH is keyed with the biblionumber, or the given $hashKey.
33
The biblionumber is injected to the MARC::Record-object to be easily accessable,
34
so we can get it like this:
35
    $records->{$key}->{biblionumber};
36
37
See C4::Biblio::TransformKohaToMarc() for how the biblio- or biblioitem-tables'
38
columns need to be given.
39
=cut
40
41
sub createTestGroup {
42
    my ($biblios, $hashKey) = @_;
43
    my %records;
44
    foreach my $biblio (@$biblios) {
45
        my $record = C4::Biblio::TransformKohaToMarc($biblio);
46
        my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio($record,'');
47
        $record->{biblionumber} = $biblionumber;
48
49
        my $key = t::db_dependent::TestObjects::ObjectFactory::getHashKey($biblio, $biblionumber, $hashKey);
50
51
        $records{$key} = $record;
52
    }
53
    return \%records;
54
}
55
56
=head
57
58
    my $records = createTestGroup();
59
    ##Do funky stuff
60
    deleteTestGroup($records);
61
62
Removes the given test group from the DB.
63
64
=cut
65
66
sub deleteTestGroup {
67
    my $records = shift;
68
69
    my ( $biblionumberFieldCode, $biblionumberSubfieldCode ) =
70
            C4::Biblio::GetMarcFromKohaField( "biblio.biblionumber", '' );
71
72
    my $schema = Koha::Database->new_schema();
73
    while( my ($key, $record) = each %$records) {
74
        my $biblionumber = $record->subfield($biblionumberFieldCode, $biblionumberSubfieldCode);
75
        $schema->resultset('Biblio')->search($biblionumber)->delete_all();
76
        $schema->resultset('Biblioitem')->search($biblionumber)->delete_all();
77
    }
78
}
79
sub _deleteTestGroupFromIdentifiers {
80
    my $testGroupIdentifiers = shift;
81
82
    my $schema = Koha::Database->new_schema();
83
    foreach my $isbn (@$testGroupIdentifiers) {
84
        $schema->resultset('Biblio')->search({"biblioitems.isbn" => $isbn},{join => 'biblioitems'})->delete();
85
        $schema->resultset('Biblioitem')->search({isbn => $isbn})->delete();
86
    }
87
}
88
89
sub createTestGroup1 {
90
    return t::db_dependent::TestObjects::Biblios::ExampleBiblios::createTestGroup1();
91
}
92
sub deleteTestGroup1 {
93
    return t::db_dependent::TestObjects::Biblios::ExampleBiblios::deleteTestGroup1();
94
}
95
96
1;
(-)a/t/db_dependent/TestObjects/Biblios/ExampleBiblios.pm (+75 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Biblios::ExampleBiblios;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use t::db_dependent::TestObjects::Biblios::BiblioFactory;
25
26
=head createTestGroupX
27
28
    my $hash_of_records = TestObjects::C4::Biblio::ExampleBiblios::createTestGroup1();
29
30
    ##Do stuff with the test group##
31
32
    #Remember to delete them after use.
33
    TestObjects::C4::Biblio::ExampleBiblios::deleteTestGroup1($biblionumbers);
34
=cut
35
36
my @testGroup1Identifiers = ('9519671580', '9519671581', '9519671582', '9519671583',
37
                            ); #So we can later delete these Biblios
38
sub createTestGroup1 {
39
    my @records = [
40
        {
41
            "biblio.title"         => 'I wish I met your mother',
42
            "biblio.author"        => 'Pertti Kurikka',
43
            "biblio.copyrightdate" => '1960',
44
            "biblioitems.isbn"     => $testGroup1Identifiers[0],
45
            "biblioitems.itemtype" => 'BK',
46
        },
47
        {
48
            "biblio.title"         => 'Me and your mother',
49
            "biblio.author"        => 'Jaakko Kurikka',
50
            "biblio.copyrightdate" => '1961',
51
            "biblioitems.isbn"     => $testGroup1Identifiers[1],
52
            "biblioitems.itemtype" => 'BK',
53
        },
54
        {
55
            "biblio.title"         => 'How I met your mother',
56
            "biblio.author"        => 'Martti Kurikka',
57
            "biblio.copyrightdate" => '1962',
58
            "biblioitems.isbn"     => $testGroup1Identifiers[2],
59
            "biblioitems.itemtype" => 'DV',
60
        },
61
        {
62
            "biblio.title"         => 'How I wish I had met your mother',
63
            "biblio.author"        => 'Tapio Kurikka',
64
            "biblio.copyrightdate" => '1963',
65
            "biblioitems.isbn"     => $testGroup1Identifiers[3],
66
            "biblioitems.itemtype" => 'DV',
67
        },
68
    ];
69
    return createTestGroup(\@records);
70
}
71
sub deleteTestGroup1 {
72
    t::db_dependent::TestObjects::Biblios::BiblioFactory::_deleteTestGroupFromIdentifiers(\@testGroup1Identifiers);
73
}
74
75
1;
(-)a/t/db_dependent/TestObjects/Borrowers/BorrowerFactory.pm (+91 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Borrowers::BorrowerFactory;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use C4::Members;
25
26
use t::db_dependent::TestObjects::Borrowers::ExampleBorrowers;
27
use t::db_dependent::TestObjects::ObjectFactory qw(getHashKey);
28
29
=head t::db_dependent::TestObjects::Borrowers::BorrowerFactory::createTestGroup( $data [, $hashKey] )
30
Returns a HASH of borrowers.
31
The HASH is keyed with the PRIMARY KEY, or the given $hashKey.
32
33
See C4::Members::AddMember() for how the table columns need to be given.
34
=cut
35
36
sub createTestGroup {
37
    my ($objects, $hashKey) = @_;
38
39
    my %objects;
40
    foreach my $object (@$objects) {
41
        my $borrowernumber = C4::Members::AddMember(%$object);
42
        #If adding failed, we still get some strange borrowernumber result.
43
        #Check for sure by finding the real borrower.
44
        my $borrower = C4::Members::GetMember(cardnumber => $object->{cardnumber});
45
        unless ($borrower) {
46
            carp "BorrowerFactory:> No borrower for cardnumber '".$object->{cardnumber}."'";
47
            next();
48
        }
49
50
        my $key = t::db_dependent::TestObjects::ObjectFactory::getHashKey($borrower, $borrowernumber, $hashKey);
51
52
        $objects{$key} = $borrower;
53
    }
54
    return \%objects;
55
}
56
57
=head
58
59
    my $records = createTestGroup();
60
    ##Do funky stuff
61
    deleteTestGroup($records);
62
63
Removes the given test group from the DB.
64
65
=cut
66
67
sub deleteTestGroup {
68
    my $objects = shift;
69
70
    my $schema = Koha::Database->new_schema();
71
    while( my ($key, $object) = each %$objects) {
72
        $schema->resultset('Borrower')->find($object->{borrowernumber})->delete();
73
    }
74
}
75
sub _deleteTestGroupFromIdentifiers {
76
    my $testGroupIdentifiers = shift;
77
78
    my $schema = Koha::Database->new_schema();
79
    foreach my $key (@$testGroupIdentifiers) {
80
        $schema->resultset('Borrower')->find({"cardnumber" => $key})->delete();
81
    }
82
}
83
84
sub createTestGroup1 {
85
    return t::db_dependent::TestObjects::Borrowers::ExampleBorrowers::createTestGroup1();
86
}
87
sub deleteTestGroup1 {
88
    return t::db_dependent::TestObjects::Borrowers::ExampleBorrowers::deleteTestGroup1();
89
}
90
91
1;
(-)a/t/db_dependent/TestObjects/Borrowers/ExampleBorrowers.pm (+60 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Borrowers::ExampleBorrowers;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use t::db_dependent::TestObjects::Borrowers::BorrowerFactory;
25
26
=head createTestGroupX
27
28
    You should use the appropriate Factory-class to create these test-objects.
29
30
=cut
31
32
my @testGroup1Identifiers = ('167Azel0001', '167Azel0002', '167Azel0003', '167Azel0004',
33
                             '167Azel0005', '167Azel0006', '167Azel0007', '167Azel0008',
34
                            );
35
sub createTestGroup1 {
36
    my @borrowers = (
37
        {cardnumber => $testGroup1Identifiers[0], branchcode => 'CPL', categorycode => 'YA',
38
         surname => 'Costly', firstname => 'Colt', address => 'Street 11', zipcode => '10221'},
39
        {cardnumber => $testGroup1Identifiers[1], branchcode => 'CPL', categorycode => 'YA',
40
         surname => 'Dearly', firstname => 'Colt', address => 'Street 12', zipcode => '10222'},
41
        {cardnumber => $testGroup1Identifiers[2], branchcode => 'CPL', categorycode => 'YA',
42
         surname => 'Pricy', firstname => 'Colt', address => 'Street 13', zipcode => '10223'},
43
        {cardnumber => $testGroup1Identifiers[3], branchcode => 'CPL', categorycode => 'YA',
44
         surname => 'Expensive', firstname => 'Colt', address => 'Street 14', zipcode => '10224'},
45
        {cardnumber => $testGroup1Identifiers[4], branchcode => 'FTL', categorycode => 'YA',
46
         surname => 'Cheap', firstname => 'Colt', address => 'Street 15', zipcode => '10225'},
47
        {cardnumber => $testGroup1Identifiers[5], branchcode => 'FTL', categorycode => 'YA',
48
         surname => 'Poor', firstname => 'Colt', address => 'Street 16', zipcode => '10226'},
49
        {cardnumber => $testGroup1Identifiers[6], branchcode => 'FTL', categorycode => 'YA',
50
         surname => 'Stingy', firstname => 'Colt', address => 'Street 17', zipcode => '10227'},
51
        {cardnumber => $testGroup1Identifiers[7], branchcode => 'FTL', categorycode => 'YA',
52
         surname => 'Impoverished', firstname => 'Colt', address => 'Street 18', zipcode => '10228'},
53
    );
54
    return t::db_dependent::TestObjects::Borrowers::BorrowerFactory::createTestGroup(\@borrowers);
55
}
56
sub deleteTestGroup1 {
57
    t::db_dependent::TestObjects::Borrowers::BorrowerFactory::_deleteTestGroupFromIdentifiers(\@testGroup1Identifiers);
58
}
59
60
1;
(-)a/t/db_dependent/TestObjects/Issues/ExampleIssues.pm (+41 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Issues::ExampleIssues;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use t::db_dependent::TestObjects::Issues::IssueFactory;
25
26
=head createTestGroupX
27
28
    You should use the appropriate Factory-class to create these test-objects.
29
30
=cut
31
32
33
34
sub createTestGroup1 {
35
    return t::db_dependent::TestObjects::Issues::IssueFactory::createTestGroup();
36
}
37
sub deleteTestGroup1 {
38
    t::db_dependent::TestObjects::Issues::IssueFactory::_deleteTestGroupFromIdentifiers();
39
}
40
41
1;
(-)a/t/db_dependent/TestObjects/Issues/IssueFactory.pm (+143 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Issues::IssueFactory;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use C4::Circulation;
25
use C4::Members;
26
use C4::Items;
27
28
use t::db_dependent::TestObjects::Issues::ExampleIssues;
29
use t::db_dependent::TestObjects::ObjectFactory;
30
31
=head t::db_dependent::TestObjects::Issues::IssueFactory::createTestGroup( $data [, $hashKey, $checkoutBranchRule] )
32
Returns a Issues-HASH.
33
The HASH is keyed with the PRIMARY KEY, or the given $hashKey.
34
35
@PARAM1, ARRAY of HASHes.
36
  [ {
37
        cardnumber  => '167Azava0001',
38
        barcode     => '167Nfafa0010',
39
        daysOverdue => 7,     #This issue's duedate was 7 days ago. If undef, then uses today as the checkout day.
40
        daysAgoIssued  => 28, #This Issue hapened 28 days ago. If undef, then uses today.
41
    },
42
    {
43
        ...
44
    }
45
  ]
46
@PARAM2, String, the HASH-element to use as the returning HASHes key.
47
@PARAM3, String, the rule on where to check these Issues out:
48
                 'homebranch', uses the Item's homebranch as the checkout branch
49
                 'holdingbranch', uses the Item's holdingbranch as the checkout branch
50
                 undef, uses the current Environment branch
51
                 '<branchCode>', checks out all Issues from the given branchCode
52
=cut
53
54
sub createTestGroup {
55
    my ($objects, $hashKey, $checkoutBranchRule) = @_;
56
57
    my $oldContextBranch = C4::Context->userenv()->{branch};
58
59
    my %objects;
60
    foreach my $issueParams (@$objects) {
61
        my $borrower = C4::Members::GetMember(cardnumber => $issueParams->{cardnumber});
62
        my $item = C4::Items::GetItem(undef, $issueParams->{barcode});
63
64
        my $duedate = DateTime->now(time_zone => C4::Context->tz());
65
        if ($issueParams->{daysOverdue}) {
66
            $duedate->subtract(days =>  $issueParams->{daysOverdue}  );
67
        }
68
69
        my $issuedate = DateTime->now(time_zone => C4::Context->tz());
70
        if ($issueParams->{daysAgoIssued}) {
71
            $issuedate->subtract(days =>  $issueParams->{daysAgoIssued}  );
72
        }
73
74
        #Set the checkout branch
75
        my $checkoutBranch;
76
        if (not($checkoutBranchRule)) {
77
            #Use the existing userenv()->{branch}
78
        }
79
        elsif ($checkoutBranchRule eq 'homebranch') {
80
            $checkoutBranch = $item->{homebranch};
81
        }
82
        elsif ($checkoutBranchRule eq 'holdingbranch') {
83
            $checkoutBranch = $item->{holdingbranch};
84
        }
85
        elsif ($checkoutBranchRule) {
86
            $checkoutBranch = $checkoutBranchRule;
87
        }
88
        C4::Context->userenv()->{branch} = $checkoutBranch if $checkoutBranch;
89
90
        my $datedue = C4::Circulation::AddIssue( $borrower, $issueParams->{barcode}, $duedate, undef, $issuedate );
91
        #We want the issue_id as well.
92
        my $issues = C4::Circulation::GetIssues({ borrowernumber => $borrower->{borrowernumber}, itemnumber => $item->{itemnumber} });
93
        my $issue = $issues->[0];
94
        unless ($issue) {
95
            carp "IssueFactory:> No issue for cardnumber '".$issueParams->{cardnumber}."' and barcode '".$issueParams->{barcode}."'";
96
            next();
97
        }
98
99
        my $key = t::db_dependent::TestObjects::ObjectFactory::getHashKey($issue, $issue->{issue_id}, $hashKey);
100
101
        $issue->{barcode} = $issueParams->{barcode}; #Save the barcode here as well for convenience.
102
        $objects{$key} = $issue;
103
    }
104
105
    C4::Context->userenv()->{branch} = $oldContextBranch;
106
    return \%objects;
107
}
108
109
=head
110
111
    my $objects = createTestGroup();
112
    ##Do funky stuff
113
    deleteTestGroup($records);
114
115
Removes the given test group from the DB.
116
117
=cut
118
119
sub deleteTestGroup {
120
    my $objects = shift;
121
122
    my $schema = Koha::Database->new_schema();
123
    while( my ($key, $object) = each %$objects) {
124
        $schema->resultset('Issue')->find($object->{issue_id})->delete();
125
    }
126
}
127
sub _deleteTestGroupFromIdentifiers {
128
    my $testGroupIdentifiers = shift;
129
130
    my $schema = Koha::Database->new_schema();
131
    foreach my $key (@$testGroupIdentifiers) {
132
        $schema->resultset('Issue')->find({"barcode" => $key})->delete();
133
    }
134
}
135
136
sub createTestGroup1 {
137
    return t::db_dependent::TestObjects::Issues::ExampleIssues::createTestGroup1();
138
}
139
sub deleteTestGroup1 {
140
    return t::db_dependent::TestObjects::Issues::ExampleIssues::deleteTestGroup1();
141
}
142
143
1;
(-)a/t/db_dependent/TestObjects/Items/ExampleItems.pm (+99 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Items::ExampleItems;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use t::db_dependent::TestObjects::Items::ItemFactory;
25
26
=head createTestGroupX
27
28
    You should use the appropriate Factory-class to create these test-objects.
29
30
=cut
31
32
my @testGroup1Identifiers = ('167Nxxx0001', '167Nxxx0002', '167Nxxx0003', '167Nxxx0004',
33
                             '167Nxxx0005', '167Nxxx0006', '167Nxxx0007', '167Nxxx0008',
34
                            );
35
36
sub shareItemsToBiblios {
37
    my ($items, $biblios) = @_;
38
    my @biblioKeys = keys %$biblios;
39
40
    for (my $i=0 ; $i<@$items ; $i++) {
41
        my $item = $items->[$i];
42
        my $biblioKey = $biblioKeys[ $i % scalar(@$biblios) ]; #Split these Items to each of the Biblios
43
        my $biblio = $biblios->{$biblioKey};
44
45
        unless ($biblio && $biblio->{biblionumber}) {
46
            carp "ExampleItems:> Item \$barcode '".$item->{barcode}."' doesn't have a biblionumber, skipping.";
47
            next();
48
        }
49
        $item->{biblionumber} = $biblio->{biblionumber};
50
    }
51
}
52
53
sub createTestGroup1 {
54
    my $biblios = shift;
55
56
    my @items = (
57
        {barcode => $testGroup1Identifiers[0],
58
         homebranch => 'CPL', holdingbranch => 'CPL',
59
         price => '0.5', replacementprice => '0.5', itype => 'BK'
60
        },
61
        {barcode => $testGroup1Identifiers[1],
62
         homebranch => 'CPL', holdingbranch => 'FFL',
63
         price => '1.5', replacementprice => '1.5', itype => 'BK'
64
        },
65
        {barcode => $testGroup1Identifiers[2],
66
         homebranch => 'CPL', holdingbranch => 'FFL',
67
         price => '2.5', replacementprice => '2.5', itype => 'BK'
68
        },
69
        {barcode => $testGroup1Identifiers[3],
70
         homebranch => 'FFL', holdingbranch => 'FFL',
71
         price => '3.5', replacementprice => '3.5', itype => 'BK'
72
        },
73
        {barcode => $testGroup1Identifiers[4],
74
         homebranch => 'FFL', holdingbranch => 'FFL',
75
         price => '4.5', replacementprice => '4.5', itype => 'VM'
76
        },
77
        {barcode => $testGroup1Identifiers[5],
78
         homebranch => 'FFL', holdingbranch => 'FFL',
79
         price => '5.5', replacementprice => '5.5', itype => 'VM'
80
        },
81
        {barcode => $testGroup1Identifiers[6],
82
         homebranch => 'FFL', holdingbranch => 'CPL',
83
         price => '6.5', replacementprice => '6.5', itype => 'VM'
84
        },
85
        {barcode => $testGroup1Identifiers[7],
86
         homebranch => 'CPL', holdingbranch => 'CPL',
87
         price => '7.5', replacementprice => '7.5', itype => 'VM'
88
        },
89
    );
90
91
    shareItemsToBiblios(\@items, $biblios);
92
93
    return t::db_dependent::TestObjects::Items::ItemFactory::createTestGroup(\@items);
94
}
95
sub deleteTestGroup1 {
96
    t::db_dependent::TestObjects::Items::ItemFactory::_deleteTestGroupFromIdentifiers(\@testGroup1Identifiers);
97
}
98
99
1;
(-)a/t/db_dependent/TestObjects/Items/ItemFactory.pm (+92 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::Items::ItemFactory;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use C4::Items;
25
26
use t::db_dependent::TestObjects::Items::ExampleItems;
27
use t::db_dependent::TestObjects::ObjectFactory;
28
29
=head t::db_dependent::TestObjects::Items::ItemFactory::createTestGroup( $data [, $hashKey] )
30
Returns a HASH of objects.
31
Each Item is expected to contain the biblionumber of the Biblio they are added into.
32
    eg. $item->{biblionumber} = 550242;
33
34
The HASH is keyed with the PRIMARY KEY, or the given $hashKey.
35
36
See C4::Items::AddItem() for how the table columns need to be given.
37
=cut
38
39
sub createTestGroup {
40
    my ($objects, $hashKey) = @_;
41
42
    my %objects;
43
    foreach my $object (@$objects) {
44
        my ($biblionumber, $biblioitemnumber, $itemnumber) = C4::Items::AddItem($object, $object->{biblionumber});
45
        my $item = C4::Items::GetItem($itemnumber, undef);
46
        unless ($item) {
47
            carp "ItemFactory:> No item for barcode '".$object->{barcode}."'";
48
            next();
49
        }
50
51
        my $key = t::db_dependent::TestObjects::ObjectFactory::getHashKey($item, $itemnumber, $hashKey);
52
53
        $objects{$key} = $item;
54
    }
55
    return \%objects;
56
}
57
58
=head
59
60
    my $objects = createTestGroup();
61
    ##Do funky stuff
62
    deleteTestGroup($records);
63
64
Removes the given test group from the DB.
65
66
=cut
67
68
sub deleteTestGroup {
69
    my $objects = shift;
70
71
    my $schema = Koha::Database->new_schema();
72
    while( my ($key, $object) = each %$objects) {
73
        $schema->resultset('Item')->find($object->{itemnumber})->delete();
74
    }
75
}
76
sub _deleteTestGroupFromIdentifiers {
77
    my $testGroupIdentifiers = shift;
78
79
    my $schema = Koha::Database->new_schema();
80
    foreach my $key (@$testGroupIdentifiers) {
81
        $schema->resultset('Item')->find({"barcode" => $key})->delete();
82
    }
83
}
84
85
sub createTestGroup1 {
86
    return t::db_dependent::TestObjects::Items::ExampleItems::createTestGroup1();
87
}
88
sub deleteTestGroup1 {
89
    return t::db_dependent::TestObjects::Items::ExampleItems::deleteTestGroup1();
90
}
91
92
1;
(-)a/t/db_dependent/TestObjects/LetterTemplates/ExampleLetterTemplates.pm (+92 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::LetterTemplates::ExampleLetterTemplates;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory;
25
26
=head createTestGroupX
27
28
    You should use the appropriate Factory-class to create these test-objects.
29
30
=cut
31
32
my @testGroup1Identifiers = ('circulation-ODUE1-CPL-print', 'circulation-ODUE2-CPL-print', 'circulation-ODUE3-CPL-print',
33
                             'circulation-ODUE1-CPL-email', 'circulation-ODUE2-CPL-email', 'circulation-ODUE3-CPL-email',
34
                             'circulation-ODUE1-CPL-sms', 'circulation-ODUE2-CPL-sms', 'circulation-ODUE3-CPL-sms',
35
                            );
36
37
sub createTestGroup1 {
38
    my @letterTemplates = (
39
        {letter_id => $testGroup1Identifiers[0],
40
         module => 'circulation', code => 'ODUE1', branchcode => 'CPL', name => 'Notice1',
41
         is_html => undef, title => 'Notice1', message_transport_type => 'print',
42
         content => '<item>Barcode: <<items.barcode>>, bring it back!</item>',
43
        },
44
        {letter_id => $testGroup1Identifiers[0],
45
         module => 'circulation', code => 'ODUE2', branchcode => 'CPL', name => 'Notice2',
46
         is_html => undef, title => 'Notice2', message_transport_type => 'print',
47
         content => '<item>Barcode: <<items.barcode>></item>',
48
        },
49
        {letter_id => $testGroup1Identifiers[0],
50
         module => 'circulation', code => 'ODUE3', branchcode => 'CPL', name => 'Notice3',
51
         is_html => undef, title => 'Notice3', message_transport_type => 'print',
52
         content => '<item>Barcode: <<items.barcode>>, bring back!</item>',
53
        },
54
        {letter_id => $testGroup1Identifiers[0],
55
         module => 'circulation', code => 'ODUE1', branchcode => 'CPL', name => 'Notice1',
56
         is_html => undef, title => 'Notice1', message_transport_type => 'email',
57
         content => '<item>Barcode: <<items.barcode>>, bring it back!</item>',
58
        },
59
        {letter_id => $testGroup1Identifiers[0],
60
         module => 'circulation', code => 'ODUE2', branchcode => 'CPL', name => 'Notice2',
61
         is_html => undef, title => 'Notice2', message_transport_type => 'email',
62
         content => '<item>Barcode: <<items.barcode>></item>',
63
        },
64
        {letter_id => $testGroup1Identifiers[0],
65
         module => 'circulation', code => 'ODUE3', branchcode => 'CPL', name => 'Notice3',
66
         is_html => undef, title => 'Notice3', message_transport_type => 'email',
67
         content => '<item>Barcode: <<items.barcode>>, bring back!</item>',
68
        },
69
        {letter_id => $testGroup1Identifiers[0],
70
         module => 'circulation', code => 'ODUE1', branchcode => 'CPL', name => 'Notice1',
71
         is_html => undef, title => 'Notice1', message_transport_type => 'sms',
72
         content => '<item>Barcode: <<items.barcode>>, bring it back!</item>',
73
        },
74
        {letter_id => $testGroup1Identifiers[0],
75
         module => 'circulation', code => 'ODUE2', branchcode => 'CPL', name => 'Notice2',
76
         is_html => undef, title => 'Notice2', message_transport_type => 'sms',
77
         content => '<item>Barcode: <<items.barcode>></item>',
78
        },
79
        {letter_id => $testGroup1Identifiers[0],
80
         module => 'circulation', code => 'ODUE3', branchcode => 'CPL', name => 'Notice3',
81
         is_html => undef, title => 'Notice3', message_transport_type => 'sms',
82
         content => '<item>Barcode: <<items.barcode>>, bring back!</item>',
83
        },
84
    );
85
86
    return t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory::createTestGroup(\@letterTemplates);
87
}
88
sub deleteTestGroup1 {
89
    t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory::_deleteTestGroupFromIdentifiers(\@testGroup1Identifiers);
90
}
91
92
1;
(-)a/t/db_dependent/TestObjects/LetterTemplates/LetterTemplateFactory.pm (+101 lines)
Line 0 Link Here
1
package t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
use C4::Letters;
25
26
use t::db_dependent::TestObjects::LetterTemplates::ExampleLetterTemplates;
27
use t::db_dependent::TestObjects::ObjectFactory qw(getHashKey);
28
29
=head t::db_dependent::TestObjects::LetterTemplates::LetterTemplateFactory::createTestGroup( $data [, $hashKey] )
30
Returns a HASH of Koha::Schema::Result::Letter
31
The HASH is keyed with the PRIMARY KEYS eg. 'circulation-ODUE2-CPL-print', or the given $hashKey.
32
=cut
33
34
#Incredibly the Letters-module has absolutely no Create or Update-component to operate on Letter templates?
35
#Tests like these are brittttle. :(
36
sub createTestGroup {
37
    my ($objects, $hashKey) = @_;
38
39
    my %objects;
40
    my $schema = Koha::Database->new()->schema();
41
    foreach my $object (@$objects) {
42
        my $rs = $schema->resultset('Letter');
43
        my $result = $rs->update_or_create({
44
                module     => $object->{module},
45
                code       => $object->{code},
46
                branchcode => ($object->{branchcode}) ? $object->{branchcode} : '',
47
                name       => $object->{name},
48
                is_html    => $object->{is_html},
49
                title      => $object->{title},
50
                message_transport_type => $object->{message_transport_type},
51
                content    => $object->{content},
52
        });
53
54
        my @pks = $result->id();
55
        my $key = t::db_dependent::TestObjects::ObjectFactory::getHashKey($object, join('-',@pks), $hashKey);
56
57
        $objects{$key} = $result;
58
    }
59
    return \%objects;
60
}
61
62
=head
63
64
    my $objects = createTestGroup();
65
    ##Do funky stuff
66
    deleteTestGroup($records);
67
68
Removes the given test group from the DB.
69
70
=cut
71
72
sub deleteTestGroup {
73
    my $objects = shift;
74
75
    my $schema = Koha::Database->new_schema();
76
    while( my ($key, $object) = each %$objects) {
77
        $object->delete();
78
    }
79
}
80
sub _deleteTestGroupFromIdentifiers {
81
    my $testGroupIdentifiers = shift;
82
83
    my $schema = Koha::Database->new_schema();
84
    foreach my $key (@$testGroupIdentifiers) {
85
        my ($module, $code, $branchcode, $mtt) = split('-',$key);
86
        $schema->resultset('Letter')->find({module => $module,
87
                                                    code => $code,
88
                                                    branchcode => $branchcode,
89
                                                    message_transport_type => $mtt,
90
                                                })->delete();
91
    }
92
}
93
94
sub createTestGroup1 {
95
    return t::db_dependent::TestObjects::LetterTemplates::ExampleLetterTemplates::createTestGroup1();
96
}
97
sub deleteTestGroup1 {
98
    return t::db_dependent::TestObjects::LetterTemplates::ExampleLetterTemplates::deleteTestGroup1();
99
}
100
101
1;
(-)a/t/db_dependent/TestObjects/ObjectFactory.pm (-1 / +33 lines)
Line 0 Link Here
0
- 
1
package t::db_dependent::TestObjects::ObjectFactory;
2
3
# Copyright Vaara-kirjastot 2015
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it under the
8
# terms of the GNU General Public License as published by the Free Software
9
# Foundation; either version 3 of the License, or (at your option) any later
10
# version.
11
#
12
# Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13
# WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14
# A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15
#
16
# You should have received a copy of the GNU General Public License along
17
# with Koha; if not, write to the Free Software Foundation, Inc.,
18
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
#
20
21
use Modern::Perl;
22
use Carp;
23
24
sub getHashKey {
25
    my ($object, $primaryKey, $hashKey) = @_;
26
27
    if ($hashKey && not($object->{$hashKey})) {
28
        carp "BiblioFactory::getHashKey($object, $primaryKey, $hashKey):> Given HASH has no \$hashKey '$hashKey'.";
29
    }
30
    return ($hashKey) ? $object->{$hashKey} : $primaryKey;
31
}
32
33
1;

Return to bug 13906