Bug 10306: (QA follow-up) Correct a TestBuilder call
[koha.git] / t / db_dependent / Koha.t
index 048185a..64bb045 100644 (file)
@@ -3,25 +3,32 @@
 # This is to test C4/Koha
 # It requires a working Koha database with the sample data
 
-use strict;
-use warnings;
+use Modern::Perl;
+use DateTime::Format::MySQL;
+use Test::More tests => 6;
+
+use t::lib::TestBuilder;
+
 use C4::Context;
+use Koha::Database;
 use Koha::DateUtils qw(dt_from_string);
-
-use Test::More tests => 10;
-use DateTime::Format::MySQL;
+use Koha::AuthorisedValue;
+use Koha::AuthorisedValueCategories;
 
 BEGIN {
-    use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesByCategory GetItemTypesCategorized));
+    use_ok('C4::Koha', qw( :DEFAULT GetDailyQuote GetItemTypesCategorized));
     use_ok('C4::Members');
 }
 
+my $schema  = Koha::Database->new->schema;
+$schema->storage->txn_begin;
+my $builder = t::lib::TestBuilder->new;
 my $dbh = C4::Context->dbh;
-$dbh->{AutoCommit} = 0;
-$dbh->{RaiseError} = 1;
+
+our $itype_1 = $builder->build({ source => 'Itemtype' });
 
 subtest 'Authorized Values Tests' => sub {
-    plan tests => 7;
+    plan tests => 3;
 
     my $data = {
         category            => 'CATEGORY',
@@ -31,27 +38,19 @@ subtest 'Authorized Values Tests' => sub {
         imageurl            => 'IMAGEURL'
     };
 
-
+    my $avc = Koha::AuthorisedValueCategories->find($data->{category});
+    Koha::AuthorisedValueCategory->new({ category_name => $data->{category} })->store unless $avc;
 # Insert an entry into authorised_value table
-    my $insert_success = AddAuthorisedValue($data->{category}, $data->{authorised_value}, $data->{lib}, $data->{lib_opac}, $data->{imageurl});
-    ok($insert_success, "Insert data in database");
-
-
-# Tests
-    SKIP: {
-        skip "INSERT failed", 4 unless $insert_success;
+    my $insert_success = Koha::AuthorisedValue->new(
+        {   category         => $data->{category},
+            authorised_value => $data->{authorised_value},
+            lib              => $data->{lib},
+            lib_opac         => $data->{lib_opac},
+            imageurl         => $data->{imageurl}
+        }
+    )->store;
+    ok( $insert_success, "Insert data in database" );
 
-        is ( GetAuthorisedValueByCode($data->{category}, $data->{authorised_value}), $data->{lib}, "GetAuthorisedValueByCode" );
-
-        my $sortdet=C4::Members::GetSortDetails("lost", "3");
-        is ($sortdet, "Lost and Paid For", "lost and paid works");
-
-        my $sortdet2=C4::Members::GetSortDetails("loc", "child");
-        is ($sortdet2, "Children's Area", "Child area works");
-
-        my $sortdet3=C4::Members::GetSortDetails("withdrawn", "1");
-        is ($sortdet3, "Withdrawn", "Withdrawn works");
-    }
 
 # Clean up
     if($insert_success){
@@ -63,11 +62,35 @@ subtest 'Authorized Values Tests' => sub {
     SKIP: {
         eval { require Test::Deep; import Test::Deep; };
         skip "Test::Deep required to run the GetAuthorisedValues() tests.", 2 if $@;
-        AddAuthorisedValue('BUG10656', 'ZZZ', 'Z_STAFF', 'A_PUBLIC', '');
-        AddAuthorisedValue('BUG10656', 'AAA', 'A_STAFF', 'Z_PUBLIC', '');
+        Koha::AuthorisedValueCategory->new({ category_name => 'BUG10656' })->store;
+        Koha::AuthorisedValue->new(
+            {   category         => 'BUG10656',
+                authorised_value => 'ZZZ',
+                lib              => 'Z_STAFF',
+                lib_opac         => 'A_PUBLIC',
+                imageurl         => ''
+            }
+        )->store;
+        Koha::AuthorisedValue->new(
+            {   category         => 'BUG10656',
+                authorised_value => 'AAA',
+                lib              => 'A_STAFF',
+                lib_opac         => 'Z_PUBLIC',
+                imageurl         => ''
+            }
+        )->store;
+
         # the next one sets lib_opac to NULL; in that case, the staff
         # display value is meant to be used.
-        AddAuthorisedValue('BUG10656', 'DDD', 'D_STAFF', undef, '');
+        Koha::AuthorisedValue->new(
+            {   category         => 'BUG10656',
+                authorised_value => 'DDD',
+                lib              => 'D_STAFF',
+                lib_opac         => undef,
+                imageurl         => ''
+            }
+        )->store;
+
         my $authvals = GetAuthorisedValues('BUG10656');
         cmp_deeply(
             $authvals,
@@ -76,7 +99,6 @@ subtest 'Authorized Values Tests' => sub {
                     id => ignore(),
                     category => 'BUG10656',
                     authorised_value => 'AAA',
-                    selected => 0,
                     lib => 'A_STAFF',
                     lib_opac => 'Z_PUBLIC',
                     imageurl => '',
@@ -85,7 +107,6 @@ subtest 'Authorized Values Tests' => sub {
                     id => ignore(),
                     category => 'BUG10656',
                     authorised_value => 'DDD',
-                    selected => 0,
                     lib => 'D_STAFF',
                     lib_opac => undef,
                     imageurl => '',
@@ -94,7 +115,6 @@ subtest 'Authorized Values Tests' => sub {
                     id => ignore(),
                     category => 'BUG10656',
                     authorised_value => 'ZZZ',
-                    selected => 0,
                     lib => 'Z_STAFF',
                     lib_opac => 'A_PUBLIC',
                     imageurl => '',
@@ -102,7 +122,7 @@ subtest 'Authorized Values Tests' => sub {
             ],
             'list of authorised values in staff mode sorted by staff label (bug 10656)'
         );
-        $authvals = GetAuthorisedValues('BUG10656', '', 1);
+        $authvals = GetAuthorisedValues('BUG10656', 1);
         cmp_deeply(
             $authvals,
             [
@@ -110,7 +130,6 @@ subtest 'Authorized Values Tests' => sub {
                     id => ignore(),
                     category => 'BUG10656',
                     authorised_value => 'ZZZ',
-                    selected => 0,
                     lib => 'A_PUBLIC',
                     lib_opac => 'A_PUBLIC',
                     imageurl => '',
@@ -119,7 +138,6 @@ subtest 'Authorized Values Tests' => sub {
                     id => ignore(),
                     category => 'BUG10656',
                     authorised_value => 'DDD',
-                    selected => 0,
                     lib => 'D_STAFF',
                     lib_opac => undef,
                     imageurl => '',
@@ -128,7 +146,6 @@ subtest 'Authorized Values Tests' => sub {
                     id => ignore(),
                     category => 'BUG10656',
                     authorised_value => 'AAA',
-                    selected => 0,
                     lib => 'Z_PUBLIC',
                     lib_opac => 'Z_PUBLIC',
                     imageurl => '',
@@ -140,12 +157,6 @@ subtest 'Authorized Values Tests' => sub {
 
 };
 
-subtest 'Itemtype info Tests' => sub {
-    like ( getitemtypeinfo('BK')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on unspecified interface returns intranet imageurl (legacy behavior)' );
-    like ( getitemtypeinfo('BK', 'intranet')->{'imageurl'}, qr/intranet-tmpl/, 'getitemtypeinfo on "intranet" interface returns intranet imageurl' );
-    like ( getitemtypeinfo('BK', 'opac')->{'imageurl'}, qr/opac-tmpl/, 'getitemtypeinfo on "opac" interface returns opac imageurl' );
-};
-
 ### test for C4::Koha->GetDailyQuote()
 SKIP:
     {
@@ -203,21 +214,14 @@ $dbh->do($sql);
 }
 
 
-#
-# test that &slashifyDate returns correct (non-US) date
-#
-subtest 'Date and ISBN tests' => sub {
-    plan tests => 7;
+subtest 'ISBN tests' => sub {
+    plan tests => 6;
 
-    my $date    = "01/01/2002";
-    my $newdate = &slashifyDate("2002-01-01");
     my $isbn13  = "9780330356473";
     my $isbn13D = "978-0-330-35647-3";
     my $isbn10  = "033035647X";
     my $isbn10D = "0-330-35647-X";
-    ok( $date eq $newdate, 'slashifyDate' );
-    my $undef = undef;
-    is( xml_escape($undef), '',
+    is( xml_escape(undef), '',
         'xml_escape() returns empty string on undef input' );
     my $str = q{'"&<>'};
     is(
@@ -235,72 +239,17 @@ subtest 'Date and ISBN tests' => sub {
 
 };
 
-subtest 'getFacets() tests' => sub {
-    plan tests => 2;
-
-    C4::Context->set_preference('singleBranchMode', 0);
-    my $facets = C4::Koha::getFacets();
-    is(
-        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
-        1,
-        'location facet present with singleBranchMode off (bug 10078)'
-    );
-    C4::Context->set_preference('singleBranchMode', 1);
-    $facets = C4::Koha::getFacets();
-    is(
-        scalar( grep { defined $_->{idx} && $_->{idx} eq 'location' } @$facets ),
-        1,
-        'location facet present with singleBranchMode on (bug 10078)'
-    );
-};
-
-subtest 'GetFrameworksLoop() tests' => sub {
-    plan tests => 6;
-
-    $dbh->do("DELETE FROM biblio_framework");
-
-    my $frameworksloop = GetFrameworksLoop();
-    is ( scalar(@$frameworksloop), 0, 'No frameworks' );
-
-    $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'A', 'Third framework'  )");
-    $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'B', 'Second framework' )");
-    $dbh->do("INSERT INTO biblio_framework ( frameworkcode, frameworktext ) VALUES ( 'C', 'First framework'  )");
-
-    $frameworksloop = GetFrameworksLoop();
-    is ( scalar(@$frameworksloop), 3, 'All frameworks' );
-    is ( scalar ( grep { defined $_->{'selected'} } @$frameworksloop ), 0, 'None selected' );
-
-    $frameworksloop = GetFrameworksLoop( 'B' );
-    is ( scalar ( grep { defined $_->{'selected'} } @$frameworksloop ), 1, 'One selected' );
-    my @descriptions = map { $_->{'description'} } @$frameworksloop;
-    is ( $descriptions[0], 'First framework', 'Ordered result' );
-    cmp_deeply(
-        $frameworksloop,
-        [
-            {
-                'value' => 'C',
-                'description' => 'First framework',
-                'selected' => undef,
-            },
-            {
-                'value' => 'B',
-                'description' => 'Second framework',
-                'selected' => 1,                # selected
-            },
-            {
-                'value' => 'A',
-                'description' => 'Third framework',
-                'selected' => undef,
-            }
-        ],
-        'Full check, sorted by description with selected val (Bug 12675)'
-    );
-};
-
-subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
+subtest 'GetItemTypesCategorized test' => sub{
     plan tests => 7;
 
-    my $insertGroup = AddAuthorisedValue('ITEMTYPECAT', 'Qwertyware');
+    my $avc = Koha::AuthorisedValueCategories->find('ITEMTYPECAT');
+    Koha::AuthorisedValueCategory->new({ category_name => 'ITEMTYPECAT' })->store unless $avc;
+    my $insertGroup = Koha::AuthorisedValue->new(
+        {   category         => 'ITEMTYPECAT',
+            authorised_value => 'Quertyware',
+        }
+    )->store;
+
     ok($insertGroup, "Create group Qwertyware");
 
     my $query = "INSERT into itemtypes (itemtype, description, searchcategory, hideinopac) values (?,?,?,?)";
@@ -310,15 +259,21 @@ subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
     $insertSth->execute('BKghjklo3', 'Yet another type of book', 'Qwertyware', 0);
 
     # Azertyware should not exist.
-    my @results = GetItemTypesByCategory('Azertyware');
-    is(scalar @results, 0, 'GetItemTypesByCategory: Invalid category returns nothing');
+    my @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Azertyware' });
+    is( @itemtypes, 0, 'Search item types by searchcategory: Invalid category returns nothing');
 
-    @results = GetItemTypesByCategory('Qwertyware');
+    @itemtypes = Koha::ItemTypes->search({ searchcategory => 'Qwertyware' });
+    my @got = map { $_->itemtype } @itemtypes;
     my @expected = ( 'BKghjklo2', 'BKghjklo3' );
-    is_deeply(\@results,\@expected,'GetItemTypesByCategory: valid category returns itemtypes');
+    is_deeply(\@got,\@expected,'Search item types by searchcategory: valid category returns itemtypes');
 
     # add more data since GetItemTypesCategorized's search is more subtle
-    $insertGroup = AddAuthorisedValue('ITEMTYPECAT', 'Veryheavybook');
+    $insertGroup = Koha::AuthorisedValue->new(
+        {   category         => 'ITEMTYPECAT',
+            authorised_value => 'Varyheavybook',
+        }
+    )->store;
+
     $insertSth->execute('BKghjklo4', 'Another hidden book', 'Veryheavybook', 1);
 
     my $hrCat = GetItemTypesCategorized();
@@ -331,7 +286,7 @@ subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
     ok(exists $hrCat->{Qwertyware}, 'GetItemTypesCategorized: partially visible category exists');
 
     my @only = ( 'BKghjklo1', 'BKghjklo2', 'BKghjklo3', 'BKghjklo4', 'BKghjklo5', 'Qwertyware', 'Veryheavybook' );
-    @results = ();
+    my @results = ();
     foreach my $key (@only) {
         push @results, $key if exists $hrCat->{$key};
     }
@@ -339,13 +294,4 @@ subtest 'GetItemTypesByCategory GetItemTypesCategorized test' => sub{
     is_deeply(\@results,\@expected, 'GetItemTypesCategorized: grouped and ungrouped items returned as expected.');
 };
 
-subtest 'GetItemTypes test' => sub {
-    plan tests => 1;
-    $dbh->do(q|DELETE FROM itemtypes|);
-    $dbh->do(q|INSERT INTO itemtypes(itemtype, description) VALUES ('a', 'aa desc'), ('b', 'zz desc'), ('d', 'dd desc'), ('c', 'yy desc')|);
-    my $itemtypes = C4::Koha::GetItemTypes( style => 'array' );
-    $itemtypes = [ map { $_->{itemtype} } @$itemtypes ];
-    is_deeply( $itemtypes, [ 'a', 'd', 'c', 'b' ], 'GetItemTypes(array) should return itemtypes ordered by description');
-};
-
-$dbh->rollback();
+$schema->storage->txn_rollback;