moodle/moodle · error · Exception

Activity '' cannot be used as the default idnumber. The…

Error message

Activity '' cannot be used as the default idnumber. The idnumber has a max length of 100 chars. Please manually specify an idnumber.

What it means

behat_core_generator::process_activity() defaults an activity's idnumber to its name. Because idnumbers are limited to 100 characters, when the activity name exceeds that length and no explicit idnumber was given, it throws this Exception telling the author to specify an idnumber manually.

Solutions

  1. Add an idnumber (≤100 chars) to the activity data in the Behat step
  2. Shorten the activity name to ≤100 characters
  3. If the test doesn't rely on idnumber, use a concise name and reference it elsewhere by name
  4. Centralise long names in a constants/fixtures file with a short idnumber mapping

Example fix

// before
| activity | name | course |
| forum    | A very long forum name exceeding one hundred characters ... | C1 |
// after
| activity | name | idnumber | course |
| forum    | A very long forum name exceeding one hundred characters ... | forum1 | C1 |
Defensive patterns

Strategy: validation

Validate before calling

if (array_key_exists('idnumber', $data) && strlen($data['idnumber']) > 100) {
  throw new \InvalidArgumentException('idnumber exceeds 100 chars');
}
if (!array_key_exists('idnumber', $data) && strlen($data['name']) > 100) {
  throw new \InvalidArgumentException('name too long for default idnumber; specify idnumber');
}

Type guard

is_string($data['name'] ?? null) && (isset($data['idnumber']) ? strlen($data['idnumber']) <= 100 : strlen($data['name']) <= 100)

Try / catch

try { $this->create_activity($data); }
catch (\Exception $e) {
  if (str_contains($e->getMessage(), 'default idnumber')) { /* add explicit idnumber */ }
  throw $e;
}

Prevention

When it happens

Trigger: Using an activity creation Behat step with a very long activity name and no idnumber column/value in the data.

Common situations: Verbose descriptive activity names in feature files; copied step data with long titles; localised names plus suffixes pushing past 100 chars.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


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

Appendix: source

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

        // The the_following_exists() method checks that the field exists.
        $activityname = $data['activity'];
        unset($data['activity']);

        // Convert scale name into scale id (negative number indicates using scale).
        if (isset($data['grade']) && strlen($data['grade']) && !is_number($data['grade'])) {
            $data['grade'] = - $this->get_scale_id($data['grade']);
            require_once("$CFG->libdir/grade/constants.php");

            if (!isset($data['gradetype'])) {
                $data['gradetype'] = GRADE_TYPE_SCALE;
            }
        }

        if (!array_key_exists('idnumber', $data)) {
            $data['idnumber'] = $data['name'];
            if (strlen($data['name']) > 100) {
                throw new Exception(
                    "Activity '{$activityname}' cannot be used as the default idnumber. " .
                    "The idnumber has a max length of 100 chars. " .
                    "Please manually specify an idnumber."
                );
            }
        }

        // We split $data in the activity $record and the course module $options.
        $cmoptions = array();
        $cmcolumns = $DB->get_columns('course_modules');
        foreach ($cmcolumns as $key => $value) {
            if (isset($data[$key])) {
                $cmoptions[$key] = $data[$key];
            }
        }

        $this->datagenerator->create_module($activityname, $data, $cmoptions);
    }

View on GitHub (pinned to 48ea8c3322)