41 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
50 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
66 $testDb->createPlainTables(array(
'license_ref'));
71 $dbManager = &$testDb->getDbManager();
72 $dbManager->insertTableRow(
'license_ref', array(
'rf_pk'=>$knownId,
'rf_shortname'=>$shortname));
74 $dbManager->insertTableRow(
'license_candidate', array(
75 'rf_pk' => $knownId + 2,
76 'rf_shortname' =>
"candidate-$shortname",
77 'group_fk' => $knownGroup
79 $userDao = M::mock(UserDao::class);
80 $userDao->shouldReceive(
'getGroupIdByName')
86 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'getKeyFromShortname', array($shortname)), equalTo($knownId));
87 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'getKeyFromShortname', array(
"no $shortname")), equalTo(
false));
89 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
90 'getKeyFromShortname',
91 array(
"candidate-$shortname",
"fossy")),
92 equalTo($knownId + 2));
93 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
94 'getKeyFromShortname',
95 array(
"candidate-$shortname")),
110 $licenseText =
'I am a strong license';
112 $falseLicenseText =
"I am a weak license";
114 $dbManager = M::mock(DbManager::class);
115 $dbManager->shouldReceive(
'getSingleRow')
116 ->with(
'SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1)',
119 ->andReturn(array(
'rf_pk' => $knownId));
120 $dbManager->shouldReceive(
'getSingleRow')
121 ->with(
'SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1)',
122 array($falseLicenseText))
124 $userDao = M::mock(UserDao::class);
127 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
128 'getKeyFromMd5', array($licenseText)), equalTo($knownId));
129 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
130 'getKeyFromMd5', array($falseLicenseText)), equalTo(
false));
142 $dbManager = M::mock(DbManager::class);
152 'canLicAfossy' => 200,
153 'canLicBfossy' =>
false 166 md5(
'Text of candidate license') =>
false 168 $userDao = M::mock(UserDao::class);
169 $userDao->shouldReceive(
'getGroupIdByName')
175 Reflectory::setObjectsProperty($licenseCsvImport,
'nkMap', $nkMap);
176 Reflectory::setObjectsProperty($licenseCsvImport,
'mdkMap', $mdkMap);
179 'rf_shortname' =>
'licA',
180 'rf_fullname' =>
'licennnseA',
181 'rf_text' =>
'someRandom',
182 'rf_md5' => md5(
'someRandom'),
183 'rf_detector_type' => 1,
189 $dbManager->shouldReceive(
'getSingleRow')
191 'SELECT rf_shortname, rf_fullname, rf_text, rf_url, rf_notes, rf_source, rf_risk ' .
192 'FROM license_ref WHERE rf_pk = $1', array(101), anything())
194 ->andReturn($singleRowA);
197 $dbManager->shouldReceive(
'insertTableRow')
199 'license_map', array(
200 'rf_fk' => 103,
'rf_parent' => 101,
'usage' => LicenseMap::CONCLUSION
203 $singleRowB = $singleRowA;
204 $singleRowB[
"rf_shortname"] =
"licB";
205 $singleRowB[
"rf_fullname"] =
"liceB";
206 $singleRowB[
"rf_text"] =
"txB";
207 $singleRowB[
"rf_md5"] = md5(
"txB");
208 $singleRowB[
"rf_risk"] = 0;
210 $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
211 'handleCsvLicense', array(array(
212 'shortname' =>
'licB',
213 'fullname' =>
'liceB',
219 'parent_shortname' =>
'licA',
220 'report_shortname' => null,
223 assertThat($returnB, is(
"Inserted 'licB' in DB with conclusion 'licA'"));
226 $singleRowF = $singleRowA;
227 $singleRowF[
"rf_shortname"] =
"licF";
228 $singleRowF[
"rf_fullname"] =
"liceF";
229 $singleRowF[
"rf_text"] =
"txF";
230 $singleRowF[
"rf_md5"] = md5(
"txF");
231 $singleRowF[
"rf_risk"] = 1;
233 $dbManager->shouldReceive(
'insertTableRow')
235 'license_map', array(
238 'usage' => LicenseMap::REPORT
241 $returnF = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
242 'handleCsvLicense', array(array(
243 'shortname' =>
'licF',
244 '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_fullname"] =
"liceC";
260 $singleRowC[
"rf_text"] =
"txC";
261 $singleRowC[
"rf_md5"] = md5(
"txC");
262 $singleRowC[
"rf_risk"] = 2;
264 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
265 'handleCsvLicense', array(array(
266 'shortname' =>
'licC',
267 'fullname' =>
'liceC',
273 'parent_shortname' => null,
274 'report_shortname' => null,
277 assertThat($returnC, is(
"Inserted 'licC' in DB"));
280 $canLicA = $singleRowA;
281 $canLicA[
"rf_shortname"] =
"canLicA";
282 $canLicA[
"rf_fullname"] =
"canLiceA";
283 $canLicA[
"rf_text"] =
"txcan";
284 $canLicA[
"rf_risk"] = 0;
285 $canLicA[
"rf_group"] = 4;
286 $dbManager->shouldReceive(
'getSingleRow')
288 'SELECT rf_shortname, rf_fullname, rf_text, rf_url, rf_notes, rf_source, rf_risk ' .
289 'FROM license_ref WHERE rf_pk = $1', array(200), anything())
291 ->andReturn($canLicA);
292 $dbManager->shouldReceive(
'getSingleRow')
294 "UPDATE license_candidate SET " .
295 "rf_fullname=$2,rf_text=$3,rf_md5=md5($3) WHERE rf_pk=$1;",
296 array(200,
'canDidateLicenseA',
'Text of candidate license'),
299 $dbManager->shouldReceive(
'getSingleRow')
301 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
302 anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)),
305 ->andReturn(array(
'rf_parent' => null));
306 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
307 'handleCsvLicense', array(array(
308 'shortname' =>
'canLicA',
309 'fullname' =>
'canDidateLicenseA',
310 'text' =>
'Text of candidate license',
311 'url' =>
'',
'notes' =>
'',
'source' =>
'',
'risk' => 0,
312 'parent_shortname' => null,
'report_shortname' => null,
315 assertThat($returnC, is(
316 "License 'canLicA' already exists in DB (id = 200)" .
317 ", updated fullname, updated text" 321 $dbManager->shouldReceive(
'getSingleRow')
323 "UPDATE license_ref SET " .
324 "rf_fullname=$2,rf_text=$3,rf_md5=md5($3),rf_risk=$4 WHERE rf_pk=$1;",
325 array(101,
'liceB',
'txA', 2), anything())
327 $dbManager->shouldReceive(
'getSingleRow')
329 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
330 anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)),
333 ->andReturn(array(
'rf_parent' => null));
334 $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
335 'handleCsvLicense', array(array(
336 'shortname' =>
'licA',
337 'fullname' =>
'liceB',
343 'parent_shortname' => null,
344 'report_shortname' => null,
347 assertThat($returnA, is(
348 "License 'licA' already exists in DB (id = 101)" .
349 ", updated fullname, updated text, updated the risk level"));
352 $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
353 'handleCsvLicense', array(array(
354 'shortname' =>
'licE',
355 'fullname' =>
'liceE',
361 'parent_shortname' => null,
362 'report_shortname' => null,
365 assertThat($returnE, is(
366 "Error: MD5 checksum of 'licE' collides with license id=102"));
369 $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
370 'handleCsvLicense', array(array(
371 'shortname' =>
'licG',
372 'fullname' =>
'liceG',
376 'source' =>
'_G_go_G_',
377 'parent_shortname' => null,
378 'report_shortname' => null,
382 assertThat($returnG, is(
383 "Error: MD5 checksum of 'licG' collides with license id=102"));
386 $canlicB = $singleRowA;
387 $canlicB[
"rf_shortname"] =
"canLicB";
388 $canlicB[
"rf_fullname"] =
"canLiceB";
389 $canlicB[
"rf_text"] =
"txCan";
390 $canlicB[
"rf_md5"] = md5(
"txCan");
391 $canlicB[
"rf_risk"] = 2;
392 $canlicB[
"group_fk"] = 4;
393 $canlicB[
"marydone"] =
't';
395 "license_candidate");
396 $dbManager->shouldReceive(
'booleanToDb')
400 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
401 'handleCsvLicense', array(array(
402 'shortname' =>
'canLicB',
403 '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' 439 'shortname' => 0,
'fullname' => 3,
'text' => 2,
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',
452 'shortname' => 0,
'fullname' => 3,
'text' => 2,
453 'parent_shortname' =>
false,
'report_shortname' =>
false,
'url' => 1,
454 'notes' => 4,
'source' => 5,
'risk' =>
false,
'group' => 6
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')));
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",
545 "rf_md5" => md5(
"txA"),
546 "rf_detector_type" => 1,
548 "rf_notes" =>
'noteA',
553 Reflectory::setObjectsProperty($licenseCsvImport,
'nkMap', array(
556 Reflectory::setObjectsProperty($licenseCsvImport,
'mdkMap', array(
559 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
'handleCsv',
560 array(array(
'licA',
'bar',
'txA',
'liceA',
'noteA')));
561 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'nkMap'),
562 is(array(
'licA' => 101)));
563 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,
'mdkMap'),
564 is(array(md5(
'txA') => 101)));
575 $dbManager = M::mock(DbManager::class);
576 $userDao = M::mock(UserDao::class);
578 $msg = $licenseCsvImport->handleFile(
'/tmp/thisFileNameShouldNotExists');
579 assertThat($msg, is(equalTo(_(
'Internal error'))));
590 $dbManager = M::mock(DbManager::class);
591 $userDao = M::mock(UserDao::class);
593 $msg = $licenseCsvImport->handleFile(__FILE__);
594 assertThat($msg, startsWith( _(
'Error while parsing file')));
606 $dbManager = M::mock(DbManager::class);
607 $userDao = M::mock(UserDao::class);
609 $filename = tempnam(
"/tmp",
"FOO");
610 $handle = fopen($filename,
'w');
611 fwrite($handle,
"shortname,fullname,text");
613 $msg = $licenseCsvImport->handleFile($filename);
614 assertThat($msg, startsWith( _(
'head okay')));
625 public function testSetMapTrue()
628 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
633 $dbManager = &$testDb->getDbManager();
634 $dbManager->insertTableRow(
'license_ref', array(
635 'rf_pk' => $licenseId,
636 'rf_shortname' =>
"Main License" 638 $dbManager->insertTableRow(
'license_ref', array(
639 'rf_pk' => $parentId,
640 'rf_shortname' =>
"Parent License" 642 $dbManager->insertTableRow(
'license_ref', array(
643 'rf_pk' => $reportId,
644 'rf_shortname' =>
"Reported License" 646 $userDao = M::mock(UserDao::class);
649 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
650 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
652 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
653 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
656 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
657 $statement = __METHOD__ .
".getMap";
658 $row = $dbManager->getSingleRow($sql, array($licenseId,
659 LicenseMap::CONCLUSION), $statement);
661 assertThat($row[
'rf_parent'], equalTo($parentId));
662 $row = $dbManager->getSingleRow($sql, array($licenseId,
663 LicenseMap::REPORT), $statement);
664 assertThat($row[
'rf_parent'], equalTo($reportId));
675 public function testSetMapFalse()
678 $testDb->createPlainTables(array(
'license_ref',
'license_map'));
683 $dbManager = &$testDb->getDbManager();
684 $dbManager->insertTableRow(
'license_ref', array(
685 'rf_pk' => $licenseId,
686 'rf_shortname' =>
"Main License" 688 $userDao = M::mock(UserDao::class);
691 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
692 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
694 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
695 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
698 $sql =
"SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
699 $statement = __METHOD__ .
".getMap";
700 $row = $dbManager->getSingleRow($sql, array($licenseId,
701 LicenseMap::CONCLUSION), $statement);
703 assertThat($row, equalTo(
false));
704 $row = $dbManager->getSingleRow($sql, array($licenseId,
705 LicenseMap::REPORT), $statement);
706 assertThat($row, equalTo(
false));
715 $sql =
"CREATE TABLE license_candidate (" .
716 "rf_pk, rf_shortname, rf_fullname, rf_text, rf_md5, rf_url, rf_notes, " .
717 "marydone, rf_source, rf_risk, rf_detector_type, group_fk)";
718 $dbManager->queryOnce($sql);
729 $table =
"license_ref")
731 $dbManager->shouldReceive(
'insertTableRow')
732 ->with($table, $row, anything(),
'rf_pk')
734 ->andReturn($return);
testHandleCsv()
Test for LicenseCsvImport::handleCsv()
testHandleFileIfFileNotExists()
Test for LicenseCsvImport::handleFile() (non-existing file)
addLicenseInsertToDbManager(&$dbManager, $row, $return, $table="license_ref")
testGetKeyFromMd5()
Test for LicenseCsvImport::getKeyFromMd5()
testHandleHeadCsv_missingMandidatoryKey()
Test for LicenseCsvImport::handleHeadCsv()
testHandleHeadCsv()
Test for LicenseCsvImport::handleHeadCsv()
testGetKeyFromShortname()
Test for LicenseCsvImport::getKeyFromShortname()
testHandleFileIfFileIsNotParsable()
Test for LicenseCsvImport::handleFile() (non-csv file)
testSetDelimiter()
Test for LicenseCsvImport::setDelimiter()
Utility functions for specific applications.
Import licenses from CSV.
testHandleFile()
Test for LicenseCsvImport::handleFile() (csv file with header)
testHandleCsvLicense()
Test for LicenseCsvImport::handleCsvLicense()
setUp()
One time setup for test.
testSetEnclosure()
Test for LicenseCsvImport::setEnclosure()
Test for LicenseCsvImport.
createCandidateTable($dbManager)