Bugzilla – Attachment 63430 Details for
Bug 17499
Koha objects for messaging preferences
Home
|
New
|
Browse
|
Search
|
[?]
|
Reports
|
Help
|
New Account
|
Log In
[x]
|
Forgot Password
Login:
[x]
[patch]
Bug 17499: Throw an exception on duplicate messaging preference
Bug-17499-Throw-an-exception-on-duplicate-messagin.patch (text/plain), 4.03 KB, created by
Lari Taskula
on 2017-05-12 13:26:24 UTC
(
hide
)
Description:
Bug 17499: Throw an exception on duplicate messaging preference
Filename:
MIME Type:
Creator:
Lari Taskula
Created:
2017-05-12 13:26:24 UTC
Size:
4.03 KB
patch
obsolete
>From dbb46521053cd9ca114935554cf8751aa1b60f60 Mon Sep 17 00:00:00 2001 >From: Lari Taskula <lari.taskula@jns.fi> >Date: Wed, 26 Oct 2016 16:35:15 +0300 >Subject: [PATCH] Bug 17499: Throw an exception on duplicate messaging > preference > >When trying to add a duplicate messaging preference that has the same >borrowernumber or category, and message_attribute_id as another preference, >throw Koha::Exceptions::DuplicateObject. > >To test: >1. Run t/db_dependent/Koha/Patron/Message/Preferences.t >--- > Koha/Patron/Message/Preference.pm | 47 +++++++++++++++++++++++- > t/db_dependent/Koha/Patron/Message/Preferences.t | 12 +++++- > 2 files changed, 57 insertions(+), 2 deletions(-) > >diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm >index 670e296..df0d0ec 100644 >--- a/Koha/Patron/Message/Preference.pm >+++ b/Koha/Patron/Message/Preference.pm >@@ -22,6 +22,7 @@ use Modern::Perl; > use Koha::Database; > use Koha::Exceptions; > use Koha::Patron::Categories; >+use Koha::Patron::Message::Preferences; > use Koha::Patrons; > > use base qw(Koha::Object); >@@ -36,17 +37,61 @@ Koha::Patron::Message::Preference - Koha Patron Message Preference object class > > =cut > >+=head3 new >+ >+my $preference = Koha::Patron::Message::Preference->new({ >+ borrowernumber => 123, >+ #categorycode => 'ABC', >+ message_attribute_id => 4, >+ wants_digest => 1, >+ days_in_advance => 7, >+}); >+ >+Creates a new messaging preference if no existing duplicates are found. Takes >+either borrowernumber or categorycode, but not both. >+ >+Throws Koha::Exceptions::DuplicateObject if trying to create a preference that >+already exists for the same message_attribute_id and borrowernumber/categorycode. >+ >+=cut >+ >+sub new { >+ my ($class, $params) = shift; >+ >+ my $self = $class->SUPER::new(@_); >+ >+ my $previous = Koha::Patron::Message::Preferences->search({ >+ '-and' => [ >+ borrowernumber => $self->borrowernumber, >+ categorycode => $self->categorycode, >+ message_attribute_id => $self->message_attribute_id, >+ ] >+ }); >+ if ($previous->count) { >+ Koha::Exceptions::DuplicateObject->throw( >+ error => "A preference for this borrower/category and" >+ ." message_attribute_id already exists", >+ ); >+ } >+ >+ return $self; >+} >+ > =head3 store > > Makes a validation before actual Koha::Object->store so that proper exceptions > can be thrown. See C<validate()> for documentation about exceptions. > >+Deletes any previous messaging preferences to avoid duplication. >+ > =cut > > sub store { > my $self = shift; > >- return $self->validate->SUPER::store(@_); >+ $self->validate; >+ >+ return $self->SUPER::store(@_); > } > > =head3 validate >diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t >index 0527d78..413222f 100644 >--- a/t/db_dependent/Koha/Patron/Message/Preferences.t >+++ b/t/db_dependent/Koha/Patron/Message/Preferences.t >@@ -94,7 +94,7 @@ subtest 'Add a test messaging transport' => sub { > }; > > subtest 'Add a messaging preference to patron' => sub { >- plan tests => 3; >+ plan tests => 4; > > ok($preference = Koha::Patron::Message::Preference->new({ > borrowernumber => $patron->{'borrowernumber'}, >@@ -105,6 +105,16 @@ subtest 'Add a messaging preference to patron' => sub { > is(Koha::Patron::Message::Preferences->find({ > borrowernumber => $patron->{'borrowernumber'} })->message_attribute_id, > $preference->message_attribute_id, "Found test messaging preference from database."); >+ eval { Koha::Patron::Message::Preference->new({ >+ borrowernumber => $patron->{'borrowernumber'}, >+ message_attribute_id => $attribute->message_attribute_id, >+ wants_digest => 0, >+ days_in_advance => 1, >+ })->store }; >+ is(ref $@, "Koha::Exceptions::DuplicateObject", >+ "Adding a duplicate preference" >+ ." => Koha::Exceptions::DuplicateObject"); >+ > subtest 'Attempt to add a messaging preference with invalid parameters' => sub { > plan tests => 6; > >-- >2.7.4
You cannot view the attachment while viewing its details because your browser does not support IFRAMEs.
View the attachment on a separate page
.
View Attachment As Diff
View Attachment As Raw
Actions:
View
|
Diff
|
Splinter Review
Attachments on
bug 17499
:
63427
|
63428
|
63429
|
63430
|
63431
|
63432
|
63433
|
63434
|
63786
|
63787
|
63788
|
63789
|
63790
|
63791
|
63792
|
63793
|
63801
|
63802
|
63803
|
63804
|
63805
|
63806
|
63807
|
63808
|
63809
|
63810
|
63811
|
63812
|
63878
|
63895
|
64035
|
64036
|
64037
|
64038
|
64039
|
64040
|
64041
|
64042
|
64043
|
64044
|
64795
|
66686
|
69434
|
69435
|
69458
|
91864
|
93863
|
93865
|
93872
|
94748
|
99479
|
99634
|
99668
|
99669
|
99670
|
103441
|
103442
|
103443
|
106653
|
106654
|
106655
|
106656
|
106657
|
107612
|
107613
|
107614
|
107615
|
107616
|
113010
|
113011
|
113127
|
113129
|
113686
|
113687
|
113765
|
113766
|
113767
|
113768
|
118452
|
118453
|
119470
|
119471
|
119472
|
119473
|
119474
|
119475
|
119476
|
119477
|
119478
|
119479
|
119480
|
119481
|
119482
|
119483
|
119484
|
119485
|
119486
|
119487
|
119488
|
119489
|
119490
|
119491
|
141408
|
141409
|
141410
|
141411
|
141412
|
141413
|
141414
|
141415
|
141416
|
141417
|
141418
|
146529
|
146530
|
146531
|
146532
|
146533
|
146534
|
146535
|
146536
|
146537
|
146538
|
146539
|
146540
|
151900
|
151901
|
151902
|
151903
|
151904
|
151905
|
151906
|
151907
|
151908
|
151909
|
151910
|
151911
|
151937
|
151938
|
151939
|
151940
|
151941
|
151942
|
151943
|
151944
|
151945
|
151946
|
151947
|
151948
|
155365
|
155366
|
155367
|
155368
|
155369
|
155370
|
155371
|
155372
|
155373
|
155374
|
155375
|
155376