Skip to content
Snippets Groups Projects
RechercheControllerReservationTest.php 48.3 KiB
Newer Older
  }


  /** @test */
  public function withBadMailShouldDisplayInvalidMail() {
    $this->postDatas(['user_mail' => 'bad mail address']);
    $this->assertElementError('//input[@name="user_mail"]', 'n\'est pas un email valide');
  }


  /** @test */
  public function withBadCaptchaShouldDisplayInvalidCaptchaHaveEmptyValue() {
    $this->postDatas(['code_saisi' => 'bad captcha code']);
    $this->assertElementError('//input[@name="code_saisi"][@value=""]',
                              'Le code anti-spam est incorrect');
  }
}




class RechercheControllerReservationWithMailFormValidPostTest
  extends RechercheControllerReservationWithMailFormPostTestCase{

  protected $_mails;

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

    Class_AdminVar::set('TEXTE_MAIL_RESA', 'Thanks for your hold');

    $this->postDatas(['user_name' => 'Testing user',
                      'demande' => 'testing demande',
                      'user_mail' => 'test@server.com',
                      'code_saisi' => 'e2df0']);

    $this->_mails = $this->_mail_transport->getSentMails();
  }


  protected function assertMailBodyContains($needle, $mail) {
    $this->assertContains($needle, quoted_printable_decode($mail->getBodyText(true)));
  }


  /** @test */
  public function validFormShouldSendTwoMails() {
    $this->assertCount(2, $this->_mails);
  }


  /** @test */
  public function mailsShouldHaveHoldQuerySubject() {
    foreach($this->_mails as $mail)
      $this->assertContains('Demande de réservation de document',
                            quoted_printable_decode($mail->getSubject()));
  }


  /** @test */
  public function firstMailRecipientShouldBeLibrary() {
    $this->assertContains('my@library.com', $this->_mails[0]->getRecipients());
  }


  /** @test */
  public function firstMailFromShouldBeUser() {
    $this->assertEquals('test@server.com', $this->_mails[0]->getFrom());
  }


  /** @test */
  public function firstMailBodyShouldContainsUserName() {
    $this->assertMailBodyContains('Testing user', $this->_mails[0]);
  }


  /** @test */
  public function firstMailBodyShouldContainsRecordInfo() {
    foreach(['Test record', 'P TRA', 'Test author', 'Test editor'] as $info)
      $this->assertMailBodyContains($info, $this->_mails[0]);
  }


  /** @test */
  public function firstMailBodyShouldUserMessage() {
    $this->assertMailBodyContains('testing demande', $this->_mails[0]);
  }


  /** @test */
  public function secondMailRecipientShouldBeUser() {
    $this->assertContains('test@server.com', $this->_mails[1]->getRecipients());
  }


  /** @test */
  public function secondMailFromShouldBeNobody() {
    $this->assertEquals('nobody@noreply.fr', $this->_mails[1]->getFrom());
  }


  /** @test */
  public function secondMailBodyShouldContainsTexteMailResa() {
    $this->assertMailBodyContains('Thanks for your hold', $this->_mails[1]);
  }


  /** @test */
  public function shouldRedirectToRecordView() {
    $this->assertRedirectTo('/opac/recherche/viewnotice/id/550171?type_doc=1');
}



abstract class RechercheControllerReservationPickupAjaxWithNanookPickupLocationsTestCase
  extends RechercheControllerReservationTestCase{

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

    $bib = $this
      ->fixture('Class_IntBib',
                ['id' => 12,
                 'comm_sigb' => Class_IntBib::COM_NANOOK,
                 'comm_params' => ['url_serveur' => 'http://bib.valensol.net']]);

    $this->jajm = $this->fixture('Class_Users',
                                 ['id' => 1,
                                  'login' => 'jajm',
                                  'password' => 'secret',
                                  'idabon' => '0000007',
                                  'int_bib' => $bib]);

    $this->nanook = $this->mock()
      ->whenCalled('isConnected')->answers(true)
      ->whenCalled('providesPickupLocations')->answers(true)

      ->whenCalled('pickupLocationsFor')
      ->answers(['36' => 'Annecy',
                 '37' => 'Cran',
                 '45' => 'Istres',
                 '99' => 'Lunel <strong>(Disponible)</strong>']);

    Class_WebService_SIGB_Nanook::setService(['url_serveur' => 'http://bib.valensol.net',
                                              'id_bib' => 12,
                                              'type' => Class_IntBib::COM_NANOOK,],
                                             $this->nanook);

    ZendAfi_Auth::getInstance()->logUser($this->jajm);

    $this->fixture('Class_Exemplaire', ['id' => 12]);
  }


  public function choices() {
    return [[36], [37], [45]];
  }


  /**
   * @test
   * @dataProvider choices
   */
  public function choiceShouldBePresent($location_id) {
    $this->_xpath
      ->assertXPath($this->_json->content,
                    '//input[@type="radio"][@value="' . $location_id . '"]');
  }
}



class RechercheControllerReservationPickupAjaxNanookPickupLocationsPrecheckPatronTest
  extends RechercheControllerReservationPickupAjaxWithNanookPickupLocationsTestCase {

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

    $this->nanook->whenCalled('getUserAnnexe')->answers('45');

    $this->dispatch('recherche/reservation-pickup-ajax?id_bib=2&id_origine=12&code_annexe=37&copy_id=12', true);

    $this->_json = json_decode($this->_response->getBody());
  }


  /** @test */
  public function istresShouldBeChecked() {
    $this->_xpath
      ->assertXPath($this->_json->content, '//input[@type="radio"][@value="45"][@checked]');
  }
}



class RechercheControllerReservationPickupAjaxNanookPickupLocationsPrecheckRequestedTest
  extends RechercheControllerReservationPickupAjaxWithNanookPickupLocationsTestCase {

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

    $this->nanook->whenCalled('getUserAnnexe')->answers('66');

    $this->dispatch('recherche/reservation-pickup-ajax?id_bib=2&id_origine=12&code_annexe=37&copy_id=12', true);

    $this->_json = json_decode($this->_response->getBody());
  }


  /** @test */
  public function cranShouldBeChecked() {
    $this->_xpath
      ->assertXPath($this->_json->content, '//input[@type="radio"][@value="37"][@checked]');
  }
}



class RechercheControllerReservationPickupAjaxNanookPickupLocationsPrecheckFirstTest
  extends RechercheControllerReservationPickupAjaxWithNanookPickupLocationsTestCase {

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

    $this->nanook->whenCalled('getUserAnnexe')->answers('66');

    $this->dispatch('recherche/reservation-pickup-ajax?id_bib=2&id_origine=12&code_annexe=77&copy_id=12', true);

    $this->_json = json_decode($this->_response->getBody());
  }


  /** @test */
  public function annecyShouldBeChecked() {
    $this->_xpath
      ->assertXPath($this->_json->content, '//input[@type="radio"][@value="36"][@checked]');
  }
class RechercheControllerReservationPickupAjaxNanookPickupLocationsStrongInLabel
  extends RechercheControllerReservationPickupAjaxWithNanookPickupLocationsTestCase {

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

    $this->nanook->whenCalled('getUserAnnexe')->answers('66');

    $this->dispatch('recherche/reservation-pickup-ajax?id_bib=2&id_origine=12&code_annexe=99&copy_id=12', true);

    $this->_json = json_decode($this->_response->getBody());
  }


  /**
      @see https://forge.afi-sa.net/issues/94332
      @test
  */
  public function LunelShouldContainStrong() {
    $this->_xpath
      ->assertXPath($this->_json->content, '//strong',$this->_json->content);
  }
}


class RechercheControllerReservationPickupAjaxAndNotifyByMailEnabledTest
  extends RechercheControllerReservationWithPickupChoiceTestCase {

  protected $_sent_mails = [];

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

    Class_Profil::getCurrentProfil()->setMailSite('admin@example.com');

    $int_bib = $this
      ->fixture('Class_IntBib',
                ['id' => 1,
                 'comm_sigb' => Class_IntBib::COM_KOHA,
                 'use_card_number' => 12345,
                 'comm_params' => ['url_serveur' => 'http://bib.valensol.net/cgi-bin/koha/ilsdi.pl']]);

    $bib = $this->fixture('Class_Bib',
                            ['id' => 1,
                             'notify_on_new_resa' => 1,
                             'mail'=>'adminpukapuka@example.com',
                             'int_bib' => $int_bib]);
    $this->fixture('Class_CodifAnnexe',
                   ['id' => 34,
                    'libelle'=>'BDM',
                    'id_origine' => 23]);

    $this->jajm = $this
      ->fixture('Class_Users',
                ['id' => 1,
                 'login' => 'jajm',
                 'password' => 'secret',
                 'nom'    => 'Monsieur',
                 'prenom' => 'Julian',
                 'idabon' => '00004',
                 'int_bib' => $int_bib,
                 'id_sigb' =>'0000007',
                 'bib' => $bib
                ]);


    $this->mock = Storm_Test_ObjectWrapper::mock()
      ->whenCalled('isConnected')->answers(true)
      ->whenCalled('open_url')
      ->with('http://bib.valensol.net/cgi-bin/koha/ilsdi.pl?service=HoldTitle&patron_id=0000007&bib_id=13&request_location=127.0.0.1&pickup_location=23')
      ->answers('<HoldTitle>
                                   <title>Mireille l\'abeille</title>
                                   <pickup_location>Bibliothèque Départementale de la Meuse</pickup_location>
                                 </HoldTitle>')
      ->whenCalled('reserverExemplaire')->answers(['statut'=>true,'erreur'=>'']);

    Class_WebService_SIGB_Koha::setService(['url_serveur' => 'http://bib.valensol.net/cgi-bin/koha/ilsdi.pl',
                                              'id_bib' => 1,
                                              'type' => Class_IntBib::COM_KOHA,],

    ZendAfi_Auth::getInstance()->logUser($this->jajm);

    $this->fixture('Class_Exemplaire', ['id' => 12,
                                        'id_origine' =>13,
                                        'code_barres' =>12341,
                                        'notice'=> $this->fixture('Class_Notice',
                                                                  ['id'=>54,
                                                                   'titre_principal' => "Mireille l'Abeille",
                                                                   'type_doc' => Class_TypeDoc::LIVRE])
                                        ]);


    $mock_transport = new MockMailTransport();
    Zend_Mail::setDefaultTransport($mock_transport);

    $this->dispatch('recherche/reservationajax/id/11760/id_int_bib/23/id_bib/23/id_origine/594105/code_annexe/23/render/popup/copy_id/12', true);

    $this->_sent_mails = $mock_transport->getSentMails();
  }


  /** @test */
  public function parameterCodeAnnexeShouldBeUsedForReservation() {
    $this->assertEquals('23',
                        $this->mock->getAttributesForLastCallOn('reserverExemplaire')[2]);
  }


  /** @test */
  public function mailShouldHaveBeenSent() {
    $this->assertEquals(1, count($this->_sent_mails));
  }

  /** @test */
  public function mailSubjectShouldContainsJulianMonsieurSurExemplaire12341() {
    $this->assertContains("Julian Monsieur ? =?utf8?Q?sur l'exemplaire 12341",
                          quoted_printable_decode(current($this->_sent_mails)->getSubject()));
  }


  /** @test */
  public function mailToShouldBeAdminPukaPukaAtExampleDotCom() {
    $this->assertContains("adminpukapuka@example.com",current($this->_sent_mails)->getRecipients()[0]);
  }

  /** @test */
  public function mailFromShouldBeMailAdminAtExampleDotCom() {
    $this->assertContains("admin@example.com",current($this->_sent_mails)->getFrom());
  }


  public function expectedData() {
    return [['Titre : Mireille l\'Abeille'],
            ['Annexe de retrait : BDM'],
            ['Julian Monsieur 00004'],
            ['Code Barre : 12341'],
            ['Type de Document : Livres'],
    ];
  }


  /**
   * @test
   * @dataProvider expectedData
   */
  public function mailBodyShouldContainsContent($content) {
    $this->assertContains($content,quoted_printable_decode(current($this->_sent_mails)->getBodyHtml(true)));
  }