From 58c1ddc15176a347c2d2a8a8b3b0feda3d7084b8 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 6 Jan 2021 17:11:30 +0100 Subject: [PATCH] Bug 27353: Add tests Signed-off-by: Tomas Cohen Arazi Signed-off-by: Martin Renvoize Signed-off-by: Jonathan Druart --- t/Koha/REST/Plugin/Pagination.t | 22 ++++++++++---- t/db_dependent/Koha/REST/Plugin/Objects.t | 35 +++++++++++++++++++++++ 2 files changed, 51 insertions(+), 6 deletions(-) diff --git a/t/Koha/REST/Plugin/Pagination.t b/t/Koha/REST/Plugin/Pagination.t index dff13fb770..e8d25e7360 100755 --- a/t/Koha/REST/Plugin/Pagination.t +++ b/t/Koha/REST/Plugin/Pagination.t @@ -33,19 +33,19 @@ get '/empty' => sub { get '/pagination_headers' => sub { my $c = shift; - $c->add_pagination_headers({ total => 10, params => { _page => 2, _per_page => 3, firstname => 'Jonathan' } }); + $c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 2, _per_page => 3, firstname => 'Jonathan' } }); $c->render( json => { ok => 1 }, status => 200 ); }; get '/pagination_headers_first_page' => sub { my $c = shift; - $c->add_pagination_headers({ total => 10, params => { _page => 1, _per_page => 3, firstname => 'Jonathan' } }); + $c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 1, _per_page => 3, firstname => 'Jonathan' } }); $c->render( json => { ok => 1 }, status => 200 ); }; get '/pagination_headers_last_page' => sub { my $c = shift; - $c->add_pagination_headers({ total => 10, params => { _page => 4, _per_page => 3, firstname => 'Jonathan' } }); + $c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 4, _per_page => 3, firstname => 'Jonathan' } }); $c->render( json => { ok => 1 }, status => 200 ); }; @@ -60,13 +60,13 @@ get '/dbic_merge_pagination' => sub { get '/pagination_headers_without_page_size' => sub { my $c = shift; - $c->add_pagination_headers({ total => 10, params => { _page => 2, firstname => 'Jonathan' } }); + $c->add_pagination_headers({ total => 10, base_total => 12, params => { _page => 2, firstname => 'Jonathan' } }); $c->render( json => { ok => 1 }, status => 200 ); }; get '/pagination_headers_without_page' => sub { my $c = shift; - $c->add_pagination_headers({ total => 10, params => { _per_page => 3, firstname => 'Jonathan' } }); + $c->add_pagination_headers({ total => 10, base_total => 12, params => { _per_page => 3, firstname => 'Jonathan' } }); $c->render( json => { ok => 1 }, status => 200 ); }; @@ -75,6 +75,7 @@ get '/pagination_headers_with_minus_one' => sub { $c->add_pagination_headers( { total => 10, + base_total => 12, params => { _per_page => -1, firstname => 'Jonathan' } } ); @@ -86,6 +87,7 @@ get '/pagination_headers_with_minus_one_and_invalid_page' => sub { $c->add_pagination_headers( { total => 10, + base_total => 12, params => { page => 100, _per_page => -1, firstname => 'Jonathan' } } ); @@ -101,18 +103,20 @@ use t::lib::Mocks; subtest 'add_pagination_headers() tests' => sub { - plan tests => 101; + plan tests => 109; my $t = Test::Mojo->new; $t->get_ok('/empty') ->status_is( 200 ) ->header_is( 'X-Total-Count' => undef, 'X-Total-Count is undefined' ) + ->header_is( 'X-Base-Total-Count' => undef, 'X-Base-Total-Count is undefined' ) ->header_is( 'Link' => undef, 'Link is undefined' ); $t->get_ok('/pagination_headers') ->status_is( 200 ) ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) + ->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) ->header_like( 'Link' => qr/; rel="prev",/ ) ->header_like( 'Link' => qr/; rel="prev",/ ) ->header_like( 'Link' => qr/; rel="prev",/ ) @@ -129,6 +133,7 @@ subtest 'add_pagination_headers() tests' => sub { $t->get_ok('/pagination_headers_first_page') ->status_is( 200 ) ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) + ->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) ->header_unlike( 'Link' => qr/; rel="prev",/ ) ->header_like( 'Link' => qr/; rel="next",/ ) ->header_like( 'Link' => qr/; rel="next",/ ) @@ -143,6 +148,7 @@ subtest 'add_pagination_headers() tests' => sub { $t->get_ok('/pagination_headers_last_page') ->status_is( 200 ) ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) + ->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) ->header_like( 'Link' => qr/; rel="prev",/ ) ->header_like( 'Link' => qr/; rel="prev",/ ) ->header_like( 'Link' => qr/; rel="prev",/ ) @@ -158,6 +164,7 @@ subtest 'add_pagination_headers() tests' => sub { $t->get_ok('/pagination_headers_without_page_size') ->status_is( 200 ) ->header_is( 'X-Total-Count' => 10, 'X-Total-Count contains the passed value' ) + ->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) ->header_like( 'Link' => qr/; rel="prev",/ ) ->header_like( 'Link' => qr/; rel="prev",/ ) ->header_like( 'Link' => qr/; rel="prev",/ ) @@ -174,6 +181,7 @@ subtest 'add_pagination_headers() tests' => sub { $t->get_ok('/pagination_headers_without_page') ->status_is( 200 ) ->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, even without page param' ) + ->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) @@ -190,6 +198,7 @@ subtest 'add_pagination_headers() tests' => sub { $t->get_ok('/pagination_headers_with_minus_one') ->status_is( 200 ) ->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' ) + ->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) @@ -204,6 +213,7 @@ subtest 'add_pagination_headers() tests' => sub { $t->get_ok('/pagination_headers_with_minus_one_and_invalid_page') ->status_is( 200 ) ->header_is( 'X-Total-Count' => 10, 'X-Total-Count header present, with per_page=-1' ) + ->header_is( 'X-Base-Total-Count' => 12, 'X-Base-Total-Count contains the passed value' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) ->header_unlike( 'Link' => qr/; rel="prev",/, 'First page, no previous' ) diff --git a/t/db_dependent/Koha/REST/Plugin/Objects.t b/t/db_dependent/Koha/REST/Plugin/Objects.t index cbec228ce9..4b86ad5aff 100755 --- a/t/db_dependent/Koha/REST/Plugin/Objects.t +++ b/t/db_dependent/Koha/REST/Plugin/Objects.t @@ -259,6 +259,41 @@ subtest 'objects.search helper, encoding' => sub { $schema->storage->txn_rollback; }; +subtest 'objects.search helper, X-Total-Count and X-Base-Total-Count' => sub { + + plan tests => 8; + + $schema->storage->txn_begin; + + Koha::Cities->delete; + + my $long_city_name = 'Llanfairpwllgwyngyll'; + for my $i ( 1 .. length($long_city_name) ) { + $builder->build_object( + { + class => 'Koha::Cities', + value => { + city_name => substr( $long_city_name, 0, $i ), + city_country => 'Wales' + } + } + ); + } + + my $t = Test::Mojo->new; + $t->get_ok('/cities?name=L&_per_page=10&_page=1&_match=starts_with') + ->status_is(200) + ->header_is( 'X-Total-Count' => 20, 'X-Total-Count header present' ) + ->header_is( 'X-Base-Total-Count' => 20, 'X-Base-Total-Count header present' ); + + $t->get_ok('/cities?name=Llan&_per_page=10&_page=1&_match=starts_with') + ->status_is(200) + ->header_is( 'X-Total-Count' => 17, 'X-Total-Count header present' ) + ->header_is('X-Base-Total-Count' => 20, 'X-Base-Total-Count header present' ); + + $schema->storage->txn_rollback; +}; + subtest 'objects.search helper, embed' => sub { plan tests => 2; -- 2.39.5