From 1ce2b746151bdb164da1881468691b22e1ed796e Mon Sep 17 00:00:00 2001
From: Demian Katz <demian.katz@villanova.edu>
Date: Mon, 4 Nov 2013 14:09:12 -0500
Subject: [PATCH] Smarter COinS ID loading; added unit test.

---
 .../src/VuFind/RecordDriver/SolrDefault.php   |  33 +++--
 .../RecordDriver/SolrDefaultTest.php          | 125 ++++++++++++++++++
 2 files changed, 148 insertions(+), 10 deletions(-)
 create mode 100644 module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php

diff --git a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
index 7f9a5e77c36..4cd3787f111 100644
--- a/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
+++ b/module/VuFind/src/VuFind/RecordDriver/SolrDefault.php
@@ -646,23 +646,36 @@ class SolrDefault extends AbstractBase
     }
 
     /**
-     * Get the OpenURL parameters to represent this record (useful for the
-     * title attribute of a COinS span tag).
+     * Get the COinS identifier.
      *
-     * @return string OpenURL parameters.
+     * @return string
      */
-    public function getOpenURL()
+    protected function getCoinsID()
     {
         // Get the COinS ID -- it should be in the OpenURL section of config.ini,
         // but we'll also check the COinS section for compatibility with legacy
         // configurations (this moved between the RC2 and 1.0 releases).
-        $coinsID = isset($this->mainConfig->OpenURL->rfr_id)
-            ? $this->mainConfig->OpenURL->rfr_id
-            : $this->mainConfig->COinS->identifier;
-        if (empty($coinsID)) {
-            $coinsID = 'vufind.svn.sourceforge.net';
+        if (isset($this->mainConfig->OpenURL->rfr_id)
+            && !empty($this->mainConfig->OpenURL->rfr_id)
+        ) {
+            return $this->mainConfig->OpenURL->rfr_id;
+        }
+        if (isset($this->mainConfig->COinS->identifier)
+            && !empty($this->mainConfig->COinS->identifier)
+        ) {
+            return $this->mainConfig->COinS->identifier;
         }
+        return 'vufind.svn.sourceforge.net';
+    }
 
+    /**
+     * Get the OpenURL parameters to represent this record (useful for the
+     * title attribute of a COinS span tag).
+     *
+     * @return string OpenURL parameters.
+     */
+    public function getOpenURL()
+    {
         // Get a representative publication date:
         $pubDate = $this->getPublicationDates();
         $pubDate = empty($pubDate) ? '' : $pubDate[0];
@@ -671,7 +684,7 @@ class SolrDefault extends AbstractBase
         $params = array(
             'ctx_ver' => 'Z39.88-2004',
             'ctx_enc' => 'info:ofi/enc:UTF-8',
-            'rfr_id' => "info:sid/{$coinsID}:generator",
+            'rfr_id' => 'info:sid/' . $this->getCoinsID() . ':generator',
             'rft.title' => $this->getTitle(),
             'rft.date' => $pubDate
         );
diff --git a/module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php b/module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php
new file mode 100644
index 00000000000..58a5214ccbf
--- /dev/null
+++ b/module/VuFind/tests/unit-tests/src/VuFindTest/RecordDriver/SolrDefaultTest.php
@@ -0,0 +1,125 @@
+<?php
+/**
+ * SolrDefault Record Driver Test Class
+ *
+ * PHP version 5
+ *
+ * Copyright (C) Villanova University 2010.
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License version 2,
+ * as published by the Free Software Foundation.
+ *
+ * This program 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
+ *
+ * @category VuFind2
+ * @package  Tests
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @author   Preetha Rao <vufind-tech@lists.sourceforge.net>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/vufind2:unit_tests Wiki
+ */
+namespace VuFindTest\RecordDriver;
+use VuFind\RecordDriver\SolrDefault;
+
+/**
+ * SolrDefault Record Driver Test Class
+ *
+ * @category VuFind2
+ * @package  Tests
+ * @author   Demian Katz <demian.katz@villanova.edu>
+ * @author   David Maus <maus@hab.de>
+ * @author   Preetha Rao <vufind-tech@lists.sourceforge.net>
+ * @license  http://opensource.org/licenses/gpl-2.0.php GNU General Public License
+ * @link     http://vufind.org/wiki/vufind2:unit_tests Wiki
+ */
+class SolrDefaultTest extends \VuFindTest\Unit\TestCase
+{
+    /**
+     * Test an OpenURL for a book.
+     *
+     * @return void
+     */
+    public function testBookOpenURL()
+    {
+        $driver = $this->getDriver();
+        $this->assertEquals('ctx_ver=Z39.88-2004&ctx_enc=info%3Aofi%2Fenc%3AUTF-8&rfr_id=info%3Asid%2Fvufind.svn.sourceforge.net%3Agenerator&rft.title=La+congiura+dei+Principi+Napoletani+1701+%3A+%28prima+e+seconda+stesura%29+%2F&rft.date=1992&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Abook&rft.genre=book&rft.btitle=La+congiura+dei+Principi+Napoletani+1701+%3A+%28prima+e+seconda+stesura%29+%2F&rft.series=Vico%2C+Giambattista%2C+1668-1744.+Works.+1982+%3B&rft.au=Vico%2C+Giambattista%2C+1668-1744.&rft.pub=Centro+di+Studi+Vichiani%2C&rft.edition=Fictional+edition.&rft.isbn=8820737493', $driver->getOpenURL());
+    }
+
+    /**
+     * Test an OpenURL for an article.
+     *
+     * @return void
+     */
+    public function testArticleOpenURL()
+    {
+        $overrides = array(
+            'format' => array('Article'),
+            'container_title' => 'Fake Container',
+            'container_volume' => 'XVII',
+            'container_issue' => '6',
+            'container_start_page' => '12',
+        );
+        $driver = $this->getDriver($overrides);
+        $this->assertEquals('ctx_ver=Z39.88-2004&ctx_enc=info%3Aofi%2Fenc%3AUTF-8&rfr_id=info%3Asid%2Fvufind.svn.sourceforge.net%3Agenerator&rft.date=1992&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Ajournal&rft.genre=article&rft.issn=&rft.isbn=8820737493&rft.volume=XVII&rft.issue=6&rft.spage=12&rft.jtitle=Fake+Container&rft.atitle=La+congiura+dei+Principi+Napoletani+1701+%3A+%28prima+e+seconda+stesura%29+%2F&rft.au=Vico%2C+Giambattista%2C+1668-1744.&rft.format=Article&rft.language=Italian', $driver->getOpenURL());
+    }
+
+    /**
+     * Test an OpenURL for a journal.
+     *
+     * @return void
+     */
+    public function testJournalOpenURL()
+    {
+        $overrides = array(
+            'format' => array('Journal'),
+            'issn' => array('1234-5678'),
+        );
+        $driver = $this->getDriver($overrides);
+        $this->assertEquals('ctx_ver=Z39.88-2004&ctx_enc=info%3Aofi%2Fenc%3AUTF-8&rfr_id=info%3Asid%2Fvufind.svn.sourceforge.net%3Agenerator&rft.title=La+congiura+dei+Principi+Napoletani+1701+%3A+%28prima+e+seconda+stesura%29+%2F&rft.issn=1234-5678&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&rft.creator=Vico%2C+Giambattista%2C+1668-1744.&rft.pub=Centro+di+Studi+Vichiani%2C&rft.format=Journal&rft.language=Italian', $driver->getOpenURL());
+    }
+
+    /**
+     * Test an OpenURL for an unknown material type.
+     *
+     * @return void
+     */
+    public function testUnknownTypeOpenURL()
+    {
+        $overrides = array(
+            'format' => array('Thingie'),
+        );
+        $driver = $this->getDriver($overrides);
+        $this->assertEquals('ctx_ver=Z39.88-2004&ctx_enc=info%3Aofi%2Fenc%3AUTF-8&rfr_id=info%3Asid%2Fvufind.svn.sourceforge.net%3Agenerator&rft.title=La+congiura+dei+Principi+Napoletani+1701+%3A+%28prima+e+seconda+stesura%29+%2F&rft.date=1992&rft_val_fmt=info%3Aofi%2Ffmt%3Akev%3Amtx%3Adc&rft.creator=Vico%2C+Giambattista%2C+1668-1744.&rft.pub=Centro+di+Studi+Vichiani%2C&rft.format=Thingie&rft.language=Italian', $driver->getOpenURL());
+    }
+
+    /**
+     * Get a record driver with fake data.
+     *
+     * @param array $overrides Fixture fields to override.
+     *
+     * @return SolrDefault
+     */
+    protected function getDriver($overrides = array())
+    {
+        $fixture = json_decode(
+            file_get_contents(
+                realpath(
+                    VUFIND_PHPUNIT_MODULE_PATH . '/fixtures/misc/testbug2.json'
+                )
+            ),
+            true
+        );
+
+        $record = new SolrDefault();
+        $record->setRawData($overrides + $fixture['response']['docs'][0]);
+        return $record;
+    }
+}
\ No newline at end of file
-- 
GitLab