<commit_before><?php


use PHPUnit\Framework\TestCase;
use BFITech\ZapCore\Logger;
use BFITech\ZapStore\SQLite3;
use BFITech\ZapAdmin as za;
use BFITech\ZapAdmin\AdminStoreError as Err;


if (!defined('HTDOCS'))
	define('HTDOCS', __DIR__ . '/htdocs-test');


class AdminStore extends za\AdminStore {}

class AdminStoreTest extends TestCase {

	protected static $sql;
	protected static $adm;

	protected static $pwdless_uid;

	public static function postFormatter($args) {
		return ['post' => $args];
	}

	public static function loginOK($uname='root', $upass='<PASSWORD>') {
		$result = self::$adm->adm_login(self::postFormatter([
			'uname' => $uname,
			'upass' => $upass,
		]));
		self::$adm->adm_set_user_token($result[1]['token']);
		self::$adm->adm_status();
	}

	public static function setUpBeforeClass() {
		$logfile = HTDOCS . '/zapmin-test-sqlite3.log';
		if (file_exists($logfile))
			unlink($logfile);
		$logger = new Logger(Logger::DEBUG, $logfile);
		self::$sql = new SQLite3([
			'dbname' => ':memory:'
		], $logger);
		self::$adm = (new AdminStore(self::$sql))
			->config('expiration', 600);
	}

	public static function tearDownAfterClass() {
		#self::$sql = null;
	}

	public function tearDown() {
		if (self::$adm->adm_get_safe_user_data())
			self::$adm->adm_logout();
	}

	public function test_constructor() {
		# run test on sqlite3 only
		if (self::$sql->get_connection_params()['dbtype'] != 'sqlite3')
			return;

		$logfile = HTDOCS . '/zapmin-test-constructor.log';
		if (file_exists($logfile))
			unlink($logfile);
		$logger = new Logger(Logger::ERROR, $logfile);
		$dbfile = HTDOCS . '/zapmin-test-constructor.sq3';
		$sql = new SQLite3([
			'dbname' => $dbfile,
		], $logger);

		# default maximum expiration with forced table overwrite
		$adm = (new AdminStore($sql, null, null, $logger))
			->config('force_create_table', true);
		$this->assertEquals($adm->adm_get_expiration(), 3600 * 2);

		# table check statement should not be logged
		$this->assertFalse(strpos(
			file_get_contents($logfile), "SELECT 1 FROM udata"));

		# minimum expiration
		$adm = new AdminStore($sql);
		$adm->config('expiration', 120);
		$this->assertEquals($adm->adm_get_expiration(), 600);

		# deinit, expiration goes back to default
		$adm->deinit();
		$this->assertEquals($adm->adm_get_expiration(), 3600 * 2);

		# working on invalid connection
		$adm = new AdminStore($sql, null, null, $logger);
		$sql->close();
		try {
			$adm = new AdminStore($sql, null, null, $logger);
		} catch(za\AdminStoreError $e) {}

		unlink($dbfile);
	}

	public function test_upgrade_tables(){
		# run test on sqlite3 only
		if (self::$sql->get_connection_params()['dbtype'] != 'sqlite3')
			return;

		$logfile = HTDOCS . '/zapmin-test-table-update.log';
		if (file_exists($logfile))<commit_msg>Test repeated calls of AdminStore::deinit.<commit_after><?php


use PHPUnit\Framework\TestCase;
use BFITech\ZapCore\Logger;
use BFITech\ZapStore\SQLite3;
use BFITech\ZapAdmin as za;
use BFITech\ZapAdmin\AdminStoreError as Err;


if (!defined('HTDOCS'))
	define('HTDOCS', __DIR__ . '/htdocs-test');


class AdminStore extends za\AdminStore {}

class AdminStoreTest extends TestCase {

	protected static $sql;
	protected static $adm;

	protected static $pwdless_uid;

	public static function postFormatter($args) {
		return ['post' => $args];
	}

	public static function loginOK($uname='root', $upass='<PASSWORD>') {
		$result = self::$adm->adm_login(self::postFormatter([
			'uname' => $uname,
			'upass' => $upass,
		]));
		self::$adm->adm_set_user_token($result[1]['token']);
		self::$adm->adm_status();
	}

	public static function setUpBeforeClass() {
		$logfile = HTDOCS . '/zapmin-test-sqlite3.log';
		if (file_exists($logfile))
			unlink($logfile);
		$logger = new Logger(Logger::DEBUG, $logfile);
		self::$sql = new SQLite3([
			'dbname' => ':memory:'
		], $logger);
		self::$adm = (new AdminStore(self::$sql))
			->config('expiration', 600);
	}

	public static function tearDownAfterClass() {
		#self::$sql = null;
	}

	public function tearDown() {
		if (self::$adm->adm_get_safe_user_data())
			self::$adm->adm_logout();
	}

	public function test_constructor() {
		# run test on sqlite3 only
		if (self::$sql->get_connection_params()['dbtype'] != 'sqlite3')
			return;

		$logfile = HTDOCS . '/zapmin-test-constructor.log';
		if (file_exists($logfile))
			unlink($logfile);
		$logger = new Logger(Logger::ERROR, $logfile);
		$dbfile = HTDOCS . '/zapmin-test-constructor.sq3';
		$sql = new SQLite3([
			'dbname' => $dbfile,
		], $logger);

		# default maximum expiration with forced table overwrite
		$adm = (new AdminStore($sql, null, null, $logger))
			->config('force_create_table', true);
		$this->assertEquals($adm->adm_get_expiration(), 3600 * 2);

		# table check statement should not be logged
		$this->assertFalse(strpos(
			file_get_contents($logfile), "SELECT 1 FROM udata"));

		# minimum expiration
		$adm = new AdminStore($sql);
		$adm->config('expiration', 120);
		$this->assertEquals($adm->adm_get_expiration(), 600);

		# deinit, expiration goes back to default
		$adm->deinit();
		# calling deinit repeatedly has no effect
		$adm->deinit();
		$this->assertEquals($adm->adm_get_expiration(), 3600 * 2);

		# working on invalid connection
		$adm = new AdminStore($sql, null, null, $logger);
		$sql->close();
		try {
			$adm = new AdminStore($sql, null, null, $logger);
		} catch(za\AdminStoreError $e) {}

		unlink($dbfile);
	}

	public function test_upgrade_tables(){
		# run test on sqlite3 only
		if (self::$sql->get_connection_params()['dbtype'] != 'sqlite3')
			return;

		$logfile = HTDOCS . '/zapmin-test-table-update.log';
		if (file_exists($logfile))
{'fraction_non_alphanumeric': 0.16992772783359775, 'fraction_numerical': 0.010576414595452142, 'mean_word_length': 4.7025125628140705, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 54, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3362058', 'n_tokens_mistral': 2228, 'n_tokens_neox': 1962, 'n_words': 436}