Skip to content
Snippets Groups Projects
IndexControllerTest.php 10.9 KiB
Newer Older
<?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
 * 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,
 * 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
class IndexControllerSetupDomainTest extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    Class_Adminvar::beVolatile();
  }


  /** @test */
  public function accessingIndexShouldSetupNomDomain() {
    Class_Url::setForbidenUrls(['opac3']);
    $this->dispatch('/');
    $this->assertEquals('http://localhost/usr/local/bin/phpunit', Class_AdminVar::get('NOM_DOMAINE'));
  }


  /** @test */
  public function withNomDomainAccessingIndexShouldNotSetupNomDomain() {
    $this->fixture('Class_AdminVar',
                   ['id' => 'NOM_DOMAINE',
                    'clef' => 'NOM_DOMAINE',
                    'valeur' => 'http://bokeh-library-protal.org']);
    $this->dispatch('/');
    $this->assertEquals('http://bokeh-library-protal.org', Class_Url::siteUrl());
  }


  /** @test */
  public function setLocalhostAsForbidenUrlNomDomainShouldNotBeSet() {
    Class_Url::setForbidenUrls(['localhost']);
    $this->dispatch('/');
    $this->assertEquals('', Class_AdminVar::get('NOM_DOMAINE'));
  }
llaffont's avatar
llaffont committed
class IndexControllerAsInviteTest extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected function _loginHook($account) {
    $account->ROLE_LEVEL = ZendAfi_Acl_AdminControllerRoles::INVITE;
  }
llaffont's avatar
llaffont committed

Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function withOnlyParamQShouldRedirectToRecherche() {
    $this->dispatch('index/index?q=test');
    $this->assertRedirectTo('/recherche?q=test');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function cssEditorShouldNotBeLoaded() {
    $this->dispatch('/');
    $this->assertNotXPathContentContains('//script', 'AFI-OPAC.js');
  }
llaffont's avatar
llaffont committed

Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function linkToConfigurePageShouldNotBeInFooter() {
    Class_Profil::newInstanceWithId(1, ['cfg_menus' => '']);
    $this->dispatch('/?id_profil=1');
    $this->assertNotXPath('//a[contains(@href, "admin/profil/accueil/id_profil/1")]');
  }
abstract class IndexControllerAsAdminTestCase extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected function _loginHook($account) {
    $account->ROLE_LEVEL = ZendAfi_Acl_AdminControllerRoles::ADMIN_PORTAIL;
  }
llaffont's avatar
llaffont committed

Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    Class_Profil::newInstanceWithId(1, ['header_css' => 'mycss.css']);
  }
class IndexControllerAsAdminWithCssEditorAndNoHeaderCss extends IndexControllerAsAdminTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    Class_Profil::find(1)->setHeaderCss('');
    $this->dispatch('/?id_profil=1');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function headerCssShouldNotBeIncluded() {
    $this->assertNotXPath('//link[@id="profil_css"]');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function linkToConfigurePageShouldBeInFooter() {
    $this->assertXPath('//div[@class="footer"]//a[@class="edit_profil"][contains(@href, "admin/profil/accueil/id_profil/1")]');
  }


class IndexControllerAsAdminWithCSSEditorTest extends IndexControllerAsAdminTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/?id_profil=1');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function cssEditorShouldBeLoaded() {
    $this->assertXPathContentContains('//script', 'AFI-OPAC.js');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function headerCssShouldBeMyCssWithCacheWorkaround() {
    $this->assertXPath('//link[contains(@href, "mycss.css")][contains(@href, "?cache=")]');
  }
class IndexControllerWithInvitedLevelRestrictionForProfilTest extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setup() {
    parent::setup();
    ZendAfi_Auth::getInstance()->clearIdentity();
    Class_Profil::newInstanceWithId(1, ['access_level' => '0' ,'cfg_menus' => '']);
  }


  /** @test **/
  public function anonymousAccessingProfilWithAccessLevelShouldBeRedirectedToOpacLogin() {
    $this->dispatch('/opac/index/index/id_profil/1');
    $this->assertRedirectRegex('|^'.BASE_URL.'/auth/login/id_profil/1|');
  }



class IndexControllerWithLoggedUserTest extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    ZendAfi_Auth::getInstance()->logUser($this->fixture('Class_Users',
                                                        ['id' => 1,
                                                         'login' => 'tom',
                                                         'password' => 'ok']));
    $this->dispatch('index/index', true);
  }


  /** @test */
  public function bodyShouldHaveDataLoggedTrue() {
    $this->assertXPath('//body[@data-logged="true"]');
  }



class IndexControllerAsPhoneWithPhoneProfilTest extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    $_SERVER['HTTP_USER_AGENT'] = 'iphone';
    $this->fixture('Class_Profil',
                   ['id' => 9999,
                    'browser' => 'telephone']);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/opac', true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function messageToRedirectToPhoneModeShouldBePresent() {
    $this->assertXPath('//div[@class="back_to_phone"]');
  }
}



class IndexControllerAsPhoneWithNoPhoneProfilTest extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
    $_SERVER['HTTP_USER_AGENT'] = 'iphone';
    $this->dispatch('/opac', true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function messageToRedirectToPhoneModeShouldBePresent() {
    $this->assertNotXPath('//div[@class="back_to_phone"]');
  }


class IndexControllerSitemapTest extends AbstractControllerTestCase {
Patrick Barroca's avatar
Patrick Barroca committed
  protected $_file_system;
  protected $_sitemap;
Patrick Barroca's avatar
Patrick Barroca committed
  public function setUp() {
    parent::setUp();
Patrick Barroca's avatar
Patrick Barroca committed
    $this->_sitemap = '<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
  <url>
    <loc>'.BASE_URL.'?id_profil=1</loc>
    <priority>1.0</priority>
  </url>
  <url>
    <loc>'.BASE_URL.'?id_profil=2</loc>
    <priority>0.5</priority>
  </url>
</urlset>
';

Patrick Barroca's avatar
Patrick Barroca committed
    $this->_file_system = $this->mock()
      ->whenCalled('file_exists')->answers(true)
      ->whenCalled('file_get_contents')->answers($this->_sitemap);
    Class_Sitemap::setFileSystem($this->_file_system);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->dispatch('/sitemap.xml', true);
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function contentShouldBeXml() {
    $this->assertHeaderContains('Content-Type', 'text/xml; charset=utf-8');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function withNoSitemapShouldRedirectToIndex() {
    $this->_file_system = $this->mock()
      ->whenCalled('file_exists')->answers(false);
    Class_Sitemap::setFileSystem($this->_file_system);
    $this->dispatch('/sitemap.xml', true);
Patrick Barroca's avatar
Patrick Barroca committed
    $this->assertRedirectTo('/index');
  }
Patrick Barroca's avatar
Patrick Barroca committed
  /** @test */
  public function contentShouldBeAsExpected() {
    $this->assertEquals($this->_response->getBody(), $this->_sitemap);
  }


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

    ZendAfi_Auth::getInstance()->logUser($this->fixture('Class_Users',
                                                        ['id' => 1,
                                                         'login' => 'guest',
                                                         'password' => 'guest',
                                                         'role_level' => ZendAfi_Acl_AdminControllerRoles::INVITE]));
    $this->fixture('Class_Profil', ['id' => 345,
                                    'libelle' => 'Zork - Main',
                                    'parent_id' => null,
                                    'rewrite_url' => 'zork',
                                    'access_level' => ZendAfi_Acl_AdminControllerRoles::ABONNE_SIGB]);

    $this->fixture('Class_Profil', ['id' => 879,
                                    'libelle' => 'Zork - Child',
                                    'parent_id' => 345,
                                    'rewrite_url' => 'zork-child']);
  }


  /** @test */
  public function privateProfilShouldRedirectToLogin() {
    $this->dispatch('/zork', true);
    $this->assertModule('opac');
    $this->assertController('auth');
    $this->assertAction('login');
  }


  /** @test */
  public function publicProfilWithPrivateParentProfilShouldRedirectToLogin() {
    $this->dispatch('/zork-child', true);
    $this->assertModule('opac');
    $this->assertController('auth');
    $this->assertAction('login');
  }
}



abstract class IndexControllerCnilTrackingTestCase extends AbstractControllerTestCase {
  protected $_storm_default_to_volatile = true;
  protected $_expected_message = 'showNotification({"message":"En poursuivant votre navigation sur ce site, vous acceptez l\'utilisation de cookies.","autoClose":false';

  public function tearDown() {
    Class_Cnil::setCookieJar(null);
    Class_Cnil::setTimeSource(null);
    parent::tearDown();
  }
}



class IndexControllerCnilTrackingFirstVisitTest
  extends IndexControllerCnilTrackingTestCase {

  protected $_cookie_jar;

  public function setUp() {
    parent::setUp();

    Class_Cnil::setTimeSource($this->mock()
                              ->whenCalled('time')
                              ->answers(strtotime('2015-09-25 15:45:54')));

    Class_Cnil::setCookieJar($this->_cookie_jar = $this->mock()
                             ->whenCalled('setcookie')
                             ->with('cnil8801b6c24c4d369a55a96252ed121d5c',
                                    1,
                                    1476884754)
                             ->answers(null));

    $this->dispatch('/', true);
  }


  /** @test */
  public function shouldDisplayCnilMessage() {
    $this->assertXPathContentContains('//script', $this->_expected_message);
  }


  /** @test */
  public function shouldSetCnilCookieFor13Months() {
    $this->assertTrue($this->_cookie_jar->methodHasBeenCalled('setcookie'));
  }
}



class IndexControllerCnilTrackingSecondVisitTest
  extends IndexControllerCnilTrackingTestCase {

  /** @test */
  public function shouldNotDisplayCnilMessage() {
    $this->dispatch('/', true);
    $this->_response->setBody('');
    Class_ScriptLoader::resetInstance();

    $this->dispatch('/', true);
    $this->assertNotXPathContentContains('//script', $this->_expected_message);
  }
}



class IndexControllerCnilTrackingExistingCookieVisitTest
  extends IndexControllerCnilTrackingTestCase {

  /** @test */
  public function shouldNotDisplayCnilMessage() {
    $_COOKIE['cnil' . md5(BASE_URL)] = 1;
    $this->dispatch('/', true);
    $this->assertNotXPathContentContains('//script', $this->_expected_message);
  }