Skip to content

Commit

Permalink
Merge pull request #10 from pluswerk/feature/add-tests
Browse files Browse the repository at this point in the history
[FEATURE] add tests for MailLogRepository to fix and prevent some bugs
  • Loading branch information
Kanti authored Aug 3, 2018
2 parents 5d6c390 + 8446c05 commit a7452c5
Show file tree
Hide file tree
Showing 30 changed files with 611 additions and 51 deletions.
5 changes: 2 additions & 3 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
/.idea/
/.Build/
/composer.lock
/vendor/
index.php
typo3
typo3_src
26 changes: 15 additions & 11 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,31 @@ language: php
git:
depth: 5

php:
- 5.6
- 7.0
- 7.1
- 7.2
- nightly
matrix:
fast_finish: true
include:
- php: 7.0
env: TYPO3_VERSION=8.7.*
- php: 7.1
env: TYPO3_VERSION=8.7.*
- php: 7.2
env: TYPO3_VERSION=8.7.*
allow_failures:
- php: nightly
env: TYPO3_VERSION=8.7.*

cache:
directories:
- $HOME/.composer/cache

matrix:
fast_finish: true
allow_failures:
- php: nightly

before_install:
- phpenv config-rm xdebug.ini || echo "xdebug not available"

install:
- composer install --no-progress --no-scripts --no-suggest -n
- composer require typo3/cms="$TYPO3_VERSION" --dev
- composer install --no-progress --no-suggest -n

script:
- ./vendor/bin/grumphp run
- composer test
100 changes: 65 additions & 35 deletions Classes/Domain/Repository/MailLogRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -42,15 +42,25 @@ class MailLogRepository extends Repository
protected $defaultAnonymizeAfter = '7 days';

/**
* @var array
* @var string
*/
protected $lifetime = '30 days';

/**
* @var string
*/
protected $mailLoggerSettings = '';
protected $anonymizeAfter;

/**
* @var string
*/
protected $anonymizeSymbol = '***';

/**
* @var bool
*/
protected $anonymize = true;

/**
* @return void
*/
Expand All @@ -64,7 +74,19 @@ public function initializeObject()
// mail logger typoscript settings
$configurationManager = $this->objectManager->get(ConfigurationManager::class);
$fullSettings = $configurationManager->getConfiguration(ConfigurationManagerInterface::CONFIGURATION_TYPE_FULL_TYPOSCRIPT);
$this->mailLoggerSettings = $fullSettings['module.']['tx_maillogger.']['settings.'];
$settings = $fullSettings['module.']['tx_maillogger.']['settings.'];

$this->lifetime = $this->defaultLifetime;
if (isset($settings['cleanup.']['lifetime'])) {
$this->lifetime = $settings['cleanup.']['lifetime'];
}
$this->anonymizeAfter = $this->defaultAnonymizeAfter;
if (isset($settings['cleanup.']['anonymizeAfter'])) {
$this->anonymizeAfter = $settings['cleanup.']['anonymizeAfter'];
}
if (isset($settings['cleanup.']['anonymize'])) {
$this->anonymize = (bool)$settings['cleanup.']['anonymize'];
}

// cleanup
$this->cleanupDatabase();
Expand All @@ -79,9 +101,10 @@ public function initializeObject()
*/
protected function cleanupDatabase()
{
$lifetime = $this->mailLoggerSettings['cleanup.']['lifetime'] ?: $this->defaultLifetime;
foreach ($this->findOldMailLogRecords($lifetime) as $mailLog) {
$this->remove($mailLog);
if ($this->lifetime !== '') {
foreach ($this->findOldMailLogRecords($this->lifetime) as $mailLog) {
$this->remove($mailLog);
}
}
}

Expand All @@ -91,11 +114,8 @@ protected function cleanupDatabase()
*/
protected function anonymizeAll()
{
$anonymizeAfter = $this->mailLoggerSettings['cleanup.']['anonymizeAfter'] ?: $this->defaultAnonymizeAfter;
// if anonymizeAfter is set
if ($this->mailLoggerSettings['cleanup.']['anonymize'] && $anonymizeAfter) {
foreach ($this->findOldMailLogRecords($anonymizeAfter) as $mailLog) {
$this->anonymizeMailLog($mailLog);
if ($this->anonymize) {
foreach ($this->findOldMailLogRecords($this->anonymizeAfter) as $mailLog) {
$this->update($mailLog);
}
}
Expand All @@ -108,50 +128,60 @@ protected function anonymizeAll()
protected function findOldMailLogRecords($lifeTime)
{
$query = $this->createQuery();
$now = new \DateTime();
$query->matching($query->lessThanOrEqual('crdate', $now->modify('-' . $lifeTime)));
$query->matching($query->lessThanOrEqual('crdate', date_modify(new \DateTime(), '-' . $lifeTime)));
return $query->execute();
}

/**
* @param MailLog $object
* @param MailLog $mailLog
* @return void
*/
public function add($object)
public function add($mailLog)
{
$anonymizeAfter = $this->mailLoggerSettings['cleanup.']['anonymizeAfter'] ?: $this->defaultAnonymizeAfter;
// if anonymizeAfter is not set
if ($this->mailLoggerSettings['cleanup.']['anonymize'] && !$anonymizeAfter) {
$this->anonymizeMailLog($object);
if ($mailLog->getCrdate() === null) {
$mailLog->_setProperty('crdate', time());
}
if ($mailLog->getTstamp() === null) {
$mailLog->_setProperty('tstamp', time());
}
parent::add($object);
$this->anonymizeMailLogIfNeeded($mailLog);
parent::add($mailLog);
}

/**
* @param MailLog $object
* @param MailLog $mailLog
* @return void
*/
public function update($object)
public function update($mailLog)
{
$anonymizeAfter = $this->mailLoggerSettings['cleanup.']['anonymizeAfter'] ?: $this->defaultAnonymizeAfter;
// if anonymizeAfter is not set
if ($this->mailLoggerSettings['cleanup.']['anonymize'] && !$anonymizeAfter) {
$this->anonymizeMailLog($object);
if ($mailLog->getTstamp() === null) {
$mailLog->_setProperty('tstamp', time());
}
parent::update($object);
$this->anonymizeMailLogIfNeeded($mailLog);
parent::update($mailLog);
}

/**
* @param MailLog $object
* @param MailLog $mailLog
* @return void
*/
protected function anonymizeMailLog($object)
protected function anonymizeMailLogIfNeeded(MailLog $mailLog)
{
$object->setSubject($this->anonymizeSymbol);
$object->setMessage($this->anonymizeSymbol);
$object->setMailFrom($this->anonymizeSymbol);
$object->setMailTo($this->anonymizeSymbol);
$object->setResult($this->anonymizeSymbol);
$object->setMailBlindCopy($this->anonymizeSymbol);
if ($mailLog->getCrdate() === null) {
throw new \InvalidArgumentException('MailLog must have a crdate');
}
if ($this->anonymize === false) {
return;
}
if ($mailLog->getCrdate() > date_modify(new \DateTime(), '-' . $this->anonymizeAfter)->getTimestamp()) {
return;
}

$mailLog->setSubject($this->anonymizeSymbol);
$mailLog->setMessage($this->anonymizeSymbol);
$mailLog->setMailFrom($this->anonymizeSymbol);
$mailLog->setMailTo($this->anonymizeSymbol);
$mailLog->setResult($this->anonymizeSymbol);
$mailLog->setMailBlindCopy($this->anonymizeSymbol);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.tx_maillogger.settings.cleanup {
anonymize = 1
anonymizeAfter = 7 days
lifetime = 30 days
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.tx_maillogger.settings.cleanup {
anonymize = 1
anonymizeAfter = 0
lifetime =
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
module.tx_maillogger.settings.cleanup {
anonymize = 0
anonymizeAfter = 0
lifetime = 1 min
}
Loading

0 comments on commit a7452c5

Please sign in to comment.