Fix #1457 : EntityManager:clear should not be called in a controller

This commit is contained in:
Romain Neutron
2013-09-11 16:02:55 +02:00
parent 56eb725b80
commit a7b36af01a
3 changed files with 52 additions and 11 deletions

View File

@@ -113,18 +113,9 @@ class FailureManager
->findOldFailures('-2 months');
if (0 < count($failures)) {
$n = 0;
foreach ($failures as $failure) {
$this->em->remove($failure);
if (0 === $n++ % 1000) {
$this->em->flush();
$this->em->clear();
}
}
$this->em->flush();
$this->em->clear();
}
}
}

View File

@@ -56,7 +56,7 @@ class patch_3805 implements patchInterface
$rs = $stmt->fetchAll(\PDO::FETCH_ASSOC);
$stmt->closeCursor();
$n = 0;
$n = 1;
foreach ($rs as $row) {
$date = Datetime::createFromFormat('Y-m-d h:i:s', $row['date']);

View File

@@ -3,9 +3,11 @@
namespace Alchemy\Tests\Phrasea\Authentication\Phrasea;
use Alchemy\Phrasea\Authentication\Phrasea\FailureManager;
use Entities\AuthFailure;
use Gedmo\Timestampable\TimestampableListener;
use Symfony\Component\HttpFoundation\Request;
class FailureManagerTest extends \PHPUnit_Framework_TestCase
class FailureManagerTest extends \PhraseanetPHPUnitAbstract
{
/**
* @covers Alchemy\Phrasea\Authentication\Phrasea\FailureManager::saveFailure
@@ -241,6 +243,54 @@ class FailureManagerTest extends \PHPUnit_Framework_TestCase
$manager->checkFailures($username, $request);
}
public function testFailureOlderThan2MonthsAreRemovedOnFailure()
{
self::$DI['app']['EM']->getEventManager()->removeEventSubscriber(new TimestampableListener());
$recaptcha = $this->getReCaptchaMock(null);
$ip = '192.168.16.178';
$username = 'romainneutron';
$request = $this->getRequestMock();
$request->expects($this->any())
->method('getClientIp')
->will($this->returnValue($ip));
for ($i = 0; $i < 10; $i++) {
$failure = new AuthFailure();
$failure->setIp($ip);
$failure->setUsername($username);
$failure->setLocked(false);
$failure->setCreated(new \DateTime('-3 months'));
self::$DI['app']['EM']->persist($failure);
}
for ($i = 0; $i < 2; $i++) {
$failure = new AuthFailure();
$failure->setIp($ip);
$failure->setUsername($username);
$failure->setLocked(false);
$failure->setCreated(new \DateTime('-1 months'));
self::$DI['app']['EM']->persist($failure);
}
self::$DI['app']['EM']->flush();
$this->assertCount(10, self::$DI['app']['EM']->getRepository('Entities\AuthFailure')
->findOldFailures());
$this->assertCount(12, self::$DI['app']['EM']->getRepository('Entities\AuthFailure')
->findAll());
$manager = new FailureManager(self::$DI['app']['EM'], $recaptcha, 9);
$manager->saveFailure($username, $request);
$this->assertCount(0, self::$DI['app']['EM']->getRepository('Entities\AuthFailure')
->findOldFailures());
$this->assertCount(3, self::$DI['app']['EM']->getRepository('Entities\AuthFailure')
->findAll());
self::$DI['app']['EM']->getEventManager()->addEventSubscriber(new TimestampableListener());
}
private function ArrayIze($failure, $n)
{
$failures = array();