|
| 1 | +#!/usr/bin/env bash |
| 2 | +set -euo pipefail |
| 3 | + |
| 4 | +MATOMO_DIR=${MATOMO_DIR:?MATOMO_DIR is required} |
| 5 | +BASE_URL=${BASE_URL:-http://127.0.0.1:8000} |
| 6 | +MYSQL_HOST=${MYSQL_HOST:?MYSQL_HOST is required} |
| 7 | +MYSQL_PORT=${MYSQL_PORT:-3306} |
| 8 | +MYSQL_USER=${MYSQL_USER:?MYSQL_USER is required} |
| 9 | +MYSQL_PASSWORD=${MYSQL_PASSWORD:?MYSQL_PASSWORD is required} |
| 10 | +MYSQL_DATABASE=${MYSQL_DATABASE:?MYSQL_DATABASE is required} |
| 11 | +STATE_FILE=${STATE_FILE:-$PWD/.github/scripts/mcp-smoke/.state.json} |
| 12 | + |
| 13 | +export MYSQL_PWD="$MYSQL_PASSWORD" |
| 14 | + |
| 15 | +wait_for_mysql() { |
| 16 | + for _ in $(seq 1 60); do |
| 17 | + if mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -e 'SELECT 1' >/dev/null 2>&1; then |
| 18 | + return 0 |
| 19 | + fi |
| 20 | + sleep 2 |
| 21 | + done |
| 22 | + echo "MySQL did not become ready in time" >&2 |
| 23 | + return 1 |
| 24 | +} |
| 25 | + |
| 26 | +write_config() { |
| 27 | + cat > "$MATOMO_DIR/config/config.ini.php" <<EOF |
| 28 | +; <?php exit; ?> DO NOT REMOVE THIS LINE |
| 29 | +[database] |
| 30 | +host = "$MYSQL_HOST" |
| 31 | +username = "$MYSQL_USER" |
| 32 | +password = "$MYSQL_PASSWORD" |
| 33 | +dbname = "$MYSQL_DATABASE" |
| 34 | +tables_prefix = "" |
| 35 | +charset = "utf8mb4" |
| 36 | +collation = "utf8mb4_general_ci" |
| 37 | +
|
| 38 | +[General] |
| 39 | +trusted_hosts[] = "127.0.0.1" |
| 40 | +enable_logging = 1 |
| 41 | +
|
| 42 | +[log] |
| 43 | +log_writers[] = file |
| 44 | +log_level = DEBUG |
| 45 | +logger_file_path = tmp/logs/matomo.log |
| 46 | +
|
| 47 | +[McpServer] |
| 48 | +enable_mcp = 1 |
| 49 | +log_tool_calls = 1 |
| 50 | +log_tool_call_level = DEBUG |
| 51 | +log_tool_call_parameters_full = 1 |
| 52 | +EOF |
| 53 | +} |
| 54 | + |
| 55 | +start_php_server() { |
| 56 | + mkdir -p "$MATOMO_DIR/tmp/logs" |
| 57 | + : > "$MATOMO_DIR/tmp/logs/matomo.log" |
| 58 | + php -S 127.0.0.1:8000 -t "$MATOMO_DIR" > "$MATOMO_DIR/tmp/logs/php-server.log" 2>&1 & |
| 59 | + echo $! > "$MATOMO_DIR/tmp/php-server.pid" |
| 60 | + |
| 61 | + for _ in $(seq 1 60); do |
| 62 | + if curl -sS "$BASE_URL/index.php" >/dev/null 2>&1; then |
| 63 | + return 0 |
| 64 | + fi |
| 65 | + sleep 1 |
| 66 | + done |
| 67 | + |
| 68 | + echo "PHP server did not start in time" >&2 |
| 69 | + return 1 |
| 70 | +} |
| 71 | + |
| 72 | +api_get_json() { |
| 73 | + local url="$1" |
| 74 | + curl -sS "$url" | sed 's/^\xEF\xBB\xBF//' |
| 75 | +} |
| 76 | + |
| 77 | +require_non_empty() { |
| 78 | + local key="$1" |
| 79 | + local value="$2" |
| 80 | + if [ -z "$value" ] || [ "$value" = "null" ]; then |
| 81 | + echo "Required fixture value '$key' is missing from OmniFixture/API discovery." >&2 |
| 82 | + exit 1 |
| 83 | + fi |
| 84 | +} |
| 85 | + |
| 86 | +extract_php_constant() { |
| 87 | + local file="$1" |
| 88 | + local constant_name="$2" |
| 89 | + |
| 90 | + php -r ' |
| 91 | + $file = $argv[1]; |
| 92 | + $constantName = $argv[2]; |
| 93 | + $content = @file_get_contents($file); |
| 94 | + if ($content === false) { |
| 95 | + fwrite(STDERR, "Unable to read file: {$file}\n"); |
| 96 | + exit(2); |
| 97 | + } |
| 98 | +
|
| 99 | + $linePattern = "/^\\s*public\\s+const\\s+" . preg_quote($constantName, "/") . "\\s*=\\s*\\x27(.*)\\x27;\\s*$/"; |
| 100 | + foreach (preg_split("/\\R/", $content) as $line) { |
| 101 | + if (preg_match($linePattern, $line, $matches)) { |
| 102 | + echo stripcslashes($matches[1]); |
| 103 | + exit(0); |
| 104 | + } |
| 105 | + } |
| 106 | +
|
| 107 | + fwrite(STDERR, "Could not extract constant {$constantName} from {$file}\n"); |
| 108 | + exit(3); |
| 109 | + ' "$file" "$constant_name" |
| 110 | +} |
| 111 | + |
| 112 | +main() { |
| 113 | + wait_for_mysql |
| 114 | + |
| 115 | + mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" -e "DROP DATABASE IF EXISTS \`$MYSQL_DATABASE\`; CREATE DATABASE \`$MYSQL_DATABASE\` CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;" |
| 116 | + mysql -h "$MYSQL_HOST" -P "$MYSQL_PORT" -u "$MYSQL_USER" "$MYSQL_DATABASE" < "$MATOMO_DIR/tests/resources/OmniFixture-dump.sql" |
| 117 | + |
| 118 | + write_config |
| 119 | + start_php_server |
| 120 | + |
| 121 | + # Run updates as the first Matomo console interaction after startup. |
| 122 | + (cd "$MATOMO_DIR" && php ./console core:update --yes) |
| 123 | + |
| 124 | + (cd "$MATOMO_DIR" && php ./console plugin:activate McpServer) |
| 125 | + if ! (cd "$MATOMO_DIR" && php ./console plugin:list | grep -qE '\|[[:space:]]*McpServer[[:space:]]*\|.*\|[[:space:]]*Activated[[:space:]]*\|'); then |
| 126 | + echo "McpServer plugin is not activated after setup." >&2 |
| 127 | + exit 1 |
| 128 | + fi |
| 129 | + |
| 130 | + local fixture_file="$MATOMO_DIR/tests/PHPUnit/Framework/Fixture.php" |
| 131 | + local omnifixture_file="$MATOMO_DIR/tests/PHPUnit/Fixtures/OmniFixture.php" |
| 132 | + local fixture_admin_login |
| 133 | + local fixture_admin_password |
| 134 | + local omnifixture_superuser_token |
| 135 | + fixture_admin_login=$(extract_php_constant "$fixture_file" "ADMIN_USER_LOGIN") |
| 136 | + fixture_admin_password=$(extract_php_constant "$fixture_file" "ADMIN_USER_PASSWORD") |
| 137 | + omnifixture_superuser_token=$(extract_php_constant "$omnifixture_file" "OMNIFIXTURE_SUPERUSER_TOKEN") |
| 138 | + |
| 139 | + require_non_empty "ADMIN_USER_LOGIN" "$fixture_admin_login" |
| 140 | + require_non_empty "ADMIN_USER_PASSWORD" "$fixture_admin_password" |
| 141 | + require_non_empty "OMNIFIXTURE_SUPERUSER_TOKEN" "$omnifixture_superuser_token" |
| 142 | + |
| 143 | + local token_response |
| 144 | + token_response=$(curl -sS -X POST "$BASE_URL/index.php" \ |
| 145 | + --data-urlencode "module=API" \ |
| 146 | + --data-urlencode "method=UsersManager.createAppSpecificTokenAuth" \ |
| 147 | + --data-urlencode "format=JSON" \ |
| 148 | + --data-urlencode "token_auth=${omnifixture_superuser_token}" \ |
| 149 | + --data-urlencode "userLogin=${fixture_admin_login}" \ |
| 150 | + --data-urlencode "passwordConfirmation=${fixture_admin_password}" \ |
| 151 | + --data-urlencode "description=mcp smoke ci token") |
| 152 | + |
| 153 | + local token |
| 154 | + if ! echo "$token_response" | jq -e 'type == "string" or (type == "object" and ((.value // .token_auth // .token // null) | type == "string"))' >/dev/null 2>&1; then |
| 155 | + echo "Unexpected token response schema from UsersManager.createAppSpecificTokenAuth: $token_response" >&2 |
| 156 | + exit 1 |
| 157 | + fi |
| 158 | + token=$(echo "$token_response" | jq -r 'if type == "string" then . else (.value // .token_auth // .token // empty) end') |
| 159 | + if [ -z "$token" ] || [ "$token" = "null" ] || [ "$token" = "false" ]; then |
| 160 | + echo "Failed to create app token via API. Response: $token_response" >&2 |
| 161 | + exit 1 |
| 162 | + fi |
| 163 | + |
| 164 | + local sites_json metadata_json |
| 165 | + local id_site report_unique_id |
| 166 | + local -a skip_cases |
| 167 | + |
| 168 | + sites_json=$(api_get_json "$BASE_URL/index.php?module=API&method=SitesManager.getSitesWithAtLeastViewAccess&format=JSON&token_auth=${token}") |
| 169 | + id_site=$(echo "$sites_json" | jq -r '.[0].idsite // empty') |
| 170 | + require_non_empty "idSite" "$id_site" |
| 171 | + |
| 172 | + metadata_json=$(api_get_json "$BASE_URL/index.php?module=API&method=API.getReportMetadata&idSite=${id_site}&format=JSON&token_auth=${token}") |
| 173 | + report_unique_id=$(echo "$metadata_json" | jq -r '.[] | select(.module=="Actions" and .action=="getPageUrls") | .uniqueId' | head -n1) |
| 174 | + if [ -z "$report_unique_id" ]; then |
| 175 | + report_unique_id=$(echo "$metadata_json" | jq -r '.[] | .uniqueId // empty' | head -n1) |
| 176 | + fi |
| 177 | + if [ -z "$report_unique_id" ]; then |
| 178 | + skip_cases+=("report_processed") |
| 179 | + fi |
| 180 | + |
| 181 | + jq -n \ |
| 182 | + --arg base_url "$BASE_URL" \ |
| 183 | + --arg token_auth "$token" \ |
| 184 | + --arg idSite "$id_site" \ |
| 185 | + --arg reportUniqueId "$report_unique_id" \ |
| 186 | + --argjson skip_cases "$(printf '%s\n' "${skip_cases[@]:-}" | sed '/^$/d' | jq -R . | jq -s 'unique')" \ |
| 187 | + '{base_url: $base_url, token_auth: $token_auth, idSite: $idSite, reportUniqueId: $reportUniqueId, skip_cases: $skip_cases}' > "$STATE_FILE" |
| 188 | + |
| 189 | + echo "State written to $STATE_FILE" |
| 190 | +} |
| 191 | + |
| 192 | +main "$@" |
0 commit comments