From 80a27a5e9f299b08f7135d60d199061fbe949af1 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Fri, 2 Oct 2015 15:49:47 +0200
Subject: [PATCH] hotline #23808 - Code refactoring in PhaseReview + add tests

---
 library/Class/AvisNotice.php                  |  8 ----
 .../Cosmogramme/Integration/PhaseReviews.php  | 24 +++++-------
 .../Integration/PhaseReviewsTest.php          | 37 ++++++++++++++-----
 3 files changed, 36 insertions(+), 33 deletions(-)

diff --git a/library/Class/AvisNotice.php b/library/Class/AvisNotice.php
index 6eeb31c449f..9eab3887287 100644
--- a/library/Class/AvisNotice.php
+++ b/library/Class/AvisNotice.php
@@ -67,14 +67,6 @@ class AvisNoticeLoader extends Storm_Model_Loader {
   }
 
 
-  public function findAllAfter($record_id) {
-    $where = "id > " . $record_id ;
-    return Class_AvisNotice::findAllBy(['where' => $where,
-                                        'order' => 'id',
-                                        'limit' => '100']);
-  }
-
-
   /*
     Renvoie les avis correspondants aux préférences de recherches données
     - id_catalogue: le catalogue de notices dont on veut récupérer les avis
diff --git a/library/Class/Cosmogramme/Integration/PhaseReviews.php b/library/Class/Cosmogramme/Integration/PhaseReviews.php
index 7f2c917b534..ee566bfaed1 100644
--- a/library/Class/Cosmogramme/Integration/PhaseReviews.php
+++ b/library/Class/Cosmogramme/Integration/PhaseReviews.php
@@ -27,7 +27,6 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ
 
   public function __construct($phase, $log, $chrono) {
     parent::__construct($phase, $log, $chrono);
-    $this->_previous_records = [];
 
   }
 
@@ -36,10 +35,7 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ
 
     $new_phase
       ->resetDatas()
-      ->setData('nombre', 0)
-      ->setData('pointeur_notice', 0)
-      ->setData('pointeur',
-                $last_update_date ? $last_update_date : '0000-00-00 00:00:00');
+      ->setData('nombre', 0);
 
     $this->_chrono
       ->startOnFile()
@@ -48,27 +44,23 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ
 
 
   public function _execute() {
-    xdebug_break();
-    $this->_setData('pointeur_notice', 0);
-    while ($records = Class_AvisNotice::findAllAfter($this->_getData('pointeur_notice'))) {
+    $page = 1;
+    while ($records = Class_AvisNotice::findAllBy(['order' => 'id',
+                                                 'limitPage' => [$page, 500]])) {
 
       if (0 == ($this->_getData('nombre') % 100))
         $this->_log->ecrire('<span class="vert"> ' . $this->_getData('nombre') . ' records updated </span>');
-      if ($this->_previous_records && (0 == count(array_filter(array_diff($this->_previous_records, $records)))))
-        break;
-
-      $this->_previous_records = $records;
 
       $this->runUpdateForRecords($records);
 
       $this->_resetDbConnection();
 
+      $page++;
+
     }
 
     $this->_log->ecrire('<span class="vert">'.
                         'Tous les avis ont été mis à jour :' .
-                        ' pointeur avis : ' . $this->_getData('pointeur_avis') .
-                        ' pointeur : ' . $this->_getData('pointeur') .
                         '<br>' . $this->_getData('nombre') . ' avis traités.</span>');
 
     $msg = '<span class="vert">Temps de traitement : '
@@ -97,7 +89,9 @@ class Class_Cosmogramme_Integration_PhaseReviews extends Class_Cosmogramme_Integ
     if ($notice = $record->getFirstNotice())
       $record->setIdNotice($notice->getId())->save();
 
-    $this->_setData('pointeur_notice', $record->getId());
+    if (!$notice)
+      $record->setFlags(Class_AvisNotice::ORPHAN_FLAG)->save();
+
     $this->_incrementData('nombre');
 
   }
diff --git a/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php b/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php
index ce21f084dc5..4d2c8ff175d 100644
--- a/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php
+++ b/tests/library/Class/Cosmogramme/Integration/PhaseReviewsTest.php
@@ -63,12 +63,10 @@ class PhaseReviewsExpectedPreviousPhaseTest extends PhaseReviewsTestCase {
 
     $this->millenium = $this->fixture('Class_Notice', ['id' => 816,
                                                        'titre_principal' => 'Millenium',
-                                                       'clef_alpha' => 'MILLENIUM',
+                                                       'clef_oeuvre' => 'MILLENIUM',
                                                        'auteur_principal' => 'Stieg Larsson',
                                                        'url_vignette' => '',
-                                                       'url_image' => ''
-                                                       ]
-    );
+                                                       'url_image' => '']);
 
 
     $this->avis_millenium =  $this->fixture('Class_AvisNotice', ['id' => 13,
@@ -78,14 +76,26 @@ class PhaseReviewsExpectedPreviousPhaseTest extends PhaseReviewsTestCase {
                                                                  'date_avis' => '2011-03-18 13:00:00',
 
                                                                  'statut' => 0,
+                                                                 'clef_oeuvre' => 'MILLENIUM',
                                                                  'abon_ou_bib'=>1 ,
                                                                  'notices' => [] ]);
 
+    $this->avis_tout_seul =  $this->fixture('Class_AvisNotice', ['id' => 14,
+                                                                 'entete' => "I'm an orphan",
+                                                                 'avis' => '<div><ul><li>Suspense Intense !</li><li>plop !</li></ul></div>',
+                                                                 'id_notice' => 0,
+                                                                 'note' => 5,
+                                                                 'date_avis' => '2011-03-18 13:00:00',
 
+                                                                 'statut' => 0,
+                                                                 'clef_oeuvre' => 'HUNT',
+                                                                 'abon_ou_bib'=>1 ,
+                                                                 'notices' => [] ]);
     $this
-      ->onLoaderOfModel('Class_AvisNotice')
-      ->whenCalled('findAllAfter')
-      ->answers([$this->avis_millenium]);
+      ->onLoaderOfModel('Class_Notice')
+      ->whenCalled('findAllBy')
+      ->with(['where' => 'clef_oeuvre like "HUNT-%"'])
+      ->answers([]);
   }
 
 
@@ -103,11 +113,18 @@ class PhaseReviewsExpectedPreviousPhaseTest extends PhaseReviewsTestCase {
 
   /** @test */
   public function logShouldContainsProcessedRecordCount() {
-    $this->assertLogContains('1 avis traités');
+    $this->assertLogContains('2 avis traités');
   }
 
-/** @test */
+
+  /** @test */
   public function noticeMilleniumShouldBeAttached() {
-    $this->assertEquals(816,Class_AvisNotice::find(13)->getIdNotice());
+    $this->assertEquals(816, Class_AvisNotice::find(13)->getIdNotice());
+  }
+
+
+  /** @test */
+  public function review14ShouldNotBeAttached() {
+    $this->assertEquals(0, Class_AvisNotice::find(14)->getIdNotice());
   }
 }
-- 
GitLab