forked from monperrus/bibtexbrowser
/
bibtexbrowser-test.php
executable file
·302 lines (259 loc) · 11.7 KB
/
bibtexbrowser-test.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
<?php
/** PhPUnit tests for bibtexbrowser
To run them:
$ phpunit bibtexbrowser-test.php
With coverage:
$ phpunit --coverage-html ./coverage btb-test.php
(be sure that xdebug is enabled: /etc/php5/cli/conf.d# ln -s ../../mods-available/xdebug.ini)
*/
$_GET['library']=1;
copy('bibtexbrowser.local.php','bibtexbrowser.local.php.bak');
unlink('bibtexbrowser.local.php');
require_once ('bibtexbrowser.php');
error_reporting(E_ALL);
class BTBTest extends PHPUnit_Framework_TestCase {
function createDB() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n".
"@book{aKey/withSlash,title={Slash Dangerous for web servers},author={Ap Ache},publisher={Springer},year=2009}\n"
);
fseek($test_data,0);
$btb = new BibDataBase();
$btb->update_internal("inline", $test_data);
return $btb;
}
function test_bibentry_to_html() {
$btb = $this->createDB();
$first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]];
$this->assertEquals('<span class="bibmenu"><a class="biburl" title="aKey" href="bibtexbrowser.php?key=aKey&bib=inline">[bibtex]</a></span>',$first_entry->bib2links());
$this->assertEquals('<a class="bibanchor" name=""></a>',$first_entry->anchor());
}
function testMultiSearch() {
$btb = $this->createDB();
$q=array(Q_AUTHOR=>'monperrus');
$results=$btb->multisearch($q);
$entry = $results[0];
$this->assertTrue(count($results) == 1);
$this->assertTrue($entry->getTitle() == 'A Book');
}
function testMultiSearch2() {
$btb = $this->createDB();
$q=array(Q_AUTHOR=>'monperrus|ducasse');
$results=$btb->multisearch($q);
$entry = $results[0];
$this->assertTrue(count($results) == 1);
$this->assertTrue($entry->getTitle() == 'A Book');
}
function test_config_value() {
// default value
$this->assertFalse(config_value('BIBTEXBROWSER_NO_DEFAULT'));
// setting to true
bibtexbrowser_configure('BIBTEXBROWSER_NO_DEFAULT', true);
$this->assertTrue(config_value('BIBTEXBROWSER_NO_DEFAULT'));
ob_start();
default_message();
$this->assertEquals('', ob_get_flush());
// setting to false
bibtexbrowser_configure('BIBTEXBROWSER_NO_DEFAULT', false);
$this->assertFalse(config_value('BIBTEXBROWSER_NO_DEFAULT'));
ob_start();
default_message();
$this->assertContains('Congratulations', ob_get_flush());
}
function testInternationalization() {
$btb = $this->createDB();
global $BIBTEXBROWSER_LANG;
$BIBTEXBROWSER_LANG=array();
$BIBTEXBROWSER_LANG['Refereed Conference Papers']="foo";
$this->assertEquals("foo",__("Refereed Conference Papers"));
$BIBTEXBROWSER_LANG['Books']="Livres";
$d = new AcademicDisplay();
$d->setDB($btb);
ob_start();
$d->display();
$data = ob_get_flush();
$this->assertContains('Livres', $data);
}
function testNoSlashInKey() {
$btb = $this->createDB();
$q=array(Q_SEARCH=>'Slash');
$results=$btb->multisearch($q);
$this->assertTrue(count($results) == 1);
$entry = $results[0];
$this->assertContains("aKey-withSlash",$entry->toHTML());
$q=array(Q_KEY=>'aKey-withSlash');
$results=$btb->multisearch($q);
$entry2 = $results[0];
$this->assertSame($entry2,$entry);
}
function test_string_should_be_deleted_after_update() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n".
"@String{x=2008}\n"
);
fseek($test_data,0);
$btb = new BibDataBase();
$btb->update_internal("inline", $test_data);
// print_r($btb->stringdb);
$this->assertEquals(1,count($btb->stringdb));
// replacing the existing one
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@book{aKey2,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n".
"@String{x=2009}\n"
);
fseek($test_data,0);
$btb = new BibDataBase();
$btb->update_internal("inline2", $test_data);
// print_r($btb->stringdb);
$this->assertEquals(1,count($btb->stringdb));
$this->assertEquals("2009",$btb->stringdb['x']->value);//
// now adding another one and removing the string
$test_data2 = fopen('php://memory','x+');
fwrite($test_data2, "@book{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009}\n".
"@String{y=2010}\n"
);
fseek($test_data2,0);
$btb->update_internal("inline2", $test_data2);
$this->assertEquals(1,count($btb->stringdb));//
$this->assertEquals("2010",$btb->stringdb['y']->value);//
}
function test_google_scholar_metadata() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@article{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009,pages={42--4242},number=1}\n".
"@String{x=2008}\n"
);
fseek($test_data,0);
$db = new BibDataBase();
$db->update_internal("inline", $test_data);
$dis = new BibEntryDisplay($db->getEntryByKey('aKey'));
$metadata = $dis->metadata_dict();
//print_r($metadata);
$this->assertEquals("A Book",$metadata['citation_title']);
$this->assertEquals("2009",$metadata['citation_date']);
$this->assertEquals("2009",$metadata['citation_year']);
$this->assertEquals("42",$metadata['citation_firstpage']);
$this->assertEquals("4242",$metadata['citation_lastpage']);
$this->assertEquals("1",$metadata['citation_issue']);
}
function test_metadata_opengraph() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@article{aKey,title={A Book},author={Martin Monperrus},url={http://foo.com/},publisher={Springer},year=2009,pages={42--4242},number=1}\n".
"@String{x=2008}\n"
);
fseek($test_data,0);
$db = new BibDataBase();
$db->update_internal("inline", $test_data);
$dis = new BibEntryDisplay($db->getEntryByKey('aKey'));
$metadata = $dis->metadata_dict();
//print_r($metadata);
$this->assertEquals("A Book",$metadata['og:title']);
$this->assertEquals("article",$metadata['og:type']);
$this->assertTrue(1 == preg_match("/http:.*author=Martin\+Monperrus/",$metadata['og:author']));
$this->assertEquals("2009",$metadata['og:published_time']);
}
function test_math_cal() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@book{aKey,title={{A Book{} $\mbox{foo}$ tt $\boo{t}$}} ,author={Martin Monperrus},publisher={Springer},year=2009}\n".
"@String{x=2008}\n"
);
fseek($test_data,0);
$btb = new BibDataBase();
$btb->update_internal("inline", $test_data);
$first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]];
// $this->assertTrue(strpos('A Book{} $\mbox{foo}$',$first_entry->toHTML());
$this->assertEquals('A Book $\mbox{foo}$ tt $\boo{t}$',$first_entry->getTitle());
}
function test_link_configuration() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@book{aKey,pdf={myarticle.pdf}}\n"
);
fseek($test_data,0);
$btb = new BibDataBase();
$btb->update_internal("inline", $test_data);
$first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]];
$this->assertEquals('<a href="myarticle.pdf">[pdf]</a>',$first_entry->getLink('pdf'));
$this->assertEquals('<a href="myarticle.pdf">[pdf]</a>',$first_entry->getUrlLink());
$this->assertEquals('<a href="myarticle.pdf"><img class="icon" src="pdficon.png" alt="[pdf]" title="pdf"/></a>',$first_entry->getLink('pdf','pdficon.png'));
$this->assertEquals('<a href="myarticle.pdf">[see]</a>',$first_entry->getLink('pdf',NULL,'see'));
}
// see https://github.com/monperrus/bibtexbrowser/pull/14
function test_zotero() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@book{aKey,file={myarticle.pdf}}\n"
);
fseek($test_data,0);
$btb = new BibDataBase();
$btb->update_internal("inline", $test_data);
$first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]];
$this->assertEquals('<a href="myarticle.pdf">[pdf]</a>',$first_entry->getUrlLink());
}
// https://github.com/monperrus/bibtexbrowser/issues/40
function test_doi_url() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@Article{Baldwin2014Quantum,Doi={10.1103/PhysRevA.90.012110},Url={http://link.aps.org/doi/10.1103/PhysRevA.90.012110}}"
);
fseek($test_data,0);
$btb = new BibDataBase();
$btb->update_internal("inline", $test_data);
$first_entry=$btb->bibdb[array_keys($btb->bibdb)[0]];
$this->assertEquals('<pre class="purebibtex">@Article{Baldwin2014Quantum,Doi={<a href="http://dx.doi.org/10.1103/PhysRevA.90.012110">10.1103/PhysRevA.90.012110</a>},Url={<a href="http://link.aps.org/doi/10.1103/PhysRevA.90.012110">http://link.aps.org/doi/10.1103/PhysRevA.90.012110</a>}}</pre>',$first_entry->toEntryUnformatted());
}
function test_filter_view() {
$test_data = fopen('php://memory','x+');
fwrite($test_data, "@article{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009,pages={42--4242},number=1}\n");
fseek($test_data,0);
$db = new BibDataBase();
$db->update_internal("inline", $test_data);
$dis = $db->getEntryByKey('aKey');
$this->assertEquals("@article{aKey,title={A Book},author={Martin Monperrus},publisher={Springer},year=2009,pages={42--4242},number=1}",$dis->getText());
// now ith option
bibtexbrowser_configure('BIBTEXBROWSER_BIBTEX_VIEW', 'reconstructed');
bibtexbrowser_configure('BIBTEXBROWSER_BIBTEX_VIEW_FILTEREDOUT', 'pages|number');
$this->assertEquals("@article{aKey,\n title = {A Book},\n author = {Martin Monperrus},\n publisher = {Springer},\n year = {2009},\n}\n", $dis->getText());
}
function test_BIBTEXBROWSER_USE_LATEX2HTML() {
$bibtex = "@article{aKey,title={\`a Book},author={Martin Monperrus},publisher={Springer},year=2009,pages={42--4242},number=1}\n";
bibtexbrowser_configure('BIBTEXBROWSER_USE_LATEX2HTML', true);
$test_data = fopen('php://memory','x+');
fwrite($test_data, $bibtex);
fseek($test_data,0);
$db = new BibDataBase();
$db->update_internal("inline", $test_data);
$dis = $db->getEntryByKey('aKey');
$this->assertEquals("à Book",$dis->getTitle());
bibtexbrowser_configure('BIBTEXBROWSER_USE_LATEX2HTML', false);
$test_data = fopen('php://memory','x+');
fwrite($test_data, $bibtex);
fseek($test_data,0);
$db = new BibDataBase();
$db->update_internal("inline", $test_data);
$dis = $db->getEntryByKey('aKey');
$this->assertEquals("\`a Book",$dis->getTitle());
}
function test_PagedDisplay() {
$PAGE_SIZE = 3;
bibtexbrowser_configure('BIBTEXBROWSER_DEFAULT_DISPLAY', 'PagedDisplay');
bibtexbrowser_configure('PAGE_SIZE', $PAGE_SIZE);
$_GET['bib'] = 'bibacid-utf8.bib';
$_GET['all'] = 1;
$d = new Dispatcher();
ob_start();
$d->main();
$content = "<div>".ob_get_flush()."</div>";
$xml = new SimpleXMLElement($content);
$result = $xml->xpath('//td[@class=\'bibref\']');
$this->assertEquals($PAGE_SIZE,count($result));
}
function test_getKeywords() {
$bibtex = "@article{aKey,title={\`a Book},keywords={foo,bar},author={Martin Monperrus},publisher={Springer},year=2009,pages={42--4242},number=1}\n";
bibtexbrowser_configure('BIBTEXBROWSER_USE_LATEX2HTML', true);
$test_data = fopen('php://memory','x+');
fwrite($test_data, $bibtex);
fseek($test_data,0);
$db = new BibDataBase();
$db->update_internal("inline", $test_data);
$dis = $db->getEntryByKey('aKey');
$this->assertEquals(2,count($dis->getKeywords()));
}
} // end class
?>