diff --git a/lib/Alchemy/Phrasea/Authentication/Authenticator.php b/lib/Alchemy/Phrasea/Authentication/Authenticator.php index 1026f6e35b..212ab2fff4 100644 --- a/lib/Alchemy/Phrasea/Authentication/Authenticator.php +++ b/lib/Alchemy/Phrasea/Authentication/Authenticator.php @@ -116,6 +116,15 @@ class Authenticator */ public function closeAccount() { + if (!$this->session->has('session_id')) { + throw new RuntimeException('No session to close.'); + } + + if (null !== $session = $this->em->find('Entities\Session', $this->session->get('session_id'))) { + $this->em->remove($session); + $this->em->flush(); + } + $this->session->invalidate(); $this->reinitUser(); diff --git a/lib/Alchemy/Phrasea/Controller/Api/V1.php b/lib/Alchemy/Phrasea/Controller/Api/V1.php index d49d33a1c8..b79ce7efd5 100644 --- a/lib/Alchemy/Phrasea/Controller/Api/V1.php +++ b/lib/Alchemy/Phrasea/Controller/Api/V1.php @@ -164,6 +164,10 @@ class V1 implements ControllerProviderInterface ); }); + $controllers->after(function () use ($app) { + $app['authentication']->closeAccount(); + }); + /** * Method Not Allowed Closure */ diff --git a/templates/web/account/sessions.html.twig b/templates/web/account/sessions.html.twig index cdb966e871..ee8dc9673e 100644 --- a/templates/web/account/sessions.html.twig +++ b/templates/web/account/sessions.html.twig @@ -35,8 +35,10 @@ {% set row = session['session'] %} - {% if app['session'].get('phrasea_session_id') != row.Id() %} + {% if app['session'].get('session_id') != row.Id() %} {% trans %}End Activity{% endtrans %} + {% else %} + {% trans 'Current session' %} {% endif %} diff --git a/templates/web/prod/index.html.twig b/templates/web/prod/index.html.twig index db16c131ee..77095472e3 100644 --- a/templates/web/prod/index.html.twig +++ b/templates/web/prod/index.html.twig @@ -427,14 +427,14 @@
- +
{% trans 'phraseanet::time:: a' %}
- +
diff --git a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php index 8b2a6ee258..29f7a0e0dc 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php +++ b/tests/Alchemy/Tests/Phrasea/Application/ApiAbstract.php @@ -149,6 +149,14 @@ abstract class ApiAbstract extends \PhraseanetWebTestCaseAbstract $this->assertEquals(1, $preEvent); } + public function testThatSessionIsClosedAfterRequest() + { + $this->assertCount(0, self::$DI['app']['EM']->getRepository('Entities\Session')->findAll()); + $this->setToken(self::$token); + self::$DI['client']->request('GET', '/api/v1/databoxes/list/', $this->getParameters(), array(), array('HTTP_Accept' => $this->getAcceptMimeType())); + $this->assertCount(0, self::$DI['app']['EM']->getRepository('Entities\Session')->findAll()); + } + public function provideEventNames() { return array( diff --git a/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php b/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php index 32984f4479..1086d272b8 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/LightboxTest.php @@ -27,7 +27,7 @@ class LightboxTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRouteSlash() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $baskets = $this->insertFiveBasket(); @@ -54,7 +54,7 @@ class LightboxTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testAuthenticationWithToken() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $Basket = $this->insertOneBasket(); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_VIEW, self::$DI['user_alt2']->get_id(), null, $Basket->getId()); @@ -178,7 +178,7 @@ class LightboxTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testValidate() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $basket = $this->insertOneValidationBasket(); @@ -203,7 +203,7 @@ class LightboxTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testCompare() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $basket = $this->insertOneBasket(); @@ -228,7 +228,7 @@ class LightboxTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testFeedEntry() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']); diff --git a/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php b/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php index 8cd448adc2..7e9f9f499b 100644 --- a/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php +++ b/tests/Alchemy/Tests/Phrasea/Application/OverviewTest.php @@ -61,23 +61,20 @@ class OverviewTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $this->assertForbiddenResponse(self::$DI['client']->getResponse()); } - public function testDatafilesRouteOnUnaccessibleRecordIsOkInPublicFeed() + public function testDatafilesRouteNotAuthenticatedIsOkInPublicFeed() { - $tmp = tempnam(sys_get_temp_dir(), 'testEtag'); - copy(__DIR__ . '/../../../../files/cestlafete.jpg', $tmp); + $publicFeed = \Feed_Adapter::create(self::$DI['app'], self::$DI['user'], 'titre', 'subtitre'); + $publicFeed->set_public(true); + $publisher = \Feed_Publisher_Adapter::getPublisher(self::$DI['app']['phraseanet.appbox'], $publicFeed, self::$DI['user']); + $entry = \Feed_Entry_Adapter::create(self::$DI['app'], $publicFeed, $publisher, 'titre', 'sub titre entry', 'author name', 'author email', false); + self::$DI['record_1']->move_to_collection(self::$DI['collection_no_access'], self::$DI['app']['phraseanet.appbox']); + $item = \Feed_Entry_Item::create(self::$DI['app']['phraseanet.appbox'], $entry, self::$DI['record_1']); - $media = self::$DI['app']['mediavorus']->guess($tmp); + self::$DI['client']->request('GET', '/datafiles/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/preview/'); - $file = new File(self::$DI['app'], $media, self::$DI['collection_no_access']); - $record = \record_adapter::createFromFile($file, self::$DI['app']); - $record->generate_subdefs($record->get_databox(), self::$DI['app']); - - $item = $this->insertOneFeedItem(self::$DI['user'], true, 1, $record); - - self::$DI['client']->request('GET', '/datafiles/' . $record->get_sbas_id() . '/' . $record->get_record_id() . '/preview/'); $this->assertEquals(200, self::$DI['client']->getResponse()->getStatusCode()); - - unlink($tmp); + self::$DI['record_1']->move_to_collection(self::$DI['collection'], self::$DI['app']['phraseanet.appbox']); + $publicFeed->set_public(false); } public function testDatafilesRouteNotAuthenticatedUnknownSubdef() @@ -210,12 +207,17 @@ class OverviewTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPermalinkRouteNotAuthenticatedIsOkInPublicFeed() { - $record = $this->insertOneFeedItem(self::$DI['user'], true)->getRecord(self::$DI['app']); + $publicFeed = \Feed_Adapter::create(self::$DI['app'], self::$DI['user'], 'titre', 'subtitre'); + $publicFeed->set_public(true); + $publisher = \Feed_Publisher_Adapter::getPublisher(self::$DI['app']['phraseanet.appbox'], $publicFeed, self::$DI['user']); + $entry = \Feed_Entry_Adapter::create(self::$DI['app'], $publicFeed, $publisher, 'titre', 'sub titre entry', 'author name', 'author email', false); + $item = \Feed_Entry_Item::create(self::$DI['app']['phraseanet.appbox'], $entry, self::$DI['record_1']); self::$DI['app']['authentication']->closeAccount(); - self::$DI['client']->request('GET', '/permalink/v1/' . $record->get_sbas_id() . '/' . $record->get_record_id() . '/preview/'); + self::$DI['client']->request('GET', '/permalink/v1/' . self::$DI['record_1']->get_sbas_id() . '/' . self::$DI['record_1']->get_record_id() . '/preview/'); $this->assertEquals(200, self::$DI['client']->getResponse()->getStatusCode()); + $publicFeed->set_public(false); } protected function get_a_permaviewBCcompatibility(array $headers = array()) diff --git a/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php b/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php index b8f3203d91..ae2f2eb530 100644 --- a/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php +++ b/tests/Alchemy/Tests/Phrasea/Authentication/AuthenticatorTest.php @@ -207,21 +207,25 @@ class AuthenticatorTest extends \PhraseanetPHPUnitAbstract */ public function testCloseAccount() { - $app = new Application(); - + $app = self::$DI['app']; $user = self::$DI['user']; - $app['browser'] = $browser = $this->getBrowserMock(); - $app['session'] = $session = $this->getSessionMock(); - $app['EM'] = $em = $this->getEntityManagerMock(); - - $session->set('usr_id', $user->get_id()); - - $authenticator = new Authenticator($app, $browser, $session, $em); + $authenticator = new Authenticator($app, $app['browser'], $app['session'], $app['EM']); + $authenticator->openAccount($user); + $this->assertNotNull($authenticator->getUser()); $authenticator->closeAccount(); $this->assertNull($authenticator->getUser()); } + public function testCloseAccountWhenNoSessionThrowsAnException() + { + $app = self::$DI['app']; + + $authenticator = new Authenticator($app, $app['browser'], $app['session'], $app['EM']); + $this->setExpectedException('Alchemy\Phrasea\Exception\RuntimeException', 'No session to close.'); + $authenticator->closeAccount(); + } + /** * @covers Alchemy\Phrasea\Authentication\Authenticator::isAuthenticated */ diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Admin/RootTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Admin/RootTest.php index f8ec057e56..93df91f20a 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Admin/RootTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Admin/RootTest.php @@ -11,7 +11,7 @@ class RootTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRouteSlash() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); self::$DI['client']->request('GET', '/admin/', array('section' => 'base:featured')); $this->assertTrue(self::$DI['client']->getResponse()->isOk()); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Client/RootTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Client/RootTest.php index c1ee3902bb..0ee6efcd7c 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Client/RootTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Client/RootTest.php @@ -23,7 +23,7 @@ class RootTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testGetClient() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); self::$DI['client']->request("GET", "/client/"); $this->assertTrue(self::$DI['client']->getResponse()->isOk()); } diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/QueryTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/QueryTest.php index 092a6b3ed8..cc6256891e 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/QueryTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/QueryTest.php @@ -30,7 +30,7 @@ class QueryTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testQueryAnswerTrain() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); self::$DI['record_24']; $options = new SearchEngineOptions(); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/RecordsTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/RecordsTest.php index 3c942010bf..2a72699f83 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/RecordsTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/RecordsTest.php @@ -78,7 +78,7 @@ class RecordsTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testGetRecordDetailResult() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); self::$DI['record_24']; $options = new SearchEngineOptions(); @@ -114,7 +114,7 @@ class RecordsTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testGetRecordDetailREG() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); self::$DI['record_story_1']; $this->XMLHTTPRequest('POST', '/prod/records/', array( @@ -144,7 +144,7 @@ class RecordsTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testGetRecordDetailBasket() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $basket = $this->insertOneBasket(); $record = self::$DI['record_1']; @@ -186,7 +186,7 @@ class RecordsTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testGetRecordDetailFeed() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); self::$DI['app']['notification.deliverer'] = $this->getMockBuilder('Alchemy\Phrasea\Notification\Deliverer') ->disableOriginalConstructor() diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php index 4e307baf41..e797968daa 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Prod/RootTest.php @@ -11,7 +11,7 @@ class ControllerRootTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRouteSlash() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $crawler = self::$DI['client']->request('GET', '/prod/'); diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Report/RootTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Report/RootTest.php index 2b8fac5145..07ff64abf8 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Report/RootTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Report/RootTest.php @@ -15,7 +15,7 @@ class RootTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRouteDashboard() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); self::$DI['client']->request('GET', '/report/dashboard'); @@ -26,7 +26,7 @@ class RootTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRouteDashboardJson() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $this->XMLHTTPRequest('GET', '/report/dashboard', array( 'dmin' => $this->dmin->format('Y-m-d H:i:s'), diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php index fb3d488f74..8274cecf33 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/LoginTest.php @@ -55,7 +55,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testLoginRedirectPostLog() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/', array('postlog' => '1', 'redirect' => 'prod')); $response = self::$DI['client']->getResponse(); @@ -68,7 +68,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testLoginError($type, $message) { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['app']->addFlash($type, $message); $crawler = self::$DI['client']->request('GET', '/login/'); @@ -83,7 +83,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRegisterConfirmMailNoCode() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/register-confirm/'); $response = self::$DI['client']->getResponse(); @@ -97,7 +97,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRegisterConfirmMailWrongCode() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/register-confirm/', array( 'code' => '34dT0k3n' )); @@ -113,7 +113,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRegisterConfirmMailUserNotFound() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $email = $this->generateEmail(); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, 0, null, $email); self::$DI['client']->request('GET', '/login/register-confirm/', array( @@ -131,7 +131,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRegisterConfirmMailUnlocked() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $email = $this->generateEmail(); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, self::$DI['user']->get_id(), null, $email); @@ -152,7 +152,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { $this->mockNotificationDeliverer('Alchemy\Phrasea\Notification\Mail\MailSuccessEmailConfirmationRegistered'); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $email = $this->generateEmail(); $appboxRegister = new \appbox_register(self::$DI['app']['phraseanet.appbox']); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, self::$DI['user']->get_id(), null, $email); @@ -178,7 +178,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $user = \User_Adapter::create(self::$DI['app'], 'test'.mt_rand(), \random::generatePassword(), 'email-random'.mt_rand().'@phraseanet.com', false); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $email = $this->generateEmail(); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_EMAIL, $user->get_id(), null, $email); @@ -201,7 +201,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRenewPasswordInvalidEmail() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $crawler = self::$DI['client']->request('POST', '/login/forgot-password/', array( 'email' => 'invalid.email.com', '_token' => 'token', @@ -218,7 +218,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRenewPasswordUnknowEmail() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $crawler = self::$DI['client']->request('POST', '/login/forgot-password/', array( 'email' => 'invalid_email@test.com', '_token' => 'token', @@ -236,7 +236,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { $this->mockNotificationDeliverer('Alchemy\Phrasea\Notification\Mail\MailRequestPasswordUpdate'); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('POST', '/login/forgot-password/', array( 'email' => self::$DI['user']->get_email(), '_token' => 'token', @@ -253,7 +253,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRenewPasswordBadArguments() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_PASSWORD, self::$DI['user']->get_id()); $crawler = self::$DI['client']->request('POST', '/login/renew-password/', array( 'token' => $token, @@ -269,7 +269,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRenewPasswordBadToken() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('POST', '/login/renew-password/', array( 'token' => 'badToken', '_token' => 'token', @@ -296,7 +296,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRenewPasswordNoToken() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('POST', '/login/renew-password/', array( '_token' => 'token', 'password' => array('password' => 'password', 'confirm' => 'password') @@ -325,7 +325,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRenewPassword() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_PASSWORD, self::$DI['user']->get_id()); self::$DI['client']->request('POST', '/login/renew-password/', array( @@ -347,7 +347,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRenewPasswordPageShowsFlashMessages($type, $message) { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['app']->addFlash($type, $message); $token = self::$DI['app']['tokens']->getUrlToken(\random::TYPE_PASSWORD, self::$DI['user']->get_id()); @@ -363,7 +363,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testForgotPasswordGet() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/forgot-password/'); $this->assertTrue(self::$DI['client']->getResponse()->isOk()); @@ -381,7 +381,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testForgotPasswordInvalidEmail() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $crawler = self::$DI['client']->request('POST', '/login/forgot-password/', array( '_token' => 'token', 'email' => 'invalid.email', @@ -395,7 +395,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testForgotPasswordWrongEmail() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $crawler = self::$DI['client']->request('POST', '/login/forgot-password/', array( '_token' => 'token', 'email' => 'invalid@email.com', @@ -411,7 +411,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { $this->mockNotificationDeliverer('Alchemy\Phrasea\Notification\Mail\MailRequestPasswordUpdate'); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $crawler = self::$DI['client']->request('POST', '/login/forgot-password/', array( '_token' => 'token', 'email' => self::$DI['user']->get_email(), @@ -429,7 +429,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testGetRegister($type, $message) { self::$DI['app']['registration.enabled'] = true; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['app']->addFlash($type, $message); $crawler = self::$DI['client']->request('GET', '/login/register-classic/'); @@ -442,7 +442,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testGetRegisterWithRegisterIdBindDataToForm() { self::$DI['app']['registration.enabled'] = true; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $provider = $this->getMock('Alchemy\Phrasea\Authentication\Provider\ProviderInterface'); @@ -488,7 +488,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testGetPostRegisterWhenRegistrationDisabled($method, $route) { self::$DI['app']['registration.enabled'] = false; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request($method, $route); $this->assertEquals(404, self::$DI['client']->getResponse()->getStatusCode()); } @@ -501,7 +501,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$DI['app']['registration.enabled'] = true; self::$DI['app']['registration.fields'] = $extraParameters; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $parameters = array_merge(array('_token' => 'token'), $parameters); foreach ($parameters as $key => $parameter) { @@ -528,7 +528,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPostRegisterWithoutParams() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $crawler = self::$DI['client']->request('POST', '/login/register-classic/'); $this->assertFalse(self::$DI['client']->getResponse()->isRedirect()); @@ -782,7 +782,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPostRegisterWithProviderIdAndAlreadyBoundProvider() { self::$DI['app']['registration.fields'] = array(); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $provider = $this->getMock('Alchemy\Phrasea\Authentication\Provider\ProviderInterface'); $this->addProvider('provider-test', $provider); @@ -832,7 +832,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPostRegisterWithUnknownProvider() { self::$DI['app']['registration.fields'] = array(); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $parameters = array_merge(array('_token' => 'token'), array( "password" => array( @@ -867,7 +867,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPostRegisterWithProviderNotAuthenticated() { self::$DI['app']['registration.fields'] = array(); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $provider = $this->getMock('Alchemy\Phrasea\Authentication\Provider\ProviderInterface'); $this->addProvider('provider-test', $provider); @@ -909,7 +909,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPostRegisterWithProviderId() { self::$DI['app']['registration.fields'] = array(); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $emails = array( 'Alchemy\Phrasea\Notification\Mail\MailRequestEmailConfirmation'=>0, @@ -983,7 +983,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { self::$DI['app']['registration.fields'] = $extraParameters; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $emails = array( 'Alchemy\Phrasea\Notification\Mail\MailRequestEmailConfirmation'=>0, @@ -1044,7 +1044,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testSendConfirmMailBadRequest() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/send-mail-confirm/'); $this->assertBadResponse(self::$DI['client']->getResponse()); @@ -1057,7 +1057,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract { $this->mockNotificationDeliverer('Alchemy\Phrasea\Notification\Mail\MailRequestEmailConfirmation'); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/send-mail-confirm/', array('usr_id' => self::$DI['user']->get_id())); $response = self::$DI['client']->getResponse(); @@ -1071,7 +1071,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testSendConfirmMailWrongUser() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/send-mail-confirm/', array('usr_id' => 0)); $response = self::$DI['client']->getResponse(); @@ -1091,7 +1091,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$DI['app']['authentication']->getUser()->set_password($password); self::$DI['app']['authentication']->getUser()->set_mail_locked(false); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client'] = new Client(self::$DI['app'], array()); $this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']); @@ -1116,7 +1116,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$DI['app']['authentication']->getUser()->set_password($password); self::$DI['app']['authentication']->getUser()->set_mail_locked(false); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $preEvent = 0; $phpunit = $this; @@ -1163,7 +1163,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $login = self::$DI['app']['authentication']->getUser()->get_login(); self::$DI['app']['authentication']->getUser()->set_password($password); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client'] = new Client(self::$DI['app'], array()); $this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']); @@ -1189,7 +1189,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $user->ACL()->give_access_to_base(array(self::$DI['collection']->get_base_id())); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client'] = new Client(self::$DI['app'], array()); $this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']); @@ -1221,7 +1221,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract $user = \User_Adapter::getInstance($usr_id, self::$DI['app']); $user->ACL()->give_access_to_base(array(self::$DI['collection']->get_base_id())); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client'] = new Client(self::$DI['app'], array()); $this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']); @@ -1235,7 +1235,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testGuestAuthenticateWithGetMethod() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client'] = new Client(self::$DI['app'], array()); $this->set_user_agent(self::USER_AGENT_FIREFOX8MAC, self::$DI['app']); @@ -1255,7 +1255,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testBadAuthenticate() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('POST', '/login/authenticate/', array( 'login' => self::$DI['user']->get_login(), 'password' => 'test', @@ -1272,7 +1272,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testBadAuthenticateCheckRedirect() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('POST', '/login/authenticate/', array( 'login' => self::$DI['user']->get_login(), 'password' => 'test', @@ -1290,7 +1290,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testMailLockedAuthenticate() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $password = \random::generatePassword(); self::$DI['user']->set_mail_locked(true); self::$DI['client']->request('POST', '/login/authenticate/', array( @@ -1327,7 +1327,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ->with($parameters) ->will($this->returnValue($response)); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/provider/provider-test/authenticate/', $parameters); $this->assertSame($response, self::$DI['client']->getResponse()); @@ -1367,7 +1367,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ->with($this->equalTo('provider-test')) ->will($this->throwException(new InvalidArgumentException('Provider not found'))); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request($method, $route); $this->assertEquals(404, self::$DI['client']->getResponse()->getStatusCode()); @@ -1398,7 +1398,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ->method('onCallback') ->will($this->throwException(new NotAuthenticatedException('Not authenticated.'))); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/provider/provider-test/callback/'); $this->assertSame(302, self::$DI['client']->getResponse()->getStatusCode()); @@ -1428,7 +1428,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ->method('getToken') ->will($this->returnValue($token)); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/provider/provider-test/callback/'); $this->assertSame(302, self::$DI['client']->getResponse()->getStatusCode()); @@ -1462,7 +1462,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ->with($token) ->will($this->returnValue($user)); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/provider/provider-test/callback/'); $this->assertSame(302, self::$DI['client']->getResponse()->getStatusCode()); @@ -1537,7 +1537,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ->method('isEnabled') ->will($this->returnValue(true)); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/provider/provider-test/callback/'); $this->assertSame(302, self::$DI['client']->getResponse()->getStatusCode()); @@ -1589,7 +1589,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$DI['app']['registration.enabled'] = true; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/provider/provider-test/callback/'); $this->assertSame(302, self::$DI['client']->getResponse()->getStatusCode()); @@ -1632,7 +1632,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract self::$DI['app']['registration.enabled'] = false; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/provider/provider-test/callback/'); $this->assertSame(302, self::$DI['client']->getResponse()->getStatusCode()); @@ -1651,7 +1651,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract ); self::$DI['app']['registration.fields'] = $fields; - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['client']->request('GET', '/login/registration-fields/'); $this->assertSame(200, self::$DI['client']->getResponse()->getStatusCode()); @@ -1662,7 +1662,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRegisterRedirectsNoAuthProvidersAvailable() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['app']['authentication.providers'] = new ProvidersCollection(); @@ -1674,7 +1674,7 @@ class LoginTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testRegisterDisplaysIfAuthProvidersAvailable() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $provider = $this->getMock('Alchemy\Phrasea\Authentication\Provider\ProviderInterface'); $provider->expects($this->any()) diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/RootTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/RootTest.php index a1ff36e894..19815e6361 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/RootTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/RootTest.php @@ -83,7 +83,7 @@ class RootTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testNoPersistentCookie() { $app = self::$DI['app']; - $app['authentication']->closeAccount(); + $this->logout($app); $boolean = false; @@ -102,7 +102,7 @@ class RootTest extends \PhraseanetWebTestCaseAuthenticatedAbstract public function testPersistentCookie() { $app = self::$DI['app']; - $app['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $browser = $this->getMockBuilder('\Browser') ->disableOriginalConstructor() diff --git a/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php b/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php index 770f42da3a..2cb7477db4 100644 --- a/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php +++ b/tests/Alchemy/Tests/Phrasea/Controller/Root/SessionTest.php @@ -38,7 +38,7 @@ class SessionTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testUpdSession() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $this->XMLHTTPRequest('POST', '/session/update/', array( 'usr' => self::$DI['user']->get_id(), @@ -55,7 +55,7 @@ class SessionTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testUpdSessionBadRequestMissingModuleArgument() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $this->XMLHTTPRequest('POST', '/session/update/', array( 'usr' => self::$DI['user']->get_id() diff --git a/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineOptionsTest.php b/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineOptionsTest.php index 139d501f1f..78b69c4e96 100644 --- a/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineOptionsTest.php +++ b/tests/Alchemy/Tests/Phrasea/SearchEngine/SearchEngineOptionsTest.php @@ -39,7 +39,7 @@ class SearchEngineOptionsTest extends \PhraseanetPHPUnitAbstract */ public function testFromRequest() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); foreach ($this->provideRequestData() as $pack) { list ($query, $request, $field, $dateField) = $pack; diff --git a/tests/Alchemy/Tests/Phrasea/Security/FirewallTest.php b/tests/Alchemy/Tests/Phrasea/Security/FirewallTest.php index e6b6090d20..b4d25280a0 100644 --- a/tests/Alchemy/Tests/Phrasea/Security/FirewallTest.php +++ b/tests/Alchemy/Tests/Phrasea/Security/FirewallTest.php @@ -19,7 +19,7 @@ class FirewallTest extends \PhraseanetWebTestCaseAuthenticatedAbstract */ public function testRequiredAuthNotAuthenticated() { - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); self::$DI['app']['firewall']->requireAuthentication(self::$DI['app']); } } diff --git a/tests/classes/PhraseanetPHPUnitAbstract.php b/tests/classes/PhraseanetPHPUnitAbstract.php index 8cbfb1009b..799051a2c8 100644 --- a/tests/classes/PhraseanetPHPUnitAbstract.php +++ b/tests/classes/PhraseanetPHPUnitAbstract.php @@ -1113,6 +1113,14 @@ abstract class PhraseanetPHPUnitAbstract extends WebTestCase { $app['session']->clear(); $app['session']->set('usr_id', self::$DI['user']->get_id()); + $session = new \Entities\Session(); + $session->setUsrId(self::$DI['user']->get_id()); + $session->setUserAgent(''); + self::$DI['app']['EM']->persist($session); + self::$DI['app']['EM']->flush(); + + $app['session']->set('session_id', $session->getId()); + self::$DI['app']['authentication']->reinitUser(); } diff --git a/tests/classes/Session/Session_LoggerTest.php b/tests/classes/Session/Session_LoggerTest.php index bf6aaab598..48b6213f54 100644 --- a/tests/classes/Session/Session_LoggerTest.php +++ b/tests/classes/Session/Session_LoggerTest.php @@ -17,7 +17,7 @@ class Session_LoggerTest extends PhraseanetPHPUnitAbstract { $user = self::$DI['user']; - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $logger_creater = self::$DI['app']['phraseanet.logger']; foreach ($user->ACL()->get_granted_sbas() as $databox) { @@ -52,7 +52,7 @@ class Session_LoggerTest extends PhraseanetPHPUnitAbstract $ses_id = self::$DI['app']['session']->get('session_id'); $usr_id = self::$DI['app']['authentication']->getUser()->get_id(); - self::$DI['app']['authentication']->closeAccount(); + $this->logout(self::$DI['app']); $sql = 'SELECT id FROM log WHERE sit_session = :ses_id AND usrid = :usr_id AND site = :site'; diff --git a/tests/classes/api/v1/api_v1_adapterTest.php b/tests/classes/api/v1/api_v1_adapterTest.php index 7c4cae12c0..3ce6531b57 100644 --- a/tests/classes/api/v1/api_v1_adapterTest.php +++ b/tests/classes/api/v1/api_v1_adapterTest.php @@ -160,7 +160,7 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract public function testSearch_recordsWithRecords() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $record = \record_adapter::createFromFile(BorderFile::buildFromPathfile(__DIR__ . '/../../../files/cestlafete.jpg', self::$DI['collection'], self::$DI['app']), self::$DI['app']); @@ -240,7 +240,7 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract public function testSearch_recordsWithStories() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $story = \record_adapter::createStory(self::$DI['app'], self::$DI['collection']); @@ -272,7 +272,7 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract public function testSearchWithStories() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $story = \record_adapter::createStory(self::$DI['app'], self::$DI['collection']); @@ -307,7 +307,7 @@ class API_V1_adapterTest extends PhraseanetPHPUnitAuthenticatedAbstract public function testSearchWithRecords() { - self::$DI['app']['authentication']->openAccount(self::$DI['user']); + $this->authenticate(self::$DI['app']); $record = \record_adapter::createFromFile(BorderFile::buildFromPathfile(__DIR__ . '/../../../files/cestlafete.jpg', self::$DI['collection'], self::$DI['app']), self::$DI['app']);