From 44d1ebece847fa078d50bbee4608008db6a42f07 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 3 Mar 2023 12:13:05 +0100 Subject: [PATCH] Bug 31383: Adjust and add tests Sponsored-by: Rijksmuseum, Netherlands Signed-off-by: Marcel de Rooy Signed-off-by: Martin Renvoize Signed-off-by: Tomas Cohen Arazi --- Koha/AdditionalContents.pm | 34 ++++++------ t/db_dependent/Koha/AdditionalContents.t | 69 +++++++++++++++--------- t/db_dependent/Koha/Library.t | 62 +++++++++++++++------ t/db_dependent/Letters/TemplateToolkit.t | 44 +++++++++------ 4 files changed, 136 insertions(+), 73 deletions(-) diff --git a/Koha/AdditionalContents.pm b/Koha/AdditionalContents.pm index cd3b00b896..a866aa71d0 100644 --- a/Koha/AdditionalContents.pm +++ b/Koha/AdditionalContents.pm @@ -85,7 +85,9 @@ sub search_for_display { $search_params->{category} = $params->{category} if $params->{category}; my $contents = $self->SUPER::search( $search_params, { order_by => 'number' } ); + my @all_content_id = $contents->get_column('id'); + my @translated_content_id; if ( $params->{lang} ) { my $translated_contents = Koha::AdditionalContentsLocalizations->search( { @@ -93,24 +95,22 @@ sub search_for_display { lang => $params->{lang}, } ); - my @all_content_id = $contents->get_column('id'); - my @translated_content_id = $translated_contents->get_column('additional_content_id'); - my $default_contents = Koha::AdditionalContentsLocalizations->search( - { - additional_content_id => [array_minus(@all_content_id, @translated_content_id)], - lang => 'default', - } - ); - return Koha::AdditionalContentsLocalizations->search( - { - id => [ - $translated_contents->get_column('id'), - $default_contents->get_column('id'), - ] - }, - ); + @translated_content_id = $translated_contents->get_column('additional_content_id'); } - return $contents->search( { lang => 'default' }, { order_by => 'number' } )->translated_contents; + + my $default_contents = Koha::AdditionalContentsLocalizations->search( + { + additional_content_id => [array_minus(@all_content_id, @translated_content_id)], + lang => 'default', + } + ); + + return Koha::AdditionalContentsLocalizations->search( + { + id => [@translated_content_id, $default_contents->get_column('id')] + }, + ); + } =head3 find_best_match diff --git a/t/db_dependent/Koha/AdditionalContents.t b/t/db_dependent/Koha/AdditionalContents.t index 67094cd48b..f3a05c67ad 100755 --- a/t/db_dependent/Koha/AdditionalContents.t +++ b/t/db_dependent/Koha/AdditionalContents.t @@ -33,7 +33,7 @@ my $builder = t::lib::TestBuilder->new; subtest 'Koha::AdditionalContents basic test' => sub { - plan tests => 5; + plan tests => 9; $schema->storage->txn_begin; @@ -45,29 +45,48 @@ subtest 'Koha::AdditionalContents basic test' => sub { code => 'news_1', location => 'staff_only', branchcode => $library->{branchcode}, - title => 'a news', - content => 'content for news 1', } )->store; + my $content_1 = { + title => 'a news', + content => 'content for news 1', + lang => 'default', + }; + $new_news_item_1->translated_contents([$content_1]); my $new_news_item_2 = Koha::AdditionalContent->new( { category => 'news', code => 'news_2', location => 'staff_only', branchcode => $library->{branchcode}, - title => 'another news', - content => 'content for news 2', } )->store; - - like( $new_news_item_1->idnew, qr|^\d+$|, 'Adding a new news_item should have set the idnew'); + my $content_2 = { + title => 'another news', + content => 'content for news 2', + lang => 'default', + }; + $new_news_item_2->translated_contents([$content_2]); + + like( $new_news_item_1->id, qr|^\d+$|, 'Adding a new news_item should have set the id'); is( Koha::AdditionalContents->search->count, $nb_of_news + 2, 'The 2 news should have been added' ); - my $retrieved_news_item_1 = Koha::AdditionalContents->find( $new_news_item_1->idnew ); - is( $retrieved_news_item_1->title, $new_news_item_1->title, 'Find a news_item by id should return the correct news_item' ); - is( $retrieved_news_item_1->content, $new_news_item_1->content, 'The content method return the content of the news'); - - $retrieved_news_item_1->delete; + my $retrieved_news_item_1 = Koha::AdditionalContents->find( $new_news_item_1->id )->translated_contents->next; + is( $retrieved_news_item_1->title, $content_1->{title}, 'Find a news_item by id should return the correct news_item' ); + is( $retrieved_news_item_1->content, $content_1->{content}, 'The content method return the content of the news'); + + my $default_content = $new_news_item_2->default_localization; + is( $default_content->content, $content_2->{content}, 'default_localization return the default content' ); + my $translated_content = { lang => 'nl-NL', content => 'translated_content' }; + $new_news_item_2->translated_contents( [ $translated_content, $content_2 ] )->as_list; + $default_content = $new_news_item_2->default_localization; + is( $default_content->content, $content_2->{content}, 'default_localization still return the default content' ); + my $retrieved_translated_content = $new_news_item_2->translated_content('en'); + is( $retrieved_translated_content->content, $content_2->{content}, 'default content is returned for non-existing translated interface' ); + $retrieved_translated_content = $new_news_item_2->translated_content('nl-NL'); + is( $retrieved_translated_content->content, $translated_content->{content}, 'translated content is returned if it existsOB' ); + + $new_news_item_1->delete; is( Koha::AdditionalContents->search->count, $nb_of_news + 1, 'Delete should have deleted the news_item' ); $schema->storage->txn_rollback; @@ -148,7 +167,7 @@ subtest '->author' => sub { $author->delete; - $news_item = Koha::AdditionalContents->find($news_item->idnew); + $news_item = Koha::AdditionalContents->find($news_item->id); is( ref($news_item), 'Koha::AdditionalContent', 'News are not deleted alongwith the author' ); is( $news_item->author, undef, '->author returns undef is the author has been deleted' ); @@ -176,11 +195,11 @@ subtest '->search_for_display' => sub { published_on => $today, category => 'news', location => 'staff_and_opac', - lang => 'default', branchcode => undef, number => 1, } }); + $new_expired->translated_contents( [ { lang => 'default', content => ''} ] ); my $new_not_expired = $builder->build_object({ class => 'Koha::AdditionalContents', value => { @@ -188,11 +207,11 @@ subtest '->search_for_display' => sub { published_on => $today, category => 'news', location => 'staff_and_opac', - lang => 'default', branchcode => undef, number => 2, } }); + $new_not_expired->translated_contents( [ { lang => 'default', content => '' } ] ); my $new_not_active = $builder->build_object({ class => 'Koha::AdditionalContents', value => { @@ -200,11 +219,11 @@ subtest '->search_for_display' => sub { published_on => $tomorrow, category => 'news', location => 'staff_and_opac', - lang => 'default', branchcode => undef, number => 3, } }); + $new_not_active->translated_contents( [ { lang => 'default', content => '' } ] ); my $new_slip= $builder->build_object({ class => 'Koha::AdditionalContents', value => { @@ -212,11 +231,11 @@ subtest '->search_for_display' => sub { published_on => $today, category => 'news', location => 'staff_only', - lang => 'default', branchcode => $library1->branchcode, number => 4, } }); + $new_slip->translated_contents( [ { lang => 'default', content => '' } ] ); my $new_intra = $builder->build_object({ class => 'Koha::AdditionalContents', value => { @@ -224,11 +243,11 @@ subtest '->search_for_display' => sub { published_on => $today, category => 'news', location => 'staff_only', - lang => 'default', branchcode => $library2->branchcode, number => 5, } }); + $new_intra->translated_contents( [ { lang => 'default', content => '' } ] ); my $new_intra2 = $builder->build_object({ class => 'Koha::AdditionalContents', value => { @@ -236,11 +255,11 @@ subtest '->search_for_display' => sub { published_on => $today, category => 'news', location => 'staff_only', - lang => 'default', branchcode => undef, number => 5, } }); + $new_intra2->translated_contents( [ { lang => 'default', content => '' } ] ); my $news = Koha::AdditionalContents->search_for_display({ location => 'staff_only' }); is($news->count, 1, "There is 1 news for all staff"); @@ -267,20 +286,22 @@ subtest 'find_best_match' => sub { my $library01 = $builder->build_object({ class => 'Koha::Libraries' }); my $html01 = $builder->build_object({ class => 'Koha::AdditionalContents', - value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'default' }, + value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef }, }); + my ( $default_content ) = $html01->translated_contents( [ { lang => 'default', content => '' } ] )->as_list; my $params = { category => 'html_customizations', location => 'test_best_match', lang => 'nl-NL' }; - is( Koha::AdditionalContents->find_best_match($params)->idnew, $html01->idnew, 'Found all branches, lang default' ); + is( Koha::AdditionalContents->find_best_match($params)->id, $default_content->id, 'Found all branches, lang default' ); my $html02 = $builder->build_object({ class => 'Koha::AdditionalContents', - value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef, lang => 'nl-NL' }, + value => { category => 'html_customizations', location => 'test_best_match', branchcode => undef }, }); - is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found all branches, lang nl-NL' ); + my ( $translated_content ) = $html02->translated_contents( [ { lang => 'nl-NL', content => '' } ] )->as_list; + is( Koha::AdditionalContents->find_best_match($params)->id, $translated_content->id, 'Found all branches, lang nl-NL' ); $params->{ library_id } = $library01->id; $html02->branchcode( $library01->id )->store; - is( Koha::AdditionalContents->find_best_match($params)->idnew, $html02->idnew, 'Found library01, lang nl-NL' ); + is( Koha::AdditionalContents->find_best_match($params)->id, $translated_content->id, 'Found library01, lang nl-NL' ); # Note: find_best_match is tested further via $libary->opac_info; see t/db_dependent/Koha/Library.t diff --git a/t/db_dependent/Koha/Library.t b/t/db_dependent/Koha/Library.t index c6637ddfd9..5c16eedb54 100755 --- a/t/db_dependent/Koha/Library.t +++ b/t/db_dependent/Koha/Library.t @@ -101,22 +101,52 @@ subtest 'opac_info tests' => sub { my $library01 = $builder->build_object({ class => 'Koha::Libraries' }); my $library02 = $builder->build_object({ class => 'Koha::Libraries' }); - my $html01 = $builder->build_object({ - class => 'Koha::AdditionalContents', - value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'default', content => '1', expirationdate => undef }, - }); - my $html02 = $builder->build_object({ - class => 'Koha::AdditionalContents', - value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'default', content => '2', expirationdate => undef }, - }); - my $html03 = $builder->build_object({ - class => 'Koha::AdditionalContents', - value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, lang => 'nl-NL', content => '3', expirationdate => undef }, - }); - my $html04 = $builder->build_object({ - class => 'Koha::AdditionalContents', - value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, lang => 'fr-FR', content => '4', expirationdate => undef }, - }); + my $html01 = $builder->build_object( + { + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, expirationdate => undef }, + } + ); + $html01->translated_contents( + [ + { + lang => 'default', + content => '1', + } + ] + ); + my $html02 = $builder->build_object( + { + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => $library01->id, expirationdate => undef }, + } + ); + $html02->translated_contents( + [ + { + lang => 'default', + content => '2', + }, + { + lang => 'nl-NL', + content => '3', + } + ] + ); + my $html04 = $builder->build_object( + { + class => 'Koha::AdditionalContents', + value => { category => 'html_customizations', location => 'OpacLibraryInfo', branchcode => undef, expirationdate => undef }, + } + ); + $html04->translated_contents( + [ + { + lang => 'fr-FR', + content => '4', + } + ] + ); # Start testing is( $library01->opac_info->content, '2', 'specific library, default language' ); diff --git a/t/db_dependent/Letters/TemplateToolkit.t b/t/db_dependent/Letters/TemplateToolkit.t index 7c38aef3d2..1c6fea8ee0 100755 --- a/t/db_dependent/Letters/TemplateToolkit.t +++ b/t/db_dependent/Letters/TemplateToolkit.t @@ -80,8 +80,8 @@ my $hold = $builder->build_object( my $news = $builder->build_object( { - class => 'Koha::AdditionalContents', - value => { title => 'a news title', content => 'a news content' } + class => 'Koha::AdditionalContentsLocalizations', + value => { title => 'a news title', content => 'a news content', lang => 'default' } } ); my $serial = $builder->build_object( { class => 'Koha::Serials' } ); @@ -662,14 +662,22 @@ EOF { class => 'Koha::AdditionalContents', value => { - category => 'news', - location => "slip", - branchcode => $branchcode, - lang => 'default', - title => "A wonderful news", - content => "This is the wonderful news.", - expirationdate => undef, - published_on => $one_minute_ago + category => 'news', + location => "slip", + branchcode => $branchcode, + expirationdate => undef, + published_on => $one_minute_ago + } + } + ); + my $content = $builder->build_object( + { + class => 'Koha::AdditionalContentsLocalizations', + value => { + additional_content_id => $news_item->id, + lang => 'default', + title => "A wonderful news", + content => "This is the wonderful news.", } } ); @@ -702,15 +710,17 @@ Date due: <>

+[% IF additional_contents.count %]

News

- +[% FOR content IN additional_contents %]
-
<>
-

<>

-

Posted on <>

+
[% content.title %]
+

[% content.content %]

+

Posted on [% content.published_on | \$KohaDates %]


-
+[% END %] +[% END %] EOF reset_template( { template => $template, code => $code, module => 'circulation' } ); @@ -762,6 +772,7 @@ Date due: [% overdue.date_due | \$KohaDates %]

+[% IF additional_contents.count %]

News

[% FOREACH n IN additional_contents %]
@@ -771,6 +782,7 @@ Date due: [% overdue.date_due | \$KohaDates %]

[% END %] +[% END %] EOF reset_template( { template => $tt_template, code => $code, module => 'circulation' } ); @@ -787,7 +799,7 @@ EOF # There is too many line breaks generated by the historic syntax $second_slip->{content} =~ s|

\n\n\n

|

\n\n

|s; - my $news_item_title = $news_item->title; + my $news_item_title = $content->title; like( $first_slip->{content}, qr{$news_item_title} ); is( $first_tt_slip->{content}, $first_slip->{content}, ); is( $second_tt_slip->{content}, $second_slip->{content}, ); -- 2.39.5