Skip to content

Commit

Permalink
#114 - Add NullAnonymizer
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonMellerin committed Mar 11, 2024
1 parent 120e95b commit deb7e27
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 0 deletions.
35 changes: 35 additions & 0 deletions docs/content/anonymization/core-anonymizers.md
Original file line number Diff line number Diff line change
Expand Up @@ -265,6 +265,41 @@ customer:
```
:::

## NullAnonymizer

Set all value to `NULL`.

::: code-group
```php [Attribute]
namespace App\Entity;

use Doctrine\ORM\Mapping as ORM;
use MakinaCorpus\DbToolsBundle\Attribute\Anonymize;

#[ORM\Entity()]
#[ORM\Table(name: 'customer')]
class Customer
{
// ...

#[ORM\Column]
#[Anonymize(type: 'null')] // [!code ++]
private ?string $sensibleContent = null;

// ...
}
```

```yml [YAML]
# config/anonymization.yaml

customer:
sensible_content: null

#...
```
:::

## Md5Anonymizer

This *Anonymizer* will fill configured column with a md5 hash of the pre-anonymization value.
Expand Down
29 changes: 29 additions & 0 deletions src/Anonymization/Anonymizer/Core/NullAnonymizer.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Core;

use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AbstractAnonymizer;
use MakinaCorpus\DbToolsBundle\Attribute\AsAnonymizer;
use MakinaCorpus\QueryBuilder\Query\Update;

#[AsAnonymizer(
name: 'null',
pack: 'core',
description: 'Set to NULL'
)]
class NullAnonymizer extends AbstractAnonymizer
{
/**
* @inheritdoc
*/
public function anonymize(Update $update): void
{
$expr = $update->expression();
$update->set(
$this->columnName,
$expr->raw('NULL'),
);
}
}
75 changes: 75 additions & 0 deletions tests/Functional/Anonymizer/Core/NullAnonymizerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php

declare(strict_types=1);

namespace MakinaCorpus\DbToolsBundle\Tests\Functional\Anonymizer\Core;

use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizationConfig;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizator;
use MakinaCorpus\DbToolsBundle\Anonymization\Config\AnonymizerConfig;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\AnonymizerRegistry;
use MakinaCorpus\DbToolsBundle\Anonymization\Anonymizer\Options;
use MakinaCorpus\DbToolsBundle\Test\FunctionalTestCase;

class NullAnonymizerTest extends FunctionalTestCase
{
/** @before */
protected function createTestData(): void
{
$this->createOrReplaceTable(
'table_test',
[
'id' => 'integer',
'data' => 'string',
],
[
[
'id' => '1',
'data' => 'toto1@example.com',
],
[
'id' => '2',
'data' => 'toto2@example.com',
],
[
'id' => '3',
'data' => 'toto3@example.com',
],
[
'id' => '4',
],
],
);
}

public function testAnonymize(): void
{
$config = new AnonymizationConfig();
$config->add(new AnonymizerConfig(
'table_test',
'data',
'null',
new Options()
));

$anonymizator = new Anonymizator(
$this->getConnection(),
new AnonymizerRegistry(),
$config
);

$this->assertSame(
"toto1@example.com",
$this->getConnection()->executeQuery('select data from table_test where id = 1')->fetchOne(),
);

foreach ($anonymizator->anonymize() as $message) {
}

$datas = $this->getConnection()->executeQuery('select data from table_test order by id asc')->fetchFirstColumn();
$this->assertNull($datas[0]);
$this->assertNull($datas[1]);
$this->assertNull($datas[2]);
$this->assertNull($datas[3]);
}
}

0 comments on commit deb7e27

Please sign in to comment.