Skip to content

Update ProductCommentRepository.php#227

Open
metineren wants to merge 1 commit into
PrestaShop:devfrom
metineren:patch-1
Open

Update ProductCommentRepository.php#227
metineren wants to merge 1 commit into
PrestaShop:devfrom
metineren:patch-1

Conversation

@metineren

Copy link
Copy Markdown

Fixes #226 issue
$productIds parameter array causes an error because it is not sorted.

Questions Answers
Description? $productIds parameter array causes an error because it is not sorted.
Type? bug fix
BC breaks? yes
Deprecations? no
Fixed ticket? Fixes #226.
How to test? Once add a comment to a product and approve from backoffice, after turn to front office a product listing page, inspect on chrome, show network tab, filter Fetch/XHR, find /prestashop/module/productcomments/CommentGrade request and see error.
Sponsor company Metin EREN.

Fixes PrestaShop#226 issue

$productIds parameter array causes an error because it is not sorted.
@ps-jarvis

Copy link
Copy Markdown

Hello @metineren!

This is your first pull request on productcomments repository of the PrestaShop project.

Thank you, and welcome to this Open Source community!

@github-project-automation github-project-automation Bot moved this to Ready for review in PR Dashboard Apr 15, 2026
@boo-code

Copy link
Copy Markdown

QA — fix confirmed ✅

Verified the root cause and the fix at the SQL level (PHP 8.1). The comma between products is emitted by if ($count - 1 > $index), which compares the loop key $index against a count — so it only works when $productIds is a 0-based sequential array. With non-sequential keys (e.g. an array keyed by product id, or with gaps after deletions), the comma is dropped:

getAverageGrades([5 => 223, 9 => 224], false):

  • Before ($count - 1 > $index1 > 5 is false → no comma):
    ... AS "223" SUM(IF(id_product = 224 ...
    SQLSTATE[42000] ... 1064 exactly as reported.
  • After (array_values() re-indexes to 0,1 → comma emitted):
    ... AS "223", SUM(IF(id_product = 224 ...

The array_values() approach is correct and minimal. 👍

Suggested regression test

The bug is purely in the SQL-string construction, so it can be pinned with a fast unit test using a captured connection (no DB). It fails on the base (missing comma) and passes with this PR:

<?php

namespace PrestaShop\Module\ProductComment\Tests\Unit\Repository;

use PHPUnit\Framework\TestCase;
use PrestaShop\Module\ProductComment\Repository\ProductCommentRepository;
use ReflectionClass;

class ProductCommentRepositoryTest extends TestCase
{
    public function testGetAverageGradesSeparatesProductsWhenKeysAreNotSequential(): void
    {
        $sql = $this->captureGeneratedSql([5 => 223, 9 => 224]);

        // Without the fix the comma is dropped -> `AS "223" SUM(...)` -> SQL syntax error.
        $this->assertStringContainsString('AS "223", SUM', $sql);
    }

    private function captureGeneratedSql(array $productIds): string
    {
        $connection = new class {
            public string $sql = '';

            public function executeQuery(string $sql)
            {
                $this->sql = $sql;

                return new class {
                    public function fetch()
                    {
                        return [];
                    }
                };
            }
        };

        $reflection = new ReflectionClass(ProductCommentRepository::class);
        /** @var ProductCommentRepository $repository */
        $repository = $reflection->newInstanceWithoutConstructor();
        foreach (['connection' => $connection, 'databasePrefix' => 'ps_'] as $property => $value) {
            $prop = $reflection->getProperty($property);
            $prop->setAccessible(true);
            $prop->setValue($repository, $value);
        }

        $repository->getAverageGrades($productIds, false);

        return $connection->sql;
    }
}

(The same applies to getCommentsNumberForProducts, which this PR also fixes.) The module doesn't ship a PHPUnit setup yet (only phpstan under tests/), so this is ready to drop in once a unit-test target is wired — happy to help with that if useful.

@metineren

Copy link
Copy Markdown
Author

@boo-code Audrius thank you for your PR & confirmation

@metineren

Copy link
Copy Markdown
Author

@jolelievre Hi Jonathan, can you complete the merge request?

@ps-jarvis ps-jarvis added the Waiting for QA Status: Action required, Waiting for test feedback label Jul 8, 2026
@ps-jarvis ps-jarvis moved this from Ready for review to To be tested in PR Dashboard Jul 8, 2026
@kpodemski kpodemski added this to the 8.0.1 milestone Jul 8, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Waiting for QA Status: Action required, Waiting for test feedback

Projects

Status: To be tested

Development

Successfully merging this pull request may close these issues.

incorrect algorithm for creating database queries in ProductCommentRepository class

4 participants