3 # This is to test C4/Koha
4 # It requires a working Koha database with the sample data
9 use Koha
::DateUtils
qw(dt_from_string);
11 use Test
::More tests
=> 8;
12 use DateTime
::Format
::MySQL
;
15 use_ok
('C4::Koha', qw( :DEFAULT GetDailyQuote ));
16 use_ok
('C4::Members');
19 my $dbh = C4
::Context
->dbh;
20 $dbh->{AutoCommit
} = 0;
21 $dbh->{RaiseError
} = 1;
23 subtest
'Authorized Values Tests' => sub {
27 category
=> 'CATEGORY',
28 authorised_value
=> 'AUTHORISED_VALUE',
30 lib_opac
=> 'LIBOPAC',
31 imageurl
=> 'IMAGEURL'
35 # Insert an entry into authorised_value table
36 my $insert_success = AddAuthorisedValue
($data->{category
}, $data->{authorised_value
}, $data->{lib
}, $data->{lib_opac
}, $data->{imageurl
});
37 ok
($insert_success, "Insert data in database");
42 skip
"INSERT failed", 5 unless $insert_success;
44 is
( GetAuthorisedValueByCode
($data->{category
}, $data->{authorised_value
}), $data->{lib
}, "GetAuthorisedValueByCode" );
45 is
( GetKohaImageurlFromAuthorisedValues
($data->{category
}, $data->{lib
}), $data->{imageurl
}, "GetKohaImageurlFromAuthorisedValues" );
47 my $sortdet=C4
::Members
::GetSortDetails
("lost", "3");
48 is
($sortdet, "Lost and Paid For", "lost and paid works");
50 my $sortdet2=C4
::Members
::GetSortDetails
("loc", "child");
51 is
($sortdet2, "Children's Area", "Child area works");
53 my $sortdet3=C4
::Members
::GetSortDetails
("withdrawn", "1");
54 is
($sortdet3, "Withdrawn", "Withdrawn works");
59 my $query = "DELETE FROM authorised_values WHERE category=? AND authorised_value=? AND lib=? AND lib_opac=? AND imageurl=?;";
60 my $sth = $dbh->prepare($query);
61 $sth->execute($data->{category
}, $data->{authorised_value
}, $data->{lib
}, $data->{lib_opac
}, $data->{imageurl
});
65 eval { require Test
::Deep
; import Test
::Deep
; };
66 skip
"Test::Deep required to run the GetAuthorisedValues() tests.", 2 if $@
;
67 AddAuthorisedValue
('BUG10656', 'ZZZ', 'Z_STAFF', 'A_PUBLIC', '');
68 AddAuthorisedValue
('BUG10656', 'AAA', 'A_STAFF', 'Z_PUBLIC', '');
69 # the next one sets lib_opac to NULL; in that case, the staff
70 # display value is meant to be used.
71 AddAuthorisedValue
('BUG10656', 'DDD', 'D_STAFF', undef, '');
72 my $authvals = GetAuthorisedValues
('BUG10656');
78 category
=> 'BUG10656',
79 authorised_value
=> 'AAA',
82 lib_opac
=> 'Z_PUBLIC',
87 category
=> 'BUG10656',
88 authorised_value
=> 'DDD',
96 category
=> 'BUG10656',
97 authorised_value
=> 'ZZZ',
100 lib_opac
=> 'A_PUBLIC',
104 'list of authorised values in staff mode sorted by staff label (bug 10656)'
106 $authvals = GetAuthorisedValues
('BUG10656', '', 1);
112 category
=> 'BUG10656',
113 authorised_value
=> 'ZZZ',
116 lib_opac
=> 'A_PUBLIC',
121 category
=> 'BUG10656',
122 authorised_value
=> 'DDD',
130 category
=> 'BUG10656',
131 authorised_value
=> 'AAA',
134 lib_opac
=> 'Z_PUBLIC',
138 'list of authorised values in OPAC mode sorted by OPAC label (bug 10656)'
144 subtest
'Itemtype info Tests' => sub {
145 like
( getitemtypeinfo
('BK')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
146 like
( getitemtypeinfo
('BK', 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
147 like
( getitemtypeinfo
('BK', 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
164 ### test for C4::Koha->GetDailyQuote()
167 eval { require Test
::Deep
; import Test
::Deep
; };
168 skip
"Test::Deep required to run the GetDailyQuote tests.", 1 if $@
;
170 subtest
'Daily Quotes Test' => sub {
175 skip
"C4::Koha can't \'GetDailyQuote\'!", 3 unless can_ok
('C4::Koha','GetDailyQuote');
177 # Fill the quote table with the default needed and a spare
178 $dbh->do("DELETE FROM quotes WHERE id=3 OR id=25;");
179 my $sql = "INSERT INTO quotes (id,source,text,timestamp) VALUES
180 (25,'Richard Nixon','When the President does it, that means that it is not illegal.','0000-00-00 00:00:00'),
181 (3,'Abraham Lincoln','Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.','0000-00-00 00:00:00');";
184 my $expected_quote = {
186 source
=> 'Abraham Lincoln',
187 text
=> 'Four score and seven years ago our fathers brought forth on this continent, a new nation, conceived in Liberty, and dedicated to the proposition that all men are created equal.',
188 timestamp
=> re
('\d{4}-\d{2}-\d{2}\s\d{2}:\d{2}:\d{2}'), #'0000-00-00 00:00:00',
191 # test quote retrieval based on id
193 my $quote = GetDailyQuote
('id'=>3);
194 cmp_deeply
($quote, $expected_quote, "Got a quote based on id.") or
195 diag
('Be sure to run this test on a clean install of sample data.');
197 # test quote retrieval based on today's date
199 my $query = 'UPDATE quotes SET timestamp = ? WHERE id = ?';
200 my $sth = C4
::Context
->dbh->prepare($query);
201 $sth->execute(DateTime
::Format
::MySQL
->format_datetime( dt_from_string
() ), $expected_quote->{'id'});
203 DateTime
::Format
::MySQL
->format_datetime( dt_from_string
() ) =~ m/(\d{4}-\d{2}-\d{2})/;
204 $expected_quote->{'timestamp'} = re
("^$1");
206 # $expected_quote->{'timestamp'} = DateTime::Format::MySQL->format_datetime( dt_from_string() ); # update the timestamp of expected quote data
208 $quote = GetDailyQuote
(); # this is the "default" mode of selection
209 cmp_deeply
($quote, $expected_quote, "Got a quote based on today's date.") or
210 diag
('Be sure to run this test on a clean install of sample data.');
212 # test random quote retrieval
214 $quote = GetDailyQuote
('random'=>1);
215 ok
($quote, "Got a random quote.");
222 # test that &slashifyDate returns correct (non-US) date
224 subtest
'Date and ISBN tests' => sub {
227 my $date = "01/01/2002";
228 my $newdate = &slashifyDate
("2002-01-01");
229 my $isbn13 = "9780330356473";
230 my $isbn13D = "978-0-330-35647-3";
231 my $isbn10 = "033035647X";
232 my $isbn10D = "0-330-35647-X";
233 ok
( $date eq $newdate, 'slashifyDate' );
235 is
( xml_escape
($undef), '',
236 'xml_escape() returns empty string on undef input' );
240 ''"&<>'',
241 'xml_escape() works as expected'
243 is
( $str, q{'"&<>'}, '... and does not change input in place' );
244 is
( C4
::Koha
::_isbn_cleanup
('0-590-35340-3'),
245 '0590353403', '_isbn_cleanup removes hyphens' );
246 is
( C4
::Koha
::_isbn_cleanup
('0590353403 (pbk.)'),
247 '0590353403', '_isbn_cleanup removes parenthetical' );
248 is
( C4
::Koha
::_isbn_cleanup
('978-0-321-49694-2'),
249 '0321496949', '_isbn_cleanup converts ISBN-13 to ISBN-10' );
253 subtest
'getFacets() tests' => sub {
256 C4
::Context
->set_preference('singleBranchMode', 0);
257 my $facets = C4
::Koha
::getFacets
();
259 scalar( grep { defined $_->{idx
} && $_->{idx
} eq 'location' } @
$facets ),
261 'location facet present with singleBranchMode off (bug 10078)'
263 C4
::Context
->set_preference('singleBranchMode', 1);
264 $facets = C4
::Koha
::getFacets
();
266 scalar( grep { defined $_->{idx
} && $_->{idx
} eq 'location' } @
$facets ),
268 'location facet present with singleBranchMode on (bug 10078)'
272 subtest
'GetFrameworksLoop() tests' => sub {
275 $dbh->do("DELETE FROM biblio_framework");
277 my $frameworksloop = GetFrameworksLoop
();
278 is
( scalar(@
$frameworksloop), 0, 'No frameworks' );
280 $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'A', 'Third framework' )");
281 $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'B', 'Second framework' )");
282 $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'C', 'First framework' )");
284 $frameworksloop = GetFrameworksLoop
();
285 is
( scalar(@
$frameworksloop), 3, 'All frameworks' );
286 is
( scalar ( grep { defined $_->{'selected'} } @
$frameworksloop ), 0, 'None selected' );
288 $frameworksloop = GetFrameworksLoop
( 'B' );
289 is
( scalar ( grep { defined $_->{'selected'} } @
$frameworksloop ), 1, 'One selected' );
290 my @descriptions = map { $_->{'description'} } @
$frameworksloop;
291 is
( $descriptions[0], 'First framework', 'Ordered result' );
297 'description' => 'First framework',
302 'description' => 'Second framework',
303 'selected' => 1, # selected
307 'description' => 'Third framework',
311 'Full check, sorted by description with selected val (Bug 12675)'