Skip to content
Snippets Groups Projects
AuthControllerTest.php 59.1 KiB
Newer Older
llaffont's avatar
llaffont committed
<?php
/**
 * Copyright (c) 2012, Agence Française Informatique (AFI). All rights reserved.
 *
Laurent's avatar
Laurent committed
 * BOKEH is free software; you can redistribute it and/or modify
llaffont's avatar
llaffont committed
 * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by
 * the Free Software Foundation.
 *
 * There are special exceptions to the terms and conditions of the AGPL as it
 * is applied to this software (see README file).
 *
Laurent's avatar
Laurent committed
 * BOKEH is distributed in the hope that it will be useful,
llaffont's avatar
llaffont committed
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
 *
 * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE
Laurent's avatar
Laurent committed
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
llaffont's avatar
llaffont committed
 */
require_once 'AbstractControllerTestCase.php';

abstract class PortailWithOneLoginModuleTestCase extends AbstractControllerTestCase {
	public function setUp() {
		parent::setUp();

		Storm_Test_ObjectWrapper::onLoaderOfModel('Class_IntBib')
			->whenCalled('findAllBy')
			->answers([]);

		$cfg_accueil = ['modules' => [4 => ['division' => '4',
																				'id_module' => 4,
																				'type_module' => 'LOGIN',
																				'preferences' => [
																													'identifiant' => 'Numéro de carte',
																													'mot_de_passe'=> 'Année de naissance',
																													'identifiant_exemple' => 'jj-mm-aaaa',
																													'mot_de_passe_exemple' => '1983',
																													'titre_connecte' => 'Vous êtes connecté(e)',
																													'lien_connexion' => 'please, log me',
																													'lien_mot_de_passe_oublie' => 'me rappelle plus',
																													'profil_redirect' => 1,
																													'profil_logout_redirect' => 0]]],
										'options' => []];
llaffont's avatar
llaffont committed

		Class_Profil::getCurrentProfil()
			->setBrowser('opac')
			->setCfgAccueil(ZendAfi_Filters_Serialize::serialize($cfg_accueil));
	}
llaffont's avatar
llaffont committed
}



class AuthControllerInviteLoggedTest extends PortailWithOneLoginModuleTestCase {
	protected function _loginHook($account) {
		$account->ROLE = "invite";
		$account->ROLE_LEVEL = 1;
	}

	public function setUp() {
		parent::setUp();
		$this->dispatch('/opac/',true);
llaffont's avatar
llaffont committed
	}

	/** @test */
	public function noLinkPretsShouldBePresent() {
		$this->assertNotXPath('//div[@id="boite_login"]//a[contains(@href, "prets")]');
	}

	/** @test */
	public function linkSeDeconnecterShouldBePresent() {
		$this->assertXPath('//div[@id="boite_login"]//a[contains(@href, "auth/logout")]');
	}
llaffont's avatar
llaffont committed


	/** @test */
	public function actionIndexShouldRedirectToOpac() {
		$this->bootstrap();
		$this->dispatch('/auth', true);
		$this->assertRedirectTo('/opac');
	}
llaffont's avatar
llaffont committed
}




class AuthControllerAbonneSIGBLoggedTest extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();

llaffont's avatar
llaffont committed
		$this->emprunteur_patrick = Class_WebService_SIGB_Emprunteur::newInstance(5, 'patrick')
			->empruntsAddAll(array(Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire()
														 ->setDateRetour('23/12/2056'),

														 Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire()
														 ->setDateRetour('3/2/2056'),

														 Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire()
														 ->setDateRetour('23/1/1999')))
llaffont's avatar
llaffont committed
			->reservationsAddAll(array(Class_WebService_SIGB_Reservation::newInstanceWithEmptyExemplaire()));

		ZendAfi_Auth::getInstance()->logUser(
Ghislain Loas's avatar
Ghislain Loas committed
																				 Class_Users::newInstanceWithId(5,
																																				['login' => 'patrick',
																																				 'idabon' => 456,
																																				 'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
																																				 'id_site' => 1,
																																				 'fiche_sigb' => ['fiche' => $this->emprunteur_patrick]]));
llaffont's avatar
llaffont committed

		$this->dispatch('/opac/');
	}


	/** @test */
	public function redirectProfilShouldBeProfil1(){
		$this->assertXPath('//a[contains(@href,"abonne/fiche/id_profil/1")]');
llaffont's avatar
llaffont committed
	/** @test */
	public function linkPretsShouldBePresent() {
		$this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@href, "prets")]', '3');
	}


	/** @test */
	public function linkReservationsShouldBePresent() {
		$this->assertXPath('//div[@id="boite_login"]//a[contains(@href, "reservations")]');
	}


	/** @test */
	public function linkSeDeconnecterShouldBePresent() {
		$this->assertXPath('//div[@id="boite_login"]//a[contains(@href, "auth/logout")]');
	}
llaffont's avatar
llaffont committed
	 * @group pagetitles
llaffont's avatar
llaffont committed
	 */
	public function pageAuthLoginBarreNavShouldBeConnexion() {
		$this->bootstrap();
		$this->dispatch('/opac/auth/login',true);
	  $this->assertXPathContentContains('//div[@class="barre_nav"]/span','Connexion');
class AuthControllerAbonneSIGBLoggedLogoutTest
Ghislain Loas's avatar
Ghislain Loas committed
	extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();
		$this->dispatch('/opac/auth/logout');
	}

	public function shouldRedirectToRoot() {
		$this->assertRedirectTo('/');
	}
}


class AuthControllerWithProfilPageAbonneSIGBLoggedLogoutTest
Ghislain Loas's avatar
Ghislain Loas committed
	extends PortailWithOneLoginModuleTestCase {
	protected $_profile, $_parent_profile;

	public function setUp() {
		parent::setUp();
		$this->_parent_profile = Class_Profil::getCurrentProfil();
		$this->_profile = $this->fixture('Class_Profil',
																		 ['id' => 22,
																			'browser' => 'opac',
																			'libelle' => 'Profil Adulte',
																			'hauteur_banniere' => 150,
																			'couleur_texte_bandeau' => '#F2C',
																			'couleur_lien_bandeau' => '#234',
																			'menu_haut_on' => true,
																			'cfg_menus' => [],
																			'commentaire' => 'Super bib',
																			'ref_tags' => 'bib,Adulte',
																			'parent_profil' => $this->_parent_profile]);
		Class_Profil::setCurrentProfil($this->_profile);
	public function withoutLogoutProfileShouldRedirectToParentProfile() {
		$this->dispatch('/opac/auth/logout');
		$this->assertRedirectTo('/opac/index/index/id_profil/2');
	}

	/** @test **/
	public function withLogoutProfile6InParentShouldRedirectToIt() {
		$cfg_accueil = $this->_parent_profile->getCfgAccueilAsArray();
		$cfg_accueil['modules'][4]['preferences']['profil_logout_redirect'] = 6;
		$this->_parent_profile
			->setCfgAccueil(ZendAfi_Filters_Serialize::serialize($cfg_accueil));

		$this->dispatch('/opac/auth/logout');
		$this->assertRedirectTo('/opac/index/index/id_profil/6');
llaffont's avatar
llaffont committed
abstract class AuthControllerNobodyLoggedTestCase extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();
		ZendAfi_Auth::getInstance()->clearIdentity();
class AuthControllerNobodyLoggedActivateTest extends AuthControllerNobodyLoggedTestCase {
	const ERROR_MESSAGE = 'Un probleme est survenu lors de l\'activation';
	const OK_MESSAGE = 'Ok, compte cree';
	public function setUp() {
		parent::setUp();
		Class_UsersNonValid::beVolatile();
		Class_Users::beVolatile();
		$this->fixture('Class_AdminVar', ['id' => 'USER_NON_VALIDATED',
																			'valeur' => self::ERROR_MESSAGE]);
		$this->fixture('Class_AdminVar', ['id' => 'USER_VALIDATED',
																			'valeur' => self::OK_MESSAGE]);
	}

	/** @test */
	public function withoutKeyShouldRedirect() {
		$this->dispatch('/opac/auth/activeuser', true);
		$this->assertRedirect();
	}

	/** @test */
	public function withUnknownKeyShouldDisplayError() {
		$this->dispatch('/opac/auth/activeuser/c/666', true);
		$this->assertXPathContentContains('//div', self::ERROR_MESSAGE);
	}

	/** @test */
	public function withKnownKeyShouldCreateUserAndDeleteNonValid() {
		$this->fixture('Class_UsersNonValid', ['id' => 12,
																					 'cle' => '777',
																					 'login' => 'harlock',
																					 'mail' => 'harlock@afi-sa.fr',
																					 'password' => 'cosmos']);
		$this->dispatch('/opac/auth/activeuser/c/777', true);
		$this->assertXPathContentContains('//div', self::OK_MESSAGE);
		// non valid deleted
		$this->assertEquals(0, Class_UsersNonValid::countBy([]));
		// user created
		$created = Class_Users::findFirstBy(['order' => 'id desc']);
		$this->assertEquals('harlock@afi-sa.fr', $created->getMail());
	}
}
class AuthControllerNobodyLoggedAndRegistrationAllowedBoiteLoginTest
Ghislain Loas's avatar
Ghislain Loas committed
	extends AuthControllerNobodyLoggedTestCase {
llaffont's avatar
llaffont committed
	public function setUp() {
		parent::setUp();

		Class_AdminVar::getLoader()
			->newInstanceWithId('INTERDIRE_ENREG_UTIL')
llaffont's avatar
llaffont committed
			->setValeur(0);

		$this->dispatch('/opac/',true);
	public function loginSubmitShouldBePresent(){
		$this->assertXPath('//div[@id="boite_login"]//input[@type="submit"]');
	public function titleShouldBeSeConnecter(){
		$this->assertXPathContentContains('//h1','Se connecter');
	public function inputLoginShouldSubmitOnKey13Pressed() {
		$this->assertXPath('//div[@id="boite_login"]//input[contains(@onkeypress,"if (event.keyCode == 13) {this.form.submit();return false;}")]');
	}

	/** @test */
	public function loginLinkShouldBePresent() {
llaffont's avatar
llaffont committed
		$this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@onclick, "submit")]',

	/** @test */
	public function lostPassLinkShouldBePresent() {
		$this->assertXPathContentContains('//div[@id="boite_login"]//tr/td[@colspan="2"]/a[contains(@href, "auth/lostpass")]',

	/** @test */
	public function registerLinkShouldBePresent() {
		$this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@href, "auth/register")]', "S'enregistrer");
	/** @test */
	public function shouldAccessRegisterPage() {
		$this->bootstrap();
		$this->dispatch('auth/register', true);
llaffont's avatar
llaffont committed
		$this->assertAction('register');
		$this->assertController('auth');
		$this->assertNotRedirect('/');
	}


	/** @test */
	public function inputIdentifiantShouldHavePlaceHolderJJ_MM_AAAA() {
		$this->assertXPath('//input[@name="username"][@placeholder="jj-mm-aaaa"]');
	}


	/** @test */
	public function inputPasswordShouldHavePlaceHolder1983() {
		$this->assertXPath('//input[@name="password"][@placeholder="1983"]');
	}


llaffont's avatar
llaffont committed
	/** @test */
	function headShouldContainsAbonnesJS() {
		$this->assertXPath('//head//script[contains(@src,"public/opac/js/abonne.js")]');
llaffont's avatar
llaffont committed
	}


	/** @test */
	function headShouldContainsAdminCommonJS() {
		$this->assertXPath('//head//script[contains(@src,"public/admin/js/common.js")]');
	}


	/** @test */
	function headShouldContainsJQuery() {
		$this->assertXPath('//head//script[contains(@src, "jquery")]');
	}
Ghislain Loas's avatar
Ghislain Loas committed


	/** @test */
	public function globalCssShouldBeLoadedAfterJQueryUiCss() {
		$pos_jquery = strpos($this->_response->getBody(), 'js/jquery_ui/css');
		$pos_global = strpos($this->_response->getBody(), 'css/global.css');
		$this->assertTrue(($pos_jquery < $pos_global) && ($pos_jquery > 0));
	}
class AuthControllerNobodyLoggedAndRegistrationAllowedAjaxLoginTest extends AuthControllerNobodyLoggedTestCase {
	public function setUp() {
		parent::setUp();

		Class_AdminVar::getLoader()
			->newInstanceWithId('INTERDIRE_ENREG_UTIL')
			->setValeur(0);

		$this->dispatch('/opac/auth/popup-login/render/popup', true);
		$this->_xpath = new Storm_Test_XPath();
		$this->_json = json_decode($this->_response->getBody());
		$this->_xpath->assertXPath($this->_json->content,
															 '//div//a[contains(@onclick, "getUsername")]');
	public function testLinkSenregistrer() {
		$this->_xpath->assertXPath($this->_json->content,
															 '//div//a[contains(@href, "auth/register")]');

		$this->_xpath->assertXPathContentContains(
Ghislain Loas's avatar
Ghislain Loas committed
																							$this->_json->content,
																							'//div//a[contains(@href, "auth/register")]',
																							"S'enregistrer");


	/** @test */
	public function noLinkShouldHaveRenderPopup() {
		$this->_xpath->assertNotXPath($this->_json->content,
																	'//div//a[contains(@href, "render/popup")]');
	}
llaffont's avatar
llaffont committed
class AuthControllerNobodyLoggedAndNoRegistrationTest extends AuthControllerNobodyLoggedTestCase {
efalcy's avatar
efalcy committed

llaffont's avatar
llaffont committed
	public function setUp() {
efalcy's avatar
efalcy committed
		parent::setUp();
llaffont's avatar
llaffont committed
		$interdire_enregistrement = new Class_AdminVar();
		$interdire_enregistrement
			->setId('INTERDIRE_ENREG_UTIL')
			->setValeur(1);
		Class_AdminVar::getLoader()->cacheInstance($interdire_enregistrement);

efalcy's avatar
efalcy committed

llaffont's avatar
llaffont committed
		$this->dispatch('/opac/');
	}


	public function testLinkSenregistrerNotHere() {
		$this->assertNotXPath('//div[@id="boite_login"]//a[contains(@href, "auth/register")]');
	}


	public function testCannotAccessRegisterPage() {
		$this->dispatch('auth/register');
		$this->assertRedirect('/');
	}
}



class AuthControllerNobodyLoggedAndNoRegistrationOnLoginPageTest extends AuthControllerNobodyLoggedTestCase {

	public function setUp() {
		parent::setUp();
		$interdire_enregistrement = new Class_AdminVar();
		$interdire_enregistrement
			->setId('INTERDIRE_ENREG_UTIL')
			->setValeur(1);
		Class_AdminVar::getLoader()->cacheInstance($interdire_enregistrement);


		$this->dispatch('/opac/auth/login');
	}

	/** @test **/
	public function linkRegisterShouldNotBeDisplayInLoggingBox() {
		$this->assertNotXPath('//div[@id="boite_login"]//a[contains(@href, "auth/register")]');
	}


	/** @test **/
	public function linkRegisterShouldNotBeDisplayInPageAuthLogin() {
		$this->assertNotXPath('//div[@id="fieldset-login_form"]//a[contains(@href, "auth/register")]');


	/** @test */
	public function formActionShouldBeLogin() {
		$this->assertXPath('//form[@action="/auth/login"]');
	}
class AuthControllerNobodyLoggedAndNoRegistrationAllowedAjaxLoginTest extends AuthControllerNobodyLoggedTestCase {
	public function setUp() {
		parent::setUp();

		Class_AdminVar::getLoader()
			->newInstanceWithId('INTERDIRE_ENREG_UTIL')
			->setValeur(1);

		$this->dispatch('/opac/auth/popup-login', true);

		$this->_xpath = new Storm_Test_XPath();
		$this->_json = json_decode($this->_response->getBody());
		$this->_xpath->assertXPath($this->_json->content,
															 '//div//a[contains(@onclick, "getUsername")]');
	public function testNoLinkSenregistrer() {
		$this->_xpath->assertNotXPath($this->_json->content,
																	'//div//a[contains(@href, "auth/register")]');
class AuthControllerNobodyLoggedTest extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();
		ZendAfi_Auth::getInstance()->clearIdentity();
		$this->dispatch('/opac');
	 */
	public function pageShouldBeHtml5Valid() {
		$this->assertHTML5($this->_response->getBody());
	}


	/** @test */
	public function pageShouldNotContainsRedirectInput() {
		$this->assertNotXPath('//input[@name="redirect"]');
	}


	/** @test */
	public function formActionShouldBeBoiteLogin() {
		$this->assertXPath('//div[@id="boite_login"]//form[@action="/auth/boite-login/id_module/4"]');
	}

	/** @test */
	public function withRedirectParamPageShouldContainsRedirectInput() {
		$this->bootstrap();
		$this->dispatch('/opac/auth/login/redirect/'.urlencode('/opac/paniers'));
		$this->assertXPath('//input[@name="redirect"][@value="/opac/paniers"]');
	}
	public function pageShouldNotContainsAjoutPanierInput() {
		$this->assertNotXPath('//input[@name="ajoutPanier"]');
	}
class AuthControllerNumilogConnectedUserDeconnexionTest extends PortailWithOneLoginModuleTestCase {
		$this->dispatch('/opac/auth/login?service=http://numilog?deconnexion=ok', true);
	 */
	public function pageShouldBeRedirectedToHome() {
		$this->assertRedirectTo('/opac');
class AuthControllerNoLoggedTest extends AuthControllerNobodyLoggedTestCase {
	public function pageAuthLoginWithServiceShouldIncludeHiddenService() {
		$this->dispatch('/opac/auth/login?service=http://monurlservice',true);
		$this->assertXPath('//input[@type="hidden"][@value="http://monurlservice"]');

class AuthControllerUserIsLoggedTest extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();
		$marcus = $this->fixture('Class_Users', [
Ghislain Loas's avatar
Ghislain Loas committed
																						 'id' => 1,
																						 'pseudo' => '',
																						 'login' => 'sysadm',
																						 'password' => 'sysadmPassword',
																						 'nom' => 'Miller',
																						 'prenom' => 'Marcus']);
		ZendAfi_Auth::getInstance()->logUser($marcus);
		$this->dispatch('/opac/auth/login',true);
	}

	/** @test */
	public function bienvenueMarcusMillerShouldBePresent() {
		$this->assertXPath('//div//div[@class="welcome"][contains(text(),"Bienvenue Marcus")]//span[@data-name="last-name"][contains(text(), "Miller")]');
class AuthControllerUserNameIsEscaped extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();
		$marcus = $this->fixture('Class_Users',
Ghislain Loas's avatar
Ghislain Loas committed
														 ['id' => 1,
															'pseudo' => '',
															'login' => 'sysadm',
															'password' => 'sysadmPassword',
															'nom' => '<script>Miller',
															'prenom' => '<script>Marcus']);
		ZendAfi_Auth::getInstance()->logUser($marcus);
		$this->dispatch('/opac/auth/login',true);
	}

	/** @test */
	public function userNameShouldBeEscaped() {
		$this->assertXPathContentContains("//div[@class='welcome']", "Bienvenue &lt;script&gt;Marcus");
	}

	/** @test */
	public function userSurNameShouldBeEscaped() {
		$this->assertXPathContentContains("//span[@data-name='last-name']", "&lt;script&gt;Miller");
	}
}

class AuthControllerPseudoIsEscaped extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();
		$marcus = $this->fixture('Class_Users',
Ghislain Loas's avatar
Ghislain Loas committed
														 ['id' => 1,
															'pseudo' => '<script>pseudo',
															'login' => 'sysadm',
															'password' => 'sysadmPassword',
															'nom' => 'Miller',
															'prenom' => 'Marcus']);
		ZendAfi_Auth::getInstance()->logUser($marcus);
		$this->dispatch('/opac/auth/login',true);
	}

	/** @test */
	public function userPseudoShouldBeEscaped() {
		$this->assertXPathContentContains("//div[@class='welcome']", "Bienvenue &lt;script&gt;pseudo");
	}
}
llaffont's avatar
llaffont committed
class AuthControllerAdminIsLoggedTest extends PortailWithOneLoginModuleTestCase {
	public function setUp() {
		parent::setUp();
		$this->sysadm = $this->fixture('Class_Users',
																	 ['id' => 1,
																		'pseudo' => 'sysadm',
																		'role_level' => ZendAfi_Acl_AdminControllerRoles::ADMIN_PORTAIL,
																		'role' => 'super_admin',
																		'login' => 'sysadm',
																		'password' => 'sysadmPassword',
																		'id_site' => 1]);
		ZendAfi_Auth::getInstance()->logUser($this->sysadm);
		$this->dispatch('/opac/auth/login',true);
llaffont's avatar
llaffont committed
	}


	public function testBoiteLoginDisplaysBienvenueSysadm() {
		$this->assertQueryContentContains('#boite_login .welcome', 'Bienvenue sysadm');

	/** @test */
	public function shouldNotDisplayExpiredSubscription() {
		$this->assertNotXPath('//div[@class="boite login"]//p');
	}

llaffont's avatar
llaffont committed
	public function testLinkSeDeconnecter() {
		$this->assertXPath('//div[@id="boite_login"]//a[contains(@href, "auth/logout")]');
llaffont's avatar
llaffont committed
		$this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@href, "auth/logout")]',
																			'Se déconnecter');
llaffont's avatar
llaffont committed
	}


	public function testLinkFonctionAdmin() {
		$this->assertXPath("//div[@class='configuration_module']//img[contains(@onclick,'admin/modules/auth?config=site&type_module=auth&id_profil=2&action1=login')]");
	}


	/** @test **/
	public function lienMonCompteShouldContainsClassAccount() {
		$this->assertXPath('//div[@class="form"]/a[@class="account-link"]');
	}
	public function lienSeDeconnecerShouldContainsClassAccountDisconnect() {
		$this->assertXPath('//div[@class="form"]/a[@class="account-disconnect"]');
llaffont's avatar
llaffont committed
}




class AuthControllerPostTest extends AuthControllerNobodyLoggedTestCase {
	protected $_referer;
	public function setup() {
		parent::setup();
		$this->_referer= BASE_URL.'/cms/viewarticle/12';
	  $_SERVER['HTTP_REFERER'] = $this->_referer;
	}
	public function loggingWithOutFillingUsername() {
		$this->postDispatch('/opac/auth/boite-login?id_module=4',
llaffont's avatar
llaffont committed
												array('username' => 'My overkill username',
															'password' => ''),true);
	public function loggingWithOutFillingPassword() {
		$this->postDispatch('/opac/auth/boite-login?id_module=4',
												array('username' => 'My overkill username',
															'password' => ''),true);
	}
	public function loggingWithWrongInformation() {
		$this->postDispatch('/opac/auth/boite-login?id_module=4',
												['username' => 'foo', 'password' => 'bar'],true);
	}

	public function ajaxLoginWithWrongInformation() {
		$this->postDispatch('/opac/auth/ajax-login?id_module=4',
												['username' => 'foo',
												 'password' => 'zoork',
												 'redirect' => '/auth/panier'],true);
	public function loggedAsAdmin() {
		$admin = Class_Users::newInstanceWithId(34, ['login' => 'admin'])->beAdminPortail();
		ZendAfi_Auth::getInstance()->logUser($admin);
		$this->dispatch('/opac/auth/boite-login?id_module=4',true);
	}

	/** @test */
	public function emptyUsernameShouldRedirectToReferer() {
	  $this->loggingWithOutFillingUsername();
		$this->assertRedirectTo($this->_referer);
	}

	/* @test */
	public function emptyUsernamePostActionShouldDisplayFlashMessengerWithMessageEntrezVotreIdentifiant() {
		$this->loggingWithOutFillingUsername();
		$this->assertFlashMessengerContentContains('Entrez votre identifiant');
	}

	/** @test */
	public function emptyPasswordPostActionShouldDisplayFlashMessengerWithMessageEntrezVotreMotDePasse() {
		$this->loggingWithOutFillingPassword();
		$this->assertFlashMessengerContentContains('Entrez votre mot de passe');
	}


	/** @test */
	public function withAuthenticationFailureFlashMessengerShouldDisplayMessageIdentifiantOuMotDePasseIncorrect(){
		$this->loggingWithWrongInformation();
		$this->assertFlashMessengerContentContains('Identifiant ou mot de passe incorrect');
	}

llaffont's avatar
llaffont committed

	/** @test */
	public function invalidAjaxAuthenticationShouldRedirectToActionReferrerWithPopupInFlash()	{
		$this->ajaxLoginWithWrongInformation();
		$this->assertRedirectTo($this->_referer);
Alex Arnaud's avatar
Alex Arnaud committed
		$this->assertFlashMessengerEquals([[ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => ['message' => 'Identifiant ou mot de passe incorrect.']],
																			 [ZendAfi_Controller_Action_Helper_FlashMessenger::POPUP => ['url' => '/auth/popup-login?redirect='.urlencode('/auth/panier')]]]);
	/** @test */
	public function validAuthenticationTitreBoiteShouldBeVousEtesConnecte () {
		$this->loggedAsAdmin();
		$this->assertXPathContentContains('//div[@class="boite login"]//h1','Vous êtes connecté(e)');
abstract class AuthControllerPostSimpleTestCase extends AuthControllerNobodyLoggedTestCase {
llaffont's avatar
llaffont committed
	protected $_auth;

	public function setUp() {
		parent::setUp();
llaffont's avatar
llaffont committed
		$this->_auth = Storm_Test_ObjectWrapper::mock()
			->whenCalled('authenticateLoginPassword')
			->answers(false)
			->whenCalled('hasIdentity')
			->answers(false)
			->whenCalled('getIdentity')
			->answers(null);
llaffont's avatar
llaffont committed
		ZendAfi_Auth::setInstance($this->_auth);

	public function tearDown() {
		ZendAfi_Auth::setInstance(null);
		parent::tearDown();
	}
}




abstract class AuthControllerPostSimpleSuccessfulTestCase extends AuthControllerPostSimpleTestCase {
	protected
		$_web_analytics_client,
		$_marcel;
	public function setUp() {
		parent::setUp();
Laurent's avatar
Laurent committed
		$this->group_musicme = $this->fixture('Class_UserGroup',
Laurent's avatar
Laurent committed
																					 'libelle' => 'Multimedia'])
Ghislain Loas's avatar
Ghislain Loas committed
																->addRight(Class_UserGroup::RIGHT_ACCES_MUSICME);
		$this->_marcel = Storm_Test_ObjectWrapper::on(
			Class_Users::newInstanceWithId(2, ['nom' => 'Marcel','login' =>'foo'])
			->setUserGroups([$this->group_musicme]));

		Class_Users::cacheInstance($this->_marcel);

		$this->_marcel
			->whenCalled('registerNotificationsOn')
			->willDo(function($notifiable) {
				$notifiable->notify('Message de notification');
			});

llaffont's avatar
llaffont committed
		$this->_auth
			->whenCalled('authenticateLoginPassword')
			->with('foo', 'bar')
							 function() {
								 $user = new stdClass();
								 $user->ID_USER = $this->_marcel->getId();
								 $this->_auth->whenCalled('getIdentity')->answers($user);
Ghislain Loas's avatar
Ghislain Loas committed
								 return true;
							 });

		$this->_web_analytics_client = Storm_Test_ObjectWrapper::mock()
			->whenCalled('trackEvent')
			->answers(true);
		ZendAfi_Controller_Action_Helper_TrackEvent::setDefaultWebAnalyticsClient($this->_web_analytics_client);
	}
}




class AuthControllerPostSimpleSuccessfulTest extends AuthControllerPostSimpleSuccessfulTestCase {
	public function setUp() {
		parent::setUp();
llaffont's avatar
llaffont committed
		$this->postDispatch('/opac/auth/login',
												['username' => 'foo', 'password' => 'bar'],true);
	public function responseShouldBeARedirectToOPAC() {
		$this->assertRedirectTo('/opac');
llaffont's avatar
llaffont committed
	}
	/** @test */
	public function trackEventShouldBeGenerated() {
		$this->assertTrue($this->_web_analytics_client->methodHasBeenCalledWithParams('trackEvent',
																																									['authentification',
																																									 'connexion',
																																									 'utilisateur',

	/** @test */
	public function flasMessengerShouldContainMessageDeNotification() {
Alex Arnaud's avatar
Alex Arnaud committed
		$this->assertFlashMessengerContains([ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => ['message' => 'Message de notification',
																																																					 'display' => 'popup']]);
	}
}



class AuthControllerAjaxLoginPostTest extends AuthControllerPostSimpleSuccessfulTestCase {
	public function setUp() {
		parent::setUp();
		$_SERVER['HTTP_REFERER'] = '/recherche/viewnotice';
		$this->postDispatch('/opac/auth/ajax-login',
Alex Arnaud's avatar
Alex Arnaud committed
												['username' => 'foo', 'password' => 'bar'], true);
	}


	/** @test */
	public function flashMessengerShouldContainMessageDeNotification() {
Alex Arnaud's avatar
Alex Arnaud committed
		$this->assertFlashMessengerContains(
					[ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => ['message' => 'Message de notification']]);
	/** @test */
	public function responseShouldRedirectToReferrer() {
		$this->assertRedirectTo('/recherche/viewnotice');
	}
class AuthControllerBoiteLoginPostTest extends AuthControllerPostSimpleSuccessfulTestCase {
	public function setUp() {
		parent::setUp();
		$_SERVER['HTTP_REFERER'] = '/recherche/viewnotice';
		$this->postDispatch('/opac/auth/boite-login',
												['username' => 'foo', 'password' => 'bar'],true);
	}


	/** @test */
	public function flashMessengerShouldContainMessageDeNotification() {
Alex Arnaud's avatar
Alex Arnaud committed
		$this->assertFlashMessengerContains(
Alex Arnaud's avatar
Alex Arnaud committed
																				[ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => ['message' => 'Message de notification',
																																																					 'display' => ZendAfi_Controller_Action_Helper_FlashMessenger::POPUP]]);
	}

	/** @test */
	public function responseShouldRedirectToReferrer() {
		$this->assertRedirectTo('/recherche/viewnotice');
	}
}


class AuthControllerPostSimpleSuccessfulWithRedirectTest extends AuthControllerPostSimpleSuccessfulTestCase {
	public function setUp() {
		parent::setUp();

		$this->postDispatch('/opac/auth/login',
												['username' => 'foo',
												 'password' => 'bar',
												 'redirect' => '/opac/paniers']);
	}


	/** @test */
	public function responseShouldRedirectToOpacPaniers() {
		$this->assertRedirectTo('/opac/paniers');
efalcy's avatar
efalcy committed
abstract class AuthControllerLecturaTestCase extends AuthControllerPostSimpleTestCase {
efalcy's avatar
efalcy committed
	protected $server_host;
	public function setUp() {
		parent::setUp();
efalcy's avatar
efalcy committed
		$this->server_host = $_SERVER['HTTP_HOST'];
		$_SERVER['HTTPS']='on';
		$_SERVER['HTTP_HOST'] = 'test.webuser.lectura.fr';

		$this->fixture('Class_AdminVar', ['id' => 'LECTURA_DOMAIN',
																			'valeur' => 'test.webuser.lectura.fr']);
		Class_Users::beVolatile();
efalcy's avatar
efalcy committed

	public function tearDown() {
		unset($_SERVER['HTTPS']);
		$_SERVER['HTTP_HOST'] = $this->server_host;
		parent::tearDown();
	}


efalcy's avatar
efalcy committed
}


class AuthControllerPostWrongUserFromLecturaClientTest extends AuthControllerLecturaTestCase {
	public function setUp() {
		parent::setUp();
		$this->postDispatch('/',
												['CAB' => '0007',