FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
HighlightProcessorTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 Author: Andreas Würl
5 
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License
8 version 2 as published by the Free Software Foundation.
9 
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
14 
15 You should have received a copy of the GNU General Public License along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 */
19 
20 namespace Fossology\Lib\View;
21 
26 use Mockery as M;
27 
28 class HighlightProcessorTest extends \PHPUnit\Framework\TestCase
29 {
31  private $license1;
33  private $licenseDao;
35  var $highlight;
36 
37  function setUp()
38  {
39  $this->license1 = new License(10, "shortName", "fullName", "licenseFullText", 4, "URL", 2);
40 
41  $this->licenseDao = M::mock(LicenseDao::class)
42  ->shouldReceive('getLicenseById')->with($this->license1->getId())
43  ->andReturn($this->license1)->getMock();
44 
45  $this->highlight = new HighlightProcessor($this->licenseDao);
46  }
47 
48  function tearDown()
49  {
50  M::close();
51  }
52 
53  function testAddReferenceTexts()
54  {
55  $highlight1 = new Highlight(5, 8, 'type1', 2, 6, $this->license1->getId());
56  $highlights = array($highlight1);
57 
58  $this->highlight->addReferenceTexts($highlights);
59 
60  assertThat($highlight1->getInfoText(), is("10"));
61  }
62 
63  function testSplitOverlappingHighlightEntriesReordersByStart()
64  {
65  $highlight1 = new Highlight(5, 8, 'type1', 'ref1', 0, 0);
66  $highlight2 = new Highlight(4, 8, 'text2', 'ref2', 0, 0);
67  $highlightInfos = array($highlight1, $highlight2);
68 
69  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
70 
71  assertThat($splitOverlap, anArray(
72  array(
73  4 => array(new SplitPosition(0, SplitPosition::START, $highlight2)),
74  5 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
75  8 => array(
76  new SplitPosition(1, SplitPosition::END, $highlight1),
77  new SplitPosition(0, SplitPosition::END, $highlight2)
78  )
79  )));
80  }
81 
82  function testSplitHighlightEntriesContainingAnAtomAtStart()
83  {
84  $highlight1 = new Highlight(5, 8, 'type1', 'ref1', 0, 0);
85  $highlight2 = new Highlight(5, 5, 'type1', 'ref2', 0, 0);
86  $highlightInfos = array($highlight1, $highlight2);
87 
88  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
89 
90  assertThat($splitOverlap, anArray(
91  array(
92  5 => array(
93  new SplitPosition(0, SplitPosition::START, $highlight1),
94  new SplitPosition(1, SplitPosition::ATOM, $highlight2)),
95  8 => array(new SplitPosition(0, SplitPosition::END, $highlight1))
96  )));
97  }
98 
99  function testSplitHighlightEntriesContainingAnAtomInTheMiddle()
100  {
101  $highlight1 = new Highlight(5, 8, 'type1', 'ref1', 0, 0);
102  $highlight2 = new Highlight(7, 7, 'type2', 'ref2', 0, 0);
103  $highlightInfos = array($highlight1, $highlight2);
104 
105  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
106 
107  assertThat($splitOverlap, anArray(
108  array(
109  5 => array(new SplitPosition(0, SplitPosition::START, $highlight1)),
110  7 => array(new SplitPosition(1, SplitPosition::ATOM, $highlight2)),
111  8 => array(new SplitPosition(0, SplitPosition::END, $highlight1))
112  )));
113  }
114 
115  function testSplitOverlappingHightlightEntriesReordersByLengthIfStartIsIdentical()
116  {
117  $highlight1 = new Highlight(5, 8, 'type1', 'ref1', 0, 0);
118  $highlight2 = new Highlight(5, 9, 'type2', 'ref2', 0, 0);
119  $highlightInfos = array($highlight1, $highlight2);
120 
121  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
122 
123  assertThat($splitOverlap, anArray(
124  array(
125  5 => array(new SplitPosition(0, SplitPosition::START, $highlight2),
126  new SplitPosition(1, SplitPosition::START, $highlight1)),
127  8 => array(new SplitPosition(1, SplitPosition::END, $highlight1)),
128  9 => array(new SplitPosition(0, SplitPosition::END, $highlight2))
129  )));
130  }
131 
132  function testSplitOverlappingHightlightEntriesSplitsOverlappingEntries()
133  {
134  $highlight1 = new Highlight(5, 8, 'type1', 'ref1', 0, 0);
135  $highlight2 = new Highlight(7, 11, 'type2', 'ref2', 0, 0);
136  $highlightInfos = array($highlight1, $highlight2);
137 
138  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
139 
140  assertThat($splitOverlap, anArray(
141  array(
142  5 => array(new SplitPosition(0, SplitPosition::START, $highlight1)),
143  7 => array(new SplitPosition(1, SplitPosition::START, $highlight2)),
144  8 => array(
145  new SplitPosition(1, SplitPosition::END, $highlight2),
146  new SplitPosition(0, SplitPosition::END, $highlight1),
147  new SplitPosition(1, SplitPosition::START, $highlight2)),
148  11 => array(new SplitPosition(1, SplitPosition::END, $highlight2))
149  )));
150  }
151 
152  function testSplitOverlappingHightlightEntriesKeepsNonOverlappingEntriesInDepth()
153  {
154  $highlight1 = new Highlight(5, 15, 'type1', 'ref1', 0, 0);
155  $highlight2 = new Highlight(7, 13, 'type2', 'ref2', 0, 0);
156  $highlight3 = new Highlight(9, 11, 'type3', 'ref3', 0, 0);
157  $highlightInfos = array($highlight1, $highlight2, $highlight3);
158 
159  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
160 
161  assertThat($splitOverlap, anArray(
162  array(
163  5 => array(new SplitPosition(0, SplitPosition::START, $highlight1)),
164  7 => array(new SplitPosition(1, SplitPosition::START, $highlight2)),
165  9 => array(new SplitPosition(2, SplitPosition::START, $highlight3)),
166  11 => array(new SplitPosition(2, SplitPosition::END, $highlight3)),
167  13 => array(new SplitPosition(1, SplitPosition::END, $highlight2)),
168  15 => array(new SplitPosition(0, SplitPosition::END, $highlight1))
169  )));
170  }
171 
172  function testSplitOverlappingHightlightEntriesSplitsOverlappingEntriesInDepth()
173  {
174  $highlight1 = new Highlight(5, 11, 'type1', 'ref1', 0, 0);
175  $highlight2 = new Highlight(7, 13, 'type2', 'ref2', 0, 0);
176  $highlight3 = new Highlight(9, 15, 'type3', 'ref3', 0, 0);
177  $highlightInfos = array($highlight1, $highlight2, $highlight3);
178 
179  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
180 
181  assertThat($splitOverlap, anArray(
182  array(
183  5 => array(new SplitPosition(0, SplitPosition::START, $highlight1)),
184  7 => array(new SplitPosition(1, SplitPosition::START, $highlight2)),
185  9 => array(new SplitPosition(2, SplitPosition::START, $highlight3)),
186  11 => array(
187  new SplitPosition(2, SplitPosition::END, $highlight3),
188  new SplitPosition(1, SplitPosition::END, $highlight2),
189  new SplitPosition(0, SplitPosition::END, $highlight1),
190  new SplitPosition(1, SplitPosition::START, $highlight2),
191  new SplitPosition(2, SplitPosition::START, $highlight3),),
192  13 => array(
193  new SplitPosition(2, SplitPosition::END, $highlight3),
194  new SplitPosition(1, SplitPosition::END, $highlight2),
195  new SplitPosition(2, SplitPosition::START, $highlight3),),
196  15 => array(new SplitPosition(2, SplitPosition::END, $highlight3))
197  )));
198 
199  }
200 
201  function testSplitOverlappingHightlightEntriesSplitsPartialOverlappingEntries()
202  {
203  $highlight1 = new Highlight(5, 8, 'type1', 'ref1', 0, 0);
204  $highlight2 = new Highlight(10, 13, 'type2', 'ref2', 0, 0);
205  $highlight3 = new Highlight(7, 11, 'type3', 'ref3', 0, 0);
206  $highlightInfos = array($highlight1, $highlight2, $highlight3);
207 
208  $splitOverlap = $this->highlight->calculateSplitPositions($highlightInfos);
209 
210  assertThat($splitOverlap, anArray(
211  array(
212  5 => array(new SplitPosition(0, SplitPosition::START, $highlight1)),
213  7 => array(new SplitPosition(1, SplitPosition::START, $highlight3)),
214  8 => array(
215  new SplitPosition(1, SplitPosition::END, $highlight3),
216  new SplitPosition(0, SplitPosition::END, $highlight1),
217  new SplitPosition(1, SplitPosition::START, $highlight3)),
218  10 => array(
219  new SplitPosition(1, SplitPosition::END, $highlight3),
220  new SplitPosition(0, SplitPosition::START, $highlight2),
221  new SplitPosition(1, SplitPosition::START, $highlight3)),
222  11 => array(new SplitPosition(1, SplitPosition::END, $highlight3),),
223  13 => array(new SplitPosition(0, SplitPosition::END, $highlight2))
224  )));
225  }
226 
227  function testFlattenHighlightWithSeparatedEntries()
228  {
229  $highlight1 = new Highlight(5, 8, 'type1', 'ref1', 0, 0);
230  $highlight2 = new Highlight(10, 13, 'type2', 'ref2', 0, 0);
231  $highlights = array($highlight1, $highlight2);
232 
233  $this->highlight->flattenHighlights($highlights);
234 
235  assertThat($highlights, anArray(
236  array(
237  new Highlight(5, 8, Highlight::UNDEFINED, 'ref1', 0, 0),
238  new Highlight(10, 13, Highlight::UNDEFINED, 'ref2', 0, 0)
239  )));
240  }
241 
242  function testFlattenHighlightWithOverlappingEntries()
243  {
244  $highlight1 = new Highlight(5, 10, Highlight::MATCH, 'ref1', 0, 0);
245  $highlight2 = new Highlight(8, 13, Highlight::ADDED, 'ref2', 0, 0);
246  $highlights = array($highlight1, $highlight2);
247 
248  $this->highlight->flattenHighlights($highlights);
249 
250  assertThat($highlights, anArray(
251  array(
252  new Highlight(5, 10, Highlight::UNDEFINED, 'ref1', 0, 0),
253  new Highlight(10, 13, Highlight::UNDEFINED, 'ref2', 0, 0)
254  )));
255  }
256 
257  function testFlattenHighlightWithFullyOverlappingEntries()
258  {
259  $highlight1 = new Highlight(5, 10, Highlight::MATCH, 'ref1', 0, 0);
260  $highlight2 = new Highlight(7, 9, Highlight::ADDED, 'ref2', 0, 0);
261  $highlights = array($highlight1, $highlight2);
262 
263  $this->highlight->flattenHighlights($highlights);
264 
265  assertThat($highlights, anArray(
266  array(
267  new Highlight(5, 10, Highlight::UNDEFINED, 'ref1', 0, 0),
268  )));
269  }
270 
271  function testFlattenHighlightWithIgnoredEntries()
272  {
273  $highlight1 = new Highlight(5, 10, Highlight::MATCH, 'ref1', 0, 0);
274  $highlight2 = new Highlight(7, 9, Highlight::KEYWORD, 'ref2', 0, 0);
275  $highlights = array($highlight1, $highlight2);
276 
277  $this->highlight->flattenHighlights($highlights, array(Highlight::KEYWORD));
278 
279  assertThat($highlights, anArray(
280  array(
281  new Highlight(5, 10, Highlight::UNDEFINED, 'ref1', 0, 0),
282  new Highlight(7, 9, Highlight::KEYWORD, 'ref2', 0, 0)
283  )));
284  }
285 
286  function testFlattenHighlightWithOverlappingEntriesThatHaveEqualEnd()
287  {
288  $highlight1 = new Highlight(5, 10, Highlight::MATCH, 'ref1', 0, 0);
289  $highlight2 = new Highlight(7, 10, Highlight::ADDED, 'ref2', 0, 0);
290  $highlights = array($highlight1, $highlight2);
291 
292  $this->highlight->flattenHighlights($highlights);
293 
294  assertThat($highlights, anArray(
295  array(
296  new Highlight(5, 10, Highlight::UNDEFINED, 'ref1', 0, 0),
297  )));
298  }
299 }