FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
TextRendererTest.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 namespace Fossology\Lib\View;
20 
24 use Mockery as M;
25 
26 require_once __DIR__.'/../../common-string.php';
27 
28 class TextRendererTest extends \PHPUnit\Framework\TestCase
29 {
30  const START_OFFSET = 10;
31  const FRAGMENT_TEXT = "foo bar baz quux";
32 
34  private $textFragment;
36  private $textRenderer;
37 
38  function setUp()
39  {
40  $this->textFragment = new TextFragment(self::START_OFFSET, self::FRAGMENT_TEXT);
41  $this->textRenderer = new TextRenderer(new HighlightRenderer());
42  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
43  }
44 
45  function tearDown()
46  {
47  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
48  M::close();
49  }
50 
51  function testRenderHighlightedTextWithNoSplitPosition()
52  {
53  $renderedText = $this->textRenderer->renderText($this->textFragment);
54  assertThat($renderedText, is(self::FRAGMENT_TEXT));
55  }
56 
57  function testRenderHighlightOutsideFragment()
58  {
59  $highlight1 = new Highlight(0, 5, Highlight::MATCH, 0, 0, 'ref1');
60 
61  $splitPositions = array(
62  0 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
63  5 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
64  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
65 
66  assertThat($renderedText, is("foo bar baz quux"));
67  }
68 
69  function testRenderHighlightAtStartOfFragment()
70  {
71  $highlight1 = new Highlight(self::START_OFFSET, self::START_OFFSET + 5, Highlight::MATCH, 0, 0, 'ref1');
72 
73  $splitPositions = array(
74  self::START_OFFSET => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
75  self::START_OFFSET + 5 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
76  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
77 
78  assertThat($renderedText, is("<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">foo b</span>ar baz quux"));
79  }
80 
81  function testRenderHighlightAtEndOfFragment()
82  {
83  $highlight_length = 5;
84  $end_offset = self::START_OFFSET + strlen(self::FRAGMENT_TEXT);
85  $start_offset = $end_offset - $highlight_length;
86 
87  $highlight1 = new Highlight($start_offset, $end_offset, Highlight::MATCH, 0, 0, 'ref1');
88 
89  $splitPositions = array(
90  $start_offset => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
91  $end_offset => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
92  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
93 
94  assertThat($renderedText, is("foo bar baz<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\"> quux</span>"));
95  }
96 
97  function testRenderHighlightWithinFragment()
98  {
99  $highlight1 = new Highlight(15, 18, Highlight::MATCH, 0, 0, 'ref1');
100 
101  $splitPositions = array(
102  15 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
103  18 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
104  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
105 
106  assertThat($renderedText, is("foo b<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">ar </span>baz quux"));
107  }
108 
109  function testRenderHexHighlightWithinFragment()
110  {
111  $highlight1 = new Highlight(15, 18, Highlight::MATCH, 0, 0, 'ref1');
112 
113  $splitPositions = array(
114  15 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
115  18 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
116  $renderedText = $this->textRenderer->renderHex($this->textFragment, $splitPositions);
117 
118  assertThat($renderedText, is("0x0000000A |66 6f 6f 20 62 <a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">61 72 20 </span>62 61 7a 20 71 75 75 78| |foo&nbsp;b<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">ar&nbsp;</span>baz&nbsp;quux|<br/>\n"));
119  }
120 
121  function testRenderHighlightWithBacklinkWithinFragment()
122  {
123  $highlight1 = new Highlight(15, 18, Highlight::MATCH, 0, 0, 'ref1');
124 
125  $splitPositions = array(
126  15 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
127  18 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
128  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions, true);
129 
130  assertThat($renderedText, is("foo b<a id=\"highlight\" href=\"#top\">&nbsp;&#8593;&nbsp;</a><span class=\"hi-match\" title=\"ref1\">ar </span>baz quux"));
131  }
132 
133  function testRenderHighlightedOverlapsStartOfFragment()
134  {
135  $highlight1 = new Highlight(5, 18, Highlight::MATCH, 0, 0, 'ref1');
136 
137  $splitPositions = array(
138  5 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
139  18 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
140  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
141 
142  assertThat($renderedText, is("<span class=\"hi-match\" title=\"ref1\">foo bar </span>baz quux"));
143  }
144 
145  function testRenderHighlightedOverlapsEndOfFragment()
146  {
147  $highlight1 = new Highlight(15, 28, Highlight::MATCH, 0, 0, 'ref1');
148 
149  $splitPositions = array(
150  15 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
151  28 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
152  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
153 
154  assertThat($renderedText, is("foo b<a id=\"highlight\"></a><span class=\"hi-match\" title=\"ref1\">ar baz quux</span>"));
155  }
156 
157  function testRenderFragmentFullInsideHighlight()
158  {
159  $highlight1 = new Highlight(5, 50, Highlight::MATCH, 0, 0, 'ref1');
160 
161  $splitPositions = array(
162  5 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
163  50 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
164  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
165 
166  assertThat($renderedText, is("<span class=\"hi-match\" title=\"ref1\">foo bar baz quux</span>"));
167  }
168 
169  function testRenderHighlightedTextWithFourSplitPositions()
170  {
171  $highlight1 = new Highlight(12, 18, Highlight::URL, 'ref1', 0, 0);
172  $highlight2 = new Highlight(14, 18, Highlight::MATCH, 'ref2', 0, 0);
173 
174  $splitPositions = array(
175  12 => array(new SplitPosition(1, SplitPosition::START, $highlight1)),
176  14 => array(new SplitPosition(2, SplitPosition::START, $highlight2)),
177  18 => array(new SplitPosition(2, SplitPosition::END, $highlight2)),
178  20 => array(new SplitPosition(1, SplitPosition::END, $highlight1)));
179  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
180 
181  assertThat($renderedText, is("fo<a id=\"highlight\"></a><span class=\"hi-url\" title=\"0\">o <span class=\"hi-match\" title=\"0\">bar </span>ba</span>z quux"));
182  }
183 
184  function testRenderHighlightThatIsIgnorableByBulk()
185  {
186  $highlight1 = new Highlight(14, 14, Highlight::DELETED, 0, 0, 'ref1');
187 
188  $splitPositions = array(
189  14 => array(new SplitPosition(1, SplitPosition::ATOM, $highlight1)));
190  $renderedText = $this->textRenderer->renderText($this->textFragment, $splitPositions);
191  $pastedText = strip_tags($renderedText);
192  $bulkText = preg_replace('/[!#]/',' ',$pastedText);
193  $cleanText = preg_replace('/\s\s+/',' ',$bulkText);
194 
195  assertThat($cleanText, is("foo bar baz quux"));
196  }
197 }