Problem/Motivation

Now when a user vote a choice and the you delete this choice leads to an exception showing the results as tries to find the option and it doesn't exist.

Proposed resolution

When deleting a choice if this choice has votes, them has to be deleted too.
Also test that it works with multilanguage #132339: Multilingual poll choices

Remaining tasks

User interface changes

API changes

Data model changes

Comments

edurenye created an issue. See original summary.

edurenye’s picture

Status: Active » Needs review
StatusFileSize
new1.06 KB

Done and added tests.

edurenye’s picture

StatusFileSize
new4.13 KB

Last was just test, I pressed wrong button.

The last submitted patch, 2: deleting_a_choice-2648802-2-test_only.patch, failed testing.

berdir’s picture

Status: Needs review » Needs work
  1. +++ b/src/Entity/Poll.php
    @@ -439,6 +439,15 @@ class Poll extends ContentEntityBase implements PollInterface {
    +        foreach ($removed_choices as $removed_choice) {
    +          $votes = \Drupal::entityTypeManager()->getStorage('poll')->getVotes($this);
    +          foreach ($votes as $key => $vote) {
    +            if ($key == $removed_choice && $vote != 0) {
    +              \Drupal::service('poll_vote.storage')->deleteChoiceVotes(PollChoice::load($removed_choice));
    +            }
    

    Do we need this complexity? What about making deleteChoiceVotes() a list of choice ID's? It can write a super-simple, single db delete query to delete them all in one go.

  2. +++ b/src/PollVoteStorageInterface.php
    @@ -0,0 +1,28 @@
    +   *
    +   * @return int
    +   *   The number of rows affected by the delete query.
    

    not sure if we need that.

berdir’s picture

Once this is in, lets do a follow-up to move all vote related methods from PollStorage to this new service.

edurenye’s picture

Status: Needs work » Needs review
StatusFileSize
new3.61 KB
new2.46 KB

Done.

  • Berdir committed 2d8aa3c on 8.x-1.x authored by edurenye
    Issue #2648802 by edurenye: Deleting a choice should delete the votes...
berdir’s picture

Status: Needs review » Fixed

Thanks, committed.

tduong’s picture

Title: Deleting a choice should delete the votes over it » Deleting a choice should delete the votes over it in the database
Status: Fixed » Needs review
StatusFileSize
new934 bytes

Reopening the issue and renaming the title.
I was about to start working on the followup #2648836: Alert the user that deleting a choice will delete the votes on it and after a discussion with @berdir what is missing here is to check that the vote has been actually deleted from the database. I've added this check on the new patch.

berdir’s picture

Status: Needs review » Needs work
+++ b/src/Tests/PollDeleteChoiceTest.php
@@ -73,9 +73,15 @@ class PollDeleteChoiceTest extends PollTestBase {
+
+    // Assert that the existing vote has been deleted from the database.
+    $ids = \Drupal::entityQuery('poll_choice')
+      ->condition('choice', $this->poll->choice[1]->entity->label())
+      ->execute();
+    $this->assertEqual(count($ids), 0, 'Choice 2 has been deleted in the database');

This verifies that the poll *choice* got deleted. That's fine.

But what we're actually after here is that the *vote* for the poll choice got deleted. You need to look at the poll_vote table, with a normal select query, as that's not an entity.

tduong’s picture

Status: Needs work » Needs review
StatusFileSize
new1.3 KB
new1.49 KB

Oops, was trying to do something and then forgot to re-fix it...
Now should be the right one.

  • Berdir committed 9af1806 on 8.x-1.x authored by tduong
    Issue #2648802 by tduong: Improve and fix test coverage for vote...
berdir’s picture

Status: Needs review » Fixed

Thanks, committed.

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.