Skip to content
Snippets Groups Projects
TemplatesAbonneCentralizedHoldsTest.php 23.1 KiB
Newer Older
<?php
/**
 * Copyright (c) 2012-2023, Agence Française Informatique (AFI). All rights reserved.
 *
 * 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).
 *
 * 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
 * along with BOKEH; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301  USA
 */


include_once('Class/WebService/SIGB/Orphee/SessionStrategy.php');

include_once 'tests/library/Class/WebService/SIGB/OrpheeFixtures.php';

abstract class TemplatesAbonneCentralizedHoldsTestCase
  extends AbstractControllerTestCase{
  protected
    $_search_client,
    $_orphee,
    $_henry_dupont,
    $_john_lemon,
    $_foix;

  public function setUp() {
    parent::setUp();
    Class_CosmoVar::setValueOf('centralized_hold_mode', 34);

    $this->_buildTemplateProfil(['id'=>1]);

    $comm_params = ['url_serveur' => 'tests/fixtures/orphee.wsdl',
                    'allow_hold_available_items' => true];

    $this->fixture(Class_CodifAnnexe::class,
                   ['id' => 82,
                    'code' => 34,
                    'id_origine' => 34,
                    'libelle' => 'JolieVille'
                   ]);

    $this->fixture(Class_CodifAnnexe::class,
                   ['id' => 83,
                    'code' => 37,
                    'id_origine' => 37,
                    'libelle' => 'WonderTown'
                   ]);

    $this->fixture(Class_Bib::class,
                   ['id' => 34,
                    'mail' => 'test@jolieville.net',
                    'libelle' => 'JolieVille'
                   ]);

    $this->fixture(Class_IntBib::class,
                   ['id' => 34,
                    'id_bib' => 34,
                    'nom' => 'JolieVille',
                    'nom_court' => 'JV',
                    'comm_sigb' => Class_IntBib::COM_ORPHEE,
                    'comm_params' => $comm_params
                   ]);

    $bib_wondertown = $this->fixture(Class_Bib::class,
                   ['id' => 36,
                    'mail' => 'test@WonderTown.net',
                    'libelle' => 'WonderTown'
                   ]);

    $intbib_wondertown = $this->fixture(Class_IntBib::class,
                   ['id' => 36,
                    'id_bib' => 36,
                    'nom' => 'WonderTown',
                    'nom_court' => 'WT',
                    'mail' => 'intbib@WonderTown.net',
                    'comm_sigb' => Class_IntBib::COM_ORPHEE,
                    'comm_params' => ['url_serveur' => 'tests/fixtures/orphee.wsdl',
                                      'allow_hold_available_items' => true,
                                      'use_card_number' => '1301500012']]);

    $this->fixture(Class_IntBib::class,
                   ['id' => 37,
                    'id_bib' => 37,
                    'nom' => 'Eightth Wonder',
                    'nom_court' => '8THW',
                    'mail' => 'intbibqsdf@WonderTown.net',
                    'comm_sigb' => Class_IntBib::COM_NANOOK,
                    'comm_params' => ['url_serveur' => 'http://bib.valensol.net',
                                      'use_card_number' => '1301500012']]);

    $this->fixture(Class_IntBib::class,
                   ['id' => 38,
                    'id_bib' => 38,
                    'nom' => 'Bibliothèque Sans réservations centralisées',
                    'nom_court' => 'BSRC',
                    'mail' => 'bsrc@WonderTown.net',
                    'comm_sigb' => Class_IntBib::COM_NANOOK,
                    'comm_params' => ['url_serveur' => 'http://bib.valenciel.net']]);

    $this->_john_lemon =
      $this->fixture(Class_Users::class,
                     ['id' => 142,
                      'idAbon' => '',
                      'nom' => 'Lemon',
                      'prenom' => 'John',
                      'login' => 'uselogon',
                      'idabon' => '130425',
                      'password' => 'secret',
                      'mail' => 'johnlemon@abbeyroad.net',
                      'int_bib' => $intbib_wondertown,
                      'bib' => $bib_wondertown,
                     ]);

    $this->_foix =
      $this->fixture(Class_Users::class,
                     ['id' => 143,
                      'idAbon' => '1301500012',
                      'nom' => 'FOIX',
                      'prenom' => 'FPV',
                      'login' => '1301500012',
                      'password' => 's3cr3t',
                      'id_int_bib' => 34,
                      'id_bib' => 34
                     ]);

    $this->fixture(Class_Notice::class,
                   ['id' => 3,
                    'titre' => 'La disparition',
                    'auteurs' => 'Georges PEREC',
                    'unimarc' => (new Class_NoticeUnimarc_Fluent)
                    ->zoneWithChildren('200', ['a' => 'La disparition',
                                               'f' => 'PEREC, Georges',
                                               't' => 't. 1'])
                    ->render(),

                    'type_doc' => Class_TypeDoc::LIVRE,
                   ]);

    $this->fixture(Class_Exemplaire::class,
                   ['id' => 2345,
                    'id_notice' => 3,
                    'id_bib' => 34,
                    'annexe' => 34,
                    'id_int_bib' => 34,
                    'type_doc' => Class_TypeDoc::LIVRE,
                    'id_origine' => 312412,
                    'code_barres' => '012345']);

    $this->fixture(Class_CodifAnnexe::class,
                   ['id' => 37,
                    'libelle' => 'Paris',
                    'id_bib' => 37,
                    'id_origine' => 10]);

    $this->fixture(Class_Exemplaire::class,
                   ['id' => 2346,
                    'id_notice' => 3,
                    'id_bib' => 37,
                    'id_int_bib' => 37,
                    'type_doc' => Class_TypeDoc::LIVRE,
                    'id_origine' => 312413,
    $this->fixture(Class_Exemplaire::class,
                   ['id' => 2348,
                    'id_notice' => 3,
                    'id_bib' => 37,
                    'id_int_bib' => 38,
                    'type_doc' => Class_TypeDoc::LIVRE,
                    'id_origine' => 312415,
                    'annexe' => 38,
                    'code_barres' => '012348']);

    $this->fixture(Class_Exemplaire::class,
                   ['id' => 2347,
                    'id_notice' => 3,
                    'id_bib' => 36,
                    'id_int_bib' => 36,
                    'type_doc' => Class_TypeDoc::LIVRE,
                    'id_origine' => 312414,
                    'code_barres' => '012347']);

    $this->_search_client = $this->mock();
    $this->_search_client
      ->whenCalled('__getLastRequest')
      ->answers('last request')
      ->whenCalled('__getLastResponse')
      ->answers('last response')
      ->whenCalled('__getLastResponseHeaders')
      ->answers('last response  headers')
      ->whenCalled('__getLastRequestHeaders')
      ->answers('last response')
      ->whenCalled('hasFunction')
      ->answers(false)

      ->whenCalled('__setCookie')
      ->answers(null)

      ->whenCalled('EndSession')
      ->answers(new EndSessionResponse())

      ->whenCalled('hasFunction')->with('GetId')->answers(true)

      ->whenCalled('GetId')
      ->with(new GetId())
      ->answers(GetIdResponse::withIdResult('1234'))

      ->whenCalled('hasFunction')->with('GetAdh')->answers(true)

      ->whenCalled('GetAdh')
      ->with(GetAdh::withNo('10900000753'))
      ->answers(Storm_Test_ObjectWrapper::mock()
                ->whenCalled('getXml')
                ->answers(OrpheeFixtures::xmlGetInfoUserCarteHenryDupont()))

      ->whenCalled('GetAdh')
      ->with(GetAdh::withNo('1301500012'))
      ->answers(Storm_Test_ObjectWrapper::mock()
                ->whenCalled('getXml')
                ->answers(OrpheeFixtures::xmlGetAdhFoix()))

      ->whenCalled('GetAdh')
      ->with(GetAdh::withCardAndPassword('1301500012',$this->_foix->getPassword()))
      ->answers(Storm_Test_ObjectWrapper::mock()
                ->whenCalled('getXml')
                ->answers(OrpheeFixtures::xmlGetAdhFoix()))
      ->whenCalled('GetLstDmt')
      ->with(GetLstDmt::withNtcAndFas('312412', 0))
      ->answers(GetLstDmtResponse::withResult(OrpheeFixtures::xmlGetLstDmtMillenium()))

      ->whenCalled('RsvNtcAdh')
      ->with(RsvNtcAdh::withNoticeUserNo('312412', '1301500012', 0))
      ->answers(RsvNtcAdhResponse::withResult('<datas><msg><code><![CDATA[1]]></code><libelle><![CDATA[Réservation mise en attente]]></libelle></msg></datas>'))

      ->whenCalled('RsvNtcAdh')
      ->with(RsvNtcAdh::withNoticeUserNo('312414', '1301500012', 0))
      ->answers(RsvNtcAdhResponse::withResult('<datas><msg><code><![CDATA[1]]></code><libelle><![CDATA[Réservation mise en attente]]></libelle></msg></datas>'));

    $allow_hold_available_items=true;
    $this->_orphee = Class_WebService_SIGB_Orphee_Service::getService('tests/fixtures/orphee.wsdl', null, $allow_hold_available_items);
    $this->_orphee->setSearchClient($this->_search_client);
    $this->_orphee->isConnected();

    $this->_henry_dupont =
      $this->fixture(Class_Users::class,
                     ['id' => 2,
                      'login' => '10900000753',
                      'idabon' => '100753',
                      'password' => 'secret',
                      'id_int_bib' => 34,
                      'id_site' => 34]);

    $this->_henry_dupont->beAbonneSIGB()->assertSave();

    Class_WebService_SIGB_Orphee::setService(array_merge($comm_params,
                                                         ['id_bib' => 34,
                                                          'type' => Class_IntBib::COM_ORPHEE]),
                                             $this->_orphee);

    Class_WebService_SIGB_Orphee::setService(array_merge($comm_params,
                                                         ['id_bib' => 36,
                                                          'type' => Class_IntBib::COM_ORPHEE,
                                                          'use_card_number' => '1301500012']),
                                             $this->_orphee);
  }


  public function tearDown(){
    Class_WebService_SIGB_Orphee::reset();
    parent::tearDown();
  }
}




class TemplatesAbonneCentralizedHoldsWithHoldModeFailuresTest extends TemplatesAbonneCentralizedHoldsTestCase {

  public function _setHoldMode() {
    $this->_orphee->beHoldModeItem();
    return $this;
  }


  /** @test */
  public function johnLemonReserveItem2345ShouldReturnDocumentUnavailable() {
    Class_CosmoVar::setValueOf('centralized_hold_mode', 34);
    ZendAfi_Auth::getInstance()->logUser($this->_john_lemon);
    $this->dispatch('/recherche/reservationajax/id_bib/36/copy_id/2345/id_origine/312412/id/123');
    $this->assertContains('Votre réservation est enregistrée.<br>Nous vous informerons quand le document \'La disparition / PEREC, Georges\' sera disponible à : JolieVille',
                          json_decode($this->_response->getBody())->content);
  }


  /** @test */
  public function noParameterForCentralizedLibraryShouldReturnError() {
    Class_CosmoVar::setValueOf('centralized_hold_mode',39);
    ZendAfi_Auth::getInstance()->logUser($this->_john_lemon);
    $this->dispatch('/recherche/reservationajax/id_bib/36/copy_id/2346/id_origine/312412/id/123');
    $this->assertContains('Vous ne pouvez pas réserver cet exemplaire !',
                          json_decode($this->_response->getBody())->content);
  }
class TemplatesAbonneCentralizedHoldsFailuresTest extends TemplatesAbonneCentralizedHoldsTestCase {

  /** @test */
  public function HenryDupontShouldUseUniquePatronShouldBeFalse() {
    $this->assertFalse($this->_henry_dupont->shouldUseUniquePatron());
  }


  /** @test */
  public function JohnLemonShouldUseUniquePatronShouldBeTrue() {
    $this->assertTrue($this->_john_lemon->shouldUseUniquePatron());
  }


  /** @test */
  public function FoixShouldUseUniquePatronShouldBeFalse() {
    $this->assertFalse($this->_foix->shouldUseUniquePatron());
  }


  /** @test */
  public function JohnLemonGetUseCardNumberShouldBe130150012() {
    $this->assertEquals($this->_john_lemon->getUseCardNumber(),'1301500012');
  }


  /** @test */
  public function johnLemonReserveItem2346ShouldReturnReservationImpossibleNonReservable() {
    ZendAfi_Auth::getInstance()->logUser($this->_john_lemon);
    $this->dispatch('/recherche/reservationajax/id_bib/36/copy_id/2346/id_origine/312412/id/123');
    $this->assertContains('Vous ne pouvez pas réserver cet exemplaire !',
                          json_decode($this->_response->getBody())->content);
  }


  /** @test */
  public function johnLemonReserveItem23247ShouldReturnReservationEnregistree() {
    ZendAfi_Auth::getInstance()->logUser($this->_john_lemon);
    $this->dispatch('/recherche/reservationajax/id_bib/36/copy_id/2347/id_origine/312414/id/123');
    $this->assertContains('Votre réservation est enregistrée.',
                          json_decode($this->_response->getBody())->content);
  }


  /** @test */
  public function henryDupontReservItem2345ShouldReturnReservationImpossibleDocumentBibliothequeCentrale() {
    Class_CosmoVar::setValueOf('centralized_hold_mode',36);
    $this->_henry_dupont->setIntBib(Class_IntBib::find(38));
    ZendAfi_Auth::getInstance()->logUser($this->_henry_dupont);
    $this->dispatch('/recherche/reservationajax/id_bib/36/copy_id/2347/id_origine/312412/id/123');
    $this->assertContains('Vous ne pouvez pas réserver cet exemplaire !',
                          json_decode($this->_response->getBody())->content);

  }
}



class TemplatesAbonneCentralizedHoldsReservationSuccessTest
  extends TemplatesAbonneCentralizedHoldsTestCase{
  protected $_mock_transport;
  public function setUp() {
    parent::setUp();
    $this->_mock_transport = new MockMailTransport();
    Zend_Mail::setDefaultTransport($this->_mock_transport);
    Class_Profil::getCurrentProfil()->setMailSite('laurent@afi-sa.fr');
    $this->_john_lemon
      ->setIdIntBib(37)
      ->setIntBib(Class_IntBib::find(37));
    ZendAfi_Auth::getInstance()->logUser($this->_john_lemon);
    $this->dispatch('/recherche/reservationajax/id_bib/36/copy_id/2345/id_origine/312412/id/123');
  }

  /** @test */
  public function johnLemonReserveItem312412ShouldReturnReservationEnregistree() {
    $this->assertContains('Votre réservation est enregistrée',
                          json_decode($this->_response->getBody())->content);
  }


  /** @test */
  public function EmailSentCountShouldBeThree() {
    $this->assertEquals(count($this->_mock_transport->getSentMails()), 3);
  }


  public function getSenders(){
    return [[0],[1],[2]];
  }


  /** @test
   * @dataProvider getSenders
   */
  public function emailSentFromShouldBeProfilEmail($email_count) {
    $this->assertEquals($this->_mock_transport->getSentMails()[$email_count]->getFrom(), 'laurent@afi-sa.fr');
  }


  public function getRecipients(){
    return [[0, 'test@jolieville.net'],
            [1, 'intbibqsdf@WonderTown.net'],
            [2,'johnlemon@abbeyroad.net']];
  }


  /** @test
   *  @dataProvider getRecipients
   */
  public function emailSentFirstRecipientShouldBeAsExpected($email_count, $recipient_address) {
    $this->assertEquals($this->_mock_transport->getSentMails()[$email_count]->getRecipients()[0], $recipient_address);
  }


  public function getSubjects(){
    return [[0, 'Nouvelle réservation par WonderTown pour John Lemon sur 012345'],
            [1, 'Nouvelle réservation pour John Lemon sur 012345 auprès de JolieVille'],
            [2, 'Votre réservation de La disparition pour la bibliothèque WonderTown']];
  }

  /** @test
   *  @dataProvider getSubjects
   */
  public function emailSentSubjectShouldBeAsExpected($email_count, $subject_string) {
    $this->assertEquals(iconv_mime_decode($this->_mock_transport->getSentMails()[$email_count]->getSubject()),
                          $subject_string);
  }


  public function getMailContents(){
    $mail_to_central_library = <<<HTML_CENTRAL
<p>Bonjour,</p>
<p>Une réservation a été effectuée par un utilisateur du portail BDP.</p>
<p>Il s'agit du document : La disparition de Code Barre : 012345</p>
<p>Pour l'utilisateur : John Lemon</p>
<p>Pour la bibliothèque : WonderTown.
La réservation a été reportée dans le Orphée BDP au nom de la bibliothèque WonderTown avec le code adhérent 1301500012.
Le document doit être récupéré à la Bibliothèque Départementale et envoyé vers la bibliothèque WonderTown.
HTML_CENTRAL;

    $mail_to_user_library = <<<HTML_LIBRARY
<p>Bonjour,</p>
<p>Une réservation a été effectuée par un utilisateur de votre bibliothèque sur le portail BDP.</p>
<p>Il s'agit du document : La disparition de Code Barre : 012345</p>
<p>Pour l'utilisateur : John Lemon</p>
<p>Pour la bibliothèque : WonderTown.
La réservation a été reportée dans le Orphée BDP au nom de votre bibliothèque avec le code adhérent 1301500012.
Le document doit vous être envoyé par la BDP.
HTML_LIBRARY;

    $mail_to_user= <<<HTML_USER
<p>Bonjour,</p>
<p>Une réservation a été effectuée par un utilisateur de votre bibliothèque sur le portail BDP.</p>
<p>Il s'agit du document : La disparition de Code Barre : 012345</p>
<p>Pour l'utilisateur : John Lemon</p>
<p>Pour la bibliothèque : WonderTown.
La réservation a été reportée dans le Orphée BDP au nom de votre bibliothèque avec le code adhérent 1301500012.
Le document doit vous être envoyé par la BDP.
HTML_USER;

    return [[0, $mail_to_central_library],
            [1, $mail_to_user_library],
            [2, $mail_to_user]];
  }

  /** @test
   *  @dataProvider getMailContents
   */
  public function emailSentContentShouldBeAsExpected($email_count, $mail_content) {
    $this->assertEquals(quoted_printable_decode($this->_mock_transport->getSentMails()[$email_count]->getBodyHtml()->getContent()), $mail_content);
  }
}




include_once 'tests/fixtures/NanookFixtures.php';

class TemplatesAbonneCentralizedHoldsReservationPickupAjaxNetworkTest
  extends TemplatesAbonneCentralizedHoldsTestCase{
  protected
    $_mock_web_client,
    $_service;

  public function setUp() {
    parent::setUp();
    $this
      ->_john_lemon
      ->setIntBib(Class_IntBib::find(37));

    $this->_mock_web_client = $this->mock()
                                   ->whenCalled('open_url')
                                   ->with('http://bib.valensol.net/service/GetPickupLocation/bibId/0/patronId/0/siteId/0')
    ->answers(NanookFixtures::pickupLocationsOkAnswer())
                                   ->whenCalled('open_url')
                                   ->with('http://bib.valensol.net/service/GetPickupLocation/bibId/312413/patronId/789789789/siteId/37')
                                   ->answers(NanookFixtures::pickupLocationsOkAnswer());

    $_service = Class_WebService_SIGB_Nanook_Service::newInstance()
      ->setServerRoot('http://bib.valensol.net')
      ->setUseCardNumber('1301500012')
      ->setWebClient($this->_mock_web_client);

    $this
      ->_service = Class_WebService_SIGB_Nanook::setService(['url_serveur' => 'http://bib.valensol.net',
                                                             'provide_pickup_locations' => true,
                                                             'use_card_number' => '1301500012',
                                                             'id_bib' => 37,
                                                             'type' => 7],
                                                            $_service);

    Class_WebService_SIGB_AbstractRESTService::shouldThrowError(true);

    ZendAfi_Auth::getInstance()->logUser($this->_john_lemon);
  }


  public function tearDown(){
    $this->_service = null;
    $this->_mock_web_client = null;
    Class_WebService_SIGB_Nanook::reset();
    Class_WebService_SIGB_AbstractRESTService::shouldThrowError(false);
    parent::tearDown();
  }


  /** @test */
  public function reservationPickupAjaxForItemDifferentIlsShouldDisplayReservationImpossible() {
    Class_CosmoVar::setValueOf('site_retrait_resa', 0);
    Class_CosmoVar::setValueOf('centralized_hold_mode', 34);
    $this->dispatch('/recherche/reservation-pickup-ajax/id_bib/36/copy_id/2347/id_origine/312414/id/123');
    $this->assertContains(json_decode( $this->_response->getBody())->content,'Vous ne pouvez pas réserver cet exemplaire !');
    $this->assertContains(json_decode( $this->_response->getBody())->title,'Réservation impossible');
  }


  /** @test */
  public function reservationPickupAjaxForItemCentralLibraryShouldDisplayReservationEnregistree() {
    $this->dispatch('/recherche/reservation-pickup-ajax/id_bib/34/copy_id/2345/id_origine/312412/id/123');
    $this->assertContains('Votre réservation est enregistrée',json_decode( $this->_response->getBody())->content);
    $this->assertContains(json_decode( $this->_response->getBody())->title,'Réservation');
  }


  /** @test */
  public function reservationPickupAjaxForItemSameIlsShouldShowIlsPickupLibraries() {
    Class_CosmoVar::setValueOf('site_retrait_resa', 1);
    $this->dispatch('/recherche/reservation-pickup-ajax/id_bib/37/copy_id/2346/id_origine/312413/id/123');
    $this->assertContains('First library', json_decode($this->_response->getBody())->content);
  }


  /** @test */
  public function reservationPickupAjaxForItemNotInIlsCentralizedHoldModeShouldCallCentralizedHold() {
    Class_CosmoVar::setValueOf('site_retrait_resa', 0);
    Class_CosmoVar::setValueOf('centralized_hold_mode', 36);
    $this->dispatch('/recherche/reservation-pickup-ajax/id_bib/36/copy_id/2347/id_origine/312414/id/123');
    $this->assertContains('Votre réservation est enregistrée',json_decode( $this->_response->getBody())->content);
  }


  /** @test */
  public function reservationPickupAjaxForItemNotInIlsCentralizedHoldModeAndSiteRetraitResaEnabledShouldCallCentralizedHold() {
    Class_CosmoVar::setValueOf('site_retrait_resa', 1);
    Class_CosmoVar::setValueOf('centralized_hold_mode', 36);
    $this->dispatch('/recherche/reservation-pickup-ajax/id_bib/36/copy_id/2347/id_origine/312414/id/123');
    $this->assertContains('Votre réservation est enregistrée',json_decode( $this->_response->getBody())->content);
  }


  /** @test */
  public function reservationPickupAjaxWithUnknownItemShouldContainsError() {
    $this->dispatch('/recherche/reservation-pickup-ajax/id_bib/36/copy_id/7897892347/id_origine/312414/id/123');
    $this->assertContains('Document introuvable',json_decode( $this->_response->getBody())->content);
  }