Line 0
Link Here
|
0 |
- |
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. |
14 |
# |
15 |
# You should have received a copy of the GNU General Public License |
16 |
# along with Koha; if not, see <http://www.gnu.org/licenses>. |
17 |
|
18 |
# , |
19 |
# .,cd$F |
20 |
# .,ced$$$$$$$$$$$$$$$$$$$F, |
21 |
# .c$$$$$$$$$$$$$$$$$$$$$P",z$$$c. |
22 |
# c$$$$$$$$$$$$$$$$""`.,,cd$$$$$$$$b. |
23 |
# d$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$c |
24 |
# $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$c |
25 |
# d$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$. |
26 |
# d$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
27 |
# $$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
28 |
# ?$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$ |
29 |
# `$$$$$$$$$$$$$???$$$$$$$$$$$$$$$$$$$$$$$$ |
30 |
# ?$$$$$$$$P,zcecec,"$$$$$$",cecec,"?$$$$P |
31 |
# z$?$e`$$$$$$$d$",cec,"$$$$$$$"'.,."?$$bd$$$"J$ |
32 |
# $$$r)$$$$$$$$P.P" P""L^$$$$$"J""? "`$$$$$P.$$F |
33 |
# 4$$F?$$$$$$$$ P 4 $$$$$ F `$$$$ $$$F |
34 |
# ?$F4$$$$$$$$ $ .4$$$$$.4 .4$$$$.?$$ |
35 |
# ?b.$$$Lucec$cececece$$$???%cececececece$$ $$ |
36 |
# `?$$$$$$$$$$$$$$$$$$$$d$$b^$$$$$$$$$$$$$F^" |
37 |
# ed$$$$$$$$$$$$$$$$$$$$$$F.$$$$$$$$$$$$$$ |
38 |
# $$$$$$$$$$$"$$$$$$$$.,,,z$P""^3$$$$$$$$$ |
39 |
# $$$$$$$$$$P.$$$$$$$$$$P" .e$$$$$$$$$$$F |
40 |
# ?$$$$$$$P"e$c d$$$$$$$$$$$$" |
41 |
# `"?$$$$$$$$?$. .eee$$$"e$P'd$$$$$PF" |
42 |
# .,,,,`?$bc,`",c$F.,,,,,. .::. |
43 |
# ..zd$$$$$$$$eu"???7"cd$$$$$F ::::: :. |
44 |
# .uedd$$$$$$$$$$$$$$$$$$$$$$$$$$$$$".:::::'.::::'$$$Weu. |
45 |
# ue$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$P".::::::' :::::: $$$$$$$$c |
46 |
# z$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$P".::::::::'.::::::: 9$$$$$$$$$k |
47 |
# $$$$$$$$$$$$$$$$$$$$$$$$$$$P"`.::::::::::'.:::::::: d$$$$$$$$$$$ |
48 |
# 9$$$$$$$$$$$$$$$"d$$$$$P"".::::::::::::::.::::::::'.$$$$$$$$$$$$$> |
49 |
# $$$$$$$$$$$$$$P,d$P"".:::::::::::::::::::::::::::'zeC?$$$$$$$$$$$L |
50 |
# $$$$$$$$$C?"':d".::::::::::::::::::::::::::::::'.d$$$$ -ee$$$$$$$$L |
51 |
# 9$$$$$$$$$$$ ":::::::::::::::::::::::::::::::'.$$$$$$E $$$$$$$$$$$ |
52 |
# :$$$$$$$$$$$ ::::::::::::::::::::::::::::::'.e$$$$$$$$b ?$$$$$$$$$$k |
53 |
# d$$$$$$$$$$ ::::::::::::::::::::::::::::'.e$$$$$$$$$$$$ $$$$$$$$$$$ |
54 |
# :$$$$$$$$$$E ::::::::::::::::::::::::'..,,,,,,,,,."???$$, $$$$$$$$$$$ |
55 |
# d$",cecec,"':::::::::::::::::::::'`,c>>>>'<<<<<<CCCCCcc>> .zcecec,"$$$ |
56 |
# J"z$$$$$$$$$b.`:::::::::::::::'`.cCCCCCCCCCCCCCCC>>cCCC>'e$$$$$$$$$$e"$ |
57 |
# u$$$$$$$$$$$$$$u`::::::::::'.ccCCCCCCCCCCCCCCCC'Ccccc>'d$$$$$$$$$$$$$$r> |
58 |
# d$$$$$$$$$$$$$$$$b.`:::::',cc'CCCCCCCCCCCC>(C ccCCC>',d$$$$$$$$$$$$$$$$$b |
59 |
# $$$$$$$$$$$$$$$$$$$$c.`'cCCCCCc<CCCCCCC>.cCCcCCCC',z$$$$$$$$$$$$$$$$$$$$$$ |
60 |
# $$$$$$$$$$$$$$$$$$$$$$$,<CCCCCCc`CCCCCCCCCCCCCC'z$$$$$$$$$$$$$$$$$$$$$$$$$ |
61 |
# $$$$$$$$$$$$$$$$$$$$$$$$ CCCCCCCCCCCCCCCCCCCCC'$$$$$$$$$$$'$$$$$$$$$$$$$$$ |
62 |
# $$$$$$$$$$$$$$$$$$$$$$$$ CCCCCCCCCCCCCCCCCCCC'<$$$$$$$$$$$,?$$$$$$$$$$$$$$ |
63 |
# $$$$$$$$$P?$$$$$$$$$$$$F,CCCCCCCCCCCCCCCCCCCC,<$$$$$$$$$$$$br"??$$$$$$$$$$ |
64 |
# $$$$PF"" <c"$$$$$$$$F,cCC')cCCCCCCCCC'Cc<CCCC,`$$$$$$$$$$$$" ""?$$$$$ |
65 |
# F `Cc,`"".,ccCCC)cCC'CCCCCCCCccCc`<CCCc`?$$$$$$$$P" `"? |
66 |
# `<CCCCCCCCC"-',ccCCCCCCCCCCCCCCc`<CCCCcc,``,c=" |
67 |
# `'''' `"<<CCCCCCCCCCC>>"" `'<CCC>'` |
68 |
|
69 |
use Modern::Perl; |
70 |
|
71 |
use C4::Context; |
72 |
use C4::Biblio; |
73 |
use C4::Items; |
74 |
|
75 |
use Test::More tests => 13; |
76 |
|
77 |
my $dbh = C4::Context->dbh; |
78 |
my $schema = Koha::Database->new()->schema(); |
79 |
|
80 |
# Start transaction |
81 |
$dbh->{AutoCommit} = 0; |
82 |
$dbh->{RaiseError} = 1; |
83 |
|
84 |
|
85 |
#Get the MARC subfield biblioitems.datereceived is mapped to |
86 |
my ( $datereceivedFieldCode, $datereceivedSubfieldCode ) = |
87 |
C4::Biblio::GetMarcFromKohaField( "biblioitems.datereceived", '' ); |
88 |
|
89 |
# Generate test biblio |
90 |
my $biblio = MARC::Record->new(); |
91 |
$biblio->append_fields( |
92 |
MARC::Field->new('100', ' ', ' ', a => 'Moffat, Steven'), |
93 |
MARC::Field->new('245', ' ', ' ', a => 'Proper test case constructors (setUps) and destructors (tearDowns) are golden'), |
94 |
); |
95 |
my ($biblionumber, $biblioitemnumber) = C4::Biblio::AddBiblio($biblio, ''); |
96 |
|
97 |
|
98 |
### Test1 >> that biblio.datereceived is NULL when adding a Biblio ### |
99 |
my $bibliodata = C4::Biblio::GetBiblioData($biblionumber); |
100 |
ok( (not defined $bibliodata->{datereceived}) , 'Searchable datereceived: Adding a Biblio leaves biblio.datereceived as NULL'); |
101 |
|
102 |
|
103 |
### Test2 >> items.datereceived is set when adding an Item ### |
104 |
my $datereceived = DateTime->now( time_zone => C4::Context->tz() ); |
105 |
my $datereceivedIso = $datereceived->ymd().' '.$datereceived->hms(); #Replace the 'T' in ISO standard with a ' ' |
106 |
my ( $item_bibnum, $item_bibitemnum, $itemnumber ) = C4::Items::AddItem( |
107 |
{ |
108 |
homebranch => 'MPL', |
109 |
holdingbranch => 'MPL', |
110 |
barcode => 'R00000342', |
111 |
replacementprice => 12.00, |
112 |
datereceived => $datereceivedIso, |
113 |
}, |
114 |
$biblionumber |
115 |
); |
116 |
my $item = C4::Items::GetItem($itemnumber); |
117 |
ok( ($item->{datereceived} eq $datereceivedIso) , 'Searchable datereceived: Adding an Item set items.datereceived as NOW()'); |
118 |
|
119 |
|
120 |
### Test3 >> items.datereceived can also be later modified ### |
121 |
#There should be scant need for it though, so it might be better to just javascript-hide the datereceived-column when editing Items. |
122 |
$datereceived = DateTime->now( time_zone => C4::Context->tz() ); |
123 |
$datereceivedIso = $datereceived->ymd().' '.$datereceived->hms(); #Replace the 'T' in ISO standard with a ' ' |
124 |
C4::Items::ModItem( |
125 |
{ |
126 |
datereceived => $datereceivedIso, |
127 |
}, |
128 |
$biblionumber, |
129 |
$itemnumber |
130 |
); |
131 |
$item = C4::Items::GetItem($itemnumber); |
132 |
ok( ($item->{datereceived} eq $datereceivedIso) , "Searchable datereceived: Modding an Item's items.datereceived works"); |
133 |
|
134 |
|
135 |
### Test4 && 5 >> biblio.datereceived can be set ### |
136 |
my $error = C4::Biblio::UpdateDatereceived($biblionumber); |
137 |
$bibliodata = C4::Biblio::GetBiblioData($biblionumber); |
138 |
my $record = C4::Biblio::GetMarcBiblio($biblionumber); |
139 |
|
140 |
#Compare the YMD of datereceiveds, because UpdateDatereceived set the datereceived to NOW() and seconds dont match. |
141 |
ok( (substr($bibliodata->{datereceived},0,10) eq substr($datereceivedIso,0,10)), |
142 |
"Searchable datereceived: Setting the biblio.datereceived."); |
143 |
#Compare the YMD of datereceiveds, because UpdateDatereceived set the subfield to NOW() and seconds dont match. |
144 |
ok( (substr($record->subfield($datereceivedFieldCode, $datereceivedSubfieldCode),0,10) eq substr($datereceivedIso,0,10)), |
145 |
"Searchable datereceived: Upserting the MARC Subfield datereceived is mapped to."); |
146 |
$datereceivedIso = $bibliodata->{datereceived}; #Store the new datereceived for the next test. |
147 |
|
148 |
### Test6 && 7 >> biblio.datereceived can be set only once ### |
149 |
sleep 1; #Make sure the datereceived of the previous test differs atleast by one second. |
150 |
$error = C4::Biblio::UpdateDatereceived($biblionumber); |
151 |
$bibliodata = C4::Biblio::GetBiblioData($biblionumber); |
152 |
$record = C4::Biblio::GetMarcBiblio($biblionumber); |
153 |
|
154 |
#Compare the datereceiveds. They should be the same as set during the last test. |
155 |
ok( ($bibliodata->{datereceived} eq $datereceivedIso), |
156 |
"Searchable datereceived: Setting the biblio.datereceived only once."); |
157 |
ok( ($record->subfield($datereceivedFieldCode, $datereceivedSubfieldCode) eq $datereceivedIso), |
158 |
"Searchable datereceived: Upserting the MARC Subfield datereceived is mapped only once."); |
159 |
|
160 |
|
161 |
### Test8 && 9 >> Overriding the biblio.datereceived using a bibliodata-hash. ### |
162 |
$bibliodata->{datereceived} = undef; |
163 |
$datereceived = DateTime->new(time_zone => C4::Context->tz(), |
164 |
year => 1985, |
165 |
month => 12, |
166 |
day => 10, |
167 |
hour => 2); |
168 |
$datereceivedIso = $datereceived->ymd().' '.$datereceived->hms(); #Replace the 'T' in ISO standard with a ' ' |
169 |
$error = C4::Biblio::UpdateDatereceived($bibliodata, $datereceived); |
170 |
$bibliodata = C4::Biblio::GetBiblioData($biblionumber); |
171 |
$record = C4::Biblio::GetMarcBiblio($biblionumber); |
172 |
|
173 |
#Compare the datereceiveds. They should be the same as set during the last test. |
174 |
ok( ($bibliodata->{datereceived} eq $datereceivedIso), |
175 |
"Searchable datereceived: Overriding the biblio.datereceived."); |
176 |
ok( ($record->subfield($datereceivedFieldCode, $datereceivedSubfieldCode) eq $datereceivedIso), |
177 |
"Searchable datereceived: Overriding the MARC Subfield datereceived is mapped."); |
178 |
|
179 |
|
180 |
### Test10 >> No biblionumber. ### |
181 |
$bibliodata->{biblionumber} = undef; |
182 |
$error = C4::Biblio::UpdateDatereceived($bibliodata); |
183 |
ok( ($error eq 'NO_BIBLIONUMBER'), |
184 |
"Searchable datereceived: ERROR, No biblionumber in bibliodata caught."); |
185 |
|
186 |
### Test11 >> No biblionumber2. ### |
187 |
$error = C4::Biblio::UpdateDatereceived(undef); |
188 |
ok( ($error eq 'NO_BIBLIONUMBER'), |
189 |
"Searchable datereceived: ERROR, No biblionumber caught."); |
190 |
|
191 |
### Test12 >> No bibliodate found from biblionumber. ### |
192 |
$error = C4::Biblio::UpdateDatereceived(9999559995); |
193 |
ok( ($error eq 'NO_BIBLIODATA'), |
194 |
"Searchable datereceived: ERROR, No bibliodata from biblionumber caught."); |
195 |
|
196 |
### Test13 >> datereceived must be a DateTime. ### |
197 |
$error = C4::Biblio::UpdateDatereceived($biblionumber, '2012-12-31T23:45:12'); |
198 |
ok( ($error eq 'NOT_DATETIME'), |
199 |
"Searchable datereceived: ERROR, Not a DateTime caught."); |
200 |
|
201 |
|
202 |
$dbh->rollback(); |