Skip to content

Commit

Permalink
"global" run time enable (2) (#61)
Browse files Browse the repository at this point in the history
* use global enabled flag

* basic test

* simple enable/disable

* actual entity test

* test both ways

* unnessesary, is checked by $this->isAudited()

* update verbage

* update phpDocs

* fix tests, add enabled test
  • Loading branch information
Chad Meyers authored and DamienHarper committed Apr 16, 2019
1 parent b97a659 commit 326f67b
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
43 changes: 43 additions & 0 deletions src/DoctrineAuditBundle/AuditConfiguration.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ class AuditConfiguration
*/
private $entities = [];

/**
* @var bool
*/
private $enabled = true;

/**
* @var UserProviderInterface
*/
Expand Down Expand Up @@ -72,6 +77,40 @@ public function setEntities(array $entities): void
$this->entities = $entities;
}

/**
* enabled audit.
*
* @return $this
*/
public function enable(): self
{
$this->enabled = true;

return $this;
}

/**
* disable audit.
*
* @return $this
*/
public function disable(): self
{
$this->enabled = false;

return $this;
}

/**
* Get enabled flag.
*
* @return bool
*/
public function isEnabled(): bool
{
return $this->enabled;
}

/**
* Returns true if $entity is audited.
*
Expand Down Expand Up @@ -101,6 +140,10 @@ public function isAuditable($entity): bool
*/
public function isAudited($entity): bool
{
if (!$this->enabled) {
return false;
}

$class = DoctrineHelper::getRealClass($entity);

// is $entity part of audited entities?
Expand Down
80 changes: 80 additions & 0 deletions tests/DoctrineAuditBundle/AuditConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,29 @@ public function testCustomTableSuffix(): void
$this->assertSame('_audit_log', $configuration->getTableSuffix(), 'custom table_suffix is "_audit_log".');
}

public function testDefaultEnabled(): void
{
$configuration = $this->getAuditConfiguration();

$this->assertTrue($configuration->isEnabled(), 'Enabled by default.');
}

public function testEnabled(): void
{
$configuration = $this->getAuditConfiguration();
$configuration->enable();

$this->assertTrue($configuration->isEnabled(), 'Enabled by default.');
}

public function testDisabled(): void
{
$configuration = $this->getAuditConfiguration();
$configuration->disable();

$this->assertFalse($configuration->isEnabled(), 'Disabled. Global enabled is set to false.');
}

public function testGloballyIgnoredColumns(): void
{
$ignored = [
Expand Down Expand Up @@ -154,6 +177,62 @@ public function testIsAuditedHonorsEnabledFlag(): void
$this->assertFalse($configuration->isAudited(Post::class), 'entity "'.Post::class.'" is not audited.');
}

/**
* @depends testIsAudited
*/
public function testIsAuditedWhenAuditIsEnabled(): void
{
$entities = [
Post::class => [
'enabled' => true,
],
];

$configuration = $this->getAuditConfiguration([
'entities' => $entities,
]);

$configuration->enable();

$this->assertTrue($configuration->isAudited(Post::class), 'entity "'.Post::class.'" is audited.');

$entities = [
Post::class => [
'enabled' => false,
],
];

$configuration = $this->getAuditConfiguration([
'entities' => $entities,
]);

$configuration->enable();

$this->assertFalse($configuration->isAudited(Post::class), 'entity "'.Post::class.'" is not audited.');
}

/**
* @depends testIsAudited
*/
public function testIsAuditedWhenAuditIsDisabled(): void
{
$entities = [
Post::class => [
'enabled' => true,
],
];

$configuration = $this->getAuditConfiguration([
'entities' => $entities,
]);

$this->assertTrue($configuration->isAudited(Post::class), 'entity "'.Post::class.'" is audited.');

$configuration->disable();

$this->assertFalse($configuration->isAudited(Post::class), 'entity "'.Post::class.'" is not audited.');
}

/**
* @depends testIsAudited
*/
Expand Down Expand Up @@ -294,6 +373,7 @@ protected function getAuditConfiguration(array $options = []): AuditConfiguratio
'table_suffix' => '_audit',
'ignored_columns' => [],
'entities' => [],
'enabled' => true,
], $options),
new TokenStorageUserProvider(new Security($container)),
new RequestStack(),
Expand Down

0 comments on commit 326f67b

Please sign in to comment.