FOSSology  4.4.0
Open Source License Compliance by Open Source Software
CopyrightDaoTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2018 Siemens AG
4  Author: Daniele Fognini, Steffen Weber
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
8 
9 namespace Fossology\Lib\Dao;
10 
16 use Mockery as M;
18 
19 if (!function_exists('Traceback_uri')) {
20  function Traceback_uri()
21  {
22  return 'Traceback_uri_if_desired';
23  }
24 }
25 
26 class CopyrightDaoTest extends \PHPUnit\Framework\TestCase
27 {
29  private $testDb;
31  private $dbManager;
32 
33  protected function setUp() : void
34  {
35  $this->testDb = new TestPgDb();
36  $this->dbManager = $this->testDb->getDbManager();
37  }
38 
39  protected function tearDown() : void
40  {
41  $this->testDb = null;
42  $this->dbManager = null;
43 
44  M::close();
45  }
46 
47  public function testGetCopyrightHighlights()
48  {
49  $this->testDb->createPlainTables(array(),true);
50  $this->testDb->createInheritedTables();
51  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
52  $uploadDao->shouldReceive('getUploadEntry')->with(1)->andReturn(array('pfile_fk'=>8));
53  $uploadDao->shouldReceive('getUploadEntry')->with(2)->andReturn(array('pfile_fk'=>9));
54  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
55  $noHighlights = $copyrightDao->getHighlights($uploadTreeId=1);
56  assertThat($noHighlights,emptyArray());
57 
58  $this->testDb->insertData(array('copyright'));
59  $highlights = $copyrightDao->getHighlights($uploadTreeId = 1);
60  assertThat($highlights,arrayWithSize(1));
61  $highlight0 = $highlights[0];
62  assertThat($highlight0,anInstanceOf(Highlight::class));
63  $this->assertInstanceOf('Fossology\Lib\Data\Highlight', $highlight0);
64  assertThat($highlight0->getEnd(),equalTo(201));
65 
66  $hilights = $copyrightDao->getHighlights($uploadTreeId=2);
67  assertThat($hilights,arrayWithSize(1));
68  $hilight0 = $hilights[0];
69  assertThat($hilight0->getStart(),equalTo(0));
70  }
71 
72  private function runCopyright()
73  {
74  $sysConf = $this->testDb->getFossSysConf().time();
75  mkdir($sysConf);
76  copy($this->testDb->getFossSysConf()."/Db.conf","$sysConf/Db.conf");
77  system("touch ".$sysConf."/fossology.conf");
78  $copyDir = dirname(dirname(dirname(dirname(__DIR__))))."/copyright/";
79  system("install -D $copyDir/VERSION-copyright $sysConf/mods-enabled/copyright/VERSION");
80 
81  $retCode = 0;
82  system("echo | ".$copyDir."agent/copyright -c ".$sysConf." 2>/dev/null", $retCode);
83  $this->assertEquals(0, $retCode, 'this test requires a working copyright agent which creates the necessary tables');
84 
85  unlink("$sysConf/mods-enabled/copyright/VERSION");
86  rmdir("$sysConf/mods-enabled/copyright");
87  rmdir("$sysConf/mods-enabled");
88  unlink($sysConf."/fossology.conf");
89  unlink($sysConf."/Db.conf");
90  rmdir($sysConf);
91  }
92 
93  private function setUpClearingTables()
94  {
95  $this->testDb->createPlainTables(array('copyright','uploadtree','copyright_decision','copyright_event'));
96  $this->testDb->createInheritedTables(array('uploadtree_a'));
97  $this->testDb->insertData(array('copyright','uploadtree_a'));
98 
99  $this->testDb->createSequences(array('copyright_pk_seq','copyright_decision_pk_seq'));
100  $this->testDb->alterTables(array('copyright','copyright_decision'));
101  }
102 
103  private function searchContent($array, $content, $key='content')
104  {
105  foreach ($array as $entry) {
106  if (array_key_exists($key, $entry)) {
107  if ($entry[$key] === $content) {
108  return true;
109  }
110  }
111  }
112  return false;
113  }
114 
115  public function testGetAllEntries()
116  {
117  $this->setUpClearingTables();
118 
119  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
120  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
121 
122  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a");
123  $this->assertEquals(14, count($entries));
124  $this->assertTrue($this->searchContent($entries,"info@3dfx.com"));
125  }
126 
127  public function testGetAllEntriesReport()
128  {
129  $this->setUpClearingTables();
130 
131  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
132  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
133 
134  $entries = $copyrightDao->getAllEntriesReport("copyright", 1, "uploadtree_a");
135  $this->assertEquals(15, count($entries));
136  $this->assertTrue($this->searchContent($entries,"copyright 3dfx interactive, inc. 1999, all rights reserved this \n"));
137  }
138 
139  public function testGetAllEntriesOnlyStatementsAndIndentifyedIfCleared()
140  {
141  $this->setUpClearingTables();
142 
143  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
144  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
145 
146  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", false, DecisionTypes::IDENTIFIED);
147  $this->assertEquals(13, count($entries));
148  $this->assertFalse($this->searchContent($entries,"info@3dfx.com"));
149  }
150 
151  public function testGetAllEntriesOnlyStatementsWithFilterAndIndentifyedIfCleared()
152  {
153  $this->setUpClearingTables();
154 
155  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
156  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
157 
158  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", false, DecisionTypes::IDENTIFIED, "C.content LIKE '%permission of 3dfx interactiv%'");
159  $this->assertEquals(1, count($entries));
160  $this->assertTrue($this->searchContent($entries, "written permission of 3dfx interactive, \ninc. see the 3dfx glide general public license for a full text of the \n"));
161  }
162 
163  public function testGetAllEntriesOnlyStatementsAndOnlyClearedIndentifyed()
164  {
165  $this->setUpClearingTables();
166 
167  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
168  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
169 
170  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", true, DecisionTypes::IDENTIFIED);
171  $this->assertEquals(0, count($entries));
172  }
173 
174  public function testGetAllEntriesForReport_afterADecision()
175  {
176  $this->setUpClearingTables();
177 
178  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
179  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
180 
181  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IDENTIFIED,"desc","text","comment"); // pfile_fk=4 => uploadtree_pk=7
182  $entries = $copyrightDao->getAllEntriesReport("copyright", 1, "uploadtree_a", "statement", false, DecisionTypes::IDENTIFIED);
183  $this->assertTrue($this->searchContent($entries, "desc", 'description'));
184  $this->assertTrue($this->searchContent($entries, "text", 'textfinding'));
185  $this->assertTrue($this->searchContent($entries, "comment", 'comments'));
186  }
187 
188  public function testGetAllEntriesOnlyStatementsAndOnlyClearedIndentifyed_afterADecision()
189  {
190  $this->setUpClearingTables();
191 
192  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
193  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
194 
195  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IDENTIFIED,"desc","text","comment"); // pfile_fk=4 => uploadtree_pk=7
196  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", true, DecisionTypes::IDENTIFIED);
197  $this->assertEquals(3, count($entries));
198 
199  $expected = array(
200  array(
201  "description"=> "desc",
202  "textfinding" => "text",
203  "comments" => "comment",
204  "uploadtree_pk" => "7",
205  "clearing_decision_type_fk" => "5",
206  "content" => "copyright 3dfx interactive, inc. 1999, all rights reserved this \n"),
207  array(
208  "description"=> "desc",
209  "textfinding" => "text",
210  "comments" => "comment",
211  "uploadtree_pk" => "7",
212  "clearing_decision_type_fk" => "5",
213  "content" => "copyright laws of \nthe united states. \n\ncopyright 3dfx interactive, inc. 1999, all rights reserved\" \n"),
214  array(
215  "description" => "desc",
216  "textfinding" => "text",
217  "comments" => "comment",
218  "uploadtree_pk" => "7",
219  "clearing_decision_type_fk"=> "5",
220  "content" => "written permission of 3dfx interactive, \ninc. see the 3dfx glide general public license for a full text of the \n"
221  )
222  );
223  $this->assertEquals($expected, $entries);
224  }
225 
226  public function testGetAllEntriesOnlyStatementsWithFilterAndOnlyClearedIndentifyed_afterIdentifiedDecision()
227  {
228  $this->setUpClearingTables();
229 
230  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
231  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
232 
233  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IDENTIFIED,"desc","text","comment");
234  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", true, DecisionTypes::IDENTIFIED, "C.content LIKE 'written%'");
235  $this->assertEquals(1, count($entries));
236  $this->assertTrue($this->searchContent($entries, "written permission of 3dfx interactive, \ninc. see the 3dfx glide general public license for a full text of the \n"));
237  }
238 
239  public function testGetAllEntriesOnlyStatementsOnlyClearedIndentifyed_irrelevantDecisionIsIrrelevant()
240  {
241  $this->setUpClearingTables();
242 
243  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
244  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
245 
246  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IRRELEVANT,"desc1","text1","comment1");
247  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", true, DecisionTypes::IDENTIFIED);
248  $this->assertEquals(0, count($entries));
249  }
250 
251  public function testGetAllEntriesOnlyStatementsWithFilterAndOnlyClearedIndentifyed_afterTwoDecisions()
252  {
253  $this->setUpClearingTables();
254 
255  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
256  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
257 
258  $decisionId = $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IDENTIFIED,"desc","text","comment");
259  $copyrightDao->removeDecision("copyright_decision", 4, $decisionId);
260  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IRRELEVANT,"desc1","text1","comment1");
261  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", true, DecisionTypes::IDENTIFIED, "C.content LIKE 'written%'");
262  $this->assertEquals(0, count($entries));
263  }
264 
265  public function testGetAllEntriesOnlyStatementsWithFilterAndOnlyClearedIndentifyed_afterTwoDecisionsWinsSecond()
266  {
267  $this->setUpClearingTables();
268 
269  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
270  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
271 
272  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IDENTIFIED,"desc","text","comment");
273  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IRRELEVANT,"desc1","text1","comment1");
274  $copyrightDao->saveDecision("copyright_decision", 4, 2, DecisionTypes::IDENTIFIED,"desc2","text","comment");
275  $entries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a", "statement", true, DecisionTypes::IDENTIFIED, "C.content LIKE 'written%'");
276  $this->assertEquals(1, count($entries));
277  $this->assertTrue($this->searchContent($entries, "written permission of 3dfx interactive, \ninc. see the 3dfx glide general public license for a full text of the \n"));
278  $this->assertEquals("desc2", $entries[0]['description']);
279  }
280 
281  public function testUpdateTable()
282  {
283  $this->setUpClearingTables();
284 
285  $container = M::mock('ContainerBuilder');
286  $agentDao = M::mock('Fossology\Lib\Dao\AgentDao');
287  $agentDao->shouldReceive('arsTableExists')->withArgs(['copyright'])
288  ->andReturn(true);
289  $agentDao->shouldReceive('arsTableExists')->withArgs(['reso'])
290  ->andReturn(true);
291  $agentDao->shouldReceive('getSuccessfulAgentEntries')
292  ->withArgs(['copyright', 1])->andReturn([['agent_id' => '8',
293  'agent_rev' => 'trunk.271e3e', 'agent_name' => 'copyright']]);
294  $agentDao->shouldReceive('getSuccessfulAgentEntries')
295  ->withArgs(['reso', 1])->andReturn([['agent_id' => '8',
296  'agent_rev' => 'trunk.271e3e', 'agent_name' => 'reso']]);
297  $agentDao->shouldReceive('getCurrentAgentRef')->withArgs(['copyright'])
298  ->andReturn(new AgentRef(8, 'copyright', 'trunk.271e3e'));
299  $agentDao->shouldReceive('getCurrentAgentRef')->withArgs(['reso'])
300  ->andReturn(new AgentRef(8, 'reso', 'trunk.271e3e'));
301 
302  $container->shouldReceive('get')->withArgs(['dao.agent'])
303  ->andReturn($agentDao);
304  $GLOBALS['container'] = $container;
305 
306  $item = new ItemTreeBounds(6,'uploadtree_a',1,17,18);
307  $hash2 = '0x3a910990f114f12f';
308  $ctPk = 2;
309  $content = 'foo';
310 
311  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
312  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
313  $copyrightDao->updateTable($item, $hash2, $content, '55', 'copyright', 'update', '1');
314 
315  $updatedCp = $this->dbManager->getSingleRow('SELECT * FROM copyright_event WHERE copyright_fk=$1',array($ctPk),__METHOD__.'.cp');
316  assertThat($updatedCp['content'],is(equalTo($content)));
317  }
318 
319  public function testDeleteCopyright()
320  {
321  $this->setUpClearingTables();
322 
323  $container = M::mock('ContainerBuilder');
324  $agentDao = M::mock('Fossology\Lib\Dao\AgentDao');
325  $agentDao->shouldReceive('arsTableExists')->withArgs(['copyright'])
326  ->andReturn(true);
327  $agentDao->shouldReceive('arsTableExists')->withArgs(['reso'])
328  ->andReturn(true);
329  $agentDao->shouldReceive('getSuccessfulAgentEntries')
330  ->withArgs(['copyright', 1])->andReturn([['agent_id' => '8',
331  'agent_rev' => 'trunk.271e3e', 'agent_name' => 'copyright']]);
332  $agentDao->shouldReceive('getSuccessfulAgentEntries')
333  ->withArgs(['reso', 1])->andReturn([['agent_id' => '8',
334  'agent_rev' => 'trunk.271e3e', 'agent_name' => 'reso']]);
335  $agentDao->shouldReceive('getCurrentAgentRef')->withArgs(['copyright'])
336  ->andReturn(new AgentRef(8, 'copyright', 'trunk.271e3e'));
337  $agentDao->shouldReceive('getCurrentAgentRef')->withArgs(['reso'])
338  ->andReturn(new AgentRef(8, 'reso', 'trunk.271e3e'));
339 
340  $container->shouldReceive('get')->withArgs(['dao.agent'])
341  ->andReturn($agentDao);
342  $GLOBALS['container'] = $container;
343 
344  $uploadDao = M::mock('Fossology\Lib\Dao\UploadDao');
345  $copyrightDao = new CopyrightDao($this->dbManager,$uploadDao);
346  $initialEntries = $copyrightDao->getAllEntries("copyright", 1, "uploadtree_a");
347  $initialCount = count($initialEntries);
348 
349  $item = new ItemTreeBounds(6,'uploadtree_a',1,17,18);
350  $copyrightDao->updateTable($item, '0x3a910990f114f12f', '', '55', 'copyright', 'delete', '1');
351  $updatedCp = $this->dbManager->getSingleRow('SELECT * FROM copyright_event WHERE copyright_fk=$1',array(2),__METHOD__.'.cpDel');
352  $deletedIdCheck = array_search($updatedCp['uploadtree_fk'], array_column($initialEntries, 'uploadtree_pk'));
353  unset($initialEntries[$deletedIdCheck]);
354  $remainingCount = count($initialEntries);
355  assertThat($remainingCount,is(equalTo($initialCount-1)));
356  }
357 }
Traceback_uri()
Get the URI without query to this location.
Definition: common-parm.php:97
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16