Test Case Linking Fix - sebiboga/testlink-upgraded GitHub Wiki

Test Case Linking Crash Fix

Issue #138

Fixed critical bug: Application crash when linking test cases to requirements with INACTIVE versions.

Problem

When attempting to link a test case to a requirement, the application would crash if the test case's latest version was marked as INACTIVE.

Error

Fatal error: Call to a member function on null

Root Cause

In lib/functions/requirement_mgr.class.php, the assign_to_tcase() function did not validate whether the latest test case version was active before attempting to use it.

The code would:

  1. Call get_last_active_version() to retrieve the latest active version
  2. NOT check if the result was null
  3. Attempt to access properties on a null value → CRASH

Solution

Added early validation to check if the latest test case version is INACTIVE:

// Get Latest Active Test Case Version 
$tcv = current($tcMgr->get_last_active_version($testcase_id));
if ($tcv == null) {
  // Latest version is Inactive => operation not allowed
  return $output;
}

Behavior:

  • If latest test case version is INACTIVE → return with no link created
  • If latest test case version is ACTIVE → proceed with linking
  • No error/exception thrown → graceful handling

Implementation

File Changed

  • lib/functions/requirement_mgr.class.php
  • Function: assign_to_tcase()

Code Pattern

function assign_to_tcase($req_id, $testcase_id, $author_id) {
  $tcMgr = new testcase($this->db);
  
  // Check if test case version is active FIRST
  if ($testcase_id && $req_id) {
    $tcv = current($tcMgr->get_last_active_version($testcase_id));
    if ($tcv == null) {
      // Latest version is Inactive => operation not allowed
      return 0;  // Return early, no link created
    }
  }

  // Go ahead with requirement linking...
  if ($testcase_id && $req_id) {
    // ... rest of linking logic ...
  }
}

How to Use

Linking Requirements to Active Test Cases

✅ WORKS: Test case latest version is ACTIVE
   → Requirement successfully linked

Attempting to Link to Inactive Test Cases

âš ī¸  BLOCKED: Test case latest version is INACTIVE
   → Operation returns silently (graceful failure)
   → No crash
   → No link created

User Impact

Before Fix

  • ❌ Application crashes
  • ❌ Error page displayed
  • ❌ Session may be interrupted

After Fix

  • ✅ Operation handled gracefully
  • ✅ No error message (silent failure)
  • ✅ User can continue working
  • â„šī¸ User should activate test case version first

Related Workflow

Recommended Process

  1. Create Test Case

    • New test cases are ACTIVE by default
  2. Link to Requirement

    • Click requirement → "Link to Test Case"
    • Select test case
    • Link created successfully ✅
  3. Deactivate Test Case (if needed)

    • Change test case status to INACTIVE
    • Existing requirement links remain
    • Cannot create NEW links until reactivated
  4. Reactivate Test Case

    • Change status back to ACTIVE
    • Can link to new requirements again ✅

Testing

Test Case 1: Link to Active Test Case

Preconditions:
  - Test case exists with ACTIVE version
  - Requirement exists
  
Steps:
  1. Navigate to requirement
  2. Click "Link Test Case"
  3. Select active test case
  4. Click "Link"
  
Expected Result:
  ✅ Link created successfully
  ✅ No errors

Test Case 2: Link to Inactive Test Case

Preconditions:
  - Test case exists with INACTIVE version
  - Requirement exists
  
Steps:
  1. Navigate to requirement
  2. Click "Link Test Case"
  3. Select inactive test case
  4. Click "Link"
  
Expected Result:
  ✅ Operation completes
  ✅ No crash/error
  ✅ No link created
  â„šī¸ May show info message (optional enhancement)

Test Case 3: Multiple Versions

Preconditions:
  - Test case has multiple versions
  - Latest version is INACTIVE
  - Earlier versions are ACTIVE
  
Expected Result:
  ✅ Operation blocked (uses latest version check)
  ✅ User must activate latest version to link

Enhancement Opportunities

Future Improvements

  1. User Feedback

    if ($tcv == null) {
      // Add message
      return ['status' => 'error', 
              'message' => lang_get('test_case_inactive')];
    }
    
  2. Automatic Version Activation

    • Option to auto-activate when linking
  3. UI Indication

    • Show INACTIVE status in test case selector
    • Disable selection of inactive versions
  4. Logging

    • Log failed link attempts
    • Track inactive test case linking attempts

Related Features

Technical Details

Affected Function

  • requirement_mgr::assign_to_tcase()
  • requirement_mgr::create_tc_from_requirement()

Related Methods

  • testcase::get_last_active_version() - Gets latest ACTIVE version
  • testcase::getActiveStatus() - Checks if version is active

Commit Information

  • Commit ID: b0dd23a70
  • Message: "fix: logic to avoid crash when trying to add req link to a test case that has latest version INACTIVE"
  • Status: ✅ Already integrated in sebiboga branch

References

  • GitHub Issue: #138
  • Original commit (sebiboga_2.0): e57510a84
  • Current branch: sebiboga (b0dd23a70)

Status: ✅ Fixed
Issue: #138
Type: Critical Bug Fix
Severity: High
Impact: Zero breaking changes