Skip to content
Snippets Groups Projects
AuthControllerTest.php 67 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 {
Patrick Barroca's avatar
Patrick Barroca committed
  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' => []];

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



class AuthControllerInviteLoggedTest extends PortailWithOneLoginModuleTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected function _loginHook($account) {
    $account->ROLE = "invite";
    $account->ROLE_LEVEL = 1;
  }

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

  /** @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")]');
  }


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




class AuthControllerAbonneSIGBLoggedTest extends PortailWithOneLoginModuleTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    $this->emprunteur_patrick = Class_WebService_SIGB_Emprunteur::newInstance(5, 'patrick')
      ->empruntsAddAll(array(Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire()
                             ->setDateRetour('23/12/2056'),
Patrick Barroca's avatar
Patrick Barroca committed
                             Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire()
                             ->setDateRetour('3/2/2056'),
Patrick Barroca's avatar
Patrick Barroca committed
                             Class_WebService_SIGB_Emprunt::newInstanceWithEmptyExemplaire()
                             ->setDateRetour('23/1/1999')))
Patrick Barroca's avatar
Patrick Barroca committed
      ->reservationsAddAll(array(Class_WebService_SIGB_Reservation::newInstanceWithEmptyExemplaire()));
Patrick Barroca's avatar
Patrick Barroca committed
    ZendAfi_Auth::getInstance()->logUser(
                                         Class_Users::newInstanceWithId(5,
                                                                        ['login' => 'patrick',
                                                                         'idabon' => 456,
                                                                         'role_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB,
                                                                         'id_site' => 1,
                                                                         'fiche_sigb' => ['fiche' => $this->emprunteur_patrick]]));
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/opac/');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function redirectProfilShouldBeProfil1(){
    $this->assertXPath('//a[contains(@href,"abonne/fiche/id_profil/1")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function linkPretsShouldBePresent() {
    $this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@href, "prets")]', '3');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function linkReservationsShouldBePresent() {
    $this->assertXPath('//div[@id="boite_login"]//a[contains(@href, "reservations")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function linkSeDeconnecterShouldBePresent() {
    $this->assertXPath('//div[@id="boite_login"]//a[contains(@href, "auth/logout")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /**
   * @test
   * @group pagetitles
   */
  public function pageAuthLoginBarreNavShouldBeConnexion() {
    $this->bootstrap();
    $this->dispatch('/opac/auth/login',true);
    $this->assertXPathContentContains('//div[@class="barre_nav"]/span','Connexion');
  }
class AuthControllerAbonneSIGBLoggedLogoutTest
Patrick Barroca's avatar
Patrick Barroca committed
  extends PortailWithOneLoginModuleTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('/opac/auth/logout');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function shouldRedirectToRoot() {
    $this->assertRedirectTo('/');
  }
class AuthControllerWithProfilPageAbonneSIGBLoggedLogoutTest
Patrick Barroca's avatar
Patrick Barroca 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);
  }


  /** @test **/
  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 {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    ZendAfi_Auth::getInstance()->clearIdentity();
  }
class AuthControllerNobodyLoggedActivateTest extends AuthControllerNobodyLoggedTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  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
Patrick Barroca's avatar
Patrick Barroca committed
  extends AuthControllerNobodyLoggedTestCase {
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    Class_AdminVar::getLoader()
      ->newInstanceWithId('INTERDIRE_ENREG_UTIL')
      ->setValeur(0);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/opac/',true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function loginSubmitShouldBePresent(){
    $this->assertXPath('//div[@id="boite_login"]//input[@type="submit"]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function titleShouldBeSeConnecter(){
    $this->assertXPathContentContains('//h1','Se connecter');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test **/
  public function inputLoginShouldSubmitOnKey13Pressed() {
    $this->assertXPath('//div[@id="boite_login"]//input[contains(@onkeypress,"if (event.keyCode == 13) {this.form.submit();return false;}")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function loginLinkShouldBePresent() {
    $this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@onclick, "submit")]',
                                      'please, log me');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function lostPassLinkShouldBePresent() {
    $this->assertXPathContentContains('//div[@id="boite_login"]//tr/td[@colspan="2"]/a[contains(@href, "auth/lostpass")]',
                                      'me rappelle plus');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function registerLinkShouldBePresent() {
    $this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@href, "auth/register")]', "S'enregistrer");
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function shouldAccessRegisterPage() {
    $this->bootstrap();
    $this->dispatch('auth/register', true);
    $this->assertAction('register');
    $this->assertController('auth');
    $this->assertNotRedirect('/');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function inputIdentifiantShouldHavePlaceHolderJJ_MM_AAAA() {
    $this->assertXPath('//input[@name="username"][@placeholder="jj-mm-aaaa"]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function inputPasswordShouldHavePlaceHolder1983() {
    $this->assertXPath('//input[@name="password"][@placeholder="1983"]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  function headShouldContainsAbonnesJS() {
    $this->assertXPath('//head//script[contains(@src,"public/opac/js/abonne.js")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  function headShouldContainsAdminCommonJS() {
    $this->assertXPath('//head//script[contains(@src,"public/admin/js/common.js")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  function headShouldContainsJQuery() {
    $this->assertXPath('//head//script[contains(@src, "jquery")]');
  }
Patrick Barroca's avatar
Patrick Barroca 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 {
Patrick Barroca's avatar
Patrick Barroca committed
  protected
    $_json,
    $_xpath;
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    Class_AdminVar::getLoader()
      ->newInstanceWithId('INTERDIRE_ENREG_UTIL')
      ->setValeur(0);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/opac/auth/popup-login/render/popup', true);
    $this->_xpath = new Storm_Test_XPath();
    $this->_json = json_decode($this->_response->getBody());
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function testLinkLostPassword() {
    $this->_xpath->assertXPath($this->_json->content,
                               '//div//a[contains(@onclick, "getUsername")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function testLinkSenregistrer() {
    $this->_xpath->assertXPath($this->_json->content,
                               '//div//a[contains(@href, "auth/register")]');
Patrick Barroca's avatar
Patrick Barroca committed
    $this->_xpath->assertXPathContentContains(
                                              $this->_json->content,
                                              '//div//a[contains(@href, "auth/register")]',
                                              "S'enregistrer");
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @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

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

Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/opac/');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function testLinkSenregistrerNotHere() {
    $this->assertNotXPath('//div[@id="boite_login"]//a[contains(@href, "auth/register")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function testCannotAccessRegisterPage() {
    $this->dispatch('auth/register');
    $this->assertRedirect('/');
  }
class AuthControllerNobodyLoggedAndNoRegistrationOnLoginPageTest extends AuthControllerNobodyLoggedTestCase {

Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    $interdire_enregistrement = new Class_AdminVar();
    $interdire_enregistrement
      ->setId('INTERDIRE_ENREG_UTIL')
      ->setValeur(1);
    Class_AdminVar::getLoader()->cacheInstance($interdire_enregistrement);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/opac/auth/login');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test **/
  public function linkRegisterShouldNotBeDisplayInLoggingBox() {
    $this->assertNotXPath('//div[@id="boite_login"]//a[contains(@href, "auth/register")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test **/
  public function linkRegisterShouldNotBeDisplayInPageAuthLogin() {
    $this->assertNotXPath('//div[@id="fieldset-login_form"]//a[contains(@href, "auth/register")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function formActionShouldBeLogin() {
    $this->assertXPath('//form[@action="/auth/login"]');
  }
class AuthControllerNobodyLoggedAndNoRegistrationAllowedAjaxLoginTest extends AuthControllerNobodyLoggedTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected
    $_json,
    $_xpath;
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    Class_AdminVar::getLoader()
      ->newInstanceWithId('INTERDIRE_ENREG_UTIL')
      ->setValeur(1);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/opac/auth/popup-login', true);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->_xpath = new Storm_Test_XPath();
    $this->_json = json_decode($this->_response->getBody());
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function testLinkLostPassword() {
    $this->_xpath->assertXPath($this->_json->content,
                               '//div//a[contains(@onclick, "getUsername")]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function testNoLinkSenregistrer() {
    $this->_xpath->assertNotXPath($this->_json->content,
                                  '//div//a[contains(@href, "auth/register")]');
  }
class AuthControllerNobodyLoggedTest extends PortailWithOneLoginModuleTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    ZendAfi_Auth::getInstance()->clearIdentity();
    $this->dispatch('/opac');
  }

  /**
   * @group integration
   * @test
   */
  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"]');
  }

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

class AuthControllerUserIsLoggedTest extends PortailWithOneLoginModuleTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    $marcus = $this->fixture('Class_Users', [
                                             '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 {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    $marcus = $this->fixture('Class_Users',
                             ['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 {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    $marcus = $this->fixture('Class_Users',
                             ['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 {
Patrick Barroca's avatar
Patrick Barroca committed
  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);
  }


  public function testBoiteLoginDisplaysBienvenueSysadm() {
    $this->assertQueryContentContains('#boite_login .welcome',
                                      'Bienvenue sysadm');
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function shouldNotDisplayExpiredSubscription() {
    $this->assertNotXPath('//div[@class="boite login"]//p',
                          $this->_response->getBody());
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function testLinkSeDeconnecter() {
    $this->assertXPath('//div[@id="boite_login"]//a[contains(@href, "auth/logout")]');
    $this->assertXPathContentContains('//div[@id="boite_login"]//a[contains(@href, "auth/logout")]',
                                      'Se déconnecter');
  }
Patrick Barroca's avatar
Patrick Barroca 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')]");
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test **/
  public function lienMonCompteShouldContainsClassAccount() {
    $this->assertXPath('//div[@class="form"]/a[@class="account-link"]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test **/
  public function lienSeDeconnecerShouldContainsClassAccountDisconnect() {
    $this->assertXPath('//div[@class="form"]/a[@class="account-disconnect"]');
  }
llaffont's avatar
llaffont committed
}




class AuthControllerPostTest extends AuthControllerNobodyLoggedTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected $_referer;
Patrick Barroca's avatar
Patrick Barroca committed
  public function setup() {
    parent::setup();
    $this->_referer= BASE_URL.'/cms/viewarticle/12';
    $_SERVER['HTTP_REFERER'] = $this->_referer;
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function loggingWithOutFillingUsername() {
    $this->postDispatch('/opac/auth/boite-login?id_module=4',
                        array('username' => 'My overkill username',
                              'password' => ''),true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function loggingWithOutFillingPassword() {
    $this->postDispatch('/opac/auth/boite-login?id_module=4',
                        array('username' => 'My overkill username',
                              'password' => ''),true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function loggingWithWrongInformation() {
    $this->postDispatch('/opac/auth/boite-login?id_module=4',
                        ['username' => 'foo', 'password' => 'bar'],true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function ajaxLoginWithWrongInformation() {
    $this->postDispatch('/opac/auth/ajax-login?id_module=4',
                        ['username' => 'foo',
                         'password' => 'zoork',
                         'redirect' => '/auth/panier'],true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  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);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function emptyUsernameShouldRedirectToReferer() {
    $this->loggingWithOutFillingUsername();
    $this->assertRedirectTo($this->_referer);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /* @test */
  public function emptyUsernamePostActionShouldDisplayFlashMessengerWithMessageEntrezVotreIdentifiant() {
    $this->loggingWithOutFillingUsername();
    $this->assertFlashMessengerContentContains('Entrez votre identifiant');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function emptyPasswordPostActionShouldDisplayFlashMessengerWithMessageEntrezVotreMotDePasse() {
    $this->loggingWithOutFillingPassword();
    $this->assertFlashMessengerContentContains('Entrez votre mot de passe');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function withAuthenticationFailureFlashMessengerShouldDisplayMessageIdentifiantOuMotDePasseIncorrect(){
    $this->loggingWithWrongInformation();
    $this->assertFlashMessengerContentContains('Identifiant ou mot de passe incorrect');
  }
llaffont's avatar
llaffont committed

Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function invalidAjaxAuthenticationShouldRedirectToActionReferrerWithPopupInFlash() {
    $this->ajaxLoginWithWrongInformation();
    $this->assertRedirectTo($this->_referer);
    $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')]]]);
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function validAuthenticationTitreBoiteShouldBeVousEtesConnecte () {
    $this->loggedAsAdmin();
    $this->assertXPathContentContains('//div[@class="boite login"]//h1','Vous êtes connecté(e)');
  }
abstract class AuthControllerPostSimpleTestCase extends AuthControllerNobodyLoggedTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected $_auth;

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

    ZendAfi_Auth::setInstance($this->_auth);
  }


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

abstract class AuthControllerPostSimpleSuccessfulTestCase extends AuthControllerPostSimpleTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected
    $_web_analytics_client,
    $_marcel;

  public function setUp() {
    parent::setUp();
    $this->group_musicme = $this->fixture('Class_UserGroup',
                                          ['id' => 20,
                                           'libelle' => 'Multimedia'])
                                ->addRight(Class_UserGroup::RIGHT_ACCES_MUSICME);

    $this->_marcel = Storm_Test_ObjectWrapper::on(
      Class_Users::newInstanceWithId(2, ['nom' => 'Marcel','login' =>'foo'])
      ->beAbonneSIGB()
      ->setUserGroups([$this->group_musicme]));

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

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


    $this->_auth
      ->whenCalled('authenticateLoginPassword')
      ->with('foo', 'bar')
      ->willDo(
               function() {
                 $user = new stdClass();
                 $user->ID_USER = $this->_marcel->getId();
                 $this->_auth->whenCalled('getIdentity')->answers($user);
                 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 {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();

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


  /** @test */
  public function responseShouldBeARedirectToOPAC() {
    $this->assertRedirectTo('/opac');
  }


  /** @test */
  public function trackEventShouldBeGenerated() {
    $this->assertTrue($this->_web_analytics_client->methodHasBeenCalledWithParams('trackEvent',
                                                                                  ['authentification',
                                                                                   'connexion',
                                                                                   'utilisateur',
                                                                                   2]));
  }

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



class AuthControllerAjaxLoginPostTest extends AuthControllerPostSimpleSuccessfulTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    $_SERVER['HTTP_REFERER'] = '/recherche/viewnotice';
    $this->postDispatch('/opac/auth/ajax-login',
                        ['username' => 'foo', 'password' => 'bar'], true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function flashMessengerShouldContainMessageDeNotification() {
    $this->assertFlashMessengerContains(
          [ZendAfi_Controller_Action_Helper_FlashMessenger::NOTIFICATION => ['message' => 'Message de notification']]);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function responseShouldRedirectToReferrer() {
    $this->assertRedirectTo('/recherche/viewnotice');
  }
class AuthControllerBoiteLoginPostTest extends AuthControllerPostSimpleSuccessfulTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  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() {
    $this->assertFlashMessengerContains(
                                        [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 {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    $this->postDispatch('/opac/auth/login',
                        ['username' => 'foo',
                         'password' => 'bar',
                         'redirect' => '/opac/paniers']);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function responseShouldRedirectToOpacPaniers() {
    $this->assertRedirectTo('/opac/paniers');
  }
efalcy's avatar
efalcy committed
abstract class AuthControllerLecturaTestCase extends AuthControllerPostSimpleTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected $server_host;
  public function setUp() {
    parent::setUp();
    $this->server_host = $_SERVER['HTTP_HOST'];
    $_SERVER['HTTPS']='on';
    $_SERVER['HTTP_HOST'] = 'test.webuser.lectura.fr';
Patrick Barroca's avatar
Patrick Barroca committed
    $this->fixture('Class_AdminVar', ['id' => 'LECTURA_DOMAIN',
                                      'valeur' => 'test.webuser.lectura.fr']);
    Class_Users::beVolatile();
Patrick Barroca's avatar
Patrick Barroca committed
  }
Patrick Barroca's avatar
Patrick Barroca committed
  public function tearDown() {
    unset($_SERVER['HTTPS']);
    $_SERVER['HTTP_HOST'] = $this->server_host;
    parent::tearDown();
  }
efalcy's avatar
efalcy committed
}


class AuthControllerPostWrongUserFromLecturaClientTest extends AuthControllerLecturaTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();