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)',
108 ->andReturn(array(
'rf_pk' => $knownId));
109 $dbManager->shouldReceive(
'getSingleRow')
110 ->with(
'SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1)',
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,
180 $dbManager->shouldReceive(
'getSingleRow')
182 'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype ' .
183 'FROM license_ref WHERE rf_pk = $1', array(101), anything())
185 ->andReturn($singleRowA);
188 $dbManager->shouldReceive(
'insertTableRow')
190 'license_map', array(
191 'rf_fk' => 103,
'rf_parent' => 101,
'usage' => LicenseMap::CONCLUSION
194 $singleRowB = $singleRowA;
195 $singleRowB[
"rf_shortname"] =
"licB";
196 $singleRowB[
"rf_licensetype"] =
"lictypeB";
197 $singleRowB[
"rf_fullname"] =
"liceB";
198 $singleRowB[
"rf_spdx_id"] =
"lrf-B";
199 $singleRowB[
"rf_text"] =
"txB";
200 $singleRowB[
"rf_md5"] = md5(
"txB");
201 $singleRowB[
"rf_risk"] = 0;
203 $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
204 'handleCsvLicense', array(array(
205 'shortname' =>
'licB',
206 'spdx_id' =>
'lrf-B',
207 'licensetype' =>
'lictypeB',
208 'fullname' =>
'liceB',
214 'parent_shortname' =>
'licA',
215 'report_shortname' =>
null,
218 assertThat($returnB, is(
"Inserted 'licB' in DB with conclusion 'licA'"));
221 $singleRowF = $singleRowA;
222 $singleRowF[
"rf_shortname"] =
"licF";
223 $singleRowF[
"rf_licensetype"] =
"lictypeF";
224 $singleRowF[
"rf_fullname"] =
"liceF";
225 $singleRowF[
"rf_spdx_id"] =
null;
226 $singleRowF[
"rf_text"] =
"txF";
227 $singleRowF[
"rf_md5"] = md5(
"txF");
228 $singleRowF[
"rf_risk"] = 1;
230 $dbManager->shouldReceive(
'insertTableRow')
232 'license_map', array(
235 'usage' => LicenseMap::REPORT
238 $returnF = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
239 'handleCsvLicense', array(array(
240 'shortname' =>
'licF',
241 'licensetype' =>
'lictypeF',
242 'fullname' =>
'liceF',
249 'parent_shortname' =>
null,
250 'report_shortname' =>
'licZ',
253 assertThat($returnF, is(
"Inserted 'licF' in DB reporting 'licZ'"));
256 $singleRowC = $singleRowA;
257 $singleRowC[
"rf_shortname"] =
"licC";
258 $singleRowC[
"rf_licensetype"] =
"lictypeC";
259 $singleRowC[
"rf_fullname"] =
"liceC";
260 $singleRowC[
"rf_spdx_id"] =
"lrf-licC";
261 $singleRowC[
"rf_text"] =
"txC";
262 $singleRowC[
"rf_md5"] = md5(
"txC");
263 $singleRowC[
"rf_risk"] = 2;
265 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
266 'handleCsvLicense', array(array(
267 'shortname' =>
'licC',
268 'licensetype' =>
'lictypeC',
269 'fullname' =>
'liceC',
270 'spdx_id' =>
'lrf-licC',
276 'parent_shortname' =>
null,
277 'report_shortname' =>
null,
280 assertThat($returnC, is(
"Inserted 'licC' in DB"));
283 $canLicA = $singleRowA;
284 $canLicA[
"rf_shortname"] =
"canLicA";
285 $canLicA[
"rf_licensetype"] =
"canLicTypeA";
286 $canLicA[
"rf_fullname"] =
"canLiceA";
287 $canLicA[
"rf_spdx_id"] =
null;
288 $canLicA[
"rf_text"] =
"txcan";
289 $canLicA[
"rf_risk"] = 0;
290 $canLicA[
"rf_group"] = 4;
291 $dbManager->shouldReceive(
'getSingleRow')
293 'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype ' .
294 'FROM license_ref WHERE rf_pk = $1', array(200), anything())
296 ->andReturn($canLicA);
297 $dbManager->shouldReceive(
'getSingleRow')
299 "UPDATE license_candidate SET " .
300 "rf_fullname=$2,rf_spdx_id=$3,rf_text=$4,rf_md5=md5($4) WHERE rf_pk=$1;",
301 array(200,
'canDidateLicenseA',
'lrf-canLicA',
'Text of candidate license'),
304 $dbManager->shouldReceive(
'getSingleRow')
306 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
307 anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)),
310 ->andReturn(array(
'rf_parent' =>
null));
311 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
312 'handleCsvLicense', array(array(
313 'shortname' =>
'canLicA',
314 'licensetype' =>
'canLicTypeA',
315 'fullname' =>
'canDidateLicenseA',
316 'spdx_id' =>
'lrf-canLicA',
317 'text' =>
'Text of candidate license',
318 'url' =>
'',
'notes' =>
'',
'source' =>
'',
'risk' => 0,
319 'parent_shortname' =>
null,
'report_shortname' =>
null,
322 assertThat($returnC, is(
323 "License 'canLicA' already exists in DB (id = 200)" .
324 ", updated fullname, updated SPDX ID, updated text"
328 $dbManager->shouldReceive(
'getSingleRow')
330 "UPDATE license_ref SET " .
331 "rf_fullname=$2,rf_text=$3,rf_md5=md5($3),rf_risk=$4 WHERE rf_pk=$1;",
332 array(101,
'liceB',
'txA', 2), anything())
334 $dbManager->shouldReceive(
'getSingleRow')
336 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
337 anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)),
340 ->andReturn(array(
'rf_parent' =>
null));
341 $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
342 'handleCsvLicense', array(array(
343 'shortname' =>
'licA',
344 'licensetype' =>
'lictypeA',
345 'fullname' =>
'liceB',
351 'parent_shortname' =>
null,
352 'report_shortname' =>
null,
355 assertThat($returnA, is(
356 "License 'licA' already exists in DB (id = 101)" .
357 ", updated fullname, updated text, updated the risk level"));
360 $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
361 'handleCsvLicense', array(array(
362 'shortname' =>
'licE',
363 'licensetype' =>
'lictypeE',
364 'fullname' =>
'liceE',
370 'parent_shortname' =>
null,
371 'report_shortname' =>
null,
374 assertThat($returnE, is(
375 "Error: MD5 checksum of 'licE' collides with license id=102"));
378 $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
379 'handleCsvLicense', array(array(
380 'shortname' =>
'licG',
381 'licensetype' =>
'lictypeG',
382 'fullname' =>
'liceG',
386 'source' =>
'_G_go_G_',
387 'parent_shortname' =>
null,
388 'report_shortname' =>
null,
392 assertThat($returnG, is(
393 "Error: MD5 checksum of 'licG' collides with license id=102"));
396 $canlicB = $singleRowA;
397 $canlicB[
"rf_shortname"] =
"canLicB";
398 $canlicB[
"rf_licensetype"] =
"canLicTypeB";
399 $canlicB[
"rf_fullname"] =
"canLiceB";
400 $canlicB[
"rf_spdx_id"] =
null;
401 $canlicB[
"rf_text"] =
"txCan";
402 $canlicB[
"rf_md5"] = md5(
"txCan");
403 $canlicB[
"rf_risk"] = 2;
404 $canlicB[
"group_fk"] = 4;
405 $canlicB[
"marydone"] =
't';
407 "license_candidate");
408 $dbManager->shouldReceive(
'booleanToDb')
412 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
413 'handleCsvLicense', array(array(
414 'shortname' =>
'canLicB',
415 'licensetype' =>
'canLicTypeB',
416 'fullname' =>
'canLiceB',
423 'parent_shortname' =>
null,
424 'report_shortname' =>
null,
427 assertThat($returnC, is(
"Inserted 'canLicB' in DB" .
428 " as candidate license under group fossy"));
443 $dbManager = M::mock(DbManager::class);
444 $userDao = M::mock(UserDao::class);
448 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',
450 'shortname',
'foo',
'text',
'fullname',
'notes',
'bar',
'spdx_id',
'licensetype'
453 'shortname' => 0,
'fullname' => 3,
'text' => 2,
'spdx_id' => 6,
454 'parent_shortname' =>
false,
'report_shortname' =>
false,
455 'url' =>
false,
'notes' => 4,
'source' =>
false,
'risk' => 0,
456 'group' =>
false,
'licensetype' => 7
460 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',
462 'Short Name',
'URL',
'text',
'fullname',
'notes',
'Foreign ID',
'SPDX ID',
463 'License group',
'License Type'
466 'shortname' => 0,
'fullname' => 3,
'spdx_id' => 6,
'text' => 2,
467 'parent_shortname' =>
false,
'report_shortname' =>
false,
'url' => 1,
468 'notes' => 4,
'source' => 5,
'risk' =>
false,
'group' => 7,
'licensetype' => 8
481 $this->expectException(Exception::class);
482 $dbManager = M::mock(DbManager::class);
483 $userDao = M::mock(UserDao::class);
485 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',array(array(
'shortname',
'foo',
'text')));
499 $dbManager = M::mock(DbManager::class);
500 $userDao = M::mock(UserDao::class);
503 $licenseCsvImport->setDelimiter(
'|');
504 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'delimiter'),is(
'|'));
506 $licenseCsvImport->setDelimiter(
'<>');
507 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'delimiter'),is(
'<'));
521 $dbManager = M::mock(DbManager::class);
522 $userDao = M::mock(UserDao::class);
525 $licenseCsvImport->setEnclosure(
'|');
526 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'enclosure') ,is(
'|'));
528 $licenseCsvImport->setEnclosure(
'<>');
529 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'enclosure'),is(
'<'));
541 $dbManager = M::mock(DbManager::class);
542 $userDao = M::mock(UserDao::class);
545 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleCsv',
546 array(array(
'shortname',
'licensetype',
'foo',
'text',
'fullname',
'notes',
'spdx_id')));
547 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'headrow'),
550 $dbManager->shouldReceive(
'getSingleRow')
552 'SELECT rf_shortname,rf_source,rf_pk,rf_risk FROM license_ref WHERE rf_md5=md5($1)',
556 "rf_shortname" =>
"licA",
557 "rf_licensetype" =>
"lictypeA",
558 "rf_fullname" =>
"liceA",
559 "rf_spdx_id" =>
null,
561 "rf_md5" => md5(
"txA"),
562 "rf_detector_type" => 1,
564 "rf_notes" =>
'noteA',
569 Reflectory::setObjectsProperty($licenseCsvImport,
'nkMap', array(
572 Reflectory::setObjectsProperty($licenseCsvImport,
'mdkMap', array(
575 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleCsv',
576 array(array(
'licA',
'lictypeA',
'bar',
'txA',
'liceA',
'noteA')));
577 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'nkMap'),
578 is(array(
'licA' => 101)));
579 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'mdkMap'),
580 is(array(md5(
'txA') => 101)));
591 $dbManager = M::mock(DbManager::class);
592 $userDao = M::mock(UserDao::class);
594 $msg = $licenseCsvImport->handleFile(
'/tmp/thisFileNameShouldNotExists',
'csv');
595 assertThat($msg, is(equalTo(_(
'Internal error'))));
606 $dbManager = M::mock(DbManager::class);
607 $userDao = M::mock(UserDao::class);
609 $msg = $licenseCsvImport->handleFile(__FILE__,
'csv');
610 assertThat($msg, startsWith( _(
'Error while parsing file')));
622 $dbManager = M::mock(DbManager::class);
623 $userDao = M::mock(UserDao::class);
625 $filename = tempnam(
"/tmp",
"FOO");
626 $handle = fopen($filename,
'w');
627 fwrite($handle,
"shortname,fullname,text,spdx_id");
629 $msg = $licenseCsvImport->handleFile($filename,
'csv');
630 assertThat($msg, startsWith( _(
'head okay')));
641 public function testSetMapTrue()
644 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
649 $dbManager = &$testDb->getDbManager();
650 $dbManager->insertTableRow(
'license_ref', array(
651 'rf_pk' => $licenseId,
652 'rf_shortname' =>
"Main License"
654 $dbManager->insertTableRow(
'license_ref', array(
655 'rf_pk' => $parentId,
656 'rf_shortname' =>
"Parent License"
658 $dbManager->insertTableRow(
'license_ref', array(
659 'rf_pk' => $reportId,
660 'rf_shortname' =>
"Reported License"
662 $userDao = M::mock(UserDao::class);
665 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
666 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
668 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
669 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
672 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
673 $statement = __METHOD__ .
".getMap";
674 $row = $dbManager->getSingleRow($sql, array($licenseId,
675 LicenseMap::CONCLUSION), $statement);
677 assertThat($row[
'rf_parent'], equalTo($parentId));
678 $row = $dbManager->getSingleRow($sql, array($licenseId,
679 LicenseMap::REPORT), $statement);
680 assertThat($row[
'rf_parent'], equalTo($reportId));
691 public function testSetMapFalse()
693 $testDb =
new TestLiteDb();
694 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
699 $dbManager = &$testDb->getDbManager();
700 $dbManager->insertTableRow(
'license_ref', array(
701 'rf_pk' => $licenseId,
702 'rf_shortname' =>
"Main License"
704 $userDao = M::mock(UserDao::class);
705 $licenseCsvImport =
new LicenseCsvImport($dbManager, $userDao);
707 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
708 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
710 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
711 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
714 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
715 $statement = __METHOD__ .
".getMap";
716 $row = $dbManager->getSingleRow($sql, array($licenseId,
717 LicenseMap::CONCLUSION), $statement);
719 assertThat($row, equalTo(
false));
720 $row = $dbManager->getSingleRow($sql, array($licenseId,
721 LicenseMap::REPORT), $statement);
722 assertThat($row, equalTo(
false));
731 $sql =
"CREATE TABLE license_candidate (" .
732 "rf_pk, rf_shortname, rf_fullname, rf_text, rf_md5, rf_url, rf_notes, " .
733 "marydone, rf_source, rf_risk, rf_detector_type, group_fk)";
734 $dbManager->queryOnce($sql);
745 $table =
"license_ref")
747 $dbManager->shouldReceive(
'insertTableRow')
748 ->with($table, $row, anything(),
'rf_pk')
750 ->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.