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