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

(-)a/t/db_dependent/Koha/Items.t (-2 / +121 lines)
Lines 19-25 Link Here
19
19
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::More tests => 15;
22
use Test::More tests => 16;
23
23
24
use Test::MockModule;
24
use Test::MockModule;
25
use Test::Exception;
25
use Test::Exception;
Lines 27-32 use Time::Fake; Link Here
27
27
28
use C4::Circulation qw( AddIssue LostItem AddReturn );
28
use C4::Circulation qw( AddIssue LostItem AddReturn );
29
use C4::Context;
29
use C4::Context;
30
use C4::Serials qw( NewIssue AddItem2Serial );
30
use Koha::Item;
31
use Koha::Item;
31
use Koha::Item::Transfer::Limits;
32
use Koha::Item::Transfer::Limits;
32
use Koha::Items;
33
use Koha::Items;
Lines 1811-1813 subtest 'move_to_biblio() tests' => sub { Link Here
1811
    $schema->storage->txn_rollback;
1812
    $schema->storage->txn_rollback;
1812
1813
1813
};
1814
};
1814
- 
1815
1816
subtest 'search_ordered' => sub {
1817
1818
    plan tests => 6;
1819
1820
    $schema->storage->txn_begin;
1821
1822
    my $library_a = $builder->build_object(
1823
        { class => 'Koha::Libraries', value => { branchname => 'TEST_A' } } );
1824
    my $library_z = $builder->build_object(
1825
        { class => 'Koha::Libraries', value => { branchname => 'TEST_Z' } } );
1826
    my $biblio = $builder->build_sample_biblio( { serial => 0 } );
1827
    my $item1 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1828
    my $item2 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1829
    my $item3 = $builder->build_sample_item({ biblionumber => $biblio->biblionumber });
1830
1831
    { # Is not a serial
1832
1833
        # order_by homebranch.branchname
1834
        $item1->discard_changes->update( { homebranch => $library_z->branchcode } );
1835
        $item2->discard_changes->update( { homebranch => $library_a->branchcode } );
1836
        $item3->discard_changes->update( { homebranch => $library_z->branchcode } );
1837
        is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1838
            [ $item2->itemnumber, $item1->itemnumber, $item3->itemnumber ] );
1839
1840
        # order_by me.enumchron
1841
        $biblio->items->update( { homebranch => $library_a->branchcode } );
1842
        $item1->discard_changes->update( { enumchron => 'cc' } );
1843
        $item2->discard_changes->update( { enumchron => 'bb' } );
1844
        $item3->discard_changes->update( { enumchron => 'aa' } );
1845
        is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1846
            [ $item3->itemnumber, $item2->itemnumber, $item1->itemnumber ] );
1847
1848
        # order_by LPAD( me.copynumber, 8, '0' )
1849
        $biblio->items->update( { enumchron => undef } );
1850
        $item1->discard_changes->update( { copynumber => '12345678' } );
1851
        $item2->discard_changes->update( { copynumber => '34567890' } );
1852
        $item3->discard_changes->update( { copynumber => '23456789' } );
1853
        is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1854
            [ $item1->itemnumber, $item3->itemnumber, $item2->itemnumber ] );
1855
1856
        # order_by -desc => 'me.dateaccessioned'
1857
        $biblio->items->update( { copynumber => undef } );
1858
        $item1->discard_changes->update( { dateaccessioned => '2022-08-19' } );
1859
        $item2->discard_changes->update( { dateaccessioned => '2022-07-19' } );
1860
        $item3->discard_changes->update( { dateaccessioned => '2022-09-19' } );
1861
        is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1862
            [ $item3->itemnumber, $item1->itemnumber, $item2->itemnumber ] );
1863
    }
1864
1865
    {    # Is a serial
1866
1867
        my $sub_freq = $builder->build( { source => 'SubscriptionFrequency' } );
1868
        my $sub_np =
1869
          $builder->build( { source => 'SubscriptionNumberpattern' } );
1870
        my $subscription = $builder->build_object(
1871
            {
1872
                class => 'Koha::Subscriptions',
1873
                value => {
1874
                    biblionumber  => $biblio->biblionumber,
1875
                    periodicity   => $sub_freq->{id},
1876
                    numberpattern => $sub_np->{id}
1877
                }
1878
            }
1879
        );
1880
        $builder->build_object(
1881
            {
1882
                class => 'Koha::Subscription::Histories',
1883
                value => {
1884
                    subscriptionid => $subscription->subscriptionid,
1885
                    biblionumber   => $biblio->biblionumber
1886
                }
1887
            }
1888
        );
1889
1890
        $biblio->update( { serial => 1 } );
1891
        my $serialid1 =
1892
          C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1893
            $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1894
            "notes", "routingnotes" );
1895
        C4::Serials::AddItem2Serial( $serialid1, $item1->itemnumber );
1896
        my $serialid2 =
1897
          C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1898
            $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1899
            "notes", "routingnotes" );
1900
        C4::Serials::AddItem2Serial( $serialid2, $item2->itemnumber );
1901
        my $serialid3 =
1902
          C4::Serials::NewIssue( "serialseq", $subscription->subscriptionid,
1903
            $biblio->biblionumber, 1, undef, undef, "publisheddatetext",
1904
            "notes", "routingnotes" );
1905
        C4::Serials::AddItem2Serial( $serialid3, $item3->itemnumber );
1906
        my $serial1 = Koha::Serials->find($serialid1);
1907
        my $serial2 = Koha::Serials->find($serialid2);
1908
        my $serial3 = Koha::Serials->find($serialid3);
1909
1910
        # order_by serial.publisheddate
1911
        $serial1->discard_changes->update( { publisheddate => '2022-09-19' } );
1912
        $serial2->discard_changes->update( { publisheddate => '2022-07-19' } );
1913
        $serial3->discard_changes->update( { publisheddate => '2022-08-19' } );
1914
        is_deeply(
1915
            [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1916
            [ $item2->itemnumber, $item3->itemnumber, $item1->itemnumber ]
1917
        );
1918
1919
        # order_by me.enumchron
1920
        $serial1->discard_changes->update({ publisheddate => '2022-08-19' });
1921
        $serial2->discard_changes->update({ publisheddate => '2022-08-19' });
1922
        $serial3->discard_changes->update({ publisheddate => '2022-08-19' });
1923
        $item1->discard_changes->update( { enumchron => 'cc' } );
1924
        $item2->discard_changes->update( { enumchron => 'bb' } );
1925
        $item3->discard_changes->update( { enumchron => 'aa' } );
1926
        is_deeply( [ map { $_->itemnumber } $biblio->items->search_ordered->as_list ],
1927
            [ $item3->itemnumber, $item2->itemnumber, $item1->itemnumber ] );
1928
1929
    }
1930
1931
    $schema->storage->txn_rollback;
1932
1933
};

Return to bug 31306