From 02b881e71ad24b99142e1a8ba1d8c12903c0bca6 Mon Sep 17 00:00:00 2001
From: pbarroca <pbarroca@afi-sa.fr>
Date: Fri, 22 Dec 2017 15:32:36 +0100
Subject: [PATCH] hotline #64820 : migrate to new OAI url

---
 VERSIONS_HOTLINE/64820                        |  1 +
 cosmogramme/sql/patch/patch_339.php           |  2 +
 ...eDeLaMusiqueBecomesPhilharmonieDeParis.php | 57 +++++++++++++++++++
 tests/db/UpgradeDBTest.php                    | 51 +++++++++++++++++
 4 files changed, 111 insertions(+)
 create mode 100644 VERSIONS_HOTLINE/64820
 create mode 100644 cosmogramme/sql/patch/patch_339.php
 create mode 100644 library/Class/Migration/CiteDeLaMusiqueBecomesPhilharmonieDeParis.php

diff --git a/VERSIONS_HOTLINE/64820 b/VERSIONS_HOTLINE/64820
new file mode 100644
index 00000000000..07248376110
--- /dev/null
+++ b/VERSIONS_HOTLINE/64820
@@ -0,0 +1 @@
+ - ticket #64820 : Connecteur Cité de la musique : prise en charge du nouveau serveur OAI
\ No newline at end of file
diff --git a/cosmogramme/sql/patch/patch_339.php b/cosmogramme/sql/patch/patch_339.php
new file mode 100644
index 00000000000..a024126256e
--- /dev/null
+++ b/cosmogramme/sql/patch/patch_339.php
@@ -0,0 +1,2 @@
+<?php
+(new Class_Migration_CiteDeLaMusiqueBecomesPhilharmonieDeParis())->run();
\ No newline at end of file
diff --git a/library/Class/Migration/CiteDeLaMusiqueBecomesPhilharmonieDeParis.php b/library/Class/Migration/CiteDeLaMusiqueBecomesPhilharmonieDeParis.php
new file mode 100644
index 00000000000..532d2eae344
--- /dev/null
+++ b/library/Class/Migration/CiteDeLaMusiqueBecomesPhilharmonieDeParis.php
@@ -0,0 +1,57 @@
+<?php
+/**
+ * Copyright (c) 2012-2017, 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 Class_Migration_CiteDeLaMusiqueBecomesPhilharmonieDeParis {
+  const URL = 'http://pad.philharmoniedeparis.fr/EXPLOITATION/oaiserver.ashx';
+
+  public function run() {
+    $cite = new Class_AdminVar_CiteDeLaMusique();
+    if (!$cite->isEnabled())
+      return $this;
+
+    $this
+      ->updateAdminVar()
+      ->updateUrlOrigine()
+      ->implyFullHarvest()
+      ;
+  }
+
+
+  protected function updateAdminVar() {
+    Class_AdminVar::set('CITEDELAMUSIQUE', static::URL);
+    return $this;
+  }
+
+
+  protected function updateUrlOrigine() {
+    Zend_Db_Table::getDefaultAdapter()
+      ->query('update album set url_origine="' . static::URL . '"');
+    return $this;
+  }
+
+
+  protected function implyFullHarvest() {
+    Zend_Db_Table::getDefaultAdapter()
+      ->query('delete from harvest_log where type_doc=' . Class_TypeDoc::CITEDELAMUSIQUE);
+    return $this;
+  }
+}
diff --git a/tests/db/UpgradeDBTest.php b/tests/db/UpgradeDBTest.php
index 5dcef8ae354..8d1e30c8a37 100644
--- a/tests/db/UpgradeDBTest.php
+++ b/tests/db/UpgradeDBTest.php
@@ -1793,3 +1793,54 @@ class UpgradeDB_338_Test extends UpgradeDBTestCase {
     $this->assertEquals('$2y$', substr($datas['password'], 0, 4));
   }
 }
+
+
+
+class UpgradeDB_339_Test extends UpgradeDBTestCase {
+  protected static $album_id;
+
+  public function prepare() {
+      $this->query('replace into bib_admin_var (clef, valeur) values
+ ("CITEDELAMUSIQUE", "http://media.citedelamusique.fr/EXTRANET/oaiserver.ashx"),
+ ("CITEDELAMUSIQUE_ID", "78-testing-test"),
+ ("CITEDELAMUSIQUE_SETS", "videos")');
+
+      $this->query('insert into album (titre, type_doc_id, url_origine) values("pad", 119, "http://media.citedelamusique.fr/EXTRANET/oaiserver.ashx")');
+      static::$album_id = $this->query('select id from album order by id desc limit 1')->fetch()['id'];
+
+      $this->query('insert into harvest_log (type_doc, end_date) values(119, "2017-12-22")');
+  }
+
+
+  public function tearDown() {
+    if ($album = Class_Album::find(static::$album_id))
+      $album->delete();
+
+    parent::tearDown();
+  }
+
+
+  /** @test */
+  public function adminPasswordShouldBeHashed() {
+    $datas = $this->query('select valeur from bib_admin_var where clef="CITEDELAMUSIQUE"')
+                  ->fetch();
+
+    $this->assertEquals('http://pad.philharmoniedeparis.fr/EXPLOITATION/oaiserver.ashx',
+                        $datas['valeur']);
+  }
+
+
+  /** @test */
+  public function urlOrigineShouldBeConverted() {
+    $datas = $this->query('select url_origine from album where id=' . static::$album_id)->fetch();
+    $this->assertEquals('http://pad.philharmoniedeparis.fr/EXPLOITATION/oaiserver.ashx',
+                        $datas['url_origine']);
+  }
+
+
+  /** @test */
+  public function harvestLogShouldBeDeleted() {
+    $datas = $this->query('select count(*) as numberOf from harvest_log where type_doc=119')->fetch();
+    $this->assertEquals(0, $datas['numberOf']);
+  }
+}
-- 
GitLab