From c1e0a629e4e338c10785a77dc087aeba2ea0b413 Mon Sep 17 00:00:00 2001
From: Ghislain Loas <ghislo@sandbox.pergame.net>
Date: Tue, 19 Jan 2016 09:32:32 +0100
Subject: [PATCH] hotline #36687 fix sql fatal error

---
 VERSIONS_HOTLINE/36687                         |  1 +
 library/Class/Catalogue.php                    | 10 +++++-----
 .../controllers/CatalogueControllerTest.php    |  2 +-
 tests/library/Class/CatalogueTest.php          | 18 ++++++++----------
 4 files changed, 15 insertions(+), 16 deletions(-)
 create mode 100644 VERSIONS_HOTLINE/36687

diff --git a/VERSIONS_HOTLINE/36687 b/VERSIONS_HOTLINE/36687
new file mode 100644
index 00000000000..7c2578c9401
--- /dev/null
+++ b/VERSIONS_HOTLINE/36687
@@ -0,0 +1 @@
+ - ticket #36687 : Correction d'une ambiguité sur les requetes sql des catalogues
diff --git a/library/Class/Catalogue.php b/library/Class/Catalogue.php
index 1f0b5682317..954c5307ef3 100644
--- a/library/Class/Catalogue.php
+++ b/library/Class/Catalogue.php
@@ -173,8 +173,8 @@ class CatalogueLoader extends Storm_Model_Loader {
 
     $parts = array_filter(explode(';', $docType));
     return (1 == count($parts)) ?
-      ('type_doc=' . $parts[0]) :
-      ('type_doc IN (' . implode(', ', $parts) .  ')');
+      ('notices.type_doc=' . $parts[0]) :
+      ('notices.type_doc IN (' . implode(', ', $parts) .  ')');
   }
 
 
@@ -322,7 +322,7 @@ class CatalogueLoader extends Storm_Model_Loader {
     return ['req_liste' => sprintf($sql, $fields ? implode(',', $fields) : '*')
             . $order_by . $limite,
             'req_comptage' => sprintf($sql, 'count(*)'),
-            'req_facettes' => sprintf($sql, 'notices.id_notice, type_doc, facettes')
+            'req_facettes' => sprintf($sql, 'notices.id_notice, notices.type_doc, facettes')
             . $limite,
             'req_ids' => sprintf($sql, 'notices.id_notice') . $order_by . $limite];
   }
@@ -466,7 +466,7 @@ class CatalogueLoader extends Storm_Model_Loader {
 
     return ['req_liste' => sprintf($sql, '*') . $order_by . $limite,
             'req_comptage' => sprintf($sql, 'count(*)'),
-            'req_facettes' => sprintf($sql, 'id_notice, type_doc, facettes') . $limite,
+            'req_facettes' => sprintf($sql, 'id_notice, notices.type_doc, facettes') . $limite,
             'req_ids' => sprintf($sql, 'notices.id_notice') . $order_by . $limite];
   }
 
@@ -703,7 +703,7 @@ class Class_Catalogue extends Storm_Model_Abstract {
      */
     $query =
       'update notices set facettes = clean_spaces(replace(facettes,"H'.$id_thesaurus.'","")) '.
-      'where type_doc not in ('.implode(',', [Class_TypeDoc::ARTICLE, Class_TypeDoc::RSS, Class_TypeDoc::SITE]).') '.
+      'where notices.type_doc not in ('.implode(',', [Class_TypeDoc::ARTICLE, Class_TypeDoc::RSS, Class_TypeDoc::SITE]).') '.
       'and match(facettes) against("+H'.$id_thesaurus.'" in boolean mode)';
 
     Zend_Registry::get('sql')->query($query);
diff --git a/tests/application/modules/admin/controllers/CatalogueControllerTest.php b/tests/application/modules/admin/controllers/CatalogueControllerTest.php
index 9f4975e685d..82c68019b27 100644
--- a/tests/application/modules/admin/controllers/CatalogueControllerTest.php
+++ b/tests/application/modules/admin/controllers/CatalogueControllerTest.php
@@ -569,7 +569,7 @@ class CatalogueControllerActionTesterTest extends AbstractControllerTestCase {
 
   /** @test */
   public function pageShouldDisplayRequest() {
-    $this->assertContains("select * from notices Where MATCH(facettes) AGAINST(' +(B1) +( D78308*)' IN BOOLEAN MODE) and type_doc IN (1, 3, 4, 5) and annee >= '2012' and annee <= '2012' order by alpha_titre  LIMIT 0,20",
+    $this->assertContains("select * from notices Where MATCH(facettes) AGAINST(' +(B1) +( D78308*)' IN BOOLEAN MODE) and notices.type_doc IN (1, 3, 4, 5) and annee >= '2012' and annee <= '2012' order by alpha_titre  LIMIT 0,20",
                           $this->_response->getBody());
   }
 
diff --git a/tests/library/Class/CatalogueTest.php b/tests/library/Class/CatalogueTest.php
index 3456c268c2c..aa33ba5da68 100644
--- a/tests/library/Class/CatalogueTest.php
+++ b/tests/library/Class/CatalogueTest.php
@@ -66,7 +66,7 @@ class CatalogueTestGetRequetesPanierWithIdUserAndIdPanier extends ModelTestCase
 
 
   public function testRequeteFacettes() {
-    $this->assertEquals("select id_notice, type_doc, facettes from notices where notices.clef_alpha in('STARWARS','JAMESBOND') and url_vignette > '' and url_vignette != 'NO' LIMIT 0,3",
+    $this->assertEquals("select id_notice, notices.type_doc, facettes from notices where notices.clef_alpha in('STARWARS','JAMESBOND') and url_vignette > '' and url_vignette != 'NO' LIMIT 0,3",
                         $this->requetes['req_facettes']);
   }
 
@@ -327,7 +327,7 @@ class CatalogueTestGetPagedNotices extends ModelTestCase {
   /** @test */
   public function withTypeDocShouldQueryOnColumn() {
     $this->_catalogue->setTypeDoc(7);
-    $this->_expectNoticeFindAllBy('type_doc=7');
+    $this->_expectNoticeFindAllBy('notices.type_doc=7');
     Class_Catalogue::getLoader()->loadNoticesFor($this->_catalogue);
   }
 
@@ -335,7 +335,7 @@ class CatalogueTestGetPagedNotices extends ModelTestCase {
   /** @test */
   public function withSeveralTypeDocShouldQueryOnColumn() {
     $this->_catalogue->setTypeDoc('7;10;12;');
-    $this->_expectNoticeFindAllBy('type_doc IN (7, 10, 12)');
+    $this->_expectNoticeFindAllBy('notices.type_doc IN (7, 10, 12)');
     Class_Catalogue::getLoader()->loadNoticesFor($this->_catalogue);
   }
 
@@ -468,7 +468,7 @@ class CatalogueTestGetRequetesWithFacettesAndNoCatalogue extends ModelTestCase {
 
   /** @test */
   public function requeteFacettesShouldBeSelectIdNoticeTypeDocFacet() {
-    $this->assertEquals('select notices.id_notice, type_doc, facettes from notices Where MATCH(facettes) AGAINST(\' +(T1, Y1)\' IN BOOLEAN MODE) LIMIT 5000', $this->_requetes['req_facettes']);
+    $this->assertEquals('select notices.id_notice, notices.type_doc, facettes from notices Where MATCH(facettes) AGAINST(\' +(T1, Y1)\' IN BOOLEAN MODE) LIMIT 5000', $this->_requetes['req_facettes']);
   }
 }
 
@@ -552,11 +552,11 @@ class CatalogueTestGetNoticesByPreferences extends ModelTestCase {
 
     $this->mock_sql
       ->whenCalled('fetchAllByColumn')
-      ->with("select notices.id_notice from notices Where type_doc=1 order by alpha_titre  LIMIT 0,25")
+      ->with("select notices.id_notice from notices Where notices.type_doc=1 order by alpha_titre  LIMIT 0,25")
       ->answers([23])
 
       ->whenCalled('query')
-      ->with([0 => 'update notices set facettes = clean_spaces(replace(facettes,"HCCCC0030","")) where type_doc not in (8,9,10) and match(facettes) against("+HCCCC0030" in boolean mode)'])
+      ->with([0 => 'update notices set facettes = clean_spaces(replace(facettes,"HCCCC0030","")) where notices.type_doc not in (8,9,10) and match(facettes) against("+HCCCC0030" in boolean mode)'])
       ->answers(true)
       ->beStrict();
 
@@ -1068,21 +1068,19 @@ class CatalogueGetAllNoticesIdsForDomaineTest extends Storm_Test_ModelTestCase {
 
 
 class CatalogueGetRequetesWithCatalogueTest extends ModelTestCase {
-  protected $_catalogue;
+  protected $_storm_default_to_volatile = true;
 
   public function setUp() {
     parent::setUp();
     $this->fixture('Class_Catalogue',
                    ['id' => 5,
                     'libelle' => 'My catalogue']);
-
-    $this->_requetes = Class_Catalogue::getRequetes(['id_catalogue' => 5]);
   }
 
 
   /** @test */
   public function requetesShouldBeEmpty() {
-    $this->assertEquals([], $this->_requetes);
+    $this->assertEquals([], Class_Catalogue::getRequetes(['id_catalogue' => 5]));
   }
 }
 
-- 
GitLab