From 27301b3732a78b054e151885d096dd89684f0693 Mon Sep 17 00:00:00 2001
From: Alex Arnaud <alex.arnaud@biblibre.com>
Date: Tue, 29 Sep 2015 15:28:51 +0200
Subject: [PATCH] dev #29524 - Fix test failures and move process of checking
 consent in a plugin

---
 .../opac/controllers/IndexController.php      |  2 -
 .../ZendAfi/Controller/Plugin/CnilConsent.php | 29 ++++++++++++++
 library/startup.php                           |  1 +
 .../opac/controllers/IndexControllerTest.php  | 38 ++++++++++---------
 4 files changed, 51 insertions(+), 19 deletions(-)
 create mode 100644 library/ZendAfi/Controller/Plugin/CnilConsent.php

diff --git a/application/modules/opac/controllers/IndexController.php b/application/modules/opac/controllers/IndexController.php
index 38c0a43e1b7..f5e211b6db2 100644
--- a/application/modules/opac/controllers/IndexController.php
+++ b/application/modules/opac/controllers/IndexController.php
@@ -25,8 +25,6 @@ class IndexController extends Zend_Controller_Action {
     $viewRenderer = $this->getHelper('ViewRenderer');
     $viewRenderer->setLayoutScript('portail.phtml');
 
-    (new Class_Cnil())->trackConsent();
-
     if (array_keys($this->getRequest()->getParams()) == ['controller', 'action', 'module', 'current_module', 'q']) {
       $this->_redirect('recherche?'.http_build_query(['q' => $this->_getParam('q')]));
     }
diff --git a/library/ZendAfi/Controller/Plugin/CnilConsent.php b/library/ZendAfi/Controller/Plugin/CnilConsent.php
new file mode 100644
index 00000000000..14544081a50
--- /dev/null
+++ b/library/ZendAfi/Controller/Plugin/CnilConsent.php
@@ -0,0 +1,29 @@
+<?php
+/**
+ * Copyright (c) 2012, 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
+ */
+//////////////////////////////////////////////////////////////////////////////////////////
+// OPAC3 : Conformité CNIL (consentement avant l'insertion de cookies)
+//////////////////////////////////////////////////////////////////////////////////////////
+
+class ZendAfi_Controller_Plugin_CnilConsent extends Zend_Controller_Plugin_Abstract {
+  function preDispatch(Zend_Controller_Request_Abstract $request) {
+    (new Class_Cnil())->trackConsent();
+  }
+}
diff --git a/library/startup.php b/library/startup.php
index 5860b29546e..e10a3826b4a 100644
--- a/library/startup.php
+++ b/library/startup.php
@@ -276,6 +276,7 @@ function newFrontController() {
     ->registerPlugin(new ZendAfi_Controller_Plugin_CustomFields())
     ->registerPlugin(new ZendAfi_Controller_Plugin_Lectura())
     ->registerPlugin(new ZendAfi_Controller_Plugin_InspectorGadget())
+    ->registerPlugin(new ZendAfi_Controller_Plugin_CnilConsent())
     ->setParam('useDefaultControllerAlways', false);
 }
 
diff --git a/tests/application/modules/opac/controllers/IndexControllerTest.php b/tests/application/modules/opac/controllers/IndexControllerTest.php
index 6638f456d36..735d5d6510f 100644
--- a/tests/application/modules/opac/controllers/IndexControllerTest.php
+++ b/tests/application/modules/opac/controllers/IndexControllerTest.php
@@ -318,6 +318,21 @@ class IndexControllerRewriteUrlTest extends AbstractControllerTestCase {
 abstract class IndexControllerCnilTrackingTestCase extends AbstractControllerTestCase {
   protected $_storm_default_to_volatile = true;
   protected $_expected_message = 'showNotification({"message":"En poursuivant votre navigation sur ce site, vous acceptez l\'utilisation de cookies.","autoClose":false';
+  protected $_cookie_jar;
+
+  public function setUp() {
+    parent::setUp();
+    Class_Cnil::setTimeSource($this->mock()
+                              ->whenCalled('time')
+                              ->answers(strtotime('2015-09-25 15:45:54')));
+
+    Class_Cnil::setCookieJar($this->_cookie_jar = $this->mock()
+                             ->whenCalled('setcookie')
+                             ->with('cnil' . md5(BASE_URL),
+                                    1,
+                                    1476884754)
+                             ->answers(null));
+  }
 
   public function tearDown() {
     Class_Cnil::setCookieJar(null);
@@ -331,22 +346,8 @@ abstract class IndexControllerCnilTrackingTestCase extends AbstractControllerTes
 class IndexControllerCnilTrackingFirstVisitTest
   extends IndexControllerCnilTrackingTestCase {
 
-  protected $_cookie_jar;
-
   public function setUp() {
     parent::setUp();
-
-    Class_Cnil::setTimeSource($this->mock()
-                              ->whenCalled('time')
-                              ->answers(strtotime('2015-09-25 15:45:54')));
-
-    Class_Cnil::setCookieJar($this->_cookie_jar = $this->mock()
-                             ->whenCalled('setcookie')
-                             ->with('cnil8801b6c24c4d369a55a96252ed121d5c',
-                                    1,
-                                    1476884754)
-                             ->answers(null));
-
     $this->dispatch('/', true);
   }
 
@@ -368,13 +369,16 @@ class IndexControllerCnilTrackingFirstVisitTest
 class IndexControllerCnilTrackingSecondVisitTest
   extends IndexControllerCnilTrackingTestCase {
 
-  /** @test */
-  public function shouldNotDisplayCnilMessage() {
+  public function setUp() {
+    parent::setUp();
     $this->dispatch('/', true);
     $this->_response->setBody('');
     Class_ScriptLoader::resetInstance();
-
     $this->dispatch('/', true);
+  }
+
+  /** @test */
+  public function shouldNotDisplayCnilMessageOf() {
     $this->assertNotXPathContentContains('//script', $this->_expected_message);
   }
 }
-- 
GitLab