Skip to content

Commit

Permalink
! check login
Browse files Browse the repository at this point in the history
  • Loading branch information
Spuds committed Jan 25, 2024
1 parent 98ceba3 commit 23b9c80
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 5 deletions.
3 changes: 2 additions & 1 deletion .github/setup-elkarte.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,9 @@ fi
composer install --no-interaction --quiet

# phpunit-selenium is compatible with phpunit 9.3.x, past that it runs all methods not just test methods
# This allows tests to run, but code coverage fails, see ElkArteWebSupport.php where its turned off for now
if [[ "$WEBSERVER" != "none" ]]
then
composer remove phpunit/phpunit phpunit/phpunit-selenium --dev
composer require phpunit/phpunit:8.5.36 phpunit/phpunit-selenium:8.0.0 --dev --update-with-all-dependencies --ignore-platform-reqs
composer require phpunit/phpunit:9.3.11 phpunit/phpunit-selenium:9.0.1 --dev --update-with-all-dependencies --ignore-platform-reqs
fi
1 change: 1 addition & 0 deletions .github/setup-selenium.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ else

# Copy phpunit_coverage.php into the webserver's document root directory.
cp ./vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon/phpunit_coverage.php .
sudo cp ./tests/RemoteCoverage.php ./vendor/phpunit/phpunit-selenium/PHPUnit/Extensions/SeleniumCommon

# Run the phpunit selenium tests
vendor/bin/phpunit --verbose --debug --configuration .github/phpunit-webtest.xml
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
strategy:
matrix:
include:
- db: "mysql:5.7"
- db: 'mysql:5.7'
php: '8.1'

name: WebTest (PHP ${{ matrix.php }} - DB ${{ matrix.db_alias != '' && matrix.db_alias || matrix.db }})
Expand Down
82 changes: 82 additions & 0 deletions tests/RemoteCoverage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<?php

namespace PHPUnit\Extensions\SeleniumCommon;

use Exception;
use SebastianBergmann\CodeCoverage\RawCodeCoverageData;

class RemoteCoverage
{
public function __construct($coverageScriptUrl, $testId)
{
$this->coverageScriptUrl = $coverageScriptUrl;
$this->testId = $testId;
}

public function get()
{
if (!empty($this->coverageScriptUrl)) {
$url = sprintf(
'%s?PHPUNIT_SELENIUM_TEST_ID=%s',
$this->coverageScriptUrl,
urlencode($this->testId)
);

$buffer = @file_get_contents($url);

if ($buffer !== FALSE) {
$coverageData = unserialize($buffer);
if (is_array($coverageData)) {
//throw new Exception('Here is the data from url "' . $url . '" (' . var_export($buffer, true) . ')');

$coverageWithLocalPaths = $this->matchLocalAndRemotePaths($coverageData);
return RawCodeCoverageData::fromXdebugWithoutPathCoverage($coverageWithLocalPaths);
} else {
throw new Exception('Empty or invalid code coverage data received from url "' . $url . '" (' . var_export($buffer, true) . ')');
}
}
}

return array();
}

/**
* @param array $coverage
* @return array
* @author Mattis Stordalen Flister <mattis@xait.no>
*/
protected function matchLocalAndRemotePaths(array $coverage)
{
$coverageWithLocalPaths = array();

foreach ($coverage as $originalRemotePath => $data) {
$remotePath = $originalRemotePath;
$separator = $this->findDirectorySeparator($remotePath);

while (!($localpath = stream_resolve_include_path($remotePath)) &&
strpos($remotePath, $separator) !== FALSE) {
$remotePath = substr($remotePath, strpos($remotePath, $separator) + 1);
}

if ($localpath && md5_file($localpath) == $data['md5']) {
$coverageWithLocalPaths[$localpath] = $data['coverage'];
}
}

return $coverageWithLocalPaths;
}

/**
* @param string $path
* @return string
* @author Mattis Stordalen Flister <mattis@xait.no>
*/
protected function findDirectorySeparator($path)
{
if (strpos($path, '/') !== FALSE) {
return '/';
}

return '\\';
}
}
19 changes: 16 additions & 3 deletions tests/sources/controllers/ElkArteWebSupport.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
abstract class ElkArteWebSupport extends Selenium2TestCase
{
protected $coverageScriptUrl = 'http://127.0.0.1/phpunit_coverage.php';
//protected $coverageScriptUrl = '';
protected $backupGlobalsExcludeList = ['user_info'];
protected $width = 2560;
protected $height = 1440;
Expand Down Expand Up @@ -151,11 +150,25 @@ public function adminLogin()
{
// Main page
$this->url('index.php');
$this->waitUntil(function ($testCase)
{
try
{
return $testCase->byId('menu_nav');
}
catch (PHPUnit\Extensions\Selenium2TestCase\WebDriverException $e)
{
return false;
}
}, 10000);

// Can we log in?
$check = $this->byId('menu_nav')->text();
if (strpos($check, 'Log in') !== false)
{
// Don't trip the spamProtection
$this->timeouts()->implicitWait(3000);

// Select login from the main page
$this->url('index.php?action=login');
$this->assertEquals('Log in', $this->title(), 'Unable to find the login forum');
Expand All @@ -173,11 +186,11 @@ public function adminLogin()
}
else
{
// Already logged in
return;
}

// Hang about until the page refreshes
$this->url('index.php');
$this->waitUntil(function ($testCase)
{
try
Expand All @@ -188,7 +201,7 @@ public function adminLogin()
{
return false;
}
}, 5000);
}, 10000);

// Should see the admin main menu button
$this->assertStringContainsString('Admin', $this->byId('menu_nav')->text(), $this->source());
Expand Down

0 comments on commit 23b9c80

Please sign in to comment.