xAPI-vle / moodle-logstore_xapi

A Moodle plugin to send xAPI statements to an LRS using events in the Moodle logstore.
GNU General Public License v3.0
74 stars 85 forks source link

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

Open ScottVerbeek opened 7 months ago

ScottVerbeek commented 7 months ago

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 commented 7 months ago

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