28 protected function setUp() : void
30 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
39 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
55 $testDb->createPlainTables(array(
'license_ref'));
60 $dbManager = &$testDb->getDbManager();
61 $dbManager->insertTableRow(
'license_ref', array(
'rf_pk'=>$knownId,
'rf_shortname'=>$shortname));
63 $dbManager->insertTableRow(
'license_candidate', array(
64 'rf_pk' => $knownId + 2,
65 'rf_shortname' =>
"candidate-$shortname",
66 'group_fk' => $knownGroup
68 $userDao = M::mock(UserDao::class);
69 $userDao->shouldReceive(
'getGroupIdByName')
75 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'getKeyFromShortname', array($shortname)), equalTo($knownId));
76 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'getKeyFromShortname', array(
"no $shortname")), equalTo(
false));
78 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
79 'getKeyFromShortname',
80 array(
"candidate-$shortname",
"fossy")),
81 equalTo($knownId + 2));
82 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
83 'getKeyFromShortname',
84 array(
"candidate-$shortname")),
99 $licenseText =
'I am a strong license';
101 $falseLicenseText =
"I am a weak license";
103 $dbManager = M::mock(DbManager::class);
104 $dbManager->shouldReceive(
'getSingleRow')
105 ->with(
'SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1) AND rf_external_id IS NULL',
108 ->andReturn(array(
'rf_pk' => $knownId));
109 $dbManager->shouldReceive(
'getSingleRow')
110 ->with(
'SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1) AND rf_external_id IS NULL',
111 array($falseLicenseText))
113 $userDao = M::mock(UserDao::class);
116 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
117 'getKeyFromMd5', array($licenseText)), equalTo($knownId));
118 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
119 'getKeyFromMd5', array($falseLicenseText)), equalTo(
false));
131 $dbManager = M::mock(DbManager::class);
141 'canLicAfossy' => 200,
142 'canLicBfossy' =>
false
155 md5(
'Text of candidate license') =>
false
157 $userDao = M::mock(UserDao::class);
158 $userDao->shouldReceive(
'getGroupIdByName')
164 Reflectory::setObjectsProperty($licenseCsvImport,
'nkMap', $nkMap);
165 Reflectory::setObjectsProperty($licenseCsvImport,
'mdkMap', $mdkMap);
168 'rf_shortname' =>
'licA',
169 'rf_spdx_id' =>
'lrf-licA',
170 'rf_licensetype' =>
'lictypeA',
171 'rf_fullname' =>
'licennnseA',
172 'rf_text' =>
'someRandom',
173 'rf_md5' => md5(
'someRandom'),
174 'rf_detector_type' => 1,
179 'rf_external_id' =>
null,
181 $dbManager->shouldReceive(
'getSingleRow')
183 'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype ' .
184 'FROM license_ref WHERE rf_pk = $1', array(101), anything())
186 ->andReturn($singleRowA);
189 $dbManager->shouldReceive(
'insertTableRow')
191 'license_map', array(
192 'rf_fk' => 103,
'rf_parent' => 101,
'usage' => LicenseMap::CONCLUSION
195 $singleRowB = $singleRowA;
196 $singleRowB[
"rf_shortname"] =
"licB";
197 $singleRowB[
"rf_licensetype"] =
"lictypeB";
198 $singleRowB[
"rf_fullname"] =
"liceB";
199 $singleRowB[
"rf_spdx_id"] =
"lrf-B";
200 $singleRowB[
"rf_text"] =
"txB";
201 $singleRowB[
"rf_md5"] = md5(
"txB");
202 $singleRowB[
"rf_risk"] = 0;
204 $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
205 'handleCsvLicense', array(array(
206 'shortname' =>
'licB',
207 'spdx_id' =>
'lrf-B',
208 'licensetype' =>
'lictypeB',
209 'fullname' =>
'liceB',
215 'parent_shortname' =>
'licA',
216 'report_shortname' =>
null,
219 assertThat($returnB, is(
"Inserted 'licB' in DB with conclusion 'licA'"));
222 $singleRowF = $singleRowA;
223 $singleRowF[
"rf_shortname"] =
"licF";
224 $singleRowF[
"rf_licensetype"] =
"lictypeF";
225 $singleRowF[
"rf_fullname"] =
"liceF";
226 $singleRowF[
"rf_spdx_id"] =
null;
227 $singleRowF[
"rf_text"] =
"txF";
228 $singleRowF[
"rf_md5"] = md5(
"txF");
229 $singleRowF[
"rf_risk"] = 1;
231 $dbManager->shouldReceive(
'insertTableRow')
233 'license_map', array(
236 'usage' => LicenseMap::REPORT
239 $returnF = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
240 'handleCsvLicense', array(array(
241 'shortname' =>
'licF',
242 'licensetype' =>
'lictypeF',
243 'fullname' =>
'liceF',
250 'parent_shortname' =>
null,
251 'report_shortname' =>
'licZ',
254 assertThat($returnF, is(
"Inserted 'licF' in DB reporting 'licZ'"));
257 $singleRowC = $singleRowA;
258 $singleRowC[
"rf_shortname"] =
"licC";
259 $singleRowC[
"rf_licensetype"] =
"lictypeC";
260 $singleRowC[
"rf_fullname"] =
"liceC";
261 $singleRowC[
"rf_spdx_id"] =
"lrf-licC";
262 $singleRowC[
"rf_text"] =
"txC";
263 $singleRowC[
"rf_md5"] = md5(
"txC");
264 $singleRowC[
"rf_risk"] = 2;
266 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
267 'handleCsvLicense', array(array(
268 'shortname' =>
'licC',
269 'licensetype' =>
'lictypeC',
270 'fullname' =>
'liceC',
271 'spdx_id' =>
'lrf-licC',
277 'parent_shortname' =>
null,
278 'report_shortname' =>
null,
281 assertThat($returnC, is(
"Inserted 'licC' in DB"));
284 $canLicA = $singleRowA;
285 $canLicA[
"rf_shortname"] =
"canLicA";
286 $canLicA[
"rf_licensetype"] =
"canLicTypeA";
287 $canLicA[
"rf_fullname"] =
"canLiceA";
288 $canLicA[
"rf_spdx_id"] =
null;
289 $canLicA[
"rf_text"] =
"txcan";
290 $canLicA[
"rf_risk"] = 0;
291 $canLicA[
"rf_group"] = 4;
292 $dbManager->shouldReceive(
'getSingleRow')
294 'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype ' .
295 'FROM license_ref WHERE rf_pk = $1', array(200), anything())
297 ->andReturn($canLicA);
298 $dbManager->shouldReceive(
'getSingleRow')
300 "UPDATE license_candidate SET " .
301 "rf_fullname=$2,rf_spdx_id=$3,rf_text=$4,rf_md5=md5($4) WHERE rf_pk=$1;",
302 array(200,
'canDidateLicenseA',
'lrf-canLicA',
'Text of candidate license'),
305 $dbManager->shouldReceive(
'getSingleRow')
307 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
308 anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)),
311 ->andReturn(array(
'rf_parent' =>
null));
312 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
313 'handleCsvLicense', array(array(
314 'shortname' =>
'canLicA',
315 'licensetype' =>
'canLicTypeA',
316 'fullname' =>
'canDidateLicenseA',
317 'spdx_id' =>
'lrf-canLicA',
318 'text' =>
'Text of candidate license',
319 'url' =>
'',
'notes' =>
'',
'source' =>
'',
'risk' => 0,
320 'parent_shortname' =>
null,
'report_shortname' =>
null,
321 'group' =>
'fossy',
'external_id' =>
null
323 assertThat($returnC, is(
324 "License 'canLicA' already exists in DB (id = 200)" .
325 ", updated fullname, updated SPDX ID, updated text"
329 $dbManager->shouldReceive(
'getSingleRow')
331 "UPDATE license_ref SET " .
332 "rf_fullname=$2,rf_text=$3,rf_md5=md5($3),rf_risk=$4 WHERE rf_pk=$1;",
333 array(101,
'liceB',
'txA', 2), anything())
335 $dbManager->shouldReceive(
'getSingleRow')
337 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
338 anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)),
341 ->andReturn(array(
'rf_parent' =>
null));
342 $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
343 'handleCsvLicense', array(array(
344 'shortname' =>
'licA',
345 'licensetype' =>
'lictypeA',
346 'fullname' =>
'liceB',
352 'parent_shortname' =>
null,
353 'report_shortname' =>
null,
357 assertThat($returnA, is(
358 "License 'licA' already exists in DB (id = 101)" .
359 ", updated fullname, updated text, updated the risk level"));
362 $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
363 'handleCsvLicense', array(array(
364 'shortname' =>
'licE',
365 'licensetype' =>
'lictypeE',
366 'fullname' =>
'liceE',
372 'parent_shortname' =>
null,
373 'report_shortname' =>
null,
376 assertThat($returnE, is(
377 "Error: MD5 checksum of 'licE' collides with license id=102"));
380 $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
381 'handleCsvLicense', array(array(
382 'shortname' =>
'licG',
383 'licensetype' =>
'lictypeG',
384 'fullname' =>
'liceG',
388 'source' =>
'_G_go_G_',
389 'parent_shortname' =>
null,
390 'report_shortname' =>
null,
394 assertThat($returnG, is(
395 "Error: MD5 checksum of 'licG' collides with license id=102"));
398 $canlicB = $singleRowA;
399 $canlicB[
"rf_shortname"] =
"canLicB";
400 $canlicB[
"rf_licensetype"] =
"canLicTypeB";
401 $canlicB[
"rf_fullname"] =
"canLiceB";
402 $canlicB[
"rf_spdx_id"] =
null;
403 $canlicB[
"rf_text"] =
"txCan";
404 $canlicB[
"rf_md5"] = md5(
"txCan");
405 $canlicB[
"rf_risk"] = 2;
406 $canlicB[
"group_fk"] = 4;
407 $canlicB[
"marydone"] =
't';
409 "license_candidate");
410 $dbManager->shouldReceive(
'booleanToDb')
414 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
415 'handleCsvLicense', array(array(
416 'shortname' =>
'canLicB',
417 'licensetype' =>
'canLicTypeB',
418 'fullname' =>
'canLiceB',
425 'parent_shortname' =>
null,
426 'report_shortname' =>
null,
429 assertThat($returnC, is(
"Inserted 'canLicB' in DB" .
430 " as candidate license under group fossy"));
445 $dbManager = M::mock(DbManager::class);
446 $userDao = M::mock(UserDao::class);
450 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',
452 'shortname',
'foo',
'text',
'fullname',
'notes',
'bar',
'spdx_id',
'licensetype'
455 'shortname' => 0,
'fullname' => 3,
'text' => 2,
'spdx_id' => 6,
456 'parent_shortname' =>
false,
'report_shortname' =>
false,
457 'url' =>
false,
'notes' => 4,
'source' =>
false,
'risk' => 0,
458 'group' =>
false,
'licensetype' => 7
462 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',
464 'Short Name',
'URL',
'text',
'fullname',
'notes',
'Foreign ID',
'SPDX ID',
465 'License group',
'License Type'
468 'shortname' => 0,
'fullname' => 3,
'spdx_id' => 6,
'text' => 2,
469 'parent_shortname' =>
false,
'report_shortname' =>
false,
'url' => 1,
470 'notes' => 4,
'source' => 5,
'risk' =>
false,
'group' => 7,
'licensetype' => 8
483 $this->expectException(Exception::class);
484 $dbManager = M::mock(DbManager::class);
485 $userDao = M::mock(UserDao::class);
487 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',array(array(
'shortname',
'foo',
'text')));
501 $dbManager = M::mock(DbManager::class);
502 $userDao = M::mock(UserDao::class);
505 $licenseCsvImport->setDelimiter(
'|');
506 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'delimiter'),is(
'|'));
508 $licenseCsvImport->setDelimiter(
'<>');
509 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'delimiter'),is(
'<'));
523 $dbManager = M::mock(DbManager::class);
524 $userDao = M::mock(UserDao::class);
527 $licenseCsvImport->setEnclosure(
'|');
528 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'enclosure') ,is(
'|'));
530 $licenseCsvImport->setEnclosure(
'<>');
531 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'enclosure'),is(
'<'));
543 $dbManager = M::mock(DbManager::class);
544 $userDao = M::mock(UserDao::class);
547 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleCsv',
548 array(array(
'shortname',
'licensetype',
'foo',
'text',
'fullname',
'notes',
'spdx_id')));
549 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'headrow'),
552 $dbManager->shouldReceive(
'getSingleRow')
554 'SELECT rf_shortname,rf_source,rf_pk,rf_risk FROM license_ref WHERE rf_md5=md5($1)',
558 "rf_shortname" =>
"licA",
559 "rf_licensetype" =>
"lictypeA",
560 "rf_fullname" =>
"liceA",
561 "rf_spdx_id" =>
null,
563 "rf_md5" => md5(
"txA"),
564 "rf_detector_type" => 1,
566 "rf_notes" =>
'noteA',
569 "rf_external_id" =>
null,
572 Reflectory::setObjectsProperty($licenseCsvImport,
'nkMap', array(
575 Reflectory::setObjectsProperty($licenseCsvImport,
'mdkMap', array(
578 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleCsv',
579 array(array(
'licA',
'lictypeA',
'bar',
'txA',
'liceA',
'noteA')));
580 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'nkMap'),
581 is(array(
'licA' => 101)));
582 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'mdkMap'),
583 is(array(md5(
'txA') => 101)));
594 $dbManager = M::mock(DbManager::class);
595 $userDao = M::mock(UserDao::class);
597 $msg = $licenseCsvImport->handleFile(
'/tmp/thisFileNameShouldNotExists',
'csv');
598 assertThat($msg, is(equalTo(_(
'Internal error'))));
609 $dbManager = M::mock(DbManager::class);
610 $userDao = M::mock(UserDao::class);
612 $msg = $licenseCsvImport->handleFile(__FILE__,
'csv');
613 assertThat($msg, startsWith( _(
'Error while parsing file')));
625 $dbManager = M::mock(DbManager::class);
626 $userDao = M::mock(UserDao::class);
628 $filename = tempnam(
"/tmp",
"FOO");
629 $handle = fopen($filename,
'w');
630 fwrite($handle,
"shortname,fullname,text,spdx_id");
632 $msg = $licenseCsvImport->handleFile($filename,
'csv');
633 assertThat($msg, startsWith( _(
'head okay')));
644 public function testSetMapTrue()
647 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
652 $dbManager = &$testDb->getDbManager();
653 $dbManager->insertTableRow(
'license_ref', array(
654 'rf_pk' => $licenseId,
655 'rf_shortname' =>
"Main License"
657 $dbManager->insertTableRow(
'license_ref', array(
658 'rf_pk' => $parentId,
659 'rf_shortname' =>
"Parent License"
661 $dbManager->insertTableRow(
'license_ref', array(
662 'rf_pk' => $reportId,
663 'rf_shortname' =>
"Reported License"
665 $userDao = M::mock(UserDao::class);
668 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
669 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
671 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
672 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
675 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
676 $statement = __METHOD__ .
".getMap";
677 $row = $dbManager->getSingleRow($sql, array($licenseId,
678 LicenseMap::CONCLUSION), $statement);
680 assertThat($row[
'rf_parent'], equalTo($parentId));
681 $row = $dbManager->getSingleRow($sql, array($licenseId,
682 LicenseMap::REPORT), $statement);
683 assertThat($row[
'rf_parent'], equalTo($reportId));
694 public function testSetMapFalse()
696 $testDb =
new TestLiteDb();
697 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
702 $dbManager = &$testDb->getDbManager();
703 $dbManager->insertTableRow(
'license_ref', array(
704 'rf_pk' => $licenseId,
705 'rf_shortname' =>
"Main License"
707 $userDao = M::mock(UserDao::class);
708 $licenseCsvImport =
new LicenseCsvImport($dbManager, $userDao);
710 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
711 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
713 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
714 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
717 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
718 $statement = __METHOD__ .
".getMap";
719 $row = $dbManager->getSingleRow($sql, array($licenseId,
720 LicenseMap::CONCLUSION), $statement);
722 assertThat($row, equalTo(
false));
723 $row = $dbManager->getSingleRow($sql, array($licenseId,
724 LicenseMap::REPORT), $statement);
725 assertThat($row, equalTo(
false));
734 $sql =
"CREATE TABLE license_candidate (" .
735 "rf_pk, rf_shortname, rf_fullname, rf_text, rf_md5, rf_url, rf_notes, " .
736 "marydone, rf_source, rf_risk, rf_detector_type, group_fk)";
737 $dbManager->queryOnce($sql);
748 $table =
"license_ref")
750 $dbManager->shouldReceive(
'insertTableRow')
751 ->with($table, $row, anything(),
'rf_pk')
753 ->andReturn($return);
Test for LicenseCsvImport.
testHandleFile()
Test for LicenseCsvImport::handleFile() (csv file with header)
testHandleFileIfFileIsNotParsable()
Test for LicenseCsvImport::handleFile() (non-csv file)
testGetKeyFromMd5()
Test for LicenseCsvImport::getKeyFromMd5()
createCandidateTable($dbManager)
testGetKeyFromShortname()
Test for LicenseCsvImport::getKeyFromShortname()
testSetDelimiter()
Test for LicenseCsvImport::setDelimiter()
setUp()
One time setup for test.
testHandleHeadCsv_missingMandidatoryKey()
Test for LicenseCsvImport::handleHeadCsv()
testHandleHeadCsv()
Test for LicenseCsvImport::handleHeadCsv()
addLicenseInsertToDbManager(&$dbManager, $row, $return, $table="license_ref")
testSetEnclosure()
Test for LicenseCsvImport::setEnclosure()
testHandleCsvLicense()
Test for LicenseCsvImport::handleCsvLicense()
testHandleFileIfFileNotExists()
Test for LicenseCsvImport::handleFile() (non-existing file)
testHandleCsv()
Test for LicenseCsvImport::handleCsv()
Import licenses from CSV.
Wrapper class for license map.
Utility functions for specific applications.