From 9da07a964c7ae72116d0424fe6ff20e96de08430 Mon Sep 17 00:00:00 2001
From: Patrick Barroca <pbarroca@afi-sa.fr>
Date: Fri, 14 Jun 2019 17:30:16 +0200
Subject: [PATCH] hotline #93469 : fix codif thesaurus generation

---
 VERSIONS_HOTLINE/93469                        |  1 +
 .../php/classes/KohaRecordIntegrationTest.php | 62 ++++++++++++++++++-
 .../unimarc_bd_millenium_with_authority.txt   |  1 +
 .../Record/BibliographicDynamicFacets.php     | 22 ++++---
 scripts/reindex_local_authorities.php         | 24 +++----
 .../scenarios/Authorities/AuthoritiesTest.php | 56 +++++++++++++----
 tests/scenarios/Authorities/vagabond.mrc      |  2 +-
 7 files changed, 134 insertions(+), 34 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/93469
 create mode 100644 cosmogramme/tests/php/classes/unimarc_bd_millenium_with_authority.txt

diff --git a/VERSIONS_HOTLINE/93469 b/VERSIONS_HOTLINE/93469
new file mode 100644
index 00000000000..12c56708a1d
--- /dev/null
+++ b/VERSIONS_HOTLINE/93469
@@ -0,0 +1 @@
+ - ticket #93469 : Intégrations : Amélioration de la création des facettes dynamiques
\ No newline at end of file
diff --git a/cosmogramme/tests/php/classes/KohaRecordIntegrationTest.php b/cosmogramme/tests/php/classes/KohaRecordIntegrationTest.php
index b5140863edd..4d8e068cb57 100644
--- a/cosmogramme/tests/php/classes/KohaRecordIntegrationTest.php
+++ b/cosmogramme/tests/php/classes/KohaRecordIntegrationTest.php
@@ -471,7 +471,6 @@ class KohaRecordIntegrationVagabondWithTooMany610aTest extends KohaRecordIntegra
 
 
 
-
 class KohaRecordIntegrationEscapableAnnexeCodesTest extends KohaRecordIntegrationTestCase {
   public function setUp() {
     parent::setUp();
@@ -496,3 +495,64 @@ class KohaRecordIntegrationEscapableAnnexeCodesTest extends KohaRecordIntegratio
     $this->assertEquals('CHYJR', $this->notice_data['exemplaires'][0]['annexe']);
   }
 }
+
+
+
+class KohaRecordIntegrationBdMilleniumWithAuthorityTest extends KohaRecordIntegrationTestCase {
+  public function setUp() {
+    parent::setUp();
+
+    $this->fixture('Class_CodifThesaurus',
+                   ['id' => 6,
+                    'libelle' => 'Mots-clés',
+                    'id_thesaurus' => 'MOTC',
+                    'id_origine' => null,
+                    'code' => 'MOTC',
+                    'rule_zone' => '609',
+                    'rule_label_field' => 'a',
+                    'rule_id_field' => '9']);
+
+    $attributs = [[Class_IntProfilDonnees::PROFILE_INDEX_SYSTEMS_FIELDS
+                   => [['rule' => '',
+                        'system' => 'MSAS',
+                        'default_type' => 'j',
+                        'thesaurus' => '6']]]];
+
+    $this->fixture('Class_IntProfilDonnees',
+                   ['id' => 1004,
+                    'libelle' => 'Authorities',
+                    'type_fichier' => Class_IntProfilDonnees::FT_AUTHORITY,
+                    'format' => Class_IntProfilDonnees::FORMAT_UNIMARC,
+                    'accents' => Class_IntProfilDonnees::ENCODING_UTF8,
+                    'attributs' => serialize($attributs),
+                   ]);
+
+    $this->loadNotice('unimarc_bd_millenium_with_authority');
+    $this->millenium = Class_Notice::findFirstBy(['type' => 1]);
+  }
+
+
+  /** @test */
+  public function titreShouldBeHommesQuiNaimaientPasLesFemmes() {
+    $this->assertEquals('Les hommes qui n\'aimaient pas les femmes',
+                        $this->millenium->getTitrePrincipal());
+  }
+
+
+  /** @test */
+  public function codifThesaurusIdOrigineShouldBeTrilogie() {
+    $this->assertNotNull(Class_CodifThesaurus::findFirstBy(['code' => 'MOTC',
+                                                            'id_thesaurus' => 'MOTC0001',
+                                                            'id_origine' => 'TRILOGIE',
+                                                            'libelle' => 'Trilogie',
+                                                            ]));
+  }
+
+
+  /** @test */
+  public function codifThesaurusIdOrigineShouldNotBe37483() {
+    $this->assertNull(Class_CodifThesaurus::findFirstBy(['code' => 'MOTC',
+                                                         'id_origine' => '37483',
+                                                         ]));
+  }
+}
diff --git a/cosmogramme/tests/php/classes/unimarc_bd_millenium_with_authority.txt b/cosmogramme/tests/php/classes/unimarc_bd_millenium_with_authority.txt
new file mode 100644
index 00000000000..f5a62b5922f
--- /dev/null
+++ b/cosmogramme/tests/php/classes/unimarc_bd_millenium_with_authority.txt
@@ -0,0 +1 @@
+01322     2200301   4500001000700000010002400007035002100031073001800052090001900070091000900089099000700098100004100105101000800146102000700154106000600161200019000167210004300357215006700400225001700467609002000484700004500504701002200549702004300571801006700614995010900681995012100790995010900911260265  a9782800157771brel.  a(OCoLC)868591687 1a9782800157771  9260265a260265  a2c1  tBD  a20140318              frey50          afre  aFR  ar1 aLes hommes qui n'aimaient pas les femmesbTexte impriméeseconde partiefscénario,  Sylvain Runberggdessin,  José Homsgd'après la trilogie de Stieg LarssonhTome 2hseconde partie  aPariscDupuisd2013eimpr. en Belgique  a1 vol. (64 p.)ctout ill. en coul., couv. ill. en coul.d32 cm  aMilleniumv2  aTrilogie937483  9495032aRunbergbSylvainf1971-....4690  9485177aHoms4440  9545077aLarssonbStiegf1954-20044100 3gAFNORaFRbMediatheque intercommunale Ouest Provencec20140225  f31301011644247m2014-07-0840009492216bFOS20kBD RUN62014-03-11o0cLLv14.50rBDaFOSeL1l00qNL4  f31301011644122m2014-10-0440009492217bMIR20kBD RUN62014-03-11n2014-10-25o0cLLv14.50rBDaMIReL1l00qNL4  f31301011644189m2014-10-1040009492218bENT20kBD RUN62014-03-11o0cLLv14.50rBDaENTeL1l00qNL4
\ No newline at end of file
diff --git a/library/Class/Cosmogramme/Integration/Record/BibliographicDynamicFacets.php b/library/Class/Cosmogramme/Integration/Record/BibliographicDynamicFacets.php
index 88b4276a82c..70c6d47e5c4 100644
--- a/library/Class/Cosmogramme/Integration/Record/BibliographicDynamicFacets.php
+++ b/library/Class/Cosmogramme/Integration/Record/BibliographicDynamicFacets.php
@@ -32,7 +32,7 @@ class Class_Cosmogramme_Integration_Record_BibliographicDynamicFacets {
 
 
   protected function _newFor($thesaurus, $int_bib) {
-    return $int_bib && ($index_systems = $thesaurus->authoritiesIndexSystems())
+    return ($index_systems = $thesaurus->authoritiesIndexSystems())
       ? new Class_Cosmogramme_Integration_Record_BibliographicDynamicFacets_Authority($thesaurus, $index_systems, $int_bib)
       : new Class_Cosmogramme_Integration_Record_BibliographicDynamicFacets_Simple($thesaurus);
   }
@@ -60,12 +60,20 @@ class Class_Cosmogramme_Integration_Record_BibliographicDynamicFacets_Simple {
 
   protected function _getOrCreateChildren($rules, $reader) {
     $id_labels = $rules->extractIdAndFields($reader);
-    return array_map(function($id_label)
-                     {
-                       return $this->_thesaurus->getOrCreateChild($id_label['id'],
-                                                                  $id_label['label']);
-                     },
-                     $id_labels);
+    return array_filter(array_map([$this, '_getOrCreateChild'], $id_labels));
+  }
+
+
+  protected function _getOrCreateChild($id_label) {
+    $id = trim($id_label['id']);
+    $label = trim($id_label['label']);
+    if (!$label)
+      return;
+
+    if (!$id)
+      $id = strtoupper($label);
+
+    return $this->_thesaurus->getOrCreateChild($id, $label);
   }
 }
 
diff --git a/scripts/reindex_local_authorities.php b/scripts/reindex_local_authorities.php
index 838505f1f29..13558492795 100644
--- a/scripts/reindex_local_authorities.php
+++ b/scripts/reindex_local_authorities.php
@@ -5,15 +5,6 @@ require(__DIR__.'/../console.php');
 
 echo "\n\nWelcome to the iReindex Authorities 3000 PRO tool by @patbator\n\n";
 
-$int_bib = null;
-
-while(!$int_bib) {
-  $id_int_bib = (int)readline('Please tell me the id_int_bib : ');
-  echo ($int_bib = Class_IntBib::find($id_int_bib))
-    ? ("Authorities generated by bibliographic links will be linked to : " . $int_bib->getNomCourt() . "\n\n")
-    : "IntBib not found try again \n\n";
-}
-
 echo "\n\n==== AUTHORITIES ====\n\n";
 
 class Scripts_Local_Authorities_Indexer {
@@ -79,18 +70,23 @@ while ($records = Class_Notice::findAllBy(['type' => Class_Notice::TYPE_AUTHORIT
 echo "\n\n==== BIBLIOS ====\n\n";
 
 class Scripts_Local_Biblios_Indexer {
-  protected $_dynamic_facets, $_int_bib;
+  protected $_dynamic_facets;
 
 
-  public function __construct($int_bib) {
-    $this->_int_bib = $int_bib;
+  public function __construct() {
     $this->_dynamic_facets = new Class_Cosmogramme_Integration_Record_BibliographicDynamicFacets();
   }
 
 
   public function index($record) {
     echo '.';
-    $thesauri = $this->_dynamic_facets->thesauriOf($record, $this->_int_bib);
+    if (!$first_item = $record->getFirstExemplaire())
+      return;;
+
+    if (!$bib = $first_item->getIntBib())
+      return;
+
+    $thesauri = $this->_dynamic_facets->thesauriOf($record, $bib);
     $record->updateFacette(implode(' ', array_map(function($item) { return $item->getFacetCode(); },
                                                   array_filter($thesauri))))
            ->save();
@@ -98,7 +94,7 @@ class Scripts_Local_Biblios_Indexer {
 }
 
 
-$indexer = new Scripts_Local_Biblios_Indexer($int_bib);
+$indexer = new Scripts_Local_Biblios_Indexer();
 $page = 1;
 while ($records = Class_Notice::findAllBy(['type' => Class_Notice::TYPE_BIBLIOGRAPHIC,
                                            'limitPage' => [$page, 1000]])) {
diff --git a/tests/scenarios/Authorities/AuthoritiesTest.php b/tests/scenarios/Authorities/AuthoritiesTest.php
index 49a133afd7b..c8b6ce93a9e 100644
--- a/tests/scenarios/Authorities/AuthoritiesTest.php
+++ b/tests/scenarios/Authorities/AuthoritiesTest.php
@@ -147,7 +147,7 @@ class AuthoritiesNoticeAjaxControllerTest extends AuthoritiesTestCase {
 
 
 class AuthoritiesBibliographicDynamicFacetsWithoutIndexSystemTest extends ModelTestCase {
-  protected $_thesaurus;
+  protected $_thesauri;
 
   public function setUp() {
     parent::setUp();
@@ -162,22 +162,39 @@ class AuthoritiesBibliographicDynamicFacetsWithoutIndexSystemTest extends ModelT
                     'rule_label_field' => 'a',
                     'rule_id_field' => '9']);
 
-    $thesauri = (new Class_Cosmogramme_Integration_Record_BibliographicDynamicFacets())
+    $this->_thesauri = (new Class_Cosmogramme_Integration_Record_BibliographicDynamicFacets())
       ->thesauriOf((new Class_NoticeUnimarc())->setNotice(file_get_contents(__DIR__ . '/vagabond.mrc')),
                    $this->fixture('Class_IntBib', ['id' => 3]));
-    $this->_thesaurus = reset($thesauri);
   }
 
 
   /** @test */
-  public function thesaurusIdShouldBeMots0001() {
-    $this->assertEquals('MOTS0001', $this->_thesaurus->getIdThesaurus());
+  public function shouldHave2Thesauri() {
+    $this->assertEquals(2, count($this->_thesauri));
   }
 
 
   /** @test */
-  public function thesaurusLabelShouldBeManga() {
-    $this->assertEquals('Manga', $this->_thesaurus->getLibelle());
+  public function firstThesaurusIdShouldBeMots0001() {
+    $this->assertEquals('MOTS0001', $this->_thesauri[0]->getIdThesaurus());
+  }
+
+
+  /** @test */
+  public function firstThesaurusLabelShouldBeManga() {
+    $this->assertEquals('Manga', $this->_thesauri[0]->getLibelle());
+  }
+
+
+  /** @test */
+  public function firstThesaurusIdOrigineShouldBe213399() {
+    $this->assertEquals('213399', $this->_thesauri[0]->getIdOrigine());
+  }
+
+
+  /** @test */
+  public function secondThesaurusLabelShouldBeTrimmed() {
+    $this->assertEquals('Cicatrice', $this->_thesauri[1]->getLibelle());
   }
 
 
@@ -373,19 +390,36 @@ class AuthoritiesBibliographicDynamicFacetsWithIndexSystemAlreadyExistingTest ex
 
 
   /** @test */
-  public function authorityShouldNotBeCreated() {
-    $this->assertEquals(1, Class_Notice::countBy(['type' => Class_Notice::TYPE_AUTHORITY]));
+  public function authorityItemShouldNotBeDuplicated() {
+    $this->assertEquals(1, Class_Exemplaire::countBy(['type' => Class_Notice::TYPE_AUTHORITY,
+                                                      'id_origine' => 213399]));
   }
 
 
   /** @test */
-  public function authorityItemShouldNotBeCreated() {
+  public function authorityShouldHaveOneItemOnly() {
     $this->assertEquals(1, Class_Notice::find(14)->numberOfExemplaires());
   }
 
 
   /** @test */
-  public function authorityShouldHaveThesaurusFacet() {
+  public function authorityShouldHaveThesaurusFacetHMOTS0001() {
     $this->assertContains('HMOTS0001', Class_Notice::find(14)->getFacetCodes());
   }
+
+
+  /** @test */
+  public function authorityItemForCicatriceShouldBeCreated() {
+    $this->assertNotNull(Class_Exemplaire::findFirstBy(['type' => Class_Notice::TYPE_AUTHORITY,
+                                                        'id_origine' => 78]));
+  }
+
+
+  /** @test */
+  public function authorityRecordForCicactriceShouldHaveThesaurusFacetHMOTS0002() {
+    $this->assertContains('HMOTS0002',
+                          Class_Exemplaire::findFirstBy(['type' => Class_Notice::TYPE_AUTHORITY,
+                                                         'id_origine' => 78])
+                          ->getNotice()->getFacetCodes());
+  }
 }
diff --git a/tests/scenarios/Authorities/vagabond.mrc b/tests/scenarios/Authorities/vagabond.mrc
index f4d084d69a6..3f84c12c27b 100644
--- a/tests/scenarios/Authorities/vagabond.mrc
+++ b/tests/scenarios/Authorities/vagabond.mrc
@@ -1 +1 @@
-01499nam  2200361   4500001000400000003004700004010004700051020001700098021002000115039001900135090000800154091001900162099001300181100004100194101000800235102000700243105001800250106000600268200017600274210005600450215009700506461003300603610001000636619001800646676001000664700007200674702005000746702006100796702004000857801007000897930002900967995014100996869http://catalogue.bnf.fr/ark:/12148/cb37710769n  a2-84580-144-0bbr.dPrix : 59 F : 8,99 EUR  aFRb00201411  aFRbDL 01-54100  oOPLa027037083  a869  b20140822c1a2  eUZELtBD  a20020103d2001    m  y0frey50      ba1 afre  aFR  aa   t   00|a|  ar1 aVagabondbTexte impriméh4fTakehiko Inouégd'après l'oeuvre d'Eiji Yoshikawa, "Miyamoto Musashi"g[trad. du japonais par Jacques Lalloz]g[adapté par Philippe Marcel]  aPariscÉd. Tonkamd2001e35-RennesgImpr. Oberthur  aNon paginé [ca 206] p.cill. en noir et en coul., couv. ill., jaquette ill. en coul.d19 cm 0tVagabondv4aTakehiko Inoué  aManga  aManga9213399  a741.5  313527612935oISNI0000000121039965aInouebTakehikof1967-....4070  311929525936aYoshikawabEijif1892-19624100  312040286937oISNI0000000108815456aLallozbJacques4730  312320807938aMarcelbPhilippe4010 0aFRbFR-751131015c20020103gAFNORhFRBNF3771076900000002intermrc  5FR-751131010:2003-196584  fSTJ000095w2014-06-2630009117cMTRSTJ20kBD VAG 452012-09-07o0efiction adulte bande dessineesddcrBDm2014-06-19bMTRSTJ6MTRSTJ
\ No newline at end of file
+01557nam  2200385   4500001000400000003004700004010004700051020001700098021002000115039001900135090000800154091001900162099001300181100004100194101000800235102000700243105001800250106000600268200017600274210005600450215009700506461003300603610001000636619001800646619002300664619001100687676001000698700007200708702005000780702006100830702004000891801007000931930002901001995014101030869http://catalogue.bnf.fr/ark:/12148/cb37710769n  a2-84580-144-0bbr.dPrix : 59 F : 8,99 EUR  aFRb00201411  aFRbDL 01-54100  oOPLa027037083  a869  b20140822c1a2  eUZELtBD  a20020103d2001    m  y0frey50      ba1 afre  aFR  aa   t   00|a|  ar1 aVagabondbTexte impriméh4fTakehiko Inouégd'après l'oeuvre d'Eiji Yoshikawa, "Miyamoto Musashi"g[trad. du japonais par Jacques Lalloz]g[adapté par Philippe Marcel]  aPariscÉd. Tonkamd2001e35-RennesgImpr. Oberthur  aNon paginé [ca 206] p.cill. en noir et en coul., couv. ill., jaquette ill. en coul.d19 cm 0tVagabondv4aTakehiko Inoué  aManga  aManga9213399  a   	Cicatrice	978  a98839  a741.5  313527612935oISNI0000000121039965aInouebTakehikof1967-....4070  311929525936aYoshikawabEijif1892-19624100  312040286937oISNI0000000108815456aLallozbJacques4730  312320807938aMarcelbPhilippe4010 0aFRbFR-751131015c20020103gAFNORhFRBNF3771076900000002intermrc  5FR-751131010:2003-196584  fSTJ000095w2014-06-2630009117cMTRSTJ20kBD VAG 452012-09-07o0efiction adulte bande dessineesddcrBDm2014-06-19bMTRSTJ6MTRSTJ
\ No newline at end of file
-- 
GitLab