getResults is not returning the correct results

Created on 1 August 2018, over 6 years ago
Updated 20 October 2023, over 1 year ago

I try Voting Api Widget and the module is not getting a correct result from the methods "getResults" in class "VotingApiWidgetBase" and "BaseRatingForm". votingapiResult in Voting Api module seems to have changed, and now return results based in vote type (in this case $entity->bundle()), I think that previously it worked well that way since nobody had reported this issue and certainly think it's an improvement in Voting Api module but I think we should modify these methods.

I have made a patch that seems to work and fix the issue, this is attached.
Regards
Juan

πŸ› Bug report
Status

Needs review

Version

1.0

Component

Code

Created by

πŸ‡»πŸ‡ͺVenezuela jvizcarrondo

Live updates comments and jobs are added and updated live.
  • Needs tests

    The change is currently missing an automated test that fails when run with the original code, and succeeds when the bug has been fixed.

Sign in to follow issues

Merge Requests

Comments & Activities

Not all content is available!

It's likely this issue predates Contrib.social: some issue and comment data are missing.

  • Open on Drupal.org β†’
    Core: 9.5.5 + Environment: PHP 7.3 & MySQL 5.7
    last update over 1 year ago
    Waiting for branch to pass
  • πŸ‡ΊπŸ‡¦Ukraine artem_sylchuk Lutsk

    Attempted to re-roll the patch as it doesn't apply to the dev version anymore

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    This still absolutely needs a test case to demonstrate the error and prove the fix.

  • The last submitted patch, 24: votingapi_widgets-getresults-2990032-24.patch, failed testing. View results β†’
    - codesniffer_fixes.patch Interdiff of automated coding standards fixes only.

  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia
  • πŸ‡ΊπŸ‡ΈUnited States SocialNicheGuru

    Is this needed for 2.0.x-dev?

  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    Yes I have been using this patch on 2.0.x-dev for some time, and it works well.

  • Pipeline finished with Failed
    7 months ago
    Total: 130s
    #274644
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    Here is a MR for 2.0.x

    @TR Mentioned that this needs a test to demonstrate the error and prove the fix. I unfortunately, don't know how to go about doing that.

    But you can see my comment #11 for a description of two scenarios where it is failing for me.

    These changes do resolve the issue for me and I have been using in production for some time now.

  • Pipeline finished with Failed
    about 1 month ago
    Total: 208s
    #434312
  • Pipeline finished with Failed
    about 1 month ago
    Total: 315s
    #434324
  • Pipeline finished with Failed
    about 1 month ago
    Total: 145s
    #434328
  • Pipeline finished with Failed
    19 days ago
    #453412
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    I've made some updates to the MR.

    I was noticing some issues with incorrect vote results appearing when multiple vote fields were on the same page.

    There was also some code duplication with a getResults() method in both the base form and the base widget classes so I consolidated those and and was able simplify and lean up the logic.

    This seems to be working well at first pass.

  • Pipeline finished with Failed
    19 days ago
    Total: 176s
    #453422
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles
  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles
  • Pipeline finished with Failed
    19 days ago
    Total: 230s
    #453570
  • Pipeline finished with Failed
    19 days ago
    Total: 233s
    #453577
  • πŸ‡ΊπŸ‡ΈUnited States tr Cascadia

    I think this is all good, but I really need a test case for this. I can write it if you can write down the specific steps needed to cause this error. I need to know how to set up the site - how to set up the fields/bundles/content types, then what actions have to be taken to get the wrong result and where does the wrong result show up? Do this for both of your cases mentioned in #11.

  • πŸ‡ΊπŸ‡ΈUnited States loze Los Angeles

    Thanks @tr. I agree on the need for test and wish I was able to figure that out myself.

    Here are the steps as I see them.

    To summarize: The getResults method in VotingAPI widgets returns incorrect results in certain cases due to how it combines records based on field names without considering entity types or distinct field names within the same entity.

    I have observed two specific cases where this occurs, though there may be others.

    Steps to Reproduce

    1. Fields with the Same Name on Different Entity Types

    • Create a NODE type "page" and add a VotingAPI field named field_rating.
    • Create a MEDIA type "image" and add a VotingAPI field named field_rating.
    • Cast votes on both entities.
    • Retrieve results for one of them. you can either look on screen, or programtically get them with the getResults() method

    Expected Behavior:
    Each entity type should have its own distinct vote results.

    Actual Behavior:

    • The results are either combined incorrectly across entity types or otherwise inaccurate.
    • Both the total votes cast and the average values are incorrect.
    • This occurs because getResults groups records by field name without considering the entity type.

    2. Two Fields with Similar Names on the Same Entity Type

    • Create a NODE type "page".
    • Add a VotingAPI field named field_rating.
    • Add a second VotingAPI field named field_rating2.
    • Cast votes on that node in both of the fields.

    Expected Behavior:
    Each field should return its own separate vote results.

    Actual Behavior:

    • The results are incorrect due to the same underlying issue. getResults is searching for a substring of the field name so "field_rating" is also grabbing results from "field_rating2".
    • Both the total votes cast and the average values are incorrect.

    I really wish I could set up a test for this myself. I spent time yesterday trying to figure out how to write a proper test but got lost on just the basics like creating a node and adding a votingapi_widgets field to it, let alone going about making it cast votes. So I appreciate any help or guidance on how to approach this.

Production build 0.71.5 2024