FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
ClearedGetterCommonTest.php
1 <?php
2 /*
3 Copyright (C) 2014, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
19 namespace Fossology\Lib\Report;
20 
23 use Mockery as M;
25 
26 include_once(dirname(dirname(__DIR__))."/common-string.php");
27 
29 {
30  public function __construct($groupBy = "content")
31  {
32  parent::__construct($groupBy);
33  }
34 
35  protected function getStatements($uploadId, $uploadTreeTableName, $userId = null, $groupId=null)
36  {
37  return array(
38  array("licenseId" => "371", "risk" => "5", "content" => "1", "text" => "t1", "comments" => "c1", "uploadtree_pk" => 1),
39  array("licenseId" => "213", "risk" => "4", "content" => "1", "text" => "t2", "comments" => "c1", "uploadtree_pk" => 2),
40  array("licenseId" => "243", "risk" => "4", "content" => "2", "text" => "t3", "comments" => "c3", "uploadtree_pk" => 3),
41  );
42  }
43 }
44 
46 {
47  public function __construct($groupBy = "content")
48  {
49  parent::__construct($groupBy);
50  }
51 
52  protected function getStatements($uploadId, $uploadTreeTableName, $userId = null, $groupId=null)
53  {
54  }
55 
56  public function getCleared($uploadId, $groupId = null, $extended = true, $agentcall = null, $isUnifiedReport = false, $objectAgent)
57  {
58  return array(
59  array("good" => "漢", "esc" => "escape", "uml" => ' ü ')
60  );
61  }
62 }
63 
64 class ClearedComonReportTest extends \PHPUnit\Framework\TestCase
65 {
67  private $uploadDao;
69  private $treeDao;
70 
71  protected function setUp()
72  {
73  $this->uploadDao = M::mock(UploadDao::class);
74  $this->treeDao = M::mock(TreeDao::class);
75 
76  $container = M::mock('ContainerBuilder');
77  $GLOBALS['container'] = $container;
78 
79  $container->shouldReceive('get')->with('dao.upload')->andReturn($this->uploadDao);
80  $container->shouldReceive('get')->with('dao.tree')->andReturn($this->treeDao);
81  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
82  }
83 
84  protected function tearDown()
85  {
86  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
87  }
88 
89  public function testGetFileNames()
90  {
91  $this->clearedGetterTest = new TestClearedGetter();
92 
93  $uploadId = 1;
94  $parentId = 112;
95  $uploadTreeTableName = "ut";
96 
97  $this->uploadDao
98  ->shouldReceive('getUploadtreeTableName')
99  ->with($uploadId)
100  ->andReturn($uploadTreeTableName);
101 
102  $this->treeDao
103  ->shouldReceive('getMinimalCoveringItem')
104  ->with($uploadId,$uploadTreeTableName)
105  ->andReturn($parentId);
106 
107  $this->treeDao
108  ->shouldReceive('getFullPath')
109  ->with(1, $uploadTreeTableName, $parentId)
110  ->andReturn("a/1");
111 
112  $this->treeDao
113  ->shouldReceive('getFullPath')
114  ->with(2, $uploadTreeTableName, $parentId)
115  ->andReturn("a/2");
116 
117  $this->treeDao
118  ->shouldReceive('getFullPath')
119  ->with(3, $uploadTreeTableName, $parentId)
120  ->andReturn("a/b/1");
121 
122  $statements = $this->clearedGetterTest->getCleared($uploadId,null,true,null,false,null);
123  $expected = array(
124  "statements" => array(
125  array(
126  "licenseId" => "371",
127  "risk" => "5",
128  "content" => "1",
129  "text" => "t1",
130  "comments" => "c1",
131  "files" => array("a/1", "a/2")
132  ),
133  array(
134  "licenseId" => "243",
135  "risk" => "4",
136  "content" => "2",
137  "text" => "t3",
138  "comments" => "c3",
139  "files" => array("a/b/1")
140  )
141  )
142  );
143  $expected = arsort($expected);
144  assertThat($expected, equalTo($statements));
145  }
146 
147  public function testGetFileNamesGroupByText()
148  {
149  $this->clearedGetterTest = new TestClearedGetter();
150  $uploadId = 1;
151  $parentId = 112;
152  $uploadTreeTableName = "ut";
153 
154  $this->uploadDao
155  ->shouldReceive('getUploadtreeTableName')
156  ->with($uploadId)
157  ->andReturn($uploadTreeTableName);
158 
159  $this->treeDao
160  ->shouldReceive('getMinimalCoveringItem')
161  ->with($uploadId,$uploadTreeTableName)
162  ->andReturn($parentId);
163 
164  $this->treeDao
165  ->shouldReceive('getFullPath')
166  ->with(1, $uploadTreeTableName, $parentId)
167  ->andReturn("a/1");
168 
169  $this->treeDao
170  ->shouldReceive('getFullPath')
171  ->with(2, $uploadTreeTableName, $parentId)
172  ->andReturn("a/2");
173 
174  $this->treeDao
175  ->shouldReceive('getFullPath')
176  ->with(3, $uploadTreeTableName, $parentId)
177  ->andReturn("a/b/1");
178 
179  $tester = new TestClearedGetter("text");
180  $statements = $tester->getCleared($uploadId,null,true,null,false,null);
181  $expected = array(
182  "statements" => array(
183  array(
184  "licenseId" => "371",
185  "risk" => "5",
186  "content" => "1",
187  "text" => "t1",
188  "comments" => "c1",
189  "files" => array("a/1")
190  ),
191  array(
192  "licenseId" => "213",
193  "risk" => "4",
194  "content" => "1",
195  "text" => "t2",
196  "comments" => "c1",
197  "files" => array("a/2")
198  ),
199  array(
200  "licenseId" => "243",
201  "risk" => "4",
202  "content" => "2",
203  "text" => "t3",
204  "comments" => "c3",
205  "files" => array("a/b/1")
206  )
207  )
208  );
209  $expected = arsort($expected);
210  assertThat($expected, equalTo($statements));
211  }
212 
213  function testWeirdChars()
214  {
215  $weirdCharclearedGetter = new WeirdCharclearedGetter();
216  $json = $weirdCharclearedGetter->cJson(0);
217  assertThat($json, containsString('"good":"\\u6f22"'));
218  assertThat($json, containsString('"esc":"escape"'));
219  assertThat($json, containsString('"uml":" \\u00fc "'));
220  }
221 }