#!/bin/sh
# Drive the phpMyAdmin web interface against a private database and web server.
#
# Both the database (debian/tests/lib/mariadb.sh) and lighttpd
# (debian/tests/lib/webserver.sh) are private to the test: the testbed's own
# services are not touched and nothing outside $AUTOPKGTEST_TMP is modified.
#
# The interface itself is exercised by debian/tests/test_web_interface.py, ported
# from the upstream container tests. The checks that were container-specific
# (secrets passed as files, SSL, client certificates, the container's php.ini
# values) are deliberately not carried over; what remains describes behaviour a
# Debian installation must have.
set -eu

# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/helpers.sh"
# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/mariadb.sh"
# SC1091: the library path is computed at run time, so it cannot be
# followed statically. Its contents are checked on their own.
# shellcheck disable=SC1091
. "$(dirname "$0")/lib/webserver.sh"

DBUSER=root
DBPASS=""

dep8_require mariadbd mariadb-install-db mariadb lighttpd php-cgi curl python3

if ! python3 -c 'import mechanize'; then
    dep8_fail "python3-mechanize is not installed"
fi

cleanup() {
    dep8_web_stop
    mariadb_stop
    return 0
}
trap cleanup EXIT

# --- Serve a throwaway copy pointing at the private server -------------------
# libraries/vendor_config.php is patched to read CONFIG_FILE from
# /etc/phpmyadmin/config.inc.php and to keep TempDir under /var/lib/phpmyadmin,
# so the installed tree cannot be given another configuration without editing
# /etc. Copy the tree instead and rewrite those two values inside the copy, which
# keeps the test unprivileged and leaves the system files alone.
mkdir -p "$DEP8_WEB_DIR"
DOCROOT="$DEP8_WEB_DIR/docroot"
if [ -e "$DOCROOT" ]; then
    rm -r "$DOCROOT"
fi
echo "Copying $DEP8_WEB_DOCROOT to $DOCROOT ..."
dep8_run "$DEP8_WEB_LOG" cp -a "$DEP8_WEB_DOCROOT" "$DOCROOT"
mkdir -p "$DEP8_WEB_DIR/tmp"

VENDOR="$DOCROOT/libraries/vendor_config.php"
if [ ! -f "$VENDOR" ]; then
    dep8_fail "$VENDOR is missing from the package"
fi
sed -i \
    -e "s|'/etc/phpmyadmin/config.inc.php'|'$DOCROOT/config.inc.php'|" \
    -e "s|'/var/lib/phpmyadmin/tmp/'|'$DEP8_WEB_DIR/tmp/'|" \
    "$VENDOR"
if ! grep -q "'$DOCROOT/config.inc.php'" "$VENDOR"; then
    dep8_fail "could not point CONFIG_FILE at the copy; the package layout changed"
fi

cat > "$DOCROOT/config.inc.php" <<PHP
<?php
// Written by the phpMyAdmin autopkgtest, inside a throwaway copy of the package.
\$i = 1;
\$cfg['Servers'][\$i]['host'] = '127.0.0.1';
\$cfg['Servers'][\$i]['port'] = '$DEP8_MARIADB_PORT';
\$cfg['Servers'][\$i]['socket'] = '$DEP8_MARIADB_SOCK';
\$cfg['Servers'][\$i]['auth_type'] = 'cookie';
\$cfg['Servers'][\$i]['AllowNoPassword'] = true;
\$cfg['blowfish_secret'] = 'dep8-test-blowfish-secret-0123456789abcdef';
// The PHP information page is what the ported test looks for and it is off by
// default (Config/Settings.php setShowPhpInfo).
\$cfg['ShowPhpInfo'] = true;
PHP

mariadb_start_private
DEP8_WEB_DOCROOT="$DOCROOT"
dep8_web_start

# --- Import fixture: a tiny schema, not upstream's world.sql -----------------
# The table named in it is checked on the server afterwards, so a page that merely
# echoes the statement cannot make this pass.
SQL_FILE="$AUTOPKGTEST_TMP/dep8_import.sql"
SQL_MARK="dep8_import_ran"
cat > "$SQL_FILE" <<SQL
CREATE DATABASE IF NOT EXISTS dep8_import;
USE dep8_import;
CREATE TABLE IF NOT EXISTS dep8_marker (id INT PRIMARY KEY, label VARCHAR(32) NOT NULL);
INSERT INTO dep8_marker VALUES (1, '$SQL_MARK');
SQL

echo "Running the web interface tests ..."
if ! PMA_URL="$(dep8_web_url)" \
     PMA_USERNAME="$DBUSER" \
     PMA_PASSWORD="$DBPASS" \
     PMA_SQL_FILE="$SQL_FILE" \
     PMA_SQL_MARK="$SQL_MARK" \
     python3 "$(dirname "$0")/test_web_interface.py"; then
    echo "--- $DEP8_WEB_LOG ---"
    tail -40 "$DEP8_WEB_LOG"
    dep8_fail "the web interface tests failed"
fi

# The import is only proven if its rows are in the database.
echo "Checking that the import reached the server ..."
_rows=$(mariadb_sql -N -B -e "SELECT COUNT(*) FROM dep8_import.dep8_marker WHERE label = '$SQL_MARK'")
if [ "$_rows" != "1" ]; then
    dep8_fail "the imported row is not in the database (found '$_rows')"
fi

dep8_pass "the web interface serves, logs in, reports PHP information and imports SQL"
