moodle-logstore_xapi icon indicating copy to clipboard operation
moodle-logstore_xapi copied to clipboard

TestRepository::read_records() array $query with more than one condition returns duplicate and mismatching records

Open ScottVerbeek opened this issue 2 years ago • 1 comments

The method TestRepositort::read_records takes a parameter array $query. These are the conditions that the data returned must match to. If multiple conditions are passed two problems can happens:

  1. If mutiple conditions are correct, multiple of the same records are added to $matchingrecords.
  2. If not all conditions are met, records are still added to $matchingrecords since only one has to pass to be added.

For ease here is the method:

    /**
     * Reads an array of objects from the store with the given type and query.
     *
     * @param string $type The name of the table to retrieve from.
     * @param array $query Any additional conditions to add to the query.
     * @return array
     */
    public function read_records(string $type, array $query) {
        $records = $this->testdata->$type;
        $matchingrecords = [];

        foreach ($records as $record) {
            foreach ($query as $key => $value) {
                if ($record->$key === $value) {
                    $matchingrecords[] = (object) $record;
                }
            }
        }

        return $matchingrecords;
    }

ScottVerbeek avatar Nov 28 '23 02:11 ScottVerbeek

I've refactored the method. I'll add this to this pull request: https://github.com/xAPI-vle/moodle-logstore_xapi/pull/849

ScottVerbeek avatar Nov 28 '23 02:11 ScottVerbeek