From 7b311e7e3bad8a01df732649719f368e6245f964 Mon Sep 17 00:00:00 2001
From: gloas <gloas@afi-sa.fr>
Date: Mon, 13 Nov 2017 16:15:26 +0100
Subject: [PATCH] hotline #67232 fix migration patch

---
 cosmogramme/sql/patch/patch_337.php           | 13 ++++--
 .../Class/Migration/AlbumEditorToNotes.php    | 46 +++++++++++++++++++
 tests/db/UpgradeDBTest.php                    | 31 +++++++++++--
 3 files changed, 82 insertions(+), 8 deletions(-)
 create mode 100644 library/Class/Migration/AlbumEditorToNotes.php

diff --git a/cosmogramme/sql/patch/patch_337.php b/cosmogramme/sql/patch/patch_337.php
index 0f2ae5b1ed6..165708668e2 100644
--- a/cosmogramme/sql/patch/patch_337.php
+++ b/cosmogramme/sql/patch/patch_337.php
@@ -2,10 +2,17 @@
 $adapter = Zend_Db_Table_Abstract::getDefaultAdapter();
 
 try{
-  $adapter->query('alter table album drop column auteur');
-  $adapter->query('alter table album drop column editeur');
   $adapter->query('update variables set hidden=0, verrou="" where clef="nature_docs"');
   $adapter->query('alter table album add column created_at DATE, add KEY created_at (created_at)');
   $adapter->query('alter table notices add column created_at DATE, add KEY created_at (created_at)');
   $adapter->query('update album set created_at=NOW()');
-} catch(Exception $e) {}
+} catch(Exception $e) {
+}
+
+(new Class_Migration_AlbumEditorToNotes())->run();
+
+try {
+  $adapter->query('alter table album drop column auteur');
+  $adapter->query('alter table album drop column editeur');
+} catch(Exception $e) {
+}
diff --git a/library/Class/Migration/AlbumEditorToNotes.php b/library/Class/Migration/AlbumEditorToNotes.php
new file mode 100644
index 00000000000..4deb4d6665d
--- /dev/null
+++ b/library/Class/Migration/AlbumEditorToNotes.php
@@ -0,0 +1,46 @@
+<?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_AlbumEditorToNotes {
+  public function run() {
+    try {
+      $albums = Class_Album::findAllBy(['where' => 'editeur is not null and editeur != ""']);
+    } catch (Exception $e) {
+      return;
+    }
+
+    foreach($albums as $album) {
+      $this->_migrateEditor($album);
+    }
+  }
+
+
+  protected function _migrateEditor($album) {
+    $editors = $album->getEditors();
+    if (in_array($album->getEditeur(), $editors))
+      return;
+
+    $album
+      ->addEditor($album->getEditeur())
+      ->save();
+  }
+}
+?>
\ No newline at end of file
diff --git a/tests/db/UpgradeDBTest.php b/tests/db/UpgradeDBTest.php
index 1fc6b11a5ee..6c567b73b93 100644
--- a/tests/db/UpgradeDBTest.php
+++ b/tests/db/UpgradeDBTest.php
@@ -1761,7 +1761,7 @@ class UpgradeDB_336_Test extends UpgradeDBTestCase {
 
 
 class UpgradeDB_337_Test extends UpgradeDBTestCase {
-  protected static $album_id;
+  protected static $first_album_id, $second_album_id;
 
   public function prepare() {
     $this
@@ -1771,13 +1771,22 @@ class UpgradeDB_337_Test extends UpgradeDBTestCase {
       ->silentQuery('ALTER TABLE notices drop column created_at')
       ->silentQuery('UPDATE variables set hidden=1, verrou="checked" where clef="nature_docs"');
 
-    $this->query('insert into album(titre, type_doc_id, url_origine) values("1dtouch", 114, "http://export.1dtouch.com/oai")');
-    static::$album_id = $this->query('select id from album order by id desc limit 1')->fetch()['id'];
+    $this->query('insert into album(titre, type_doc_id, url_origine, editeur) values("1dtouch", 114, "http://export.1dtouch.com/oai", "1DTouch")');
+
+    static::$first_album_id = $this->query('select id from album order by id desc limit 1')->fetch()['id'];
+
+    $this->query('insert into album(titre, type_doc_id, url_origine, editeur, notes) values("bibifoc", 114, "http://export.1dtouch.com/oai", "1DTouch", \'' . serialize([['field' => '210', 'data' => [['c', 'Touchstone'],['c', '1DTouch']]]]) . '\')');
+
+
+    static::$second_album_id = $this->query('select id from album order by id desc limit 1')->fetch()['id'];
   }
 
 
   public function tearDown() {
-    if ($album = Class_Album::find(static::$album_id))
+    if ($album = Class_Album::find(static::$first_album_id))
+      $album->delete();
+
+    if ($album = Class_Album::find(static::$second_album_id))
       $album->delete();
 
     parent::tearDown();
@@ -1826,9 +1835,21 @@ class UpgradeDB_337_Test extends UpgradeDBTestCase {
   }
 
 
+  /** @test */
+  public function firstAlbumEditorsShouldContains1DTouch() {
+    $this->assertEquals(['1DTouch'], Class_Album::find(static::$first_album_id)->getEditors());
+  }
+
+
+  /** @test */
+  public function secoundAlbumEditorsShouldContains1DTouchOnce() {
+    $this->assertEquals(['Touchstone', '1DTouch'], Class_Album::find(static::$second_album_id)->getEditors());
+  }
+
+
   /** @test */
   public function createdAtShouldHaveValue() {
-    $result = $this->query('select created_at from album where id=' . static::$album_id)
+    $result = $this->query('select created_at from album where id=' . static::$first_album_id)
                    ->fetch();
 
     $this->assertNotNull($result['created_at']);
-- 
GitLab