Skip to content
Snippets Groups Projects
IntegrationControllerTest.php 18.98 KiB
<?php
/**
 * Copyright (c) 2012-2014, 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
 */


class Cosmo_IntegrationControllerTestActionTest extends CosmoControllerTestCase {
	/** @test */
	public function responseShouldContainGetHeader() {
		$this->dispatch('/cosmo/integration/test?type=7&options[url_serveur]=' . urlencode('http://localhost/ilsdi'), true);

		$this->assertXPathContentContains('//*', 'GET /ilsdi/service/GetRecords/id/1 HTTP/1.1', $this->_response->getBody());
	}
}



abstract class Cosmo_IntegrationControllerControlActionTestCase extends CosmoControllerTestCase {
	public function setUp() {
		parent::setUp();
    $this->fixture('Class_CosmoVar',
                   ['id' => 'import_type_operation',
                    'liste' => "0:Import incrémentiel\r\n1:Suppression d'exemplaires\r\n2:Import total\r\n3:Suppression fichier d'entête Pergame\r\n"]);

    $this->fixture('Class_CosmoVar',
                   ['id' => 'type_fichier',
                    'liste' => "0:notices\r\n1:abonnés\r\n2:prêts\r\n3:reservations\r\n4:paniers\r\n"]);

		$this->file_system = $this->mock();
		Class_Cosmogramme_Integration::setFileSystem($this->file_system);
	}
}



class Cosmo_IntegrationControllerControlActionWithoutBibTest extends Cosmo_IntegrationControllerControlActionTestCase {
	public function setUp() {
		parent::setUp();
		$this->dispatch('/cosmo/integration/control/id/99', true);
	}

	/** @test */
	public function libraryShouldBeUnknown() {
		$this->assertXPathContentContains('//div[@class="message_grand"]',
																			'Bibliothèque inconnue');
	}


	/** @test */
	public function shouldNotHavePlanif() {
		$this->assertXPathContentContains('//h3', 'Plannification : Aucune');
	}

	/** @test */
	public function shouldNotHaveFiles() {
		$this->assertXPathContentContains('//h3', 'Cette bibliothèque n\'a jamais été intégrée.');
	}
}



class Cosmo_IntegrationControllerControlActionWithBibTest extends Cosmo_IntegrationControllerControlActionTestCase {
	public function setUp() {
		parent::setUp();
		$this->file_system
			->whenCalled('file_exists')->answers(true)
			->whenCalled('filesize')->answers(1024)
			;

		$profil = $this->fixture('Class_IntProfilDonnees',
														 ['id' => 1,
															'type_fichier' => 1]);

		$integration = $this->fixture('Class_Cosmogramme_Integration',
																	['id' => 29,
																	 'traite' => 'non',
																	 'date_transfert' => '2014-11-14',
																	 'type_operation' => 2,
																	 'fichier' => 'integre0000.pan',
																	 'pointeur_reprise' => 0,
																	 'nb_erreurs' => 0,
																	 'nb_warnings' => 0,
																	 'profil_donnees' => $profil]);
		$this->fixture('Class_IntBib',
									 ['id' => 19,
										'nom_court' => 'Testing library',
										'planif_mode' => 'r',
										'planif_jours' => '0010011',
										'integrations' => [$integration]]);

		$this->dispatch('/cosmo/integration/control/id/19', true);
	}


	/** @test */
	public function libraryShouldBePresent() {
		$this->assertXPathContentContains('//div[@class="message_grand"]',
																			'Testing library');
	}


	/** @test */
	public function shouldHavePlanif() {
		$this->assertXPathContentContains('//h3', 'Plannification : Me, Sa, Di');
	}


	/** @test */
	public function shouldHaveFiles() {
		$this->assertXPath('//table');
	}


	/** @test */
	public function integre0000ShouldBePresent() {
		$this->assertXPathContentContains('//td', 'integre0000.pan');
	}


	/** @test */
	public function abonneShouldBePresent() {
		$this->assertXPathContentContains('//td', 'abonnés');
	}


	/** @test */
	public function importTotalShouldBePresent() {
		$this->assertXPathContentContains('//td', 'Import total');
	}


	/** @test */
	public function downloadLinkShouldBePresent() {
		$this->assertXPath('//a[contains(@href, "run-log/download/id/29")]');
	}


	/** @test */
	public function vendrediShouldBePresent() {
		$this->assertXPathContentContains('//td', 'vendredi');
	}
}



abstract class Cosmo_IntegrationControllerGenerateActionTestCase extends CosmoControllerTestCase {
  public function setUp() {
    parent::setUp();
    $this->fixture('Class_CosmoVar',
                   ['id' => 'ftp_path',
                    'valeur' => '../ftp/transfert/']);

    $subdir_calls = 0;
    $subdir_provider = function() use (&$subdir_calls) {
      $subdir_calls++;
      $mapping = [1 => '.', 2 => '..', 3 => 'library1', 4 => 'test'];
      return array_key_exists($subdir_calls, $mapping) ? $mapping[$subdir_calls] : false;
    };

    $file_system = $this->mock()
                        ->whenCalled('file_exists')->with('../ftp/transfert/')
                        ->answers(true)

                        ->whenCalled('opendir')->with('../ftp/transfert/')
                        ->answers(true)

                        ->whenCalled('readdir')->with(true)
                        ->willDo($subdir_provider)

                        ->whenCalled('is_dir')->with('../ftp/transfert/library1')
                        ->answers(true)

                        ->whenCalled('is_readable')
                        ->with('../ftp/transfert/library1/etalon')
                        ->answers(true)

                        ->whenCalled('is_readable')
                        ->with('../ftp/transfert/library1/etalon/annexes.txt')
                        ->answers(true)

                        ->whenCalled('is_readable')
                        ->with('../ftp/transfert/library1/etalon/genres.txt')
                        ->answers(true)

                        ->whenCalled('is_readable')
                        ->with('../ftp/transfert/library1/etalon/sections.txt')
                        ->answers(true)

                        ->whenCalled('is_readable')
                        ->with('../ftp/transfert/library1/etalon/emplacements.txt')
                        ->answers(true)

                        ->whenCalled('file')
                        ->with('../ftp/transfert/library1/etalon/annexes.txt')
                        ->answers(['BIB_SPS_UTT|ID_SITE|LIBELLE',
                                   '20|library1',
                                   '12|library2'])

                        ->whenCalled('file')
                        ->with('../ftp/transfert/library1/etalon/sections.txt')
                        ->answers(['BIB_C_SECTION|CODE|LIBELLE',
                                   '1|Adulte',
                                   '2|Jeunesse'])

                        ->whenCalled('file')
                        ->with('../ftp/transfert/library1/etalon/emplacements.txt')
                        ->answers(['BIB_C_EMPLACEMENT|CODE|LIBELLE',
                                   '4|Coin des tout-petits',
                                   '5|Livres CD',
                                   '6|Bacs imagiers',
                                   '7|Albums'])

                        ->whenCalled('file')
                        ->with('../ftp/transfert/library1/etalon/genres.txt')
                        ->answers(['BIB_GENRES|SUPPORT|CODE|LIBELLE|DOC',
                                   '0|1|Album|f',
                                   '0|2|Bande dessinée|f',
                                   '0|3|Biographie|f'])

                        ->beStrict();

    Class_Cosmogramme_LandingDirectory::setFileSystem($file_system);
  }


  public function tearDown() {
    Class_Log::getInstance()->reset();
    parent::tearDown();
  }
}



class Cosmo_IntegrationControllerGenerateActionTest extends Cosmo_IntegrationControllerGenerateActionTestCase {
  public function setUp() {
    parent::setUp();
    $this->dispatch('cosmo/integration/generate', true);
  }


  public function tearDown() {
    Class_Cosmogramme_LandingDirectory::setFileSystem(null);
    parent::tearDown();
  }


  /** @test */
  public function titleShouldBeGenerationAutomatique() {
    $this->assertXPathContentContains('//h1', 'Génération automatique');
  }


  /** @test */
  public function library1ShouldBeSelectable() {
    $this->assertXPath('//select[@name="path_ftp"]//option[@value="library1"]');
  }


  /** @test */
  public function testShouldNotBeSelectable() {
    $this->assertNotXPath('//select[@name="path_ftp"]//option[@value="test"]');
  }

  /** @test */
  public function parentShouldNotBeSelectable() {
    $this->assertNotXPath('//select[@name="path_ftp"]//option[@value=".."]');
  }


  /** @test */
  public function sigbNanookShouldBeSelectable() {
    $this->assertXPath('//select[@name="type_sigb"]//option[@value="13"]');
  }


  /** @test */
  public function shouldBeAbleToChooseBranchCreationMode() {
    $this->assertXPath('//select[@name="creer_annexes"]');
  }


  /** @test */
  public function shouldBeAbleToEtalonSynchMode() {
    $this->assertXPath('//select[@name="synchro"]');
  }
}



class Cosmo_IntegrationControllerGenerateActionNanookPostTest
  extends Cosmo_IntegrationControllerGenerateActionTestCase {

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

    $this->fixture('Class_IntProfilDonnees',
                   ['id' => 104,
                    'libelle' => 'Unimarc Nanook',
                    'type_fichier' => 0]);

    $this->fixture('Class_IntProfilDonnees',
                   ['id' => 105,
                    'libelle' => 'Abonné Nanook',
                    'type_fichier' => 1]);

    $time_source = $this->mock()->whenCalled('time')->answers(strtotime('2016-01-28'));
    Class_Cosmogramme_Generator::setTimeSource($time_source);
    Class_Cosmogramme_Generator_AbstractTask::setTimeSource($time_source);

    $this->postDispatch('cosmo/integration/generate',
                        ['path_ftp' => 'library1',
                         'type_sigb' => 13,
                         'service_nanook' => 'http://nanook-ws.net/ilsdi/',
                         'creer_annexes' => 1,
                         'synchro' => 1]);
  }


  public function tearDown() {
    Class_Cosmogramme_Generator::setTimeSource(null);
    Class_Cosmogramme_Generator_AbstractTask::setTimeSource(null);
    parent::tearDown();
  }


  /** @test */
  public function shouldNotContainsForm() {
    $this->assertNotXPath('//form');
  }


  /** @test */
  public function shouldNotHaveError() {
    $this->assertNotXPath('//font[@color="red"]', $this->_response->getBody());
  }


  /** @test */
  public function shouldLogLibraryCreation() {
    $this->assertXPathContentContains('//h3', '1 - Création des bibliothèques', $this->_response->getBody());
  }


  /** @test */
  public function shouldHaveCreated2Libraries() {
    $this->assertEquals(2, Class_Bib::count());
  }


  /** @test */
  public function library1ShouldBeCreated() {
    $this->assertNotNull(Class_Bib::findFirstBy(['id_site' => 20,
                                                 'libelle' => 'library1',
                                                 'ville' => 'library1',
                                                 'id_zone' => 1,
                                                 'visibilite' => Class_Bib::V_DATA]),
                         $this->_response->getBody());
  }


  /** @test */
  public function library1IntegrationShouldBeCreated() {
    $this->assertNotNull($bib = Class_IntBib::findFirstBy(['id_bib' => 20,
                                                           'nom' => 'library1',
                                                           'nom_court' => 'library1',
                                                           'qualite' => 5,
                                                           'sigb' => Class_IntBib::SIGB_NANOOK,
                                                           'planif_mode' => 'r',
                                                           'planif_jours' => '1111111',
                                                           'comm_sigb' => Class_IntBib::COM_NANOOK]),
                         $this->_response->getBody());
    return $bib;
  }


  /**
   * @test
   * @depends library1IntegrationShouldBeCreated
   */
  public function library1IntegrationCommParamsShouldContainsNanookWebservice($bib) {
    $this->assertContains('http://nanook-ws.net/ilsdi/', $bib->getCommParams());
  }


  /** @test */
  public function library1BranchShouldBeCreated() {
    $this->assertNotNull(Class_CodifAnnexe::findFirstBy(['id_bib' => 20,
                                                         'code' => 20,
                                                         'libelle' => 'library1',
                                                         'invisible' => 0]),
                         $this->_response->getBody());
  }


  /** @test */
  public function barCodeProfileShouldBeCreated() {
    $this->assertNotNull($profile = Class_IntProfilDonnees::findFirstBy(['libelle' => 'Liste de codes-barres']));
    return $profile;
  }


  /**
   * @test
   * @depends barCodeProfileShouldBeCreated
   */
  public function library1ShouldHeveItemDeletionPlanned($profile) {
    $this->assertNotNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                        'rang' => 2001,
                                                        'profil' => $profile->getId(),
                                                        'type_operation' => Class_IntMajAuto::OP_ITEMS_DELETION,
                                                        'nom_fichier' => '../ftp/transfert/library1/site20/suppressions.txt']));
  }


  /** @test */
  public function library1ShouldHaveRecordsTotalPlanned() {
    $this->assertNotNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                        'profil' => 104,
                                                        'type_operation' => Class_IntMajAuto::OP_FULL_IMPORT,
                                                        'nom_fichier' => '../ftp/transfert/library1/site20/notices_total.txt']));
  }


  /** @test */
  public function library1ShouldHaveRecordsPartialPlanned() {
    $this->assertNotNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                        'profil' => 104,
                                                        'type_operation' => Class_IntMajAuto::OP_PARTIAL_IMPORT,
                                                        'nom_fichier' => '../ftp/transfert/library1/site20/notices.txt']));
  }


  /** @test */
  public function library1ShouldHaveUsersPlanned() {
    $this->assertNotNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                        'profil' => 105,
                                                        'type_operation' => Class_IntMajAuto::OP_FULL_IMPORT,
                                                        'nom_fichier' => '../ftp/transfert/library1/site20/abonnes.txt']));
  }


  /** @test */
  public function library1ShouldHaveLoansPlanned() {
    $this->assertNotNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                        'profil' => 102,
                                                        'type_operation' => Class_IntMajAuto::OP_FULL_IMPORT,
                                                        'nom_fichier' => '../ftp/transfert/library1/site20/prets.txt']));
  }


  /** @test */
  public function library1ShouldNotHaveHoldsPlanned() {
    $this->assertNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                     'profil' => 103,
                                                     'type_operation' => Class_IntMajAuto::OP_FULL_IMPORT,
                                                     'nom_fichier' => '../ftp/transfert/library1/site20/reservations.txt']));
  }


  /** @test */
  public function basketsProfileShouldBeCreated() {
    $profile = Class_IntProfilDonnees::findFirstBy(['accents' => Class_IntProfilDonnees::ENCODING_UTF8,
                                                    'type_fichier' => Class_IntProfilDonnees::FT_BASKETS,
                                                    'format' => Class_IntProfilDonnees::FORMAT_PIPED_ASCII]);
    $this->assertNotNull($profile);
    return $profile;
  }



  /**
   * @test
   * @depends basketsProfileShouldBeCreated
   */
  public function library1ShouldHaveBasketsFullPlanned($profile) {
    $this->assertNotNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                        'profil' => $profile->getId(),
                                                        'type_operation' => Class_IntMajAuto::OP_FULL_IMPORT,
                                                        'nom_fichier' => '../ftp/transfert/library1/site20/paniers_total.txt']));
  }


  /**
   * @test
   * @depends basketsProfileShouldBeCreated
   */
  public function library1ShouldHaveBasketsPartialPlanned($profile) {
    $this->assertNotNull(Class_IntMajAuto::findFirstBy(['id_bib' => 20,
                                                        'profil' => $profile->getId(),
                                                        'type_operation' => Class_IntMajAuto::OP_PARTIAL_IMPORT,
                                                        'nom_fichier' => '../ftp/transfert/library1/site20/paniers.txt']));
  }


  /** @test */
  public function adulteSectionShouldBeCreated() {
    $this->assertNotNull(Class_CodifSection::findFirstBy(['libelle' => 'Adulte',
                                                          'regles' => '995$9=1']));
  }


  /** @test */
  public function coinLocationShouldBeCreated() {
    $this->assertNotNull(Class_CodifEmplacement::findFirstBy(['libelle' => 'Coin des tout-petits',
                                                              'regles' => '995$6=4']));
  }


  /** @test */
  public function bdKindShouldBeCReated() {
    $this->assertNotNull(Class_CodifGenre::findFirstBy(['libelle' => 'Biographie',
                                                        'regles' => '995$7=3']));
  }


  /** @test */
  public function shouldHaveCreated10Deweys() {
    $this->assertEquals(10, Class_CodifDewey::count());
  }


  /** @test */
  public function generaliteDeweyShouldHaveId0() {
    $this->assertNotNull(Class_CodifDewey::findFirstBy(['id_dewey' => 0, 'libelle' => 'Généralités']));
  }


  /** @test */
  public function histoireDeweyShouldHaveId9() {
    $this->assertNotNull(Class_CodifDewey::findFirstBy(['id_dewey' => 9, 'libelle' => 'Histoire, géographie']));
  }


  /** @test */
  public function shouldSayFinished() {
    $this->assertXPathContentContains('//h2', 'Traitement terminé');
  }
}