Bugzilla – Attachment 63804 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), 5.93 KB, created by
Marc Véron
on 2017-05-29 12:29:10 UTC
(
hide
)
Description:
Bug 17499: Throw an exception on duplicate messaging preference
Filename:
MIME Type:
Creator:
Marc Véron
Created:
2017-05-29 12:29:10 UTC
Size:
5.93 KB
patch
obsolete
>From 390be224b0a439b54f4700257ae2c4af1c395ea9 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 >MIME-Version: 1.0 >Content-Type: text/plain; charset=UTF-8 >Content-Transfer-Encoding: 8bit > >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 > >Signed-off-by: Marc Véron <veron@veron.ch> >--- > Koha/Patron/Message/Preference.pm | 48 ++++++++++++++++++++++++ > t/db_dependent/Koha/Patron/Message/Preferences.t | 43 ++++++++++++++++++++- > 2 files changed, 89 insertions(+), 2 deletions(-) > >diff --git a/Koha/Patron/Message/Preference.pm b/Koha/Patron/Message/Preference.pm >index ce055d7..447919e 100644 >--- a/Koha/Patron/Message/Preference.pm >+++ b/Koha/Patron/Message/Preference.pm >@@ -38,6 +38,38 @@ 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, >+}); >+ >+Takes either borrowernumber or categorycode, but not both. >+ >+days_in_advance may not be available. See message_attributes table for takes_days >+configuration. >+ >+wants_digest may not be available. See message_transports table for is_digest >+configuration. >+ >+You can instantiate a new object without custom validation errors, but when >+storing, validation may throw exceptions. See C<validate()> for more >+documentation. >+ >+=cut >+ >+sub new { >+ my ($class, $params) = shift; >+ >+ my $self = $class->SUPER::new(@_); >+ >+ return $self; >+} >+ > =head3 store > > Makes a validation before actual Koha::Object->store so that proper exceptions >@@ -62,6 +94,8 @@ Throws following exceptions regarding parameters. > > See $_->parameter to identify the parameter causing the exception. > >+Throws Koha::Exceptions::DuplicateObject if this preference already exists. >+ > Returns Koha::Patron::Message::Preference object. > > =cut >@@ -94,6 +128,20 @@ sub validate { > ) unless Koha::Patron::Categories->find($self->categorycode); > } > >+ if (!$self->in_storage) { >+ my $previous = Koha::Patron::Message::Preferences->search({ >+ 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', >+ ); >+ } >+ } >+ > my $attr; > if ($self->days_in_advance || $self->wants_digest) { > $attr = Koha::Patron::Message::Attributes->find( >diff --git a/t/db_dependent/Koha/Patron/Message/Preferences.t b/t/db_dependent/Koha/Patron/Message/Preferences.t >index b608981..7fa3688 100644 >--- a/t/db_dependent/Koha/Patron/Message/Preferences.t >+++ b/t/db_dependent/Koha/Patron/Message/Preferences.t >@@ -49,7 +49,7 @@ subtest 'Test Koha::Patron::Message::Preferences' => sub { > my $attribute = build_a_test_attribute(); > > subtest 'Test for a patron' => sub { >- plan tests => 2; >+ plan tests => 3; > > my $patron = build_a_test_patron(); > Koha::Patron::Message::Preference->new({ >@@ -66,6 +66,17 @@ subtest 'Test Koha::Patron::Message::Preferences' => sub { > ok($preference->borrower_message_preference_id > 0, > 'Added a new messaging preference for patron.'); > >+ subtest 'Test set not throwing an exception on duplicate object' => sub { >+ plan tests => 1; >+ >+ Koha::Patron::Message::Attributes->find({ >+ message_attribute_id => $attribute->message_attribute_id >+ })->set({ takes_days => 1 })->store; >+ $preference->set({ days_in_advance => 1 })->store; >+ is(ref($preference), 'Koha::Patron::Message::Preference', >+ 'Updating the preference does not cause duplicate object exception'); >+ }; >+ > $preference->delete; > is(Koha::Patron::Message::Preferences->search({ > borrowernumber => $patron->borrowernumber, >@@ -100,7 +111,7 @@ subtest 'Test Koha::Patron::Message::Preferences' => sub { > }; > > subtest 'Test adding a new preference with invalid parameters' => sub { >- plan tests => 3; >+ plan tests => 4; > > subtest 'Missing parameters' => sub { > plan tests => 1; >@@ -178,6 +189,34 @@ subtest 'Test adding a new preference with invalid parameters' => sub { > > $schema->storage->txn_rollback; > }; >+ >+ subtest 'Duplicate object' => sub { >+ plan tests => 2; >+ >+ $schema->storage->txn_begin; >+ >+ my $attribute = build_a_test_attribute(); >+ my $patron = build_a_test_patron(); >+ my $preference = Koha::Patron::Message::Preference->new({ >+ borrowernumber => $patron->borrowernumber, >+ message_attribute_id => $attribute->message_attribute_id, >+ wants_digest => 0, >+ days_in_advance => undef, >+ })->store; >+ ok($preference->borrower_message_preference_id, >+ 'Added a new messaging preference for patron.'); >+ eval { Koha::Patron::Message::Preference->new({ >+ borrowernumber => $patron->borrowernumber, >+ message_attribute_id => $attribute->message_attribute_id, >+ wants_digest => 0, >+ days_in_advance => undef, >+ })->store }; >+ is(ref $@, 'Koha::Exceptions::DuplicateObject', >+ 'Adding a duplicate preference' >+ .' => Koha::Exceptions::DuplicateObject'); >+ >+ $schema->storage->txn_rollback; >+ }; > }; > > sub build_a_test_attribute { >-- >2.1.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