FOSSology  4.4.0
Open Source License Compliance by Open Source Software
ClearedGetterCommonTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
8 namespace Fossology\Lib\Report;
9 
12 use Mockery as M;
13 use Mockery\MockInterface;
14 
15 include_once(dirname(dirname(__DIR__))."/common-string.php");
16 
18 {
19  public function __construct($groupBy = "content")
20  {
21  parent::__construct($groupBy);
22  }
23 
24  protected function getStatements($uploadId, $uploadTreeTableName, $userId = null, $groupId=null)
25  {
26  return array(
27  array("licenseId" => "371", "risk" => "5", "content" => "1", "text" => "t1", "comments" => "c1", "uploadtree_pk" => 1),
28  array("licenseId" => "213", "risk" => "4", "content" => "1", "text" => "t2", "comments" => "c1", "uploadtree_pk" => 2),
29  array("licenseId" => "243", "risk" => "4", "content" => "2", "text" => "t3", "comments" => "c3", "uploadtree_pk" => 3),
30  );
31  }
32 }
33 
35 {
36  public function __construct($groupBy = "content")
37  {
38  parent::__construct($groupBy);
39  }
40 
41  protected function getStatements($uploadId, $uploadTreeTableName, $userId = null, $groupId=null)
42  {
43  }
44 
45  public function getCleared($uploadId, $objectAgent, $groupId = null, $extended = true, $agentcall = null, $isUnifiedReport = false)
46  {
47  return array(
48  array("good" => "漢", "esc" => "esc␛ape", "uml" => ' ü ')
49  );
50  }
51 }
52 
53 class ClearedGetterCommonTest extends \PHPUnit\Framework\TestCase
54 {
56  private $uploadDao;
58  private $treeDao;
59 
60  protected function setUp() : void
61  {
62  $this->uploadDao = M::mock(UploadDao::class);
63  $this->treeDao = M::mock(TreeDao::class);
64 
65  $container = M::mock('ContainerBuilder');
66  $GLOBALS['container'] = $container;
67 
68  $container->shouldReceive('get')->with('dao.upload')->andReturn($this->uploadDao);
69  $container->shouldReceive('get')->with('dao.tree')->andReturn($this->treeDao);
70  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
71  }
72 
73  protected function tearDown() : void
74  {
75  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
76  }
77 
78  public function testGetFileNames()
79  {
80  $this->clearedGetterTest = new TestClearedGetter();
81 
82  $uploadId = 1;
83  $parentId = 112;
84  $uploadTreeTableName = "ut";
85 
86  $this->uploadDao
87  ->shouldReceive('getUploadtreeTableName')
88  ->with($uploadId)
89  ->andReturn($uploadTreeTableName);
90 
91  $this->treeDao
92  ->shouldReceive('getMinimalCoveringItem')
93  ->with($uploadId,$uploadTreeTableName)
94  ->andReturn($parentId);
95 
96  $this->treeDao
97  ->shouldReceive('getFullPath')
98  ->with(1, $uploadTreeTableName, $parentId)
99  ->andReturn("a/1");
100 
101  $this->treeDao
102  ->shouldReceive('getItemHashes')
103  ->with(1)
104  ->andReturn(array('sha1'=> "9B12538E" ,'md5'=> "MD52538E"));
105 
106  $this->treeDao
107  ->shouldReceive('getFullPath')
108  ->with(2, $uploadTreeTableName, $parentId)
109  ->andReturn("a/2");
110 
111  $this->treeDao
112  ->shouldReceive('getItemHashes')
113  ->with(2)
114  ->andReturn(array('sha1'=> "8C2275AE" ,'md5'=> "MD5275AE"));
115 
116  $this->treeDao
117  ->shouldReceive('getFullPath')
118  ->with(3, $uploadTreeTableName, $parentId)
119  ->andReturn("a/b/1");
120 
121  $this->treeDao
122  ->shouldReceive('getItemHashes')
123  ->with(3)
124  ->andReturn(array('sha1'=> "CA10238C" ,'md5'=> "MD50238C"));
125 
126  $this->treeDao
127  ->shouldReceive('getFullPath')
128  ->with(4, $uploadTreeTableName, $parentId)
129  ->andReturn("a/4");
130 
131  $this->treeDao
132  ->shouldReceive('getItemHashes')
133  ->with(4)
134  ->andReturn(array('sha1'=> "AB12838A" ,'md5'=> "MD52838A"));
135 
136  $statements = $this->clearedGetterTest->getCleared($uploadId, null);
137  $expected = array(
138  "statements" => array(
139  array(
140  "licenseId" => "371",
141  "risk" => "5",
142  "content" => "1",
143  "text" => "d1",
144  "comments" => "c1",
145  "files" => array("a/1", "a/2"),
146  "hash" => array("9B12538E","8C2275AE")
147  ),
148  array(
149  "licenseId" => "213",
150  "risk" => "5",
151  "content" => "tf1",
152  "text" => "d1",
153  "comments" => "c1",
154  "files" => array("a/1"),
155  "hash" => array("9B12538E")
156  ),
157  array(
158  "licenseId" => "243",
159  "risk" => "3",
160  "content" => "tf1",
161  "text" => "d2",
162  "comments" => "c4",
163  "files" => array("a/4"),
164  "hash" => array("AB12838A")
165  ),
166  array(
167  "licenseId" => "8",
168  "risk" => "4",
169  "content" => "2",
170  "text" => "t3",
171  "comments" => "c3",
172  "files" => array("a/b/1"),
173  "hash" => array("CA10238C")
174  )
175  )
176  );
177  assertThat(arsort($expected), equalTo($statements));
178  }
179 
180  public function testGetFileNamesGroupByText()
181  {
182  $this->clearedGetterTest = new TestClearedGetter();
183  $uploadId = 1;
184  $parentId = 112;
185  $uploadTreeTableName = "ut";
186 
187  $this->uploadDao
188  ->shouldReceive('getUploadtreeTableName')
189  ->with($uploadId)
190  ->andReturn($uploadTreeTableName);
191 
192  $this->treeDao
193  ->shouldReceive('getMinimalCoveringItem')
194  ->with($uploadId,$uploadTreeTableName)
195  ->andReturn($parentId);
196 
197  $this->treeDao
198  ->shouldReceive('getFullPath')
199  ->with(1, $uploadTreeTableName, $parentId)
200  ->andReturn("a/1");
201 
202  $this->treeDao
203  ->shouldReceive('getItemHashes')
204  ->with(1)
205  ->andReturn(array('sha1'=> "9B12538E" ,'md5'=> "MD52538E"));
206 
207  $this->treeDao
208  ->shouldReceive('getFullPath')
209  ->with(2, $uploadTreeTableName, $parentId)
210  ->andReturn("a/2");
211 
212  $this->treeDao
213  ->shouldReceive('getItemHashes')
214  ->with(2)
215  ->andReturn(array('sha1'=> "8C2275AE" ,'md5'=> "MD5275AE"));
216 
217  $this->treeDao
218  ->shouldReceive('getFullPath')
219  ->with(3, $uploadTreeTableName, $parentId)
220  ->andReturn("a/b/1");
221 
222  $this->treeDao
223  ->shouldReceive('getItemHashes')
224  ->with(3)
225  ->andReturn(array('sha1'=> "CA10238C" ,'md5'=> "MD50238C"));
226 
227  $this->treeDao
228  ->shouldReceive('getFullPath')
229  ->with(4, $uploadTreeTableName, $parentId)
230  ->andReturn("a/4");
231 
232  $this->treeDao
233  ->shouldReceive('getItemHashes')
234  ->with(4)
235  ->andReturn(array('sha1'=> "AB12838A" ,'md5'=> "MD52838A"));
236 
237  $tester = new TestClearedGetter("text");
238  $statements = $tester->getCleared($uploadId, null);
239  $expected = array(
240  "statements" => array(
241  array(
242  "licenseId" => "371",
243  "risk" => "5",
244  "content" => "tf1",
245  "text" => "d1",
246  "comments" => "c1",
247  "files" => array("a/1"),
248  "hash" => array("9B12538E")
249  ),
250  array(
251  "licenseId" => "243",
252  "risk" => "4",
253  "content" => "1",
254  "text" => "t2",
255  "comments" => "c1",
256  "files" => array("a/2"),
257  "hash" => array("8C2275AE")
258  ),
259  array(
260  "licenseId" => "243",
261  "risk" => "4",
262  "content" => "2",
263  "text" => "t3",
264  "comments" => "c3",
265  "files" => array("a/b/1"),
266  "hash" => array("CA10238C")
267  ),
268  array(
269  "licenseId" => "8",
270  "risk" => "3",
271  "content" => "tf1",
272  "text" => "d1",
273  "comments" => "c1",
274  "files" => array("a/4"),
275  "hash" => array("AB12838A")
276  )
277  )
278  );
279  assertThat(arsort($expected), equalTo($statements));
280  }
281 
282  function testWeirdChars()
283  {
284  $weirdCharclearedGetter = new WeirdCharclearedGetter();
285  $json = $weirdCharclearedGetter->cJson(0);
286  assertThat($json, containsString('"good":"\\u6f22"'));
287  assertThat($json, containsString('"esc":"escape"'));
288  assertThat($json, containsString('"uml":" \\u00fc "'));
289  }
290 }