moodle/moodle · error · Exception

contentbank_content requires the field reference to be…

Error message

contentbank_content requires the field reference to be specified

What it means

process_contentbank_content throws this when the 'reference' field is missing. After validating contextlevel, the generator needs reference (the instance identifier within that contextlevel) to resolve the exact context id for storing the content.

Solutions

  1. Add a 'reference' row with the instance identifier matching the contextlevel (course shortname like C1, username for user context)
  2. Ensure the referenced instance exists earlier in the scenario
  3. Verify the header is exactly 'reference'
  4. Confirm the empty() vs isset() semantics: an empty reference string is treated as missing

Example fix

// before
  | contextlevel | contenttype     | name       |
  | course       | contenttype_h5p | sample.h5p |
// after
  | contextlevel | reference | contenttype     | name       |
  | course       | C1        | contenttype_h5p | sample.h5p |
Defensive patterns

Strategy: validation

Validate before calling

if (empty($data['contextlevel']) || empty($data['reference']) || empty($data['contenttype'])) {
    throw new ExpectationException("'contentbank content' requires contextlevel, reference and contenttype", $this->getSession());
}

Type guard

function isCompleteContentbankRow(array $data): bool {
    return !empty($data['contextlevel']) && !empty($data['reference']) && !empty($data['contenttype']);
}

Try / catch

try {
    $this->getGenerator()->process_contentbank_content($data);
} catch (\Exception $e) {
    if (str_contains($e->getMessage(), 'reference')) {
        throw new ExpectationException('Add a reference row (instance id matching contextlevel) to your fixture', $this->getSession());
    }
    throw $e;
}

Prevention

When it happens

Trigger: A 'contentbank content' Behat table includes contextlevel but no 'reference' column or an empty reference cell; raised at behat_core_generator.php:1038.

Common situations: Fixtures where the instance shortname column was dropped during editing; users providing the course fullname in a differently-named column instead of the required 'reference'; generated fixtures missing the last column due to CSV parsing.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of moodle/moodle@48ea8c3322 (2026-09-14). Data as JSON: /api/errors/38f9882738622106. Report an issue: GitHub.

Appendix: source

Thrown at public/lib/behat/classes/behat_core_generator.php:1038

    protected function process_user_preferences(array $data) {
        set_user_preference($data['preference'], $data['value'], $data['userid']);
    }

    /**
     * Create content in the given context's content bank.
     *
     * @param array $data
     * @return void
     */
    protected function process_contentbank_content(array $data) {
        global $CFG;

        if (empty($data['contextlevel'])) {
            throw new Exception('contentbank_content requires the field contextlevel to be specified');
        }

        if (!isset($data['reference'])) {
            throw new Exception('contentbank_content requires the field reference to be specified');
        }

        if (empty($data['contenttype'])) {
            throw new Exception('contentbank_content requires the field contenttype to be specified');
        }

        $contenttypeclass = "\\".$data['contenttype']."\\contenttype";
        if (class_exists($contenttypeclass)) {
            $context = $this->get_context($data['contextlevel'], $data['reference']);
            $contenttype = new $contenttypeclass($context);
            $record = new stdClass();
            $record->usercreated = $data['userid'];
            $record->name = $data['contentname'];
            if (isset($data['visibility'])) {
                $record->visibility = $data['visibility'];
            }
            $content = $contenttype->create_content($record);

View on GitHub (pinned to 48ea8c3322)