{"record":{"id":"562e7e13283d2a22","repo":"moodle/moodle","slug":"blocks-requires-the-field-reference-to-be-specified","errorCode":null,"errorMessage":"'blocks' requires the field 'reference' to be specified","messagePattern":"'blocks' requires the field 'reference' to be specified","errorType":"validation","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"public/lib/behat/classes/behat_core_generator.php","lineNumber":502,"sourceCode":"     * @param array $data should mostly match the fields of the block_instances table.\n     *     The block type is specified by blockname.\n     *     The parentcontextid is set from contextlevel and reference.\n     *     Missing values are filled in by testing_block_generator::prepare_record.\n     *     $data is passed to create_block as both $record and $options. Normally\n     *     the keys are different, so this is a way to let people set values in either place.\n     */\n    protected function process_block_instance($data) {\n\n        if (empty($data['blockname'])) {\n            throw new Exception('\\'blocks\\' requires the field \\'block\\' type to be specified');\n        }\n\n        if (empty($data['contextlevel'])) {\n            throw new Exception('\\'blocks\\' requires the field \\'contextlevel\\' to be specified');\n        }\n\n        if (!isset($data['reference'])) {\n            throw new Exception('\\'blocks\\' requires the field \\'reference\\' to be specified');\n        }\n\n        $context = $this->get_context($data['contextlevel'], $data['reference']);\n        $data['parentcontextid'] = $context->id;\n\n        // Pass $data as both $record and $options. I think that is unlikely to\n        // cause problems since the relevant key names are different.\n        // $options is not used in most blocks I have seen, but where it is, it is necessary.\n        $this->datagenerator->create_block($data['blockname'], $data, $data);\n    }\n\n    /**\n     * Creates language customisation.\n     *\n     * @throws Exception\n     * @throws dml_exception\n     * @param array $data\n     * @return void","sourceCodeStart":484,"sourceCodeEnd":520,"githubUrl":"https://github.com/moodle/moodle/blob/48ea8c33227277e916cdd4079dbecef448b37f2c/public/lib/behat/classes/behat_core_generator.php#L484-L520","documentation":"This exception comes from the Behat test data generator for blocks (behat_core_generator::process_block_instance). When a Behat scenario creates a block instance, the 'reference' field is mandatory: it identifies the entity (e.g. a course or course module) whose context the block is attached to, resolved via get_context($data['contextlevel'], $data['reference']). If 'reference' is absent the generator cannot compute parentcontextid and throws instead of failing later with an opaque database error.","triggerScenarios":"Calling the blocks generator (e.g. the 'And the following \"blocks\" exist:' step or behat_core_generator's create_block_instance path) without a 'reference' field/column, even when 'contextlevel' is provided.","commonSituations":"Behat Gherkin fixture tables missing the reference column; refactors renaming the identifying column; copy-pasted fixture rows from other generators that use differently-named keys.","solutions":["Add a 'reference' field naming the existing entity (e.g. course shortname or cm idnumber).","Ensure the 'contextlevel' field is also set and consistent with the referenced entity (course, coursecat, module, system).","If using a Behat table, include a 'reference' column header with the entity identifier in each row.","Verify fixture keys match process_block_instance's expected keys exactly (contextlevel, reference, blockname)."],"exampleFix":"// before (Behat fixture)\n| blockname | contextlevel |\n| comments  | course       |\n\n// after\n| blockname | contextlevel | reference |\n| comments  | course       | C1        |","handlingStrategy":"validation","validationCode":"$required = ['contextlevel', 'reference', 'blockname'];\nforeach ($required as $key) {\n    if (empty($data[$key])) {\n        throw new InvalidArgumentException(\"blocks generator requires field '$key'\");\n    }\n}","typeGuard":null,"tryCatchPattern":"try {\n    $generator->create_block('comments', ['contextlevel' => 'course', 'reference' => 'C1']);\n} catch (Exception $e) {\n    if (strpos($e->getMessage(), \"requires the field 'reference'\") !== false) {\n        // log fixture name and rethrow with context\n    }\n    throw $e;\n}","preventionTips":["Always include contextlevel+reference pairs in block fixtures.","Copy column headers from the official Behat generator docs for blocks.","Validate fixtures in CI by running the Behat scenario once before merging.","Keep block fixture rows consistent with other context-based generators."],"tags":["behat","moodle","test-data","missing-field"],"backgroundTag":"missing-required-argument","analyzedSha":"48ea8c33227277e916cdd4079dbecef448b37f2c","analyzedAt":"2026-09-14T20:34:48.527Z","contentChangedAt":"2026-09-14T20:34:48.527Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}