ILIAS  Release_4_4_x_branch Revision 61816
 All Data Structures Namespaces Files Functions Variables Groups Pages
BibTexTest.php
Go to the documentation of this file.
1 <?php
2  // Call Structures_BibTexTest::main() if this source file is executed directly.
3 if (!defined("PHPUnit_MAIN_METHOD")) {
4  define("PHPUnit_MAIN_METHOD", "BibTexTest::main");
5  }
6 
7 require_once "PHPUnit/Framework/TestCase.php";
8 require_once "PHPUnit/Framework/TestSuite.php";
9 
10 // You may remove the following line when all tests have been implemented.
11 require_once "PHPUnit/Framework/IncompleteTestError.php";
12 
13 require_once "Structures/BibTex.php";
14 require_once "PEAR.php";
15 
21 {
22  var $obj;
23 
30  public static function main() {
31  require_once "PHPUnit/TextUI/TestRunner.php";
32  $suite = new PHPUnit_Framework_TestSuite("Structures_BibTexTest");
33  $result = PHPUnit_TextUI_TestRunner::run($suite);
34  }
35 
42  protected function setUp() {
43  $this->obj = new Structures_BibTex();
44  }
45 
52  protected function tearDown() {
53  unset($this->obj);
54  }
55 
56  public function testLoadFileFileExists() {
57  $ret = $this->obj->loadFile("BibTexTest.php");
58  $this->content = ''; //Erasing the loaded content again because it is senseless
59  $this->assertTrue($ret);
60  }
61 
62  public function testLoadFileFileDoesNotExists() {
63  $ret = $this->obj->loadFile((string)time());
64  $this->assertTrue(PEAR::isError($ret));
65  }
66 
70  public function test_parseEntry() {
71  //Remember here that there is no closing brace!
72  $test = "@foo{bar,john=doe";
73  $shouldbe = array();
74  $shouldbe['john'] = 'doe';
75  $shouldbe['cite'] = 'bar';
76  $shouldbe['entryType'] = 'foo';
77  $this->assertEquals($shouldbe, $this->obj->_parseEntry($test));
78  }
79 
80  public function test_checkEqualSignTrue() {
81  $test = "={=}";
82  $this->assertTrue($this->obj->_checkEqualSign($test,0));
83  }
84  public function test_checkEqualSignFalse() {
85  $test = "={=}";
86  $this->assertFalse($this->obj->_checkEqualSign($test,2));
87  }
88 
89  public function testClearWarnings() {
90  $this->obj->clearWarnings();
91  $this->obj->_generateWarning('type', 'entry');
92  $this->obj->clearWarnings();
93  $this->assertFalse($this->obj->hasWarning());
94  }
95 
96  /*
97  * the next tests check for the generation of the following Warnings:
98  * - WARNING_AT_IN_BRACES
99  * - WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES
100  * - WARNING_UNBALANCED_AMOUNT_OF_BRACES
101  */
103  $this->obj->clearWarnings();
104  $test = '{john@doe}';
105  $this->obj->_validateValue($test, '');
106  $this->assertEquals('WARNING_AT_IN_BRACES', $this->obj->warnings[0]['warning']);
107  }
109  $this->obj->clearWarnings();
110  $test = '"john\"doe"';
111  $this->obj->_validateValue($test, '');
112  $this->assertEquals('WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES', $this->obj->warnings[0]['warning']);
113  }
115  $this->obj->clearWarnings();
116  $test = '{john{doe}';
117  $this->obj->_validateValue($test, '');
118  $this->assertEquals('WARNING_UNBALANCED_AMOUNT_OF_BRACES', $this->obj->warnings[0]['warning']);
119  }
121  $this->obj->clearWarnings();
122  $test = '{john}doe}';
123  $this->obj->_validateValue($test, '');
124  $this->assertEquals('WARNING_UNBALANCED_AMOUNT_OF_BRACES', $this->obj->warnings[0]['warning']);
125  }
126 
127  public function test_generateWarning() {
128  $this->obj->clearWarnings();
129  $this->obj->_generateWarning('type', 'entry');
130  $ret = $this->obj->hasWarning();
131  $this->obj->clearWarnings();
132  $this->assertTrue($ret);
133  }
134 
135  public function testHasWarning()
136  {
137  $this->obj->clearWarnings();
138  $this->assertFalse($this->obj->hasWarning());
139  }
140 
141  public function testAmount()
142  {
143  $teststring = "@Article {art1,author = {John Doe and Jane Doe}}@Article { art2,author = {John Doe and Jane Doe}}";
144  $this->obj->content = $teststring;
145  $this->obj->parse();
146  $this->assertEquals(2, $this->obj->amount());
147  }
148 
149  public function testGetStatistic()
150  {
151  $teststring = "@Article {art1,author = {John Doe and Jane Doe}}@Article { art2,author = {John Doe and Jane Doe}}";
152  $this->obj->content = $teststring;
153  $this->obj->parse();
154  $shouldbe = array();
155  $shouldbe['article'] = 2;
156  $this->assertEquals($shouldbe, $this->obj->getStatistic());
157  }
158 
159  function testSingleParse()
160  {
161  $teststring="@Article { ppm_jon:1991,
162 author = {John Doe and Jane Doe}
163 }";
164  $this->obj->content=$teststring;
165  $this->obj->parse();
166  $this->assertEquals(1,$this->obj->amount());
167  }
168 
169  function testMultiParse()
170  {
171  $teststring = "@Article { art1,
172 author = {John Doe and Jane Doe}
173 }
174 @Article { art2,
175 author = {John Doe and Jane Doe}
176 }";
177  $this->obj->content = $teststring;
178  $this->obj->parse();
179  $this->assertEquals(2,$this->obj->amount());
180  }
181 
182  function testParse()
183  {
184  $teststring = "@Article { art1,
185 title = {Titel1},
186 author = {John Doe and Jane Doe}
187 }";
188  $shouldbe = array();
189  $shouldbe[0]['entryType'] = 'article';
190  $shouldbe[0]['cite'] = 'art1';
191  $shouldbe[0]['title'] = 'Titel1';
192  $shouldbe[0]['author'][0]['first'] = 'John';
193  $shouldbe[0]['author'][0]['von'] = '';
194  $shouldbe[0]['author'][0]['last'] = 'Doe';
195  $shouldbe[0]['author'][0]['jr'] = '';
196  $shouldbe[0]['author'][1]['first'] = 'Jane';
197  $shouldbe[0]['author'][1]['von'] = '';
198  $shouldbe[0]['author'][1]['last'] = 'Doe';
199  $shouldbe[0]['author'][1]['jr'] = '';
200  $this->obj->content = $teststring;
201  $this->obj->parse();
202  $this->assertEquals($shouldbe,$this->obj->data);
203  }
204 
205  function testBibTex()
206  {
207  $testarray = array();
208  $testarray[0]['entryType'] = 'Article';
209  $testarray[0]['cite'] = 'art1';
210  $testarray[0]['title'] = 'Titel1';
211  $testarray[0]['author'][0]['first'] = 'John';
212  $testarray[0]['author'][0]['von'] = '';
213  $testarray[0]['author'][0]['last'] = 'Doe';
214  $testarray[0]['author'][0]['jr'] = '';
215  $testarray[0]['author'][1]['first'] = 'Jane';
216  $testarray[0]['author'][1]['von'] = '';
217  $testarray[0]['author'][1]['last'] = 'Doe';
218  $testarray[0]['author'][1]['jr'] = '';
219  $shouldbe = "@article { art1,\n\ttitle = {Titel1},\n\tauthor = {Doe, , John and Doe, , Jane}\n}";
220  $this->obj->data = $testarray;
221  $this->assertEquals(trim($shouldbe),trim($this->obj->bibTex()));
222  }
223 
224  function testAddEntry()
225  {
226  $addarray = array();
227  $addarray['type'] = 'Article';
228  $addarray['cite'] = 'art2';
229  $addarray['title'] = 'Titel2';
230  $addarray['author'][0] = 'John Doe';
231  $addarray['author'][1] = 'Jane Doe';
232  $teststring = "@Article { art1,
233 title = {Titel1},
234 author = {John Doe and Jane Doe}
235 }";
236  $this->obj->content = $teststring;
237  $this->obj->parse();
238  $this->obj->addEntry($addarray);
239  $this->assertEquals(2,$this->obj->amount());
240  }
241 
243  {
244  //Entry found at http://en.wikipedia.org/wiki/BibTeX
245  $teststring = "@Book{abramowitz+stegun,
246  author = \"Milton Abramowitz and Irene A. Stegun\",
247  title = \"Handbook of Mathematical Functions with
248  Formulas, Graphs, and Mathematical Tables\",
249  publisher = \"Dover\",
250  year = 1964,
251  address = \"New York\",
252  edition = \"ninth Dover printing, tenth GPO printing\",
253  isbn = \"0-486-61272-4\"
254 }";
255  $shouldbe = array();
256  $shouldbe[0]['entryType'] = 'book';
257  $shouldbe[0]['cite'] = 'abramowitz+stegun';
258  $shouldbe[0]['title'] = "Handbook of Mathematical Functions with
259  Formulas, Graphs, and Mathematical Tables";
260  $shouldbe[0]['author'][0]['first'] = 'Milton';
261  $shouldbe[0]['author'][0]['von'] = '';
262  $shouldbe[0]['author'][0]['last'] = 'Abramowitz';
263  $shouldbe[0]['author'][0]['jr'] = '';
264  $shouldbe[0]['author'][1]['first'] = 'Irene A.';
265  $shouldbe[0]['author'][1]['von'] = '';
266  $shouldbe[0]['author'][1]['last'] = 'Stegun';
267  $shouldbe[0]['author'][1]['jr'] = '';
268  $shouldbe[0]['publisher'] = 'Dover';
269  $shouldbe[0]['year'] = '1964';
270  $shouldbe[0]['address'] = 'New York';
271  $shouldbe[0]['edition'] = 'ninth Dover printing, tenth GPO printing';
272  $shouldbe[0]['isbn'] = '0-486-61272-4';
273  $this->obj->content = $teststring;
274  $this->obj->parse();
275  $this->assertEquals($shouldbe, $this->obj->data);
276  }
277 
279  {
280  $teststring = "@Article { art1,
281 title = {Titel1},
282 author = {John Doe and Jane Doe}
283 }
284 Here is a comment
285 @Article { art2,
286 title = {Titel2},
287 author = {John Doe and Jane Doe}
288 }";
289  $shouldbe = array();
290  $shouldbe[0]['entryType'] = 'article';
291  $shouldbe[0]['cite'] = 'art1';
292  $shouldbe[0]['title'] = 'Titel1';
293  $shouldbe[0]['author'][0]['first'] = 'John';
294  $shouldbe[0]['author'][0]['von'] = '';
295  $shouldbe[0]['author'][0]['last'] = 'Doe';
296  $shouldbe[0]['author'][0]['jr'] = '';
297  $shouldbe[0]['author'][1]['first'] = 'Jane';
298  $shouldbe[0]['author'][1]['von'] = '';
299  $shouldbe[0]['author'][1]['last'] = 'Doe';
300  $shouldbe[0]['author'][1]['jr'] = '';
301  $shouldbe[1]['entryType'] = 'article';
302  $shouldbe[1]['cite'] = 'art2';
303  $shouldbe[1]['title'] = 'Titel2';
304  $shouldbe[1]['author'][0]['first'] = 'John';
305  $shouldbe[1]['author'][0]['von'] = '';
306  $shouldbe[1]['author'][0]['last'] = 'Doe';
307  $shouldbe[1]['author'][0]['jr'] = '';
308  $shouldbe[1]['author'][1]['first'] = 'Jane';
309  $shouldbe[1]['author'][1]['von'] = '';
310  $shouldbe[1]['author'][1]['last'] = 'Doe';
311  $shouldbe[1]['author'][1]['jr'] = '';
312  $this->obj->content = $teststring;
313  $this->obj->parse();
314  $this->assertTrue($this->obj->data == $shouldbe);
315  }
316  /*
317  function testWrongBraces1() {
318  $teststring = "@Article { art1,
319  title = {Tit}el1},
320  author = {John Doe and Jane Doe}
321  }";
322  $this->obj->content = $teststring;
323  $this->assertTrue(PEAR::isError($this->obj->parse()));
324  }
325 
326  function testWrongBraces2() {
327  $teststring = "@Article { art1,
328  title = {Titel1},
329  author = {John Doe and }Jane }Doe}
330  }";
331  $this->obj->content = $teststring;
332  $this->assertTrue(PEAR::isError($this->obj->parse()));
333  }
334  */
335  function testWrongBraces3() {
336  $teststring = "@Article { art1,
337 title = {Titel1},
338 author = {John {Doe and {Jane Doe}
339 }";
340  $this->obj->content = $teststring;
341  $this->assertTrue(PEAR::isError($this->obj->parse()));
342  }
343 
345  $teststring = "@Article { art1,
346 title = {Titel1},
347 author = {John Doe and @Jane Doe}
348 }";
349  $this->obj->content = $teststring;
350  $this->obj->parse();
351  $this->assertTrue('WARNING_AT_IN_BRACES'==$this->obj->warnings[0]['warning']);
352  }
354  $teststring = "@Article { art1,
355 title = {Titel1},
356 author = \"John Doe and \\\"Jane Doe\"
357 }";
358  $this->obj->content = $teststring;
359  $this->obj->parse();
360  $this->assertTrue('WARNING_ESCAPED_DOUBLE_QUOTE_INSIDE_DOUBLE_QUOTES'==$this->obj->warnings[0]['warning']);
361  }
362  /*
363  function testWarningAmountBraces() {
364  $teststring = "@Article { art1,
365  title = {Tit{el1},
366  author = {John Doe and }Jane Doe}
367  }";
368  $this->obj->content = $teststring;
369  $this->obj->parse();
370  $this->assertTrue('WARNING_UNBALANCED_AMOUNT_OF_BRACES'==$this->obj->warnings[0]['warning']);
371  }
372  */
374  $teststring = "@Article { art1,
375 title = {Titel1},
376 author = {John Doe and Jane Doe}
377 }
378 @Article { art2,
379 title = {Titel1},
380 author = {John Doe and Jane Doe}
381 }
382 @Article { art1,
383 title = {Titel1},
384 author = {John Doe and Jane Doe}
385 }
386 @Article { art2,
387 title = {Titel1},
388 author = {John Doe and Jane Doe}
389 }
390 @Article { art3,
391 title = {Titel1},
392 author = {John Doe and Jane Doe}
393 }";
394  $this->obj->content = $teststring;
395  $this->obj->parse();
396  $this->assertTrue('WARNING_MULTIPLE_ENTRIES'==$this->obj->warnings[0]['warning']);
397  }
398 
399  /* This testing suite is needed to get the Authors correct.
400  for more information: http://artis.imag.fr/%7EXavier.Decoret/resources/xdkbibtex/bibtex_summary.html
401  The names of the functions are build as follows:
402  "test": Of course it is a unit test
403  "Author": Function testing the authors
404  "First": There are three different ways writing an author this is the first one
405  "Simple": Description of the tes
406  */
408  $test = "AA BB";
409  $shouldbe = array();
410  $shouldbe[0]['first'] = 'AA';
411  $shouldbe[0]['von'] = '';
412  $shouldbe[0]['last'] = 'BB';
413  $shouldbe[0]['jr'] = '';
414  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
415  }
416 
418  $test = "AA";
419  $shouldbe = array();
420  $shouldbe[0]['first'] = '';
421  $shouldbe[0]['von'] = '';
422  $shouldbe[0]['last'] = 'AA';
423  $shouldbe[0]['jr'] = '';
424  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
425  }
426 
428  $test = "AA bb";
429  $shouldbe = array();
430  $shouldbe[0]['first'] = 'AA';
431  $shouldbe[0]['von'] = '';
432  $shouldbe[0]['last'] = 'bb';
433  $shouldbe[0]['jr'] = '';
434  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
435  }
436 
438  $test = "aa";
439  $shouldbe = array();
440  $shouldbe[0]['first'] = '';
441  $shouldbe[0]['von'] = '';
442  $shouldbe[0]['last'] = 'aa';
443  $shouldbe[0]['jr'] = '';
444  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
445  }
446 
448  $test = "AA bb CC";
449  $shouldbe = array();
450  $shouldbe[0]['first'] = 'AA';
451  $shouldbe[0]['von'] = 'bb';
452  $shouldbe[0]['last'] = 'CC';
453  $shouldbe[0]['jr'] = '';
454  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
455  }
457  $test = "AA bb CC dd EE";
458  $shouldbe = array();
459  $shouldbe[0]['first'] = 'AA';
460  $shouldbe[0]['von'] = 'bb CC dd';
461  $shouldbe[0]['last'] = 'EE';
462  $shouldbe[0]['jr'] = '';
463  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
464  }
466  $test = "AA 1B cc dd";
467  $shouldbe = array();
468  $shouldbe[0]['first'] = 'AA 1B';
469  $shouldbe[0]['von'] = 'cc';
470  $shouldbe[0]['last'] = 'dd';
471  $shouldbe[0]['jr'] = '';
472  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
473  }
475  $test = "AA 1b cc dd";
476  $shouldbe = array();
477  $shouldbe[0]['first'] = 'AA';
478  $shouldbe[0]['von'] = '1b cc';
479  $shouldbe[0]['last'] = 'dd';
480  $shouldbe[0]['jr'] = '';
481  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
482  }
484  $test = "AA {b}B cc dd";
485  $shouldbe = array();
486  $shouldbe[0]['first'] = 'AA {b}B';
487  $shouldbe[0]['von'] = 'cc';
488  $shouldbe[0]['last'] = 'dd';
489  $shouldbe[0]['jr'] = '';
490  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
491  }
493  $test = "AA {b}b cc dd";
494  $shouldbe = array();
495  $shouldbe[0]['first'] = 'AA';
496  $shouldbe[0]['von'] = '{b}b cc';
497  $shouldbe[0]['last'] = 'dd';
498  $shouldbe[0]['jr'] = '';
499  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
500  }
502  $test = "AA {B}B cc dd";
503  $shouldbe = array();
504  $shouldbe[0]['first'] = 'AA {B}B';
505  $shouldbe[0]['von'] = 'cc';
506  $shouldbe[0]['last'] = 'dd';
507  $shouldbe[0]['jr'] = '';
508  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
509  }
511  $test = "AA {B}b cc dd";
512  $shouldbe = array();
513  $shouldbe[0]['first'] = 'AA';
514  $shouldbe[0]['von'] = '{B}b cc';
515  $shouldbe[0]['last'] = 'dd';
516  $shouldbe[0]['jr'] = '';
517  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
518  }
520  $test = "AA \BB{b} cc dd";
521  $shouldbe = array();
522  $shouldbe[0]['first'] = 'AA \BB{b}';
523  $shouldbe[0]['von'] = 'cc';
524  $shouldbe[0]['last'] = 'dd';
525  $shouldbe[0]['jr'] = '';
526  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
527  }
529  $test = "AA \bb{b} cc dd";
530  $shouldbe = array();
531  $shouldbe[0]['first'] = 'AA';
532  $shouldbe[0]['von'] = '\bb{b} cc';
533  $shouldbe[0]['last'] = 'dd';
534  $shouldbe[0]['jr'] = '';
535  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
536  }
538  $test = "AA {bb} cc DD";
539  $shouldbe = array();
540  $shouldbe[0]['first'] = 'AA {bb}';
541  $shouldbe[0]['von'] = 'cc';
542  $shouldbe[0]['last'] = 'DD';
543  $shouldbe[0]['jr'] = '';
544  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
545  }
547  $test = "AA bb {cc} DD";
548  $shouldbe = array();
549  $shouldbe[0]['first'] = 'AA';
550  $shouldbe[0]['von'] = 'bb';
551  $shouldbe[0]['last'] = '{cc} DD';
552  $shouldbe[0]['jr'] = '';
553  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
554  }
556  $test = "AA {bb} CC";
557  $shouldbe = array();
558  $shouldbe[0]['first'] = 'AA {bb}';
559  $shouldbe[0]['von'] = '';
560  $shouldbe[0]['last'] = 'CC';
561  $shouldbe[0]['jr'] = '';
562  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
563  }
565  $test = "bb CC, AA";
566  $shouldbe = array();
567  $shouldbe[0]['first'] = 'AA';
568  $shouldbe[0]['von'] = 'bb';
569  $shouldbe[0]['last'] = 'CC';
570  $shouldbe[0]['jr'] = '';
571  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
572  }
574  $test = "bb CC, aa";
575  $shouldbe = array();
576  $shouldbe[0]['first'] = 'aa';
577  $shouldbe[0]['von'] = 'bb';
578  $shouldbe[0]['last'] = 'CC';
579  $shouldbe[0]['jr'] = '';
580  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
581  }
583  $test = "bb CC dd EE, AA";
584  $shouldbe = array();
585  $shouldbe[0]['first'] = 'AA';
586  $shouldbe[0]['von'] = 'bb CC dd';
587  $shouldbe[0]['last'] = 'EE';
588  $shouldbe[0]['jr'] = '';
589  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
590  }
592  $test = "bb, AA";
593  $shouldbe = array();
594  $shouldbe[0]['first'] = 'AA';
595  $shouldbe[0]['von'] = '';
596  $shouldbe[0]['last'] = 'bb';
597  $shouldbe[0]['jr'] = '';
598  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
599  }
601  $test = "BB,";
602  $shouldbe = array();
603  $shouldbe[0]['first'] = '';
604  $shouldbe[0]['von'] = '';
605  $shouldbe[0]['last'] = 'BB';
606  $shouldbe[0]['jr'] = '';
607  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
608  }
610  $test = "bb CC,XX, AA";
611  $shouldbe = array();
612  $shouldbe[0]['first'] = 'AA';
613  $shouldbe[0]['von'] = 'bb';
614  $shouldbe[0]['last'] = 'CC';
615  $shouldbe[0]['jr'] = 'XX';
616  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
617  }
619  $test = "bb CC,xx, AA";
620  $shouldbe = array();
621  $shouldbe[0]['first'] = 'AA';
622  $shouldbe[0]['von'] = 'bb';
623  $shouldbe[0]['last'] = 'CC';
624  $shouldbe[0]['jr'] = 'xx';
625  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
626  }
628  $test = "BB,, AA";
629  $shouldbe = array();
630  $shouldbe[0]['first'] = 'AA';
631  $shouldbe[0]['von'] = '';
632  $shouldbe[0]['last'] = 'BB';
633  $shouldbe[0]['jr'] = '';
634  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
635  }
636  /*Testing the case determination needed for the authors*/
637  function testCaseUpperSimple() {
638  $test = 'AA';
639  $this->assertEquals(1, $this->obj->_determineCase($test));
640  }
641  function testCaseLowerSimple() {
642  $test = 'aa';
643  $this->assertEquals(0, $this->obj->_determineCase($test));
644  }
646  $test = '{a}';
647  $this->assertEquals(-1, $this->obj->_determineCase($test));
648  }
650  $test = '{A}A';
651  $this->assertEquals(1, $this->obj->_determineCase($test));
652  }
654  $test = '{a}a';
655  $this->assertEquals(0, $this->obj->_determineCase($test));
656  }
658  $test = '1A';
659  $this->assertEquals(1, $this->obj->_determineCase($test));
660  }
662  $test = '1a';
663  $this->assertEquals(0, $this->obj->_determineCase($test));
664  }
666  $test = ' A';
667  $this->assertEquals(1, $this->obj->_determineCase($test));
668  }
670  $test = ' a';
671  $this->assertEquals(0, $this->obj->_determineCase($test));
672  }
674  $test = '';
675  $this->assertTrue(PEAR::isError($this->obj->_determineCase($test)));
676  }
678  $test = 2;
679  $this->assertTrue(PEAR::isError($this->obj->_determineCase($test)));
680  }
681 
682  function testAllowedTypeTrue() {
683  $test = 'article';
684  $this->assertTrue($this->obj->_checkAllowedEntryType($test));
685  }
686  function testAllowedTypeFalse() {
687  $test = 'foo';
688  $this->assertFalse($this->obj->_checkAllowedEntryType($test));
689  }
690 
691  public function testAllowedTypeWarning() {
692  $this->obj->clearWarnings();
693  $test = "@Foo { art1,
694 title = {Titel1},
695 author = {John Doe and Jane Doe}
696 }";
697  $this->obj->content = $test;
698  $this->obj->setOption('validate', true);
699  $this->obj->parse();
700  $this->assertEquals('WARNING_NOT_ALLOWED_ENTRY_TYPE', $this->obj->warnings[0]['warning']);
701  }
702 
703  public function testMissingLastBraceParsing() {
704  $this->obj->clearWarnings();
705  $test = '
706 @phdthesis{foo1,
707 school = {school1},
708 title = {title1},
709 author = {author1},
710 year = {year1}
711 }
712 @phdthesis{foo2,
713 school = {school2},
714 title = {title2},
715 author = {author2},
716 year = {year2}
717 
718 @phdthesis{foo3,
719 school = {school3},
720 title = {title3},
721 author = {author3},
722 year = {year3}
723 }
724 @phdthesis{foo4,
725 school = {school4},
726 title = {title4},
727 author = {author4},
728 year = {year4}
729 }
730  ';
731  $this->obj->content = $test;
732  $this->obj->setOption('validate', true);
733  $this->obj->parse();
734  $this->assertEquals($this->obj->amount(), 4);
735  }
736 
737  public function testMissingLastBraceParsing2() {
738  $this->obj->clearWarnings();
739  $test = '
740 @phdthesis{foo1,
741 school = {school1},
742 title = {title1},
743 author = {author1},
744 year = {year1}
745  ';
746  $this->obj->content = $test;
747  $this->obj->setOption('validate', true);
748  $this->obj->parse();
749  $this->assertEquals($this->obj->amount(), 1);
750  }
751 
752  public function testMissingLastBraceWarning() {
753  $this->obj->clearWarnings();
754  $test = '
755 @phdthesis{foo1,
756 school = {school1},
757 title = {title1},
758 author = {author1},
759 year = {year1}
760 }
761 @phdthesis{foo2,
762 school = {school2},
763 title = {title2},
764 author = {author2},
765 year = {year2}
766 
767 @phdthesis{foo3,
768 school = {school3},
769 title = {title3},
770 author = {author3},
771 year = {year3}
772 }
773 @phdthesis{foo4,
774 school = {school4},
775 title = {title4},
776 author = {author4},
777 year = {year4}
778 }
779  ';
780  $this->obj->content = $test;
781  $this->obj->setOption('validate', true);
782  $this->obj->parse();
783  $this->assertEquals($this->obj->warnings[0]['warning'], 'WARNING_MISSING_END_BRACE');
784  }
785 
786  public function testNewlineInAuthorField() {
787  $this->obj->clearWarnings();
788  $test = '
789 @phdthesis{foo1,
790 school = {school1},
791 title = {title1},
792 author = {author1 and
793 author2},
794 year = {year1}
795 }
796  ';
797  $shouldbe = array(array('first'=>'','von'=>'','last'=>'author1','jr'=>''), array('first'=>'','von'=>'','last'=>'author2','jr'=>''));
798  $this->obj->content = $test;
799  $this->obj->setOption('unwrap', false);
800  $this->obj->parse();
801  $this->assertEquals($shouldbe, $this->obj->data[0]['author']);
802  }
803 
804  public function testNotRemoveCurlyBraces() {
805  $this->obj->clearWarnings();
806  $test = '
807 @phdthesis{foo4,
808 school = {school4},
809 title = {Do {S}omething},
810 author = {author4},
811 year = {year4}
812 }
813  ';
814  $shouldbe = 'Do {S}omething';
815  $this->obj->content = $test;
816  $this->obj->setOption('removeCurlyBraces', false);
817  $this->obj->parse();
818  //print_r($this->obj->data[0]['author']);
819  $this->assertEquals($shouldbe, $this->obj->data[0]['title']);
820  }
821 
822  public function testRemoveCurlyBraces() {
823  $this->obj->clearWarnings();
824  $test = '
825 @phdthesis{foo4,
826 school = {school4},
827 title = {Do {S}omething},
828 author = {author4},
829 year = {year4}
830 }
831  ';
832  $shouldbe = 'Do Something';
833  $this->obj->content = $test;
834  $this->obj->setOption('removeCurlyBraces', true);
835  $this->obj->parse();
836  //print_r($this->obj->data[0]['author']);
837  $this->assertEquals($shouldbe, $this->obj->data[0]['title']);
838  }
839 
840  public function testRemoveCurlyBraces2() {
841  $this->obj->clearWarnings();
842  $test = '
843 @phdthesis{foo4,
844 school = {school4},
845 title = {Do {S}o{me\}th}ing},
846 author = {author4},
847 year = {year4}
848 }
849  ';
850  $shouldbe = 'Do Some\}thing';
851  $this->obj->content = $test;
852  $this->obj->setOption('removeCurlyBraces', true);
853  $this->obj->parse();
854  $this->assertEquals($shouldbe, $this->obj->data[0]['title']);
855  }
856 
858  $this->obj->clearWarnings();
859  $test = '
860 @phdthesis{foo4,
861 school = {school4},
862 title = {Do Something},
863 author = {author4},
864 year = {year4}
865 }
866  ';
867  $shouldbe = 'Do Something';
868  $this->obj->content = $test;
869  $this->obj->setOption('removeCurlyBraces', true);
870  $this->obj->parse();
871  $this->assertEquals($shouldbe, $this->obj->data[0]['title']);
872  }
873 
874  public function testRtfExport() {
875  $test = '
876 @phdthesis{foo4,
877 title = {title},
878 author = {author},
879 journal = {journal},
880 year = {year}
881 }
882  ';
883  $shouldbe = '{\rtf
884 author title journal year
885 \par
886 }';
887  $this->obj->content = $test;
888  /*Setting formating option and saving old state*/
889  $oldrtfstring = $this->obj->rtfstring;
890  $this->obj->rtfstring = 'AUTHORS TITLE JOURNAL YEAR';
891  $oldauthorstring = $this->obj->authorstring;
892  $this->obj->authorstring = 'LAST';
893  $this->obj->parse();
894 
895  $rtf = $this->obj->rtf();
896  /*Resetting old state*/
897  $this->obj->rtfstring = $oldrtfstring;
898  $this->obj->authorstring = $oldauthorstring;
899 
900  $this->assertEquals($shouldbe, $rtf);
901  }
902 
903  public function testHtmlExport() {
904  $test = '
905 @phdthesis{foo4,
906 title = {title},
907 author = {author},
908 journal = {journal},
909 year = {year}
910 }
911  ';
912  $shouldbe = '<p>
913 author title journal year
914 </p>
915 ';
916  $this->obj->content = $test;
917  /*Setting formating option and saving old state*/
918  $oldhtmlstring = $this->obj->htmlstring;
919  $this->obj->htmlstring = 'AUTHORS TITLE JOURNAL YEAR';
920  $oldauthorstring = $this->obj->authorstring;
921  $this->obj->authorstring = 'LAST';
922  $this->obj->parse();
923 
924  $html = $this->obj->html();
925  /*Resetting old state*/
926  $this->obj->htmlstring = $oldhtmlstring;
927  $this->obj->authorstring = $oldauthorstring;
928 
929  $this->assertEquals($shouldbe, $html);
930  }
931 
932  public function testFormatAuthor() {
933  $test = '
934 @phdthesis{foo4,
935 author = {von Last, Jr ,First}
936 }
937  ';
938  $shouldbe = 'von Last Jr First';
939  $this->obj->content = $test;
940  /*Setting formating option and saving old state*/
941  $oldhtmlstring = $this->obj->htmlstring;
942  $this->obj->htmlstring = 'AUTHORS';
943  $oldauthorstring = $this->obj->authorstring;
944  $this->obj->authorstring = 'VON LAST JR FIRST';
945  $this->obj->parse();
946 
947  $html = $this->obj->html();
948  /*Dropping tags and trimming*/
949  $html = trim(strip_tags($html));
950  /*Resetting old state*/
951  $this->obj->htmlstring = $oldhtmlstring;
952  $this->obj->authorstring = $oldauthorstring;
953 
954  $this->assertEquals($shouldbe, $html);
955  }
956 
957  public function testExtractAuthorWhitespace() {
958  $test = 'John Doe';
959  $shouldbe = array();
960  $shouldbe[0]['first'] = 'John';
961  $shouldbe[0]['von'] = '';
962  $shouldbe[0]['last'] = 'Doe';
963  $shouldbe[0]['jr'] = '';
964  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
965  }
966 
967  public function testExtractAuthorTilde() {
968  $test = 'John~Doe';
969  $shouldbe = array();
970  $shouldbe[0]['first'] = 'John';
971  $shouldbe[0]['von'] = '';
972  $shouldbe[0]['last'] = 'Doe';
973  $shouldbe[0]['jr'] = '';
974  $this->assertEquals($shouldbe, $this->obj->_extractAuthors($test));
975  }
976 
977  public function testNotExtractAuthors() {
978  $this->obj->clearWarnings();
979  $test = '
980 @phdthesis{foo4,
981 author = {John Doe},
982 }
983  ';
984  $shouldbe = 'John Doe';
985  $this->obj->content = $test;
986  $this->obj->setOption('extractAuthors', false);
987  $this->obj->parse();
988  $this->assertEquals($shouldbe, $this->obj->data[0]['author']);
989  }
990 
991  public function testExtractAuthors() {
992  $this->obj->clearWarnings();
993  $test = '
994 @phdthesis{foo4,
995 author = {John Doe},
996 }
997  ';
998  $shouldbe = array();
999  $shouldbe[0]['first'] = 'John';
1000  $shouldbe[0]['von'] = '';
1001  $shouldbe[0]['last'] = 'Doe';
1002  $shouldbe[0]['jr'] = '';
1003  $this->obj->content = $test;
1004  $this->obj->setOption('extractAuthors', true);
1005  $this->obj->parse();
1006  $this->assertEquals($shouldbe, $this->obj->data[0]['author']);
1007  }
1008 
1010  $this->obj->clearWarnings();
1011  $test = "@phdthesis { foo4,\n\tauthor = {John Doe}\n}\n\n";
1012  $this->obj->content = $test;
1013  $this->obj->setOption('extractAuthors', false);
1014  $this->obj->parse();
1015  $this->assertEquals($test, $this->obj->bibTex());
1016  }
1017 
1019  $this->obj->clearWarnings();
1020  $test = "@phdthesis { foo4,\n\tauthor = {John Doe}\n}\n\n";
1021  $shouldbe = '{\rtf
1022 John Doe, "{\b }", {\i },
1023 \par
1024 }';
1025  $this->obj->content = $test;
1026  $this->obj->setOption('extractAuthors', false);
1027  $this->obj->parse();
1028  $this->assertEquals($shouldbe, $this->obj->rtf());
1029  }
1030 
1032  $this->obj->clearWarnings();
1033  $test = "@phdthesis { foo4,\n\tauthor = {John Doe}\n}\n\n";
1034  $shouldbe = '<p>
1035 John Doe, "<strong></strong>", <em></em>, <br />
1036 </p>
1037 ';
1038  $this->obj->content = $test;
1039  $this->obj->setOption('extractAuthors', false);
1040  $this->obj->parse();
1041  $this->assertEquals($shouldbe, $this->obj->html());
1042  }
1043 }
1044 
1045 // Call Structures_BibTexTest::main() if this source file is executed directly.
1046 if (PHPUnit_MAIN_METHOD == "BibTexTest::main") {
1047  BibTexTest::main();
1048  }
1049 ?>