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_fullname' =>
'licennnseA',
171 'rf_text' =>
'someRandom',
172 'rf_md5' => md5(
'someRandom'),
173 'rf_detector_type' => 1,
179 $dbManager->shouldReceive(
'getSingleRow')
181 'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk ' .
182 'FROM license_ref WHERE rf_pk = $1', array(101), anything())
184 ->andReturn($singleRowA);
187 $dbManager->shouldReceive(
'insertTableRow')
189 'license_map', array(
190 'rf_fk' => 103,
'rf_parent' => 101,
'usage' => LicenseMap::CONCLUSION
193 $singleRowB = $singleRowA;
194 $singleRowB[
"rf_shortname"] =
"licB";
195 $singleRowB[
"rf_fullname"] =
"liceB";
196 $singleRowB[
"rf_spdx_id"] =
"lrf-B";
197 $singleRowB[
"rf_text"] =
"txB";
198 $singleRowB[
"rf_md5"] = md5(
"txB");
199 $singleRowB[
"rf_risk"] = 0;
201 $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
202 'handleCsvLicense', array(array(
203 'shortname' =>
'licB',
204 'spdx_id' =>
'lrf-B',
205 'fullname' =>
'liceB',
211 'parent_shortname' =>
'licA',
212 'report_shortname' =>
null,
215 assertThat($returnB, is(
"Inserted 'licB' in DB with conclusion 'licA'"));
218 $singleRowF = $singleRowA;
219 $singleRowF[
"rf_shortname"] =
"licF";
220 $singleRowF[
"rf_fullname"] =
"liceF";
221 $singleRowF[
"rf_spdx_id"] =
null;
222 $singleRowF[
"rf_text"] =
"txF";
223 $singleRowF[
"rf_md5"] = md5(
"txF");
224 $singleRowF[
"rf_risk"] = 1;
226 $dbManager->shouldReceive(
'insertTableRow')
228 'license_map', array(
231 'usage' => LicenseMap::REPORT
234 $returnF = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
235 'handleCsvLicense', array(array(
236 'shortname' =>
'licF',
237 'fullname' =>
'liceF',
244 'parent_shortname' =>
null,
245 'report_shortname' =>
'licZ',
248 assertThat($returnF, is(
"Inserted 'licF' in DB reporting 'licZ'"));
251 $singleRowC = $singleRowA;
252 $singleRowC[
"rf_shortname"] =
"licC";
253 $singleRowC[
"rf_fullname"] =
"liceC";
254 $singleRowC[
"rf_spdx_id"] =
"lrf-licC";
255 $singleRowC[
"rf_text"] =
"txC";
256 $singleRowC[
"rf_md5"] = md5(
"txC");
257 $singleRowC[
"rf_risk"] = 2;
259 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
260 'handleCsvLicense', array(array(
261 'shortname' =>
'licC',
262 'fullname' =>
'liceC',
263 'spdx_id' =>
'lrf-licC',
269 'parent_shortname' =>
null,
270 'report_shortname' =>
null,
273 assertThat($returnC, is(
"Inserted 'licC' in DB"));
276 $canLicA = $singleRowA;
277 $canLicA[
"rf_shortname"] =
"canLicA";
278 $canLicA[
"rf_fullname"] =
"canLiceA";
279 $canLicA[
"rf_spdx_id"] =
null;
280 $canLicA[
"rf_text"] =
"txcan";
281 $canLicA[
"rf_risk"] = 0;
282 $canLicA[
"rf_group"] = 4;
283 $dbManager->shouldReceive(
'getSingleRow')
285 'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk ' .
286 'FROM license_ref WHERE rf_pk = $1', array(200), anything())
288 ->andReturn($canLicA);
289 $dbManager->shouldReceive(
'getSingleRow')
291 "UPDATE license_candidate SET " .
292 "rf_fullname=$2,rf_spdx_id=$3,rf_text=$4,rf_md5=md5($4) WHERE rf_pk=$1;",
293 array(200,
'canDidateLicenseA',
'lrf-canLicA',
'Text of candidate license'),
296 $dbManager->shouldReceive(
'getSingleRow')
298 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
299 anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)),
302 ->andReturn(array(
'rf_parent' =>
null));
303 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
304 'handleCsvLicense', array(array(
305 'shortname' =>
'canLicA',
306 'fullname' =>
'canDidateLicenseA',
307 'spdx_id' =>
'lrf-canLicA',
308 'text' =>
'Text of candidate license',
309 'url' =>
'',
'notes' =>
'',
'source' =>
'',
'risk' => 0,
310 'parent_shortname' =>
null,
'report_shortname' =>
null,
313 assertThat($returnC, is(
314 "License 'canLicA' already exists in DB (id = 200)" .
315 ", updated fullname, updated SPDX ID, updated text"
319 $dbManager->shouldReceive(
'getSingleRow')
321 "UPDATE license_ref SET " .
322 "rf_fullname=$2,rf_text=$3,rf_md5=md5($3),rf_risk=$4 WHERE rf_pk=$1;",
323 array(101,
'liceB',
'txA', 2), anything())
325 $dbManager->shouldReceive(
'getSingleRow')
327 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
328 anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)),
331 ->andReturn(array(
'rf_parent' =>
null));
332 $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
333 'handleCsvLicense', array(array(
334 'shortname' =>
'licA',
335 'fullname' =>
'liceB',
341 'parent_shortname' =>
null,
342 'report_shortname' =>
null,
345 assertThat($returnA, is(
346 "License 'licA' already exists in DB (id = 101)" .
347 ", updated fullname, updated text, updated the risk level"));
350 $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
351 'handleCsvLicense', array(array(
352 'shortname' =>
'licE',
353 'fullname' =>
'liceE',
359 'parent_shortname' =>
null,
360 'report_shortname' =>
null,
363 assertThat($returnE, is(
364 "Error: MD5 checksum of 'licE' collides with license id=102"));
367 $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
368 'handleCsvLicense', array(array(
369 'shortname' =>
'licG',
370 'fullname' =>
'liceG',
374 'source' =>
'_G_go_G_',
375 'parent_shortname' =>
null,
376 'report_shortname' =>
null,
380 assertThat($returnG, is(
381 "Error: MD5 checksum of 'licG' collides with license id=102"));
384 $canlicB = $singleRowA;
385 $canlicB[
"rf_shortname"] =
"canLicB";
386 $canlicB[
"rf_fullname"] =
"canLiceB";
387 $canlicB[
"rf_spdx_id"] =
null;
388 $canlicB[
"rf_text"] =
"txCan";
389 $canlicB[
"rf_md5"] = md5(
"txCan");
390 $canlicB[
"rf_risk"] = 2;
391 $canlicB[
"group_fk"] = 4;
392 $canlicB[
"marydone"] =
't';
394 "license_candidate");
395 $dbManager->shouldReceive(
'booleanToDb')
399 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
400 'handleCsvLicense', array(array(
401 'shortname' =>
'canLicB',
402 'fullname' =>
'canLiceB',
409 'parent_shortname' =>
null,
410 'report_shortname' =>
null,
413 assertThat($returnC, is(
"Inserted 'canLicB' in DB" .
414 " as candidate license under group fossy"));
429 $dbManager = M::mock(DbManager::class);
430 $userDao = M::mock(UserDao::class);
434 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',
436 'shortname',
'foo',
'text',
'fullname',
'notes',
'bar',
'spdx_id'
439 'shortname' => 0,
'fullname' => 3,
'text' => 2,
'spdx_id' => 6,
440 'parent_shortname' =>
false,
'report_shortname' =>
false,
441 'url' =>
false,
'notes' => 4,
'source' =>
false,
'risk' => 0,
446 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',
448 'Short Name',
'URL',
'text',
'fullname',
'notes',
'Foreign ID',
'SPDX ID',
452 'shortname' => 0,
'fullname' => 3,
'spdx_id' => 6,
'text' => 2,
453 'parent_shortname' =>
false,
'report_shortname' =>
false,
'url' => 1,
454 'notes' => 4,
'source' => 5,
'risk' =>
false,
'group' => 7
467 $this->expectException(Exception::class);
468 $dbManager = M::mock(DbManager::class);
469 $userDao = M::mock(UserDao::class);
471 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleHeadCsv',array(array(
'shortname',
'foo',
'text')));
485 $dbManager = M::mock(DbManager::class);
486 $userDao = M::mock(UserDao::class);
489 $licenseCsvImport->setDelimiter(
'|');
490 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'delimiter'),is(
'|'));
492 $licenseCsvImport->setDelimiter(
'<>');
493 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'delimiter'),is(
'<'));
507 $dbManager = M::mock(DbManager::class);
508 $userDao = M::mock(UserDao::class);
511 $licenseCsvImport->setEnclosure(
'|');
512 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'enclosure') ,is(
'|'));
514 $licenseCsvImport->setEnclosure(
'<>');
515 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'enclosure'),is(
'<'));
527 $dbManager = M::mock(DbManager::class);
528 $userDao = M::mock(UserDao::class);
531 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleCsv',
532 array(array(
'shortname',
'foo',
'text',
'fullname',
'notes',
'spdx_id')));
533 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'headrow'),
536 $dbManager->shouldReceive(
'getSingleRow')
538 'SELECT rf_shortname,rf_source,rf_pk,rf_risk FROM license_ref WHERE rf_md5=md5($1)',
542 "rf_shortname" =>
"licA",
543 "rf_fullname" =>
"liceA",
544 "rf_spdx_id" =>
null,
546 "rf_md5" => md5(
"txA"),
547 "rf_detector_type" => 1,
549 "rf_notes" =>
'noteA',
554 Reflectory::setObjectsProperty($licenseCsvImport,
'nkMap', array(
557 Reflectory::setObjectsProperty($licenseCsvImport,
'mdkMap', array(
560 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleCsv',
561 array(array(
'licA',
'bar',
'txA',
'liceA',
'noteA')));
562 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'nkMap'),
563 is(array(
'licA' => 101)));
564 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'mdkMap'),
565 is(array(md5(
'txA') => 101)));
576 $dbManager = M::mock(DbManager::class);
577 $userDao = M::mock(UserDao::class);
579 $msg = $licenseCsvImport->handleFile(
'/tmp/thisFileNameShouldNotExists');
580 assertThat($msg, is(equalTo(_(
'Internal error'))));
591 $dbManager = M::mock(DbManager::class);
592 $userDao = M::mock(UserDao::class);
594 $msg = $licenseCsvImport->handleFile(__FILE__);
595 assertThat($msg, startsWith( _(
'Error while parsing file')));
607 $dbManager = M::mock(DbManager::class);
608 $userDao = M::mock(UserDao::class);
610 $filename = tempnam(
"/tmp",
"FOO");
611 $handle = fopen($filename,
'w');
612 fwrite($handle,
"shortname,fullname,text,spdx_id");
614 $msg = $licenseCsvImport->handleFile($filename);
615 assertThat($msg, startsWith( _(
'head okay')));
626 public function testSetMapTrue()
629 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
634 $dbManager = &$testDb->getDbManager();
635 $dbManager->insertTableRow(
'license_ref', array(
636 'rf_pk' => $licenseId,
637 'rf_shortname' =>
"Main License"
639 $dbManager->insertTableRow(
'license_ref', array(
640 'rf_pk' => $parentId,
641 'rf_shortname' =>
"Parent License"
643 $dbManager->insertTableRow(
'license_ref', array(
644 'rf_pk' => $reportId,
645 'rf_shortname' =>
"Reported License"
647 $userDao = M::mock(UserDao::class);
650 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
651 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
653 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
654 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
657 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
658 $statement = __METHOD__ .
".getMap";
659 $row = $dbManager->getSingleRow($sql, array($licenseId,
660 LicenseMap::CONCLUSION), $statement);
662 assertThat($row[
'rf_parent'], equalTo($parentId));
663 $row = $dbManager->getSingleRow($sql, array($licenseId,
664 LicenseMap::REPORT), $statement);
665 assertThat($row[
'rf_parent'], equalTo($reportId));
676 public function testSetMapFalse()
678 $testDb =
new TestLiteDb();
679 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
684 $dbManager = &$testDb->getDbManager();
685 $dbManager->insertTableRow(
'license_ref', array(
686 'rf_pk' => $licenseId,
687 'rf_shortname' =>
"Main License"
689 $userDao = M::mock(UserDao::class);
690 $licenseCsvImport =
new LicenseCsvImport($dbManager, $userDao);
692 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
693 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
695 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
696 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
699 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
700 $statement = __METHOD__ .
".getMap";
701 $row = $dbManager->getSingleRow($sql, array($licenseId,
702 LicenseMap::CONCLUSION), $statement);
704 assertThat($row, equalTo(
false));
705 $row = $dbManager->getSingleRow($sql, array($licenseId,
706 LicenseMap::REPORT), $statement);
707 assertThat($row, equalTo(
false));
716 $sql =
"CREATE TABLE license_candidate (" .
717 "rf_pk, rf_shortname, rf_fullname, rf_text, rf_md5, rf_url, rf_notes, " .
718 "marydone, rf_source, rf_risk, rf_detector_type, group_fk)";
719 $dbManager->queryOnce($sql);
730 $table =
"license_ref")
732 $dbManager->shouldReceive(
'insertTableRow')
733 ->with($table, $row, anything(),
'rf_pk')
735 ->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.