From e55995a274ea2a1966bb4c15a558ac1a932cc631 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Wed, 4 Jan 2023 11:13:54 +0100 Subject: [PATCH] Bug 32559: Add tests --- t/db_dependent/selenium/patrons_search.t | 79 +++++++++++++++++++++--- 1 file changed, 71 insertions(+), 8 deletions(-) diff --git a/t/db_dependent/selenium/patrons_search.t b/t/db_dependent/selenium/patrons_search.t index dac9bcb9755..ef0470c8c95 100755 --- a/t/db_dependent/selenium/patrons_search.t +++ b/t/db_dependent/selenium/patrons_search.t @@ -17,9 +17,16 @@ use Modern::Perl; +my $original_dateformat = C4::Context->preference('dateformat'); +my $original_DefaultPatronSearchFields = C4::Context->preference('DefaultPatronSearchFields'); +my $original_PatronsPerPage = C4::Context->preference('PatronsPerPage'); our @cleanup; END { + unless ( @cleanup ) { say "WARNING: Cleanup failed!" } + C4::Context->set_preference('dateformat', $original_dateformat); + C4::Context->set_preference('DefaultPatronSearchFields', $original_DefaultPatronSearchFields); + C4::Context->set_preference('PatronsPerPage', $original_PatronsPerPage); $_->delete for @cleanup; }; @@ -52,7 +59,7 @@ my $builder = t::lib::TestBuilder->new; my $schema = Koha::Database->schema; subtest 'Search patrons' => sub { - plan tests => 25; + plan tests => 26; if ( Koha::Patrons->search({surname => {-like => "test_patron_%"}})->count ) { BAIL_OUT("Cannot run this test, data we need to create already exist in the DB"); @@ -73,8 +80,6 @@ subtest 'Search patrons' => sub { my $library = $builder->build_object( { class => 'Koha::Libraries', value => { branchname => $branchname } } ); - my $default_patron_search_fields = C4::Context->preference('DefaultPatronSearchFields'); - my $default_patron_per_page = C4::Context->preference('PatronsPerPage'); for my $i ( 1 .. 25 ) { push @patrons, $builder->build_object( @@ -111,7 +116,7 @@ subtest 'Search patrons' => sub { my $library_2 = $builder->build_object( { class => 'Koha::Libraries', value => { branchname => 'X' . $branchname } } ); - push @patrons, + my $patron_26 = $builder->build_object( { class => 'Koha::Patrons', @@ -123,9 +128,11 @@ subtest 'Search patrons' => sub { borrowernotes => $borrowernotes, address => $address, email => $email, + dateofbirth => '1980-06-17', } } ); + push @patrons, $patron_26; my $attribute_type = Koha::Patron::Attribute::Type->new( { @@ -304,7 +311,7 @@ subtest 'Search patrons' => sub { '//input[@type="checkbox"][@name="borrowernumber"]'); $checkboxes[0]->click; $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text; - is( $patron_selected_text, "Patrons selected: 3", "Tree patrons are selected" ); + is( $patron_selected_text, "Patrons selected: 3", "Three patrons are selected" ); # Perform another search @@ -313,7 +320,7 @@ subtest 'Search patrons' => sub { $s->submit_form; $s->wait_for_ajax; $patron_selected_text = $driver->find_element('//div[@id="patron_search_selected"]/span')->get_text; - is( $patron_selected_text, "Patrons selected: 3", "Tree patrons still selected" ); + is( $patron_selected_text, "Patrons selected: 3", "Three patrons still selected" ); $driver->find_element('//*[@id="patronlist-menu"]')->click; $driver->find_element('//a[@class="patron-list-add"]')->click; @@ -328,13 +335,69 @@ subtest 'Search patrons' => sub { $patron_list->delete; }; + subtest 'filter by date of birth' => sub { + plan tests => 7; + + C4::Context->set_preference('dateformat', 'metric'); + + # We have a patron with date of birth=1980-06-17 => formatted as 17/06/1980 + + $driver->get( $base_url . "/members/members-home.pl" ); + $s->fill_form( { search_patron_filter => 'test_patron' } ); + $s->submit_form; + $s->wait_for_ajax; + + $s->show_all_entries('//div[@id="'.$table_id.'_wrapper"]'); + my $dob_search_filter = $s->driver->find_element('//table[@id="'.$table_id.'"]//input[@placeholder="Date of birth search"]'); + + $dob_search_filter->send_keys('1980'); + $s->wait_for_ajax; + is( is_patron_shown($patron_26), 1, 'search by correct year shows the patron' ); + $dob_search_filter->clear; + + $dob_search_filter->send_keys('1986'); + $s->wait_for_ajax; + is( is_patron_shown($patron_26), 0, 'search by incorrect year does not show the patron' ); + $dob_search_filter->clear; + + $dob_search_filter->send_keys('1980-06'); + $s->wait_for_ajax; + is( is_patron_shown($patron_26), 1, 'search by correct year-month shows the patron' ); + $dob_search_filter->clear; + + $dob_search_filter->send_keys('1980-06-17'); + $s->wait_for_ajax; + is( is_patron_shown($patron_26), 1, 'search by correct full iso date shows the patron' ); + $dob_search_filter->clear; + + $dob_search_filter->send_keys('1986-06-17'); + $s->wait_for_ajax; + is( is_patron_shown($patron_26), 0, 'search by incorrect full iso date does not show the patron' ); + $dob_search_filter->clear; + + $dob_search_filter->send_keys('17/06/1980'); + $s->wait_for_ajax; + is( is_patron_shown($patron_26), 1, 'search by correct full formatted date shows the patron' ); + $dob_search_filter->clear; + + $dob_search_filter->send_keys('17/06/1986'); + $s->wait_for_ajax; + is( is_patron_shown($patron_26), 0, 'search by incorrect full formatted date does not show the patron' ); + $dob_search_filter->clear; + }; + push @cleanup, $_ for @patrons; push @cleanup, $library; push @cleanup, $library_2; push @cleanup, $patron_category; push @cleanup, $attribute_type, $attribute_type_searchable; - C4::Context->set_preference('DefaultPatronSearchFields',$default_patron_search_fields); - C4::Context->set_preference('PatronsPerPage',$default_patron_per_page); $driver->quit(); }; + +sub is_patron_shown { + my ( $patron ) = @_; + + my @checkboxes = $driver->find_elements('//input[@type="checkbox"][@name="borrowernumber"]'); + return scalar(grep {$_->get_value == $patron->borrowernumber} @checkboxes); +} -- 2.25.1