Project

General

Profile

« Previous | Next » 

Revision 2f65af04

Added by Andreas Kohlbecker almost 5 years ago

fix #8304 redirect from name to taxon page depending on drupal path argument, with test

View differences:

modules/cdm_dataportal/cdm_dataportal.module
586 586
  $items['cdm_dataportal/name/%'] = array(
587 587
    // 'page callback' => 'cdm_dataportal_view_name',
588 588
    'page callback' => 'cdm_dataportal_name_page_view',
589
    'page arguments' => array(2, 3, 4),
589
    'page arguments' => array(2, 3, 4, 5),
590 590
    'access arguments' => array('access cdm content'),
591 591
    'type' => MENU_CALLBACK,
592 592
  );
......
1715 1715
 *   The uuid of the CDM TaxonName to show a name page for
1716 1716
 * @param string $taxon_to_hide_uuid
1717 1717
 *   A taxon which should not be displayed in the taxon list
1718
 * @param $redirect_to_taxon
1719
 *   Automatically redirect to the related taxon if there is only one
1720
 *   accepted taxon for this name.
1718 1721
 * @param string $highlite_synonym_uuid
1719 1722
 *   Optinal parameter wich takes another taxon uuid, if given the
1720 1723
 *   target taxon pages will show the syonymy tab where the taxon
......
1724 1727
 * @return mixed
1725 1728
 *   The formatted name page as node.
1726 1729
 */
1727
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = NULL) {
1730
function cdm_dataportal_name_page_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid = NULL, $redirect_to_taxon = '') {
1728 1731

  
1729 1732
  cdm_check_valid_portal_page();
1730 1733

  
1731
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $synonym_uuid);
1734
  // redirect_to_taxon is a path element of the name page path, see path_to_name()
1735
  $do_redirect_to_taxon = $redirect_to_taxon == 'redirect_to_taxon';
1736
  $taxonname_page = cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $do_redirect_to_taxon, $synonym_uuid);
1732 1737
  if (!empty($taxonname_page)) {
1733 1738
    return cdm_node_show(NODETYPE_NAME, $taxon_name_uuid, $taxonname_page->title, $taxonname_page->content);
1734 1739
  }
......
1755 1760
 *   The uuid of the CDM TaxonName to show a name page for
1756 1761
 * @param string $taxon_to_hide_uuid
1757 1762
 *   A taxon which should not be displayed in the taxon list
1763
 * @param $redirect_to_taxon
1764
 *   Automatically redirect to the related taxon if there is only one
1765
 *   accepted taxon for this name.
1758 1766
 * @param string $highlite_synonym_uuid
1759
 *   Optinal parameter wich takes another taxon uuid, if given the
1760
 *   target taxon pages will show the syonymy tab where the taxon
1761
 *   refenrenced by the $highlite_synonym_uuid will be highlighted
1767
 *   Optional parameter which takes another taxon uuid, if given the
1768
 *   target taxon pages will show the synonymy tab where the taxon
1769
 *   referenced by the $highlite_synonym_uuid will be highlighted
1762 1770
 *   in case it is found on this page.
1763 1771
 *
1764
 * @return object
1772
 * @return object|boolean
1765 1773
 *   An object with two fields:
1766 1774
 *     - title: the page title
1767 1775
 *     - content: the page content
1776
 *   or FALSE
1777
 *
1768 1778
 */
1769
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $highlite_synonym_uuid = NULL) {
1779
function cdm_dataportal_name_view($taxon_name_uuid, $taxon_to_hide_uuid, $redirect_to_taxon, $highlite_synonym_uuid = NULL) {
1770 1780
  // Getting the full taxonname object from the server.
1771 1781
  $taxon_name = cdm_ws_get(CDM_WS_PORTAL_NAME, array($taxon_name_uuid));
1772 1782
  if (!$taxon_name) {
......
1792 1802
    }
1793 1803
  }
1794 1804
  // Show the taxa list or go to the singular taxon.
1795
  if (sizeof($taxon_pager->records) == 1) {// Single taxon case.
1805
  if (sizeof($taxon_pager->records) == 1 && $redirect_to_taxon) {
1806
    // redirect to the taxon if there is only one.
1796 1807
    $singleTaxon = array_pop($taxon_pager->records);
1797 1808
    if ($singleTaxon->class != "Taxon") {
1798 1809
      // It is a Synonym -> look for the accepted.
......
1814 1825
      }
1815 1826
    }
1816 1827
  }
1817
  else {// More than one taxa case.
1828
  else {
1829
    // display the name page content
1818 1830
    $taxon_name_page = new stdClass();
1819 1831
    $taxon_name_page->title = theme('cdm_name_page_title', array('taxon_name' => $taxon_name));
1832
    $content = array();
1833

  
1834
    // name relationships
1835
    // TODO ....
1836
    // related taxa
1820 1837
    if ($taxon_pager->records) {
1821
      $taxon_name_page->content = compose_list_of_taxa($taxon_pager->records);
1838
      $content['related_taxa'] = compose_list_of_taxa($taxon_pager->records);
1822 1839
    }
1823 1840
    else {
1824
      $taxon_name_page->content = 'This name is not assigned to a taxon.';
1841
      $content['related_taxa'] = markup_to_render_array('This name is not assigned to a taxon.');
1825 1842
    }
1843
    $taxon_name_page->content = $content;
1826 1844
    return $taxon_name_page;
1827 1845
  }
1828 1846
}
......
2215 2233
 *   target taxon pages will show the synonymy tab where the taxon
2216 2234
 *   referenced by the $highlite_synonym_uuid will be highlighted
2217 2235
 *   in case it is found on this page.
2236
 * @param $redirect_to_taxon
2237
 *   If true, the name page will redirect to the related taxon if there is a single one
2238
 *   for this name only.
2218 2239
 *
2219 2240
 * @return string
2220 2241
 *  URI path element as string
2221 2242
 */
2222
function path_to_name($name_uuid, $taxon_to_hide_uuid = NULL, $highlited_synonym_uuid  = NULL) {
2243
function path_to_name($name_uuid, $taxon_to_hide_uuid = NULL, $highlited_synonym_uuid  = NULL, $redirect_to_taxon = false) {
2223 2244
  $res = FALSE;
2224 2245
  if ($name_uuid) {
2225
    $res = 'cdm_dataportal/name/' . $name_uuid;
2226
  }
2227
  if($taxon_to_hide_uuid){
2228
    $res .= '/' . $taxon_to_hide_uuid;
2229
    if($highlited_synonym_uuid){
2230
      $res .= '/' . $highlited_synonym_uuid;
2231
    }
2246
    $res = 'cdm_dataportal/name/' . $name_uuid .
2247
    '/' . (is_uuid($taxon_to_hide_uuid) ? $taxon_to_hide_uuid : "null") .
2248
    '/' . (is_uuid($highlited_synonym_uuid) ? $highlited_synonym_uuid : "null") .
2249
    '/' . ($redirect_to_taxon ? "redirect_to_taxon" : "");
2232 2250
  }
2251

  
2233 2252
  return $res;
2234 2253
}
2235 2254

  
modules/cdm_dataportal/includes/name.inc
930 930
  $highlited_synonym_uuid = isset ($name->taxonBases[0]->uuid) ? $name->taxonBases[0]->uuid : '';
931 931
  if(!$show_name_cache_only){
932 932
    $relationship_markup = render_taxon_or_name($name,
933
      url(path_to_name($name->uuid, $current_taxon_uuid, $highlited_synonym_uuid))
933
      url(path_to_name($name->uuid, $current_taxon_uuid, $highlited_synonym_uuid, false))
934 934
    );
935 935
  } else {
936 936
    $relationship_markup = l(
937 937
      '<span class="' . html_class_attribute_ref($name) . '"">' . $name->nameCache . '</span>',
938
      path_to_name($name->uuid, $current_taxon_uuid, $highlited_synonym_uuid),
938
      path_to_name($name->uuid, $current_taxon_uuid, $highlited_synonym_uuid, false),
939 939
      array('html' => true)
940 940
    );
941 941
  }
modules/cdm_dataportal/theme/cdm_dataportal.descriptions.theme
415 415
      }
416 416

  
417 417
      $markup = '<li class="descriptionText DescriptionElement">';
418
      $name_used_in_source_link_to_show_use = l($description_source->originalNameString, path_to_name($originalTaxonUsedInSource->uuid), array(
419
        'absolute' => TRUE,
420
        'html' => TRUE,
421
      ));
418
      $name_used_in_source_link_to_show_use = l(
419
        $description_source->originalNameString,
420
        path_to_name($originalTaxonUsedInSource->uuid, null, null, true),
421
        array(
422
          'absolute' => TRUE,
423
          'html' => TRUE,
424
        ));
422 425
      $markup .= $name_used_in_source_link_to_show_use . ': ';
423 426
      $markup .= $useSummary;
424 427
      foreach ($description->sources as $element_source) {
src/main/java/eu/etaxonomy/dataportal/pages/PortalPage.java
10 10
import java.util.ArrayList;
11 11
import java.util.List;
12 12
import java.util.concurrent.TimeUnit;
13
import java.util.regex.Matcher;
14
import java.util.regex.Pattern;
13 15

  
14 16
import org.apache.commons.io.FileUtils;
15 17
import org.apache.log4j.Logger;
......
50 52

  
51 53
    public static final Logger logger = Logger.getLogger(PortalPage.class);
52 54

  
53
    protected final static String DRUPAL_PAGE_QUERY_BASE = "?q=";
55
    protected final static String DRUPAL_PAGE_QUERY = "q=";
56

  
57
    private final static Pattern DRUPAL_PAGE_QUERY_PATTERN = Pattern.compile("q=([^&]*)");
54 58

  
55 59
    public enum MessageType {status, warning, error} // needs to be lowercase
56 60

  
......
67 71

  
68 72
    /**
69 73
     * Implementations of this method will supply the relative
70
     * path to the Drupal page. This path will usally have the form
74
     * path to the Drupal page. This path will usually have the form
71 75
     * <code>cdm_dataportal/{nodetype}</code>. For example the taxon pages all
72 76
     * have the page base <code>cdm_dataportal/taxon</code>
73 77
     */
74 78
    protected abstract String getDrupalPageBase();
75 79

  
76
    private String drupalPagePath;
80
    private String initialDrupalPagePath;
77 81

  
78 82
    protected URL pageUrl;
79 83

  
......
124 128

  
125 129
        this.wait = new JUnitWebDriverWait(driver, WAIT_SECONDS);
126 130

  
127
        this.drupalPagePath = getDrupalPageBase() + (pagePathSuffix != null ? "/" + pagePathSuffix: "");
131
        this.initialDrupalPagePath = getDrupalPageBase() + (pagePathSuffix != null ? "/" + pagePathSuffix: "");
128 132

  
129
        this.pageUrl = new URL(context.getBaseUri().toString() + DRUPAL_PAGE_QUERY_BASE + drupalPagePath);
133
        this.pageUrl = new URL(context.getBaseUri().toString() + "?" + DRUPAL_PAGE_QUERY + initialDrupalPagePath);
130 134

  
131 135
        // tell browser to navigate to the page
132 136
        driver.get(pageUrl.toString());
......
238 242
    }
239 243

  
240 244
    public String getDrupalPagePath() {
241
        return drupalPagePath;
245
        URL currentURL;
246
        try {
247
            currentURL = new URL(driver.getCurrentUrl());
248
            if(currentURL.getQuery() != null && currentURL.getQuery().contains(DRUPAL_PAGE_QUERY)){
249
                Matcher m = DRUPAL_PAGE_QUERY_PATTERN.matcher(currentURL.getQuery());
250
                m.matches();
251
                return m.group(1);
252
            } else {
253
                return currentURL.getPath().replaceFirst("^"+ context.getBaseUri().getPath(), "");
254
            }
255
        } catch (MalformedURLException e) {
256
            throw new RuntimeException(e);
257
        }
258
    }
259

  
260
    /**
261
     * Returns the the page path with which the page has initially been loaded.
262
     * Due to redirects the actual page path which can be retrieved by
263
     * {@link #getDrupalPagePath()} might be different
264
     *
265
     */
266
    public String getInitialDrupalPagePath() {
267
        return initialDrupalPagePath;
242 268
    }
243 269

  
244 270
    /**
src/test/java/eu/etaxonomy/dataportal/selenium/tests/reference/NamePageRedirectTest.java
1
/**
2
 * Copyright (C) 2009 EDIT
3
 * European Distributed Institute of Taxonomy
4
 * http://www.e-taxonomy.eu
5
 *
6
 * The contents of this file are subject to the Mozilla Public License Version 1.1
7
 * See LICENSE.TXT at the top of this package for the full license terms.
8
 */
9
package eu.etaxonomy.dataportal.selenium.tests.reference;
10

  
11
import java.net.MalformedURLException;
12
import java.util.UUID;
13

  
14
import org.junit.Before;
15
import org.junit.Test;
16

  
17
import eu.etaxonomy.dataportal.DataPortalSite;
18
import eu.etaxonomy.dataportal.junit.CdmDataPortalTestBase;
19
import eu.etaxonomy.dataportal.junit.DataPortalContextSuite.DataPortalContexts;
20
import eu.etaxonomy.dataportal.pages.GenericPortalPage;
21

  
22
/**
23
 * Issues to be covered by this TestClass:
24
 *
25
 * #3616
26
 *
27
 * @author a.kohlbecker
28
 *
29
 */
30

  
31
@DataPortalContexts( { DataPortalSite.reference })
32
public class NamePageRedirectTest extends CdmDataPortalTestBase{
33

  
34
    static final UUID taxon_achilllea_santolina_uuid = UUID.fromString("c246856f-c03e-4cb7-ac92-d9b2864084cd");
35
    static final UUID name_achilllea_santolina_uuid = UUID.fromString("2ff1fb18-7055-420f-8c10-5105b66974de");
36

  
37

  
38
    @Before
39
    public void setUp() throws Exception {
40
        driver.get(getContext().getBaseUri().toString());
41
    }
42

  
43
    /**
44
     * related to https://dev.e-taxonomy.eu/redmine/issues/8304
45
     */
46
    @Test
47
    public void testNoRedirect() throws MalformedURLException {
48

  
49
        GenericPortalPage p = new GenericPortalPage(driver, getContext(), "name/" + name_achilllea_santolina_uuid.toString() + "/null/null");
50
        assertTrue(
51
                "The target page should be a name page, no redirect must have happened.",
52
                p.getDrupalPagePath().startsWith("cdm_dataportal/name/" + name_achilllea_santolina_uuid.toString()));
53

  
54
        p = new GenericPortalPage(driver, getContext(), "name/" + name_achilllea_santolina_uuid.toString() + "/null/null/null");
55
        assertTrue(
56
                "The target page should be a name page, no redirect must have happened.",
57
                p.getDrupalPagePath().startsWith("cdm_dataportal/name/" + name_achilllea_santolina_uuid.toString()));
58

  
59
        p = new GenericPortalPage(driver, getContext(), "name/" + name_achilllea_santolina_uuid.toString() + "///");
60
        assertTrue(
61
                "The target page should be a name page, no redirect must have happened.",
62
                p.getDrupalPagePath().startsWith("cdm_dataportal/name/" + name_achilllea_santolina_uuid.toString()));
63

  
64
    }
65

  
66

  
67
    /**
68
     * related to https://dev.e-taxonomy.eu/redmine/issues/8304
69
     */
70
    @Test
71
    public void testRedirectToTaxon() throws MalformedURLException {
72

  
73
        GenericPortalPage p = new GenericPortalPage(driver, getContext(), "name/" + name_achilllea_santolina_uuid.toString() + "/null/null/redirect_to_taxon");
74
        assertTrue(
75
                "The target page should be a taxon page, the name page must have been redirected to the according taxon page.",
76
                p.getDrupalPagePath().startsWith("cdm_dataportal/taxon/" + taxon_achilllea_santolina_uuid.toString()));
77

  
78
        p = new GenericPortalPage(driver, getContext(), "name/" + name_achilllea_santolina_uuid.toString() + "///redirect_to_taxon");
79
        assertTrue(
80
                "The target page should be a taxon page, the name page must have been redirected to the according taxon page.",
81
                p.getDrupalPagePath().startsWith("cdm_dataportal/taxon/" + taxon_achilllea_santolina_uuid.toString()));
82

  
83
    }
84

  
85
}

Also available in: Unified diff