Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3,202 changes: 1,931 additions & 1,271 deletions tests/behat/features/SpecialCase1.feature

Large diffs are not rendered by default.

3,566 changes: 3,566 additions & 0 deletions tests/behat/features/SpecialCase1optim.feature

Large diffs are not rendered by default.

968 changes: 968 additions & 0 deletions tests/behat/features/SpecialCase2.feature

Large diffs are not rendered by default.

1,041 changes: 1,041 additions & 0 deletions tests/behat/features/SpecialCase2optim.feature

Large diffs are not rendered by default.

850 changes: 774 additions & 76 deletions tests/behat/features/bootstrap/FeatureContext.php

Large diffs are not rendered by default.

647 changes: 647 additions & 0 deletions tests/behat/features/teardown.feature

Large diffs are not rendered by default.

719 changes: 719 additions & 0 deletions tests/behat/features/teardownoptim.feature

Large diffs are not rendered by default.

78 changes: 78 additions & 0 deletions tests/behat/read_debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
#!/bin/bash
# ============================================================
# read_debug.sh — Read the latest Behat debug dump
#
# Usage:
# ./read_debug.sh → shows the latest meta + form_summary
# ./read_debug.sh full → shows the latest full HTML
# ./read_debug.sh all → shows meta + form_summary + full HTML
# ./read_debug.sh clean → delete all debug files
# ============================================================

# Debug files are written by FeatureContext.php on step failure.
# Naming convention: <timestamp>_<scenario>_{full.html,meta.txt,form_summary.txt}
DEBUG_DIR="$(dirname "$0")/behat_debug"

if [ ! -d "$DEBUG_DIR" ]; then
echo "No behat_debug/ directory found. No failures captured yet."
exit 0
fi

case "${1:-summary}" in
clean)
rm -f "$DEBUG_DIR"/*.html "$DEBUG_DIR"/*.txt
echo "Debug files cleaned."
exit 0
;;
full)
LATEST=$(ls -t "$DEBUG_DIR"/*_full.html 2>/dev/null | head -1)
if [ -z "$LATEST" ]; then
echo "No full HTML dump found."
exit 0
fi
echo "=== LATEST FULL HTML DUMP: $(basename "$LATEST") ==="
cat "$LATEST"
;;
all)
# Meta
LATEST_META=$(ls -t "$DEBUG_DIR"/*_meta.txt 2>/dev/null | head -1)
if [ -n "$LATEST_META" ]; then
echo ""
echo "=== META ==="
cat "$LATEST_META"
fi
echo ""
# Form summary
LATEST_SUMMARY=$(ls -t "$DEBUG_DIR"/*_form_summary.txt 2>/dev/null | head -1)
if [ -n "$LATEST_SUMMARY" ]; then
echo ""
echo "=== FORM SUMMARY ==="
cat "$LATEST_SUMMARY"
fi
echo ""
# Full HTML (first 500 lines to avoid flooding)
LATEST_FULL=$(ls -t "$DEBUG_DIR"/*_full.html 2>/dev/null | head -1)
if [ -n "$LATEST_FULL" ]; then
echo ""
echo "=== FULL HTML (first 500 lines) ==="
head -500 "$LATEST_FULL"
fi
;;
summary|*)
# Meta
LATEST_META=$(ls -t "$DEBUG_DIR"/*_meta.txt 2>/dev/null | head -1)
if [ -n "$LATEST_META" ]; then
echo "=== META ==="
cat "$LATEST_META"
echo ""
fi
# Form summary
LATEST_SUMMARY=$(ls -t "$DEBUG_DIR"/*_form_summary.txt 2>/dev/null | head -1)
if [ -n "$LATEST_SUMMARY" ]; then
echo "=== FORM SUMMARY ==="
cat "$LATEST_SUMMARY"
else
echo "No form summary found."
fi
;;
esac
97 changes: 97 additions & 0 deletions tests/behat/run_and_debug.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
#!/bin/bash
# ============================================================
# run_and_debug.sh — Run a Behat scenario AND show debug info
#
# Usage:
# ./run_and_debug.sh <feature_file> [line_number]
#
# Examples:
# ./run_and_debug.sh features/SpecialCase1.feature 10
# ./run_and_debug.sh features/debugTest.feature
#
# After execution:
# - If the test PASSES: shows "ALL PASSED"
# - If the test FAILS: shows meta + form_summary automatically
# ============================================================

BEHAT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_DIR="$(cd "$BEHAT_DIR/../.." && pwd)"
DEBUG_DIR="$BEHAT_DIR/behat_debug"
# wsl_output.txt is the Windows-readable bridge file: WSL writes here (/mnt/c/...)
# and Windows (IDE, Claude Code) reads from C:\wamp64\www\chamilo2\tests\behat\wsl_output.txt.
OUTPUT_FILE="/mnt/c/wamp64/www/chamilo2/tests/behat/wsl_output.txt"

FEATURE_FILE="$1"
LINE="$2"

if [ -z "$FEATURE_FILE" ]; then
echo "Usage: $0 <feature_file> [line_number]"
echo "Example: $0 features/SpecialCase1.feature 10"
exit 1
fi

# Clean previous debug files
rm -f "$DEBUG_DIR"/*_full.html "$DEBUG_DIR"/*_form_summary.txt "$DEBUG_DIR"/*_meta.txt 2>/dev/null

# Build the behat command
if [ -n "$LINE" ]; then
BEHAT_TARGET="${BEHAT_DIR}/${FEATURE_FILE}:${LINE}"
else
BEHAT_TARGET="${BEHAT_DIR}/${FEATURE_FILE}"
fi

echo "=================================================="
echo "Running: vendor/bin/behat $BEHAT_TARGET"
echo "=================================================="

cd "$PROJECT_DIR"
vendor/bin/behat "$BEHAT_TARGET" --config "${BEHAT_DIR}/behat.yml" 2>&1
BEHAT_EXIT=$?

echo ""
echo "=================================================="

if [ $BEHAT_EXIT -eq 0 ]; then
echo "ALL PASSED"
echo "=================================================="
# Write to output file for Windows reading
echo "ALL PASSED" > "$OUTPUT_FILE"
else
echo "FAILED — Reading debug dumps..."
echo "=================================================="
echo ""

# Collect all output into the Windows-readable file
> "$OUTPUT_FILE"

# Find the latest meta file
LATEST_META=$(ls -t "$DEBUG_DIR"/*_meta.txt 2>/dev/null | head -1)
if [ -n "$LATEST_META" ]; then
echo "=== META ===" | tee -a "$OUTPUT_FILE"
cat "$LATEST_META" | tee -a "$OUTPUT_FILE"
echo "" | tee -a "$OUTPUT_FILE"
fi

# Find the latest form summary
LATEST_SUMMARY=$(ls -t "$DEBUG_DIR"/*_form_summary.txt 2>/dev/null | head -1)
if [ -n "$LATEST_SUMMARY" ]; then
echo "=== FORM SUMMARY ===" | tee -a "$OUTPUT_FILE"
cat "$LATEST_SUMMARY" | tee -a "$OUTPUT_FILE"
echo "" | tee -a "$OUTPUT_FILE"
fi

# Also add first 200 lines of full HTML for deep analysis
LATEST_FULL=$(ls -t "$DEBUG_DIR"/*_full.html 2>/dev/null | head -1)
if [ -n "$LATEST_FULL" ]; then
echo "=== FULL HTML (first 200 lines) ===" >> "$OUTPUT_FILE"
head -200 "$LATEST_FULL" >> "$OUTPUT_FILE"
echo "" >> "$OUTPUT_FILE"
echo "(Full HTML available in: $LATEST_FULL)" | tee -a "$OUTPUT_FILE"
fi

echo ""
echo "Debug output also written to: $OUTPUT_FILE"
echo "(readable from Windows at C:\\wamp64\\www\\chamilo2\\tests\\behat\\wsl_output.txt)"
fi

exit $BEHAT_EXIT
92 changes: 92 additions & 0 deletions tests/behat/run_and_log.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#!/bin/bash
# =============================================================================
# run_and_log.sh — Main Behat test runner with full logging
#
# Runs Behat with any arguments forwarded from the caller, then writes output
# to two log files that are overwritten on every run:
# behat_last_run.log — complete output of the run (all steps, all colors)
# behat_last_errors.log — extracted failures/errors only, for quick review
#
# Usage (direct):
# ./run_and_log.sh features/SpecialCase1.feature
# ./run_and_log.sh features/SpecialCase1.feature:42 # single scenario by line
# ./run_and_log.sh # run the full default suite
#
# Typically called indirectly via:
# run_scenario.sh — shorthand wrapper (line number + feature name)
# watch_and_run.sh — auto-re-run on .feature file change
#
# WSL note: run this script from inside WSL (/var/www/chamilo-lms), not from
# Windows directly — ChromeDriver and PHP must run in the Linux environment.
# =============================================================================
set -euo pipefail

# Launch Behat and log output
# Helps identify changes upstream by comparing logs

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
PROJECT_ROOT="$(cd "$SCRIPT_DIR/../.." && pwd)"
LOG_DIR="$SCRIPT_DIR"
FULL_LOG="$LOG_DIR/behat_last_run.log"
ERROR_LOG="$LOG_DIR/behat_last_errors.log"

echo "========================================" | tee "$FULL_LOG"
echo "Behat run started at $(date '+%Y-%m-%d %H:%M:%S')" | tee -a "$FULL_LOG"
echo "Arguments: $*" | tee -a "$FULL_LOG"
echo "========================================" | tee -a "$FULL_LOG"

cd "$SCRIPT_DIR"

# Temporarily disable errexit so a Behat failure (non-zero exit) does not kill
# this script before we can capture the exit code and extract error details.
set +e
php "$PROJECT_ROOT/vendor/bin/behat" \
--config behat.yml \
--format pretty \
--no-interaction \
--stop-on-failure \
--colors \
"$@" 2>&1 | tee -a "$FULL_LOG"
# PIPESTATUS[0] captures Behat's exit code, not tee's (which is always 0).
EXIT_CODE=${PIPESTATUS[0]}
set -e

echo "" | tee -a "$FULL_LOG"
echo "========================================" | tee -a "$FULL_LOG"
echo "Behat run finished at $(date '+%Y-%m-%d %H:%M:%S') -- exit code: $EXIT_CODE" | tee -a "$FULL_LOG"
echo "========================================" | tee -a "$FULL_LOG"

# Post-process the full log into a compact error summary.
# The grep extracts the 3 lines before/after each failure keyword so you can
# see the failing step in context without reading thousands of lines.
echo "Extracting errors into $ERROR_LOG ..."
{
echo "=== BEHAT ERROR SUMMARY ==="
echo "Command: behat $*"
echo "Exit code: $EXIT_CODE"
echo ""

if [ "$EXIT_CODE" -eq 0 ]; then
echo "All scenarios passed. No errors."
else
echo "--- Failed / errored steps ---"
grep -n -B 3 -A 3 -iE '(failed|error|exception|timed out|not found|skipped)' "$FULL_LOG" 2>/dev/null || echo "(no matching lines found)"
echo ""
echo "--- Summary lines ---"
grep -E '(scenario|step|failed|passed|pending|undefined)' "$FULL_LOG" | tail -20
fi
} > "$ERROR_LOG"

echo ""
echo "Done."
echo " Full log: $FULL_LOG"
echo " Error log: $ERROR_LOG"
echo ""

if [ "$EXIT_CODE" -ne 0 ]; then
echo ">>> THERE WERE FAILURES. <<<"
echo ""
cat "$ERROR_LOG"
fi

exit $EXIT_CODE
38 changes: 38 additions & 0 deletions tests/behat/run_scenario.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/bin/bash
# =============================================================================
# run_scenario.sh — Shorthand wrapper around run_and_log.sh
#
# Convenience script: pass a line number and a feature name (without extension)
# instead of typing the full path each time. Delegates all real work to
# run_and_log.sh, which produces behat_last_run.log and behat_last_errors.log.
#
# Usage:
# ./run_scenario.sh <line> [FeatureName]
# ./run_scenario.sh 0 [FeatureName] # 0 = run ALL scenarios in the file
#
# Arguments:
# $1 line number of the scenario to run (use 0 to run the whole file)
# $2 feature file name WITHOUT .feature extension (default: SpecialCase1)
#
# Examples:
# ./run_scenario.sh 42 SpecialCase2 # runs SpecialCase2.feature:42
# ./run_scenario.sh 0 SpecialCase1optim # runs all of SpecialCase1optim.feature
# ./run_scenario.sh 0 # runs all of SpecialCase1.feature
# =============================================================================
set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
LINE="${1:-0}"
FEATURE="${2:-SpecialCase1}"

# The `2>/dev/null` suppresses the arithmetic error when $LINE is non-numeric
# (e.g. if someone passes "all" by mistake) — it falls through to the else branch.
if [ "$LINE" -eq 0 ] 2>/dev/null || [ "$LINE" = "0" ]; then
echo "=== Running ALL scenarios in ${FEATURE}.feature ==="
echo ""
bash "$SCRIPT_DIR/run_and_log.sh" "features/${FEATURE}.feature"
else
echo "=== Running ${FEATURE}.feature:${LINE} ==="
echo ""
bash "$SCRIPT_DIR/run_and_log.sh" "features/${FEATURE}.feature:${LINE}"
fi
51 changes: 51 additions & 0 deletions tests/behat/watch_and_run.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
#!/bin/bash
# =============================================================================
# watch_and_run.sh — Watch .feature files and re-run Behat on changes to
# featureContext.php and any feature in the features/ directory.
# Requires: inotifywait (sudo apt install inotify-tools)
#
# Usage:
# ./watch_and_run.sh # watch all features
# ./watch_and_run.sh SpecialCase1.feature # watch one file only
# =============================================================================

set -uo pipefail

SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)"
FEATURES_DIR="$SCRIPT_DIR/features"
WATCH_TARGET="${1:-}"

if ! command -v inotifywait &>/dev/null; then
echo "ERROR: inotifywait not found. Install it with: sudo apt install inotify-tools"
exit 1
fi

echo "=== Behat Watch Mode ==="
echo "Watching: ${WATCH_TARGET:-all .feature files in $FEATURES_DIR}"
echo "Press Ctrl+C to stop."
echo ""

while true; do
if [ -n "$WATCH_TARGET" ]; then
WATCH_PATH="$FEATURES_DIR/$WATCH_TARGET"
else
WATCH_PATH="$FEATURES_DIR"
fi

# Block until any modify or create event fires on the watched path.
# -q suppresses the "Watching..." startup line; --format '%f' returns only
# the filename so we can pass it directly to run_and_log.sh.
CHANGED=$(inotifywait -q -e modify -e create --format '%f' "$WATCH_PATH" 2>/dev/null)
echo ""
echo ">>> Change detected: $CHANGED at $(date '+%H:%M:%S') — running Behat..."
echo ""

if [ -n "$WATCH_TARGET" ]; then
bash "$SCRIPT_DIR/run_and_log.sh" "features/$WATCH_TARGET"
else
bash "$SCRIPT_DIR/run_and_log.sh" "features/$CHANGED"
fi

echo ""
echo ">>> Waiting for next change..."
done
Loading