Open Journal Systems  3.3.0
TextTest.php
1 <?php
2 /*
3  * citeproc-php
4  *
5  * @link http://github.com/seboettg/citeproc-php for the source repository
6  * @copyright Copyright (c) 2016 Sebastian Böttger.
7  * @license https://opensource.org/licenses/MIT
8  */
9 
11 
12 use PHPUnit\Framework\TestCase;
18 
19 class TextTest extends TestCase
20 {
21 
22  private $textXml = <<<EOT
23 <?xml version="1.0" encoding="utf-8"?>
24 <style>
25  <citation>
26  <layout>
27  <text variable="title"/>
28  </layout>
29  </citation>
30 </style>;
31 EOT;
32 
33 
34  private $dataTitle = '{"title":"Ein herzzerreißendes Werk von umwerfender Genialität","type":"book"}';
35  private $dataPublisherPlace = '{"publisher-place":"Frankfurt am Main"}';
36 
40  private $citeproc;
41 
42  public function setUp()
43  {
44 
45  }
46 
47 
48 
49  public function testMacro()
50  {
51 
52  $macroXml = <<<EOT
53 <?xml version="1.0" encoding="utf-8"?>
54 <style xmlns="http://purl.org/net/xbiblio/csl" version="1.0">
55  <macro name="title">
56  <choose>
57  <if type="book">
58  <text variable="title" font-style="italic"/>
59  </if>
60  <else>
61  <text variable="title"/>
62  </else>
63  </choose>
64  </macro>
65  <citation>
66  <layout>
67  <text macro="title"/>
68  </layout>
69  </citation>
70 </style>
71 EOT;
72  $citeProc = new CiteProc($macroXml);
73 
74 
75 
76  $this->assertEquals(
77  "<i>Ein Buch</i>",
78  $citeProc->render(json_decode("[{\"title\":\"Ein Buch\", \"type\": \"book\"}]"), "citation")
79  );
80 
81  $this->assertEquals(
82  "Ein Buch",
83  $citeProc->render(json_decode("[{\"title\":\"Ein Buch\", \"type\": \"thesis\"}]"), "citation")
84  );
85  }
86 
87  public function testEnrichMarkupTitles()
88  {
89  $cslJson = '[{
90  "author": [
91  {
92  "family": "Doe",
93  "given": "John",
94  "id": "doe"
95  },
96  {
97  "family": "Müller",
98  "given": "Alexander"
99  }
100  ],
101  "id": "item-1",
102  "issued": {
103  "date-parts": [
104  [
105  "2001"
106  ]
107  ]
108  },
109  "title": "My Anonymous Heritage",
110  "type": "book"
111  }]';
112 
113  $enrichTitleWithLinkFunction = function($citeItem, $renderedVariable) {
114  return isset($citeItem->id) ? '<a href="https://example.org/publication/' . $citeItem->id . '" title="' . $renderedVariable . '">'
115  . $renderedVariable . '</a>' : $renderedVariable;
116  };
117 
118  $apa = StyleSheet::loadStyleSheet("apa");
119  $citeproc = new CiteProc($apa, "de-DE",
120  [
121  'title' => $enrichTitleWithLinkFunction
122  ]
123  );
124  $actual = $citeproc->render(json_decode($cslJson), "bibliography");
125 
126  $expected = '<div class="csl-bib-body">
127  <div class="csl-entry">Doe, J., &#38; Müller, A. (2001). <i><a href="https://example.org/publication/item-1" title="My Anonymous Heritage">My Anonymous Heritage</a></i>.</div>
128 </div>';
129  $this->assertEquals($expected, $actual);
130  }
131 
135  public function testEnrichMarkupURL()
136  {
137  $cslJson = '[{
138  "author": [
139  {
140  "family": "Doe",
141  "given": "John",
142  "id": "doe"
143  },
144  {
145  "family": "Müller",
146  "given": "Alexander"
147  }
148  ],
149  "container-title": "Heritages and taxes. How to avoid responsibility.",
150  "id": "item-1",
151  "issued": {
152  "date-parts": [
153  [
154  "2001"
155  ]
156  ]
157  },
158  "page": "123-127",
159  "publisher": "Initiative Neue Soziale Marktwirtschaft (INSM)",
160  "publisher-place": "Berlin, Germany",
161  "title": "My Anonymous Heritage",
162  "type": "book",
163  "URL": "https://example.org/publication/item-1"
164  }]';
165 
166  $enrichUrlWithLinkFunction = function($citeItem, $renderedVariable) {
167  return preg_match("/http[s]?:\/\/.+/", $citeItem->URL) ? '<a href="' . $citeItem->URL . '">'
168  . $citeItem->URL . '</a>' : $citeItem->URL;
169  };
170 
171  $apa = StyleSheet::loadStyleSheet("apa");
172  $citeproc = new CiteProc($apa, "en-US", [
173  'URL' => $enrichUrlWithLinkFunction
174  ]);
175  $actual = $citeproc->render(json_decode($cslJson), "bibliography");
176 
177  $expected = '<div class="csl-bib-body">
178  <div class="csl-entry">Doe, J., &#38; Müller, A. (2001). My Anonymous Heritage. In <i>Heritages and taxes. How to avoid responsibility.</i> (pp. 123-127). Initiative Neue Soziale Marktwirtschaft (INSM). <a href="https://example.org/publication/item-1">https://example.org/publication/item-1</a></div>
179 </div>';
180  $this->assertEquals($expected, $actual);
181  }
182 
183  public function testEnrichMarkupCitationNumber()
184  {
185  $cslJson = '[
186  {
187  "author": [
188  {
189  "family": "Doe",
190  "given": "James",
191  "suffix": "III"
192  }
193  ],
194  "id": "item-1",
195  "issued": {
196  "date-parts": [
197  [
198  "2001"
199  ]
200  ]
201  },
202  "title": "My Anonymous Heritage",
203  "type": "book"
204  },
205  {
206  "author": [
207  {
208  "family": "Anderson",
209  "given": "John",
210  "id": "anderson.j"
211  },
212  {
213  "family": "Brown",
214  "given": "John",
215  "id": "brown.j"
216  }
217  ],
218  "issued": {
219  "date-parts": [
220  [
221  "1998"
222  ]
223  ]
224  },
225  "id": "ITEM-2",
226  "type": "book",
227  "title": "Two authors writing a book"
228  }]';
229 
230  $apa = StyleSheet::loadStyleSheet("elsevier-with-titles");
231 
232  $citeproc = new CiteProc($apa, "en-US",
233  [
234  "bibliography" => [
235  "citation-number" => function($citeItem, $renderedVariable) {
236  return isset($citeItem->id) ? '<a id="' . $citeItem->id. '" href="#' . $citeItem->id . '">'
237  . $renderedVariable . '</a>' : $renderedVariable;
238  }
239  ],
240  "citation" => [
241  "citation-number" => function($citeItem, $renderedVariable) {
242  return isset($citeItem->id) ? '<a href="#' . $citeItem->id . '">'
243  . $renderedVariable . '</a>' : $renderedVariable;
244  }
245  ]
246  ]
247  );
248 
249  $actual = $citeproc->render(json_decode($cslJson), "bibliography");
250 
251  $expected = '<div class="csl-bib-body">
252  <div class="csl-entry"><div class="csl-left-margin">[<a id="item-1" href="#item-1">1</a>]</div><div class="csl-right-inline">J. Doe III, My Anonymous Heritage, 2001.</div></div>
253  <div class="csl-entry"><div class="csl-left-margin">[<a id="ITEM-2" href="#ITEM-2">2</a>]</div><div class="csl-right-inline">J. Anderson, J. Brown, Two authors writing a book, 1998.</div></div>
254 </div>';
255  $this->assertEquals($expected, $actual);
256 
257  $actual = $citeproc->render(json_decode($cslJson), "citation");
258 
259  $expected = '[<a href="#item-1">1</a>,<a href="#ITEM-2">2</a>]';
260 
261  $this->assertEquals($expected, $actual);
262  }
263 }
Seboettg\CiteProc\Style\Macro
Definition: Macro.php:37
Seboettg\CiteProc\Context
Definition: Context.php:32
Seboettg\CiteProc\Locale\Locale
Definition: Locale.php:33
Seboettg\CiteProc\Rendering\TextTest
Definition: TextTest.php:19
Seboettg\CiteProc\StyleSheet\loadStyleSheet
static loadStyleSheet($styleName)
Definition: StyleSheet.php:35
Seboettg\CiteProc\Rendering\TextTest\testEnrichMarkupCitationNumber
testEnrichMarkupCitationNumber()
Definition: TextTest.php:186
Seboettg\CiteProc\CiteProc
Definition: CiteProc.php:32
Seboettg\CiteProc\Rendering\TextTest\testMacro
testMacro()
Definition: TextTest.php:52
Seboettg\CiteProc\Rendering
Seboettg\CiteProc\StyleSheet
Definition: StyleSheet.php:22
Seboettg\CiteProc\Rendering\TextTest\testEnrichMarkupURL
testEnrichMarkupURL()
Definition: TextTest.php:138
Seboettg\CiteProc\Rendering\TextTest\testEnrichMarkupTitles
testEnrichMarkupTitles()
Definition: TextTest.php:90
Seboettg\CiteProc\Rendering\TextTest\setUp
setUp()
Definition: TextTest.php:45