91 lines
2.9 KiB
PHP
91 lines
2.9 KiB
PHP
<?php
|
|
namespace Xxx\Tests\Support;
|
|
|
|
/*
|
|
* The base of the unit tests. Before each test it moves to publish/ (as
|
|
* the publishing pages run there), empties the request,
|
|
* the session and the globals that the site code uses, gives the test a
|
|
* fake database with empty tables and a temporary directory (also the
|
|
* upload directory), and clears the error log.
|
|
*/
|
|
abstract class TestCase extends \PHPUnit\Framework\TestCase {
|
|
protected string $tmp;
|
|
private array $envNames = array();
|
|
private string $cwd;
|
|
|
|
protected function setUp(): void {
|
|
// publish/post.lib.php includes files by paths relative to publish/
|
|
$this->cwd = getcwd();
|
|
chdir(XXX_ROOT . '/publish');
|
|
FakeDb::reset();
|
|
SiteData::$passwordLength = NULL;
|
|
SiteData::install();
|
|
$GLOBALS['db'] = NULL;
|
|
$GLOBALS['db_class'] = FakeDb::class;
|
|
|
|
$_GET = $_POST = $_REQUEST = $_FILES = $_COOKIE = $_SESSION = array();
|
|
$_SERVER['REMOTE_ADDR'] = '192.0.2.1';
|
|
$_SERVER['REQUEST_URI'] = '/';
|
|
unset($_SERVER['HTTPS']);
|
|
foreach (array('is_admin' => FALSE, 'action' => NULL, 'queue' => NULL, 'id' => NULL,
|
|
'num_stories' => NULL, 'editable' => NULL, 'username' => NULL,
|
|
'validate' => array()) as $name => $value)
|
|
$GLOBALS[$name] = $value;
|
|
unset($GLOBALS['file_src']);
|
|
|
|
$this->tmp = sys_get_temp_dir() . '/xxx-test-' . bin2hex(random_bytes(6));
|
|
mkdir($this->tmp, 0700);
|
|
$this->setEnv('UPLOAD_DIR', $this->tmp . '/uploads');
|
|
file_put_contents(XXX_TEST_LOG, '');
|
|
}
|
|
|
|
protected function tearDown(): void {
|
|
FakeRecaptcha::uninstall();
|
|
foreach ($this->envNames as $name)
|
|
putenv($name);
|
|
$this->envNames = array();
|
|
self::removeTree($this->tmp);
|
|
chdir($this->cwd);
|
|
}
|
|
|
|
// Set an environment variable (which config() reads first) for this test
|
|
protected function setEnv(string $name, string $value): void {
|
|
$this->envNames[] = $name;
|
|
putenv("$name=$value");
|
|
}
|
|
|
|
protected function request(array $get = array(), array $post = array()): void {
|
|
$_GET = $get;
|
|
$_POST = $post;
|
|
$_REQUEST = array_merge($get, $post);
|
|
}
|
|
|
|
protected function asEditor(string $name = 'ed'): void {
|
|
$GLOBALS['is_admin'] = TRUE;
|
|
$GLOBALS['username'] = $name;
|
|
$_SESSION['editor'] = $name;
|
|
}
|
|
|
|
// What the code wrote to the PHP error log during this test
|
|
protected function loggedErrors(): string {
|
|
return (string)file_get_contents(XXX_TEST_LOG);
|
|
}
|
|
|
|
// A 1x1 PNG image
|
|
public static function png(): string {
|
|
return base64_decode('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mNk+M9QDwADhgGAWjR9awAAAABJRU5ErkJggg==');
|
|
}
|
|
|
|
public static function removeTree(string $path): void {
|
|
if (is_dir($path) && !is_link($path)) {
|
|
foreach (scandir($path) as $name) {
|
|
if ($name !== '.' && $name !== '..')
|
|
self::removeTree("$path/$name");
|
|
}
|
|
rmdir($path);
|
|
} else if (file_exists($path) || is_link($path)) {
|
|
unlink($path);
|
|
}
|
|
}
|
|
}
|