Files

201 lines
9.2 KiB
PowerShell

param(
[Parameter(Mandatory)][string]$Env
)
. "$PSScriptRoot\common.ps1"
$environment = Get-TestLabEnvironment -Name $Env
Write-ComposeFile -Environment $environment
$result = [ordered]@{
environment = $environment.name
wordpressImage = $environment.wordpressImage
dbImage = $environment.dbImage
hostPort = $environment.hostPort
startedAt = (Get-Date).ToString('o')
checks = @()
}
function Add-Check {
param([string]$Name, [bool]$Passed, [string]$Detail = '')
$script:result.checks += [ordered]@{ name = $Name; passed = $Passed; detail = $Detail }
if ($Passed) {
Write-Host "[PASS] $Name $Detail"
} else {
Write-Host "[FAIL] $Name $Detail"
throw "Check failed: $Name $Detail"
}
}
try {
Invoke-Compose $environment @('up', '-d') | Out-Host
$wpCli = 'if ! command -v wp >/dev/null 2>&1; then curl -fsSL https://raw.githubusercontent.com/wp-cli/builds/gh-pages/phar/wp-cli.phar -o /usr/local/bin/wp && chmod +x /usr/local/bin/wp; fi'
Invoke-WpCliRoot $environment $wpCli | Out-Host
Add-Check 'wp-cli available' $true
if ($environment.PSObject.Properties.Name -contains 'wordpressVersion' -and $environment.wordpressVersion) {
Invoke-WpCli $environment "wp core download --version=$($environment.wordpressVersion) --force --path=/var/www/html" | Out-Host
Add-Check 'wordpress core version prepared' $true "target=$($environment.wordpressVersion)"
}
$installScript = @'
#!/usr/bin/env bash
set -e
for i in $(seq 1 40); do
if wp core is-installed --path=/var/www/html >/dev/null 2>&1; then
exit 0
fi
if wp core install --path=/var/www/html --url=http://localhost:__HOST_PORT__ --title=PluginTest --admin_user=admin --admin_password=adminpass --admin_email=admin@example.test --skip-email; then
exit 0
fi
sleep 3
done
exit 1
'@ -replace '__HOST_PORT__', [string]$environment.hostPort
Write-ContainerTextFile $environment '/tmp/brt-install.sh' $installScript '0755'
Invoke-WpCli $environment 'bash /tmp/brt-install.sh' | Out-Host
$installed = (Invoke-WpCli $environment 'wp core is-installed --path=/var/www/html && echo installed' | Out-String).Trim()
Add-Check 'wordpress installed' ($installed -eq 'installed') $installed
$phpVersion = (Invoke-WpCliRoot $environment "php -r 'echo PHP_VERSION;'" | Out-String).Trim()
$wpVersion = (Invoke-WpCli $environment 'wp core version --path=/var/www/html' | Out-String).Trim()
Add-Check 'runtime versions' $true "WP=$wpVersion PHP=$phpVersion"
$result.phpVersion = $phpVersion
$result.wordpressVersion = $wpVersion
Invoke-WpCli $environment "wp plugin activate $PluginSlug --path=/var/www/html" | Out-Host
Add-Check 'plugin activated' $true
$pluginVersion = (Invoke-WpCli $environment "wp plugin get $PluginSlug --field=version --path=/var/www/html" | Out-String).Trim()
Add-Check 'plugin version detected' ($pluginVersion -eq $ExpectedPluginVersion) "version=$pluginVersion"
$result.pluginVersion = $pluginVersion
$tablePhp = @'
<?php
global $wpdb;
echo $wpdb->get_var($wpdb->prepare('SHOW TABLES LIKE %s', $wpdb->prefix . 'brt_verses'));
'@
Write-ContainerPhpFile $environment '/tmp/brt-table.php' $tablePhp
$tableExists = (Invoke-WpCli $environment 'wp eval-file /tmp/brt-table.php --path=/var/www/html' | Out-String).Trim()
Add-Check 'verse table created' ($tableExists -eq 'wp_brt_verses') $tableExists
$interpretationPhp = @'
<?php
$plugin = new Bible_Reference_Tooltips();
$ref = new ReflectionClass($plugin);
$call = function($method, ...$args) use ($plugin, $ref) {
$m = $ref->getMethod($method);
$m->setAccessible(true);
return $m->invokeArgs($plugin, $args);
};
$checks = [];
foreach ([
['parse_reference', 'James 1:5', 'JAS'],
['parse_reference', 'Jas 1:5', 'JAS'],
['parse_reference', 'Jm 1:5', 'JAS'],
['parse_reference', 'Mark10:45', 'MRK'],
['parse_reference', '2Jn1:6', '2JN'],
['parse_reference', 'Eze 36:26', 'EZK'],
] as $case) {
$parsed = $call($case[0], $case[1]);
if (!$parsed || $parsed['book'] !== $case[2]) $checks[] = $case[1] . '=>' . json_encode($parsed);
}
foreach ([
['EZE 36:26 source', 'EZE', 'EZK'],
['MAR 10:45 source', 'MAR', 'MRK'],
['JOH 3:16 source', 'JOH', 'JHN'],
['JAM 1:5 source', 'JAM', 'JAS'],
['1JO 1:9 source', '1JO', '1JN'],
] as $case) {
$book = $call('normalize_source_book', $case[1], 'ebible_vpl');
if (!$book || $book['abbr'] !== $case[2]) $checks[] = $case[0] . '=>' . json_encode($book);
}
$aliases = $call('frontend_book_alias_patterns');
$alias_keys = array_map('strtolower', $aliases);
foreach (['James', 'Jm', 'Eze', 'Mar', '2\\s*Jn'] as $needle) {
if (!in_array(strtolower($needle), $alias_keys, true)) $checks[] = 'missing frontend alias ' . $needle;
}
echo $checks ? json_encode($checks) : 'ok';
'@
Write-ContainerPhpFile $environment '/tmp/brt-interpretation.php' $interpretationPhp
$interpretation = (Invoke-WpCli $environment 'wp eval-file /tmp/brt-interpretation.php --path=/var/www/html' | Out-String).Trim()
Add-Check 'interpretation registry' ($interpretation -eq 'ok') $interpretation
$translationsToImport = @('WEB', 'KJV', 'ASV', 'YLT')
foreach ($translationCode in $translationsToImport) {
$importPhp = @"
<?php
`$plugin = new Bible_Reference_Tooltips();
`$ref = new ReflectionClass(`$plugin);
`$m = `$ref->getMethod('import_vpl_translation');
`$m->setAccessible(true);
`$count = `$m->invoke(`$plugin, '$translationCode', false);
echo `$count;
"@
Write-ContainerPhpFile $environment '/tmp/brt-import.php' $importPhp
$imported = (Invoke-WpCli $environment 'wp eval-file /tmp/brt-import.php --path=/var/www/html' | Out-String).Trim()
$importNumberText = [regex]::Match($imported, '[\d,]+').Value -replace ',', ''
$importNumber = if ($importNumberText) { [int]$importNumberText } else { 0 }
Add-Check "$translationCode import completed" ($importNumber -gt 30000) $imported
}
Invoke-WpCli $environment "wp option patch update brt_settings translation WEB --path=/var/www/html" | Out-Host
$countPhp = @'
<?php
global $wpdb;
$rows = $wpdb->get_results('SELECT translation, COUNT(*) row_count FROM ' . $wpdb->prefix . 'brt_verses GROUP BY translation ORDER BY translation', ARRAY_A);
echo json_encode($rows);
'@
Write-ContainerPhpFile $environment '/tmp/brt-count.php' $countPhp
$rowCount = (Invoke-WpCli $environment 'wp eval-file /tmp/brt-count.php --path=/var/www/html' | Out-String).Trim()
Add-Check 'translation rows stored' ($rowCount -match 'WEB' -and $rowCount -match 'KJV' -and $rowCount -match 'ASV' -and $rowCount -match 'YLT') $rowCount
$bookCoveragePhp = @'
<?php
global $wpdb;
$expected = ['GEN','EXO','LEV','NUM','DEU','JOS','JDG','RUT','1SA','2SA','1KI','2KI','1CH','2CH','EZR','NEH','EST','JOB','PSA','PRO','ECC','SNG','ISA','JER','LAM','EZK','DAN','HOS','JOL','AMO','OBA','JON','MIC','NAM','HAB','ZEP','HAG','ZEC','MAL','MAT','MRK','LUK','JHN','ACT','ROM','1CO','2CO','GAL','EPH','PHP','COL','1TH','2TH','1TI','2TI','TIT','PHM','HEB','JAS','1PE','2PE','1JN','2JN','3JN','JUD','REV'];
$translations = ['WEB','KJV','ASV','YLT'];
$missing = [];
foreach ($translations as $translation) {
$found = $wpdb->get_col($wpdb->prepare('SELECT DISTINCT book FROM ' . $wpdb->prefix . 'brt_verses WHERE translation = %s', $translation));
$diff = array_values(array_diff($expected, $found));
if ($diff) $missing[$translation] = $diff;
}
echo $missing ? json_encode($missing) : 'ok';
'@
Write-ContainerPhpFile $environment '/tmp/brt-book-coverage.php' $bookCoveragePhp
$bookCoverage = (Invoke-WpCli $environment 'wp eval-file /tmp/brt-book-coverage.php --path=/var/www/html' | Out-String).Trim()
Add-Check 'canonical book coverage' ($bookCoverage -eq 'ok') $bookCoverage
$refs = @('John 3:16', 'John 3.16', 'Jn3:16', 'Romans 8:28', 'James 1:5', 'Jas 1:5', 'Jm 1:5', 'Mark 10:45', 'Mark10:45', 'Mk 10:45', 'Mar 10:45', 'Ezekiel 36:26', 'Eze 36:26', '2 John 1:6', '2Jn1:6', '2 Jo 1:6')
foreach ($ref in $refs) {
$lookupPhp = @"
<?php
`$request = new WP_REST_Request('GET', '/bible-reference-tooltips/v1/lookup');
`$request->set_param('ref', '$ref');
`$request->set_param('translation', 'WEB');
`$response = rest_do_request(`$request);
`$data = `$response->get_data();
echo (!empty(`$data['ok']) && !empty(`$data['verses'])) ? 'ok' : json_encode(`$data);
"@
Write-ContainerPhpFile $environment '/tmp/brt-lookup.php' $lookupPhp
$lookupResult = (Invoke-WpCli $environment 'wp eval-file /tmp/brt-lookup.php --path=/var/www/html' | Out-String).Trim()
Add-Check "REST lookup $ref" ($lookupResult -eq 'ok') $lookupResult
}
Invoke-WpCli $environment "wp post create --post_type=post --post_status=publish --post_title='Bible Reference Tooltip Smoke Test' --post_content='John 3:16 Romans 8:28 James 1:5' --path=/var/www/html" | Out-Host
Add-Check 'test post created' $true
$result.completedAt = (Get-Date).ToString('o')
$result.passed = $true
} catch {
$result.completedAt = (Get-Date).ToString('o')
$result.passed = $false
$result.error = $_.Exception.Message
throw
} finally {
Ensure-TestLabDirectories
$result | ConvertTo-Json -Depth 8 | Set-Content -LiteralPath (Get-ResultFile -Name $environment.name) -Encoding UTF8
}