From f921d383a953fd9ddeaecfac103beced7f872f2b Mon Sep 17 00:00:00 2001 From: m Date: Fri, 6 Apr 2018 14:00:20 +0200 Subject: [PATCH] bug/test-name-on-vote-edit --- adminstuds.php | 2 ++ .../Framadate/Repositories/VoteRepository.php | 15 +++++++++++++++ app/classes/Framadate/Services/PollService.php | 7 ++++++- studs.php | 4 +++- 4 files changed, 26 insertions(+), 2 deletions(-) diff --git a/adminstuds.php b/adminstuds.php index ab2cacd..5555b82 100644 --- a/adminstuds.php +++ b/adminstuds.php @@ -223,6 +223,8 @@ if (!empty($_POST['save'])) { // Save edition of an old vote } else { $message = new Message('danger', __('Error', 'Update vote failed')); } + } catch (AlreadyExistsException $aee) { + $message = new Message('danger', __('Error', 'The name you\'ve chosen already exist in this poll!')); } catch (ConcurrentEditionException $cee) { $message = new Message('danger', __('Error', 'Poll has been updated before you vote')); } catch (ConcurrentVoteException $cve) { diff --git a/app/classes/Framadate/Repositories/VoteRepository.php b/app/classes/Framadate/Repositories/VoteRepository.php index 1a74f5a..682357c 100644 --- a/app/classes/Framadate/Repositories/VoteRepository.php +++ b/app/classes/Framadate/Repositories/VoteRepository.php @@ -85,4 +85,19 @@ class VoteRepository extends AbstractRepository { $prepared->execute([$poll_id, $name]); return $prepared->rowCount() > 0; } + + /** + * Check if name is already used for the given poll and another vote. + * + * @param int $poll_id ID of the poll + * @param string $name Name of the vote + * @param int $vote_id ID of the current vote + * @return bool true if vote already exists + */ + public function existsByPollIdAndNameAndVoteId($poll_id, $name, $vote_id) { + $prepared = $this->prepare('SELECT 1 FROM `' . Utils::table('vote') . '` WHERE poll_id = ? AND name = ? AND id != ?'); + $prepared->execute([$poll_id, $name, $vote_id]); + return $prepared->rowCount() > 0; + } + } diff --git a/app/classes/Framadate/Services/PollService.php b/app/classes/Framadate/Services/PollService.php index ad180b1..8e19789 100644 --- a/app/classes/Framadate/Services/PollService.php +++ b/app/classes/Framadate/Services/PollService.php @@ -100,7 +100,12 @@ class PollService { // Check if slots are still the same $this->checkThatSlotsDidntChanged($poll, $slots_hash); - + + // Check if vote already exists with the same name + if ($this->voteRepository->existsByPollIdAndNameAndVoteId($poll_id, $name, $vote_id)) { + throw new AlreadyExistsException(); + } + // Update vote $choices = implode($choices); return $this->voteRepository->update($poll_id, $vote_id, $name, $choices); diff --git a/studs.php b/studs.php index 6cef3ee..2d5efab 100644 --- a/studs.php +++ b/studs.php @@ -145,7 +145,9 @@ if ($accessGranted) { } else { $message = new Message('danger', __('Error', 'Update vote failed')); } - } catch (ConcurrentEditionException $cee) { + } catch (AlreadyExistsException $aee) { + $message = new Message('danger', __('Error', 'The name you\'ve chosen already exist in this poll!')); + } catch (ConcurrentEditionException $cee) { $message = new Message('danger', __('Error', 'Poll has been updated before you vote')); } catch (ConcurrentVoteException $cve) { $message = new Message('danger', __('Error', "Your vote wasn't counted, because someone voted in the meantime and it conflicted with your choices and the poll conditions. Please retry."));