From e4d19712bb17db930a632b0f663db477ab404398 Mon Sep 17 00:00:00 2001 From: Martin Monperrus Date: Tue, 27 Feb 2018 21:53:27 +0100 Subject: [PATCH] test: add test for #80, it works already well --- bibtexbrowser-test.php | 22 ++++++++++++++++++++++ bibtexbrowser.php | 7 +++---- 2 files changed, 25 insertions(+), 4 deletions(-) diff --git a/bibtexbrowser-test.php b/bibtexbrowser-test.php index f298d43..0d1a07a 100755 --- a/bibtexbrowser-test.php +++ b/bibtexbrowser-test.php @@ -728,6 +728,28 @@ class BTBTest extends PHPUnit_Framework_TestCase { $this->assertContains('[citations]', $entry->toHTML()); } + function test80() { + // entries without year are at the top + $bibtex = "@article{keyWithoutYear,title={First article},author = {Martin}},@article{key2,title={Second article},author = {Martin}, year=2007}"; + $test_data = fopen('php://memory','x+'); + fwrite($test_data, $bibtex); + fseek($test_data,0); + $db = new BibDataBase(); + $db->update_internal("inline", $test_data); + + $d = new SimpleDisplay(); + $d->setDB($db); + ob_start(); + $d->display(); + $output = ob_get_clean(); +// print($output); + $this->assertEquals("keyWithoutYear", $d->entries[0]->getKey()); + $this->assertEquals("key2", $d->entries[1]->getKey()); + // the indices have been set by SimpleDisplay, by default the one at the top is the one withuut year (the first in $d->entries) + $this->assertEquals("[2]", $db->getEntryByKey('keyWithoutYear')->getAbbrv()); + $this->assertEquals("[1]", $db->getEntryByKey('key2')->getAbbrv()); + } + } // end class @copy('bibtexbrowser.local.php.bak','bibtexbrowser.local.php'); diff --git a/bibtexbrowser.php b/bibtexbrowser.php index 1e913a6..36a0895 100755 --- a/bibtexbrowser.php +++ b/bibtexbrowser.php @@ -2066,7 +2066,7 @@ function compare_bib_entry_by_bibtex_order($a, $b) */ function compare_bib_entry_by_year($a, $b) { - $yearA = (int) $a->getYear(); + $yearA = (int) $a->getYear(); // 0 if no year $yearB = (int) $b->getYear(); if ($yearA === 0) { @@ -3103,7 +3103,7 @@ class SimpleDisplay { /** sets the entries to be shown */ function setEntries($entries) { - $this->entries = $entries; + $this->entries = array_values($entries); } function indexUp() { @@ -3161,8 +3161,7 @@ class SimpleDisplay { /** Displays a set of bibtex entries in an HTML table */ function display() { - - uasort($this->entries, 'compare_bib_entries'); + usort($this->entries, 'compare_bib_entries'); // now that the entries are sorted, setting the index of entries // this function can be overloaded