Add audit tests

This commit is contained in:
Dennis Eichhorn 2018-12-02 16:54:46 +01:00
parent 97e605492e
commit 1498ab0102
5 changed files with 64 additions and 26 deletions

View File

@ -42,13 +42,13 @@ class Installer extends InstallerAbstract
$dbPool->get()->con->prepare(
'CREATE TABLE if NOT EXISTS `' . $dbPool->get()->prefix . 'auditor_audit` (
`auditor_audit_id` int(11) NOT NULL AUTO_INCREMENT,
`auditor_audit_module` int(11) NOT NULL,
`auditor_audit_ref` int(11) NOT NULL,
`auditor_audit_module` varchar(255) DEFAULT NULL,
`auditor_audit_ref` int(11) DEFAULT NULL,
`auditor_audit_type` smallint(3) NOT NULL,
`auditor_audit_subtype` smallint(3) NOT NULL,
`auditor_audit_content` text NOT NULL,
`auditor_audit_old` text NOT NULL,
`auditor_audit_new` text NOT NULL,
`auditor_audit_content` text DEFAULT NULL,
`auditor_audit_old` text DEFAULT NULL,
`auditor_audit_new` text DEFAULT NULL,
`auditor_audit_created_at` datetime NOT NULL,
`auditor_audit_created_by` int(11) NOT NULL,
`auditor_audit_ip` int(11) NOT NULL,

View File

@ -30,7 +30,7 @@ use Modules\Auditor\Models\AuditMapper;
final class ApiController extends Controller
{
public function apiLogCreate(
Account $account,
$account,
$old,
$new,
int $type = 0,
@ -40,12 +40,14 @@ final class ApiController extends Controller
string $content = null
) : void
{
$audit = new Audit($account, null, $new->__toString(), $type, $subtype, $module, $ref, $content);
$newString = $this->stringify($new);
$audit = new Audit($account, null, $newString, $type, $subtype, $module, $ref, $content);
AuditMapper::create($audit);
}
public function apiLogUpdate(
Account $account,
$account,
$old,
$new,
int $type = 0,
@ -55,12 +57,15 @@ final class ApiController extends Controller
string $content = null
) : void
{
$audit = new Audit($account, $old->__toString(), $new->__toString(), $type, $subtype, $module, $ref, $content);
$oldString = $this->stringify($old);
$newString = $this->stringify($new);
$audit = new Audit($account, $oldString, $newString, $type, $subtype, $module, $ref, $content);
AuditMapper::create($audit);
}
public function apiLogDelete(
Account $account,
$account,
$old,
$new,
int $type = 0,
@ -70,7 +75,28 @@ final class ApiController extends Controller
string $content = null
) : void
{
$audit = new Audit($account, $new->__toString(), null, $type, $subtype, $module, $ref, $content);
$oldString = $this->stringify($old);
$audit = new Audit($account, $oldString, null, $type, $subtype, $module, $ref, $content);
AuditMapper::create($audit);
}
private function stringify($element) : ?string
{
$stringified = '';
if ($element instanceof \JsonSerializable) {
$stringified = \json_encode($element);
} elseif ($element instanceof \Serializable) {
$stringified = $element->serialize();
} elseif (\is_string($element)) {
$stringified = $element;
} elseif ($element === null) {
return null;
} else {
$stringified = $element->__toString();
}
return $stringified;
}
}

View File

@ -34,14 +34,6 @@ class Audit
*/
private $id = 0;
/**
* Audit account.
*
* @var int|Account
* @since 1.0.0
*/
private $account = 0;
/**
* Audit type.
*
@ -140,9 +132,9 @@ class Audit
* @since 1.0.0
*/
public function __construct(
Account $account,
?string $old,
?string $new,
$account = 0,
string $old = null,
string $new = null,
int $type = 0,
int $subtype = 0,
string $module = null,
@ -150,7 +142,7 @@ class Audit
string $content = null
) {
$this->createdAt = new \DateTime('now');
$this->account = $account;
$this->createdBy = $account;
$this->old = $old;
$this->new = $new;
$this->type = $type;
@ -160,6 +152,18 @@ class Audit
$this->content = $content;
}
/**
* Get id.
*
* @return int
*
* @since 1.0.0
*/
public function getId() : int
{
return $this->id;
}
/**
* Get type.
*

View File

@ -42,7 +42,7 @@ final class AuditMapper extends DataMapperAbstract
'auditor_audit_created_by' => ['name' => 'auditor_audit_created_by', 'type' => 'int', 'internal' => 'createdBy'],
'auditor_audit_created_at' => ['name' => 'auditor_audit_created_at', 'type' => 'DateTime', 'internal' => 'createdAt'],
'auditor_audit_ip' => ['name' => 'auditor_audit_ip', 'type' => 'int', 'internal' => 'ip'],
'auditor_audit_module' => ['name' => 'auditor_audit_module', 'type' => 'int', 'internal' => 'module'],
'auditor_audit_module' => ['name' => 'auditor_audit_module', 'type' => 'string', 'internal' => 'module'],
'auditor_audit_ref' => ['name' => 'auditor_audit_ref', 'type' => 'string', 'internal' => 'ref'],
'auditor_audit_type' => ['name' => 'auditor_audit_type', 'type' => 'int', 'internal' => 'type'],
'auditor_audit_subtype' => ['name' => 'auditor_audit_subtype', 'type' => 'int', 'internal' => 'subtype'],
@ -72,7 +72,7 @@ final class AuditMapper extends DataMapperAbstract
* @var string
* @since 1.0.0
*/
protected static $primaryField = 'auditor_audit';
protected static $primaryField = 'auditor_audit_id';
/**
* Created at.

View File

@ -39,7 +39,15 @@ echo $this->getData('nav')->render(); ?>
<?php $count = 0; foreach ($audits as $key => $audit) : $count++;
$url = \phpOMS\Uri\UriFactory::build('/{/lang}/backend/admin/audit/single?{?}&id=' . $audit->getId()); ?>
<tr data-href="<?= $url; ?>">
<td>
<td><?= $audit->getId(); ?>
<td><?= $audit->getModule(); ?>
<td><?= $audit->getType(); ?>
<td><?= $audit->getSubtype(); ?>
<td><?= $audit->getOld(); ?>
<td><?= $audit->getNew(); ?>
<td><?= $audit->getContent(); ?>
<td><?= $audit->getCreatedBy()->getName(); ?>
<td><?= $audit->getCreatedAt()->format('Y-m-d H:i:s'); ?>
<?php endforeach; ?>
<?php if ($count === 0) : ?>
<tr><td colspan="9" class="empty"><?= $this->getHtml('Empty', 0, 0); ?>