1<?php 2/* 3Copyright (C) 2014-2015, Siemens AG 4 5This program is free software; you can redistribute it and/or 6modify it under the terms of the GNU General Public License 7version 2 as published by the Free Software Foundation. 8 9This program is distributed in the hope that it will be useful, 10but WITHOUT ANY WARRANTY; without even the implied warranty of 11MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12GNU General Public License for more details. 13 14You should have received a copy of the GNU General Public License along 15with this program; if not, write to the Free Software Foundation, Inc., 1651 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 17*/ 18 19namespace Fossology\Lib\Application; 20 21use Fossology\Lib\BusinessRules\LicenseMap; 22use Fossology\Lib\Db\DbManager; 23use Fossology\Lib\Exception; 24use Fossology\Lib\Test\Reflectory; 25use Fossology\Lib\Test\TestLiteDb; 26use Fossology\Lib\Dao\UserDao; 27use Mockery as M; 28 29/** 30 * @class LicenseCsvImportTest 31 * @brief Test for LicenseCsvImport 32 */ 33class LicenseCsvImportTest extends \PHPUnit\Framework\TestCase 34{ 35 /** 36 * @brief One time setup for test 37 * @see PHPUnit::Framework::TestCase::setUp() 38 */ 39 protected function setUp() 40 { 41 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount(); 42 } 43 44 /** 45 * @brief Close mockery 46 * @see PHPUnit::Framework::TestCase::tearDown() 47 */ 48 protected function tearDown() 49 { 50 $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore); 51 M::close(); 52 } 53 54 /** 55 * @brief Test for LicenseCsvImport::getKeyFromShortname() 56 * @test 57 * -# Create test DB and insert a license in `license_ref`. 58 * -# Call LicenseCsvImport::getKeyFromShortname() on a known license. 59 * -# Check if the id matches. 60 * -# Call LicenseCsvImport::getKeyFromShortname() on an unknown license. 61 * -# Check if the return value is false. 62 */ 63 public function testGetKeyFromShortname() 64 { 65 $testDb = new TestLiteDb(); 66 $testDb->createPlainTables(array('license_ref')); 67 $shortname = 'licA'; 68 $knownId = 101; 69 $knownGroup = 4; 70 /*** @var DbManager ***/ 71 $dbManager = &$testDb->getDbManager(); 72 $dbManager->insertTableRow('license_ref', array('rf_pk'=>$knownId,'rf_shortname'=>$shortname)); 73 $this->createCandidateTable($dbManager); 74 $dbManager->insertTableRow('license_candidate', array( 75 'rf_pk' => $knownId + 2, 76 'rf_shortname' => "candidate-$shortname", 77 'group_fk' => $knownGroup 78 )); 79 $userDao = M::mock(UserDao::class); 80 $userDao->shouldReceive('getGroupIdByName') 81 ->with("fossy") 82 ->once() 83 ->andReturn(4); 84 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 85 86 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'getKeyFromShortname', array($shortname)), equalTo($knownId)); 87 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'getKeyFromShortname', array("no $shortname")), equalTo(false)); 88 // Candidates 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")), 96 equalTo(false)); 97 } 98 99 /** 100 * @brief Test for LicenseCsvImport::getKeyFromMd5() 101 * @test 102 * -# Create test DB and insert a license in `license_ref`. 103 * -# Call LicenseCsvImport::getKeyFromMd5() on a known license. 104 * -# Check if the id matches. 105 * -# Call LicenseCsvImport::getKeyFromMd5() on an unknown license. 106 * -# Check if the return value is false. 107 */ 108 public function testGetKeyFromMd5() 109 { 110 $licenseText = 'I am a strong license'; 111 $knownId = 101; 112 $falseLicenseText = "I am a weak license"; 113 114 $dbManager = M::mock(DbManager::class); 115 $dbManager->shouldReceive('getSingleRow') 116 ->with('SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1)', 117 array($licenseText)) 118 ->once() 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)) 123 ->andReturnNull(); 124 $userDao = M::mock(UserDao::class); 125 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 126 127 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 128 'getKeyFromMd5', array($licenseText)), equalTo($knownId)); 129 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 130 'getKeyFromMd5', array($falseLicenseText)), equalTo(false)); 131 } 132 133 /** 134 * @brief Test for LicenseCsvImport::handleCsvLicense() 135 * @test 136 * -# Mock DB manager object. 137 * -# Create object of LicenseCsvImport and initialize nkMap. 138 * -# Call several handle calls and check the log messages. 139 */ 140 public function testHandleCsvLicense() 141 { 142 $dbManager = M::mock(DbManager::class); 143 $nkMap = array( 144 'licA' => 101, 145 'licB' => false, 146 'licC' => false, 147 'licE' => false, 148 'licF' => false, 149 'licG' => false, 150 'licH' => false, 151 'licZ' => 100, 152 'canLicAfossy' => 200, 153 'canLicBfossy' => false 154 ); 155 $mdkMap = array( 156 md5('txA') => 101, 157 md5('txB') => false, 158 md5('txC') => false, 159 md5('txD') => 102, 160 md5('txE') => false, 161 md5('txF') => false, 162 md5('txG') => false, 163 md5('txH') => false, 164 md5('txZ') => 100, 165 md5('txCan') => 200, 166 md5('Text of candidate license') => false 167 ); 168 $userDao = M::mock(UserDao::class); 169 $userDao->shouldReceive('getGroupIdByName') 170 ->with("fossy") 171 ->times(3) 172 ->andReturn(4); 173 174 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 175 Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', $nkMap); 176 Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', $mdkMap); 177 178 $singleRowA = array( 179 'rf_shortname' => 'licA', 180 'rf_fullname' => 'licennnseA', 181 'rf_text' => 'someRandom', 182 'rf_md5' => md5('someRandom'), 183 'rf_detector_type' => 1, 184 'rf_url' => '', 185 'rf_notes' => '', 186 'rf_source' => '', 187 'rf_risk' => 4 188 ); 189 $dbManager->shouldReceive('getSingleRow') 190 ->with( 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()) 193 ->once() 194 ->andReturn($singleRowA); 195 196 // Test for licB insert 197 $dbManager->shouldReceive('insertTableRow') 198 ->withArgs(array( 199 'license_map', array( 200 'rf_fk' => 103, 'rf_parent' => 101, 'usage' => LicenseMap::CONCLUSION 201 ))) 202 ->once(); 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; 209 $this->addLicenseInsertToDbManager($dbManager, $singleRowB, 103); 210 $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 211 'handleCsvLicense', array(array( 212 'shortname' => 'licB', 213 'fullname' => 'liceB', 214 'text' => 'txB', 215 'url' => '', 216 'notes' => '', 217 'source' => '', 218 'risk' => 0, 219 'parent_shortname' => 'licA', 220 'report_shortname' => null, 221 'group' => null 222 ))); 223 assertThat($returnB, is("Inserted 'licB' in DB with conclusion 'licA'")); 224 225 // Test for licF insert 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; 232 $this->addLicenseInsertToDbManager($dbManager, $singleRowF, 104); 233 $dbManager->shouldReceive('insertTableRow') 234 ->withArgs(array( 235 'license_map', array( 236 'rf_fk' => 104, 237 'rf_parent' => 100, 238 'usage' => LicenseMap::REPORT 239 ))) 240 ->once(); 241 $returnF = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 242 'handleCsvLicense', array(array( 243 'shortname' => 'licF', 244 'fullname' => 'liceF', 245 'text' => 'txF', 246 'url' => '', 247 'notes' => '', 248 'source' => '', 249 'risk' => 1, 250 'parent_shortname' => null, 251 'report_shortname' => 'licZ', 252 'group' => null 253 ))); 254 assertThat($returnF, is("Inserted 'licF' in DB reporting 'licZ'")); 255 256 // Test licC insert 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; 263 $this->addLicenseInsertToDbManager($dbManager, $singleRowC, 105); 264 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 265 'handleCsvLicense', array(array( 266 'shortname' => 'licC', 267 'fullname' => 'liceC', 268 'text' => 'txC', 269 'url' => '', 270 'notes' => '', 271 'source' => '', 272 'risk' => 2, 273 'parent_shortname' => null, 274 'report_shortname' => null, 275 'group' => null 276 ))); 277 assertThat($returnC, is("Inserted 'licC' in DB")); 278 279 // Test canlicC update 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') 287 ->with( 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()) 290 ->once() 291 ->andReturn($canLicA); 292 $dbManager->shouldReceive('getSingleRow') 293 ->with( 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'), 297 anything()) 298 ->once(); 299 $dbManager->shouldReceive('getSingleRow') 300 ->with( 301 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;', 302 anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)), 303 anything()) 304 ->twice() 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, 313 'group' => 'fossy' 314 ))); 315 assertThat($returnC, is( 316 "License 'canLicA' already exists in DB (id = 200)" . 317 ", updated fullname, updated text" 318 )); 319 320 // Test licA update 321 $dbManager->shouldReceive('getSingleRow') 322 ->with( 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()) 326 ->once(); 327 $dbManager->shouldReceive('getSingleRow') 328 ->with( 329 'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;', 330 anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)), 331 anything()) 332 ->twice() 333 ->andReturn(array('rf_parent' => null)); 334 $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 335 'handleCsvLicense', array(array( 336 'shortname' => 'licA', 337 'fullname' => 'liceB', 338 'text' => 'txA', 339 'url' => '', 340 'notes' => '', 341 'source' => '', 342 'risk' => 2, 343 'parent_shortname' => null, 344 'report_shortname' => null, 345 'group' => null 346 ))); 347 assertThat($returnA, is( 348 "License 'licA' already exists in DB (id = 101)" . 349 ", updated fullname, updated text, updated the risk level")); 350 351 // Test licE md5 collision 352 $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 353 'handleCsvLicense', array(array( 354 'shortname' => 'licE', 355 'fullname' => 'liceE', 356 'text' => 'txD', 357 'url' => '', 358 'notes' => '', 359 'source' => '', 360 'risk' => false, 361 'parent_shortname' => null, 362 'report_shortname' => null, 363 'group' => null 364 ))); 365 assertThat($returnE, is( 366 "Error: MD5 checksum of 'licE' collides with license id=102")); 367 368 // Test licG md5 collision 369 $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 370 'handleCsvLicense', array(array( 371 'shortname' => 'licG', 372 'fullname' => 'liceG', 373 'text' => 'txD', 374 'url' => '', 375 'notes' => '', 376 'source' => '_G_go_G_', 377 'parent_shortname' => null, 378 'report_shortname' => null, 379 'risk' => false, 380 'group' => null 381 ))); 382 assertThat($returnG, is( 383 "Error: MD5 checksum of 'licG' collides with license id=102")); 384 385 // Test canlicB insert 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'; 394 $this->addLicenseInsertToDbManager($dbManager, $canlicB, 201, 395 "license_candidate"); 396 $dbManager->shouldReceive('booleanToDb') 397 ->with(true) 398 ->once() 399 ->andReturn('t'); 400 $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 401 'handleCsvLicense', array(array( 402 'shortname' => 'canLicB', 403 'fullname' => 'canLiceB', 404 'text' => 'txCan', 405 'url' => '', 406 'notes' => '', 407 'source' => '', 408 'risk' => 2, 409 'parent_shortname' => null, 410 'report_shortname' => null, 411 'group' => 'fossy' 412 ))); 413 assertThat($returnC, is("Inserted 'canLicB' in DB" . 414 " as candidate license under group fossy")); 415 } 416 417 /** 418 * 419 * @brief Test for LicenseCsvImport::handleHeadCsv() 420 * @test 421 * -# Initialize LicenseCsvImport. 422 * -# Call LicenseCsvImport::handleHeadCsv() on actual header names. 423 * -# Check if the header returned have required keys. 424 * -# Call LicenseCsvImport::handleHeadCsv() on alias header names. 425 * -# Check if the header returned have required keys. 426 */ 427 public function testHandleHeadCsv() 428 { 429 $dbManager = M::mock(DbManager::class); 430 $userDao = M::mock(UserDao::class); 431 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 432 433 assertThat( 434 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv', 435 array(array( 436 'shortname', 'foo', 'text', 'fullname', 'notes', 'bar' 437 ))), 438 is(array( 439 'shortname' => 0, 'fullname' => 3, 'text' => 2, 440 'parent_shortname' => false, 'report_shortname' => false, 441 'url' => false, 'notes' => 4, 'source' => false, 'risk' => 0, 442 'group' => false 443 ))); 444 445 assertThat( 446 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv', 447 array(array( 448 'Short Name', 'URL', 'text', 'fullname', 'notes', 'Foreign ID', 449 'License group' 450 ))), 451 is(array( 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 455 ))); 456 } 457 458 /** 459 * @expectedException Exception 460 * @brief Test for LicenseCsvImport::handleHeadCsv() 461 * @test 462 * -# Initialize LicenseCsvImport. 463 * -# Call LicenseCsvImport::handleHeadCsv() on missing header names. 464 * -# Function must throw an Exception. 465 */ 466 public function testHandleHeadCsv_missingMandidatoryKey() 467 { 468 $dbManager = M::mock(DbManager::class); 469 $userDao = M::mock(UserDao::class); 470 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 471 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'handleHeadCsv',array(array('shortname','foo','text'))); 472 } 473 474 /** 475 * @brief Test for LicenseCsvImport::setDelimiter() 476 * @test 477 * -# Initialize LicenseCsvImport. 478 * -# Set a new delimiter using LicenseCsvImport::setDelimiter(). 479 * -# Check if the delimiter is changed. 480 * -# Set a new delimiter using LicenseCsvImport::setDelimiter(). 481 * -# Check if the delimiter is changed with only the first character passed. 482 */ 483 public function testSetDelimiter() 484 { 485 $dbManager = M::mock(DbManager::class); 486 $userDao = M::mock(UserDao::class); 487 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 488 489 $licenseCsvImport->setDelimiter('|'); 490 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('|')); 491 492 $licenseCsvImport->setDelimiter('<>'); 493 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('<')); 494 } 495 496 /** 497 * @brief Test for LicenseCsvImport::setEnclosure() 498 * @test 499 * -# Initialize LicenseCsvImport. 500 * -# Set a new enclosure using LicenseCsvImport::setEnclosure(). 501 * -# Check if the enclosure is changed. 502 * -# Set a new enclosure using LicenseCsvImport::setEnclosure(). 503 * -# Check if the enclosure is changed with only the first character passed. 504 */ 505 public function testSetEnclosure() 506 { 507 $dbManager = M::mock(DbManager::class); 508 $userDao = M::mock(UserDao::class); 509 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 510 511 $licenseCsvImport->setEnclosure('|'); 512 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure') ,is('|')); 513 514 $licenseCsvImport->setEnclosure('<>'); 515 assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure'),is('<')); 516 } 517 518 /** 519 * @brief Test for LicenseCsvImport::handleCsv() 520 * @test 521 * -# Call LicenseCsvImport::handleCsv() for first time. headrow must be set. 522 * -# Call LicenseCsvImport::handleCsv() with sample data. 523 * -# Check if it is imported in nkMap 524 */ 525 public function testHandleCsv() 526 { 527 $dbManager = M::mock(DbManager::class); 528 $userDao = M::mock(UserDao::class); 529 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 530 531 Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleCsv', 532 array(array('shortname', 'foo', 'text', 'fullname', 'notes'))); 533 assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'headrow'), 534 is(notNullValue())); 535 536 $dbManager->shouldReceive('getSingleRow') 537 ->with( 538 'SELECT rf_shortname,rf_source,rf_pk,rf_risk FROM license_ref WHERE rf_md5=md5($1)', 539 anything()) 540 ->andReturn(false); 541 $licenseRow = array( 542 "rf_shortname" => "licA", 543 "rf_fullname" => "liceA", 544 "rf_text" => "txA", 545 "rf_md5" => md5("txA"), 546 "rf_detector_type" => 1, 547 "rf_url" => '', 548 "rf_notes" => 'noteA', 549 "rf_source" => '', 550 "rf_risk" => 0 551 ); 552 $this->addLicenseInsertToDbManager($dbManager, $licenseRow, 101); 553 Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', array( 554 'licA' => false 555 )); 556 Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', array( 557 md5('txA') => false 558 )); 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))); 565 } 566 567 /** 568 * @brief Test for LicenseCsvImport::handleFile() (non-existing file) 569 * @test 570 * -# Call LicenseCsvImport::handleFile() on non-existing file 571 * -# Function must return `Internal error` 572 */ 573 public function testHandleFileIfFileNotExists() 574 { 575 $dbManager = M::mock(DbManager::class); 576 $userDao = M::mock(UserDao::class); 577 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 578 $msg = $licenseCsvImport->handleFile('/tmp/thisFileNameShouldNotExists'); 579 assertThat($msg, is(equalTo(_('Internal error')))); 580 } 581 582 /** 583 * @brief Test for LicenseCsvImport::handleFile() (non-csv file) 584 * @test 585 * -# Call LicenseCsvImport::handleFile() on non-csv file 586 * -# Function must return `Error while parsing file` 587 */ 588 public function testHandleFileIfFileIsNotParsable() 589 { 590 $dbManager = M::mock(DbManager::class); 591 $userDao = M::mock(UserDao::class); 592 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 593 $msg = $licenseCsvImport->handleFile(__FILE__); 594 assertThat($msg, startsWith( _('Error while parsing file'))); 595 } 596 597 /** 598 * @brief Test for LicenseCsvImport::handleFile() (csv file with header) 599 * @test 600 * -# Create an empty CSV file and write a valid header. 601 * -# Call LicenseCsvImport::handleFile(). 602 * -# Function must return message starting with `head okay`. 603 */ 604 public function testHandleFile() 605 { 606 $dbManager = M::mock(DbManager::class); 607 $userDao = M::mock(UserDao::class); 608 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 609 $filename = tempnam("/tmp", "FOO"); 610 $handle = fopen($filename, 'w'); 611 fwrite($handle, "shortname,fullname,text"); 612 fclose($handle); 613 $msg = $licenseCsvImport->handleFile($filename); 614 assertThat($msg, startsWith( _('head okay'))); 615 unlink($filename); 616 } 617 618 /** 619 * @brief Test for LicenseCsvImport::setMap() 620 * @test 621 * -# Create test DB and insert 3 licenses in `license_ref`. 622 * -# Call LicenseCsvImport::setMap() with a reporting and conclusion mapping. 623 * -# Check if return value is true and mapping is done in the DB. 624 */ 625 public function testSetMapTrue() 626 { 627 $testDb = new TestLiteDb(); 628 $testDb->createPlainTables(array('license_ref', 'license_map')); 629 $licenseId = 101; 630 $parentId = 102; 631 $reportId = 103; 632 /** @var DbManager $dbManager **/ 633 $dbManager = &$testDb->getDbManager(); 634 $dbManager->insertTableRow('license_ref', array( 635 'rf_pk' => $licenseId, 636 'rf_shortname' => "Main License" 637 )); 638 $dbManager->insertTableRow('license_ref', array( 639 'rf_pk' => $parentId, 640 'rf_shortname' => "Parent License" 641 )); 642 $dbManager->insertTableRow('license_ref', array( 643 'rf_pk' => $reportId, 644 'rf_shortname' => "Reported License" 645 )); 646 $userDao = M::mock(UserDao::class); 647 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 648 649 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 650 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)), 651 equalTo(true)); 652 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 653 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)), 654 equalTo(true)); 655 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); 660 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)); 665 } 666 667 /** 668 * @brief Test for LicenseCsvImport::setMap() with empty mapping 669 * @test 670 * -# Create test DB and insert 3 licenses in `license_ref`. 671 * -# Call LicenseCsvImport::setMap() with a reporting and conclusion mapping 672 * but the conclusions should be worng licenses. 673 * -# Check if return value is false and there is no mapping done in the DB. 674 */ 675 public function testSetMapFalse() 676 { 677 $testDb = new TestLiteDb(); 678 $testDb->createPlainTables(array('license_ref', 'license_map')); 679 $licenseId = 101; 680 $parentId = false; 681 $reportId = false; 682 /** @var DbManager $dbManager **/ 683 $dbManager = &$testDb->getDbManager(); 684 $dbManager->insertTableRow('license_ref', array( 685 'rf_pk' => $licenseId, 686 'rf_shortname' => "Main License" 687 )); 688 $userDao = M::mock(UserDao::class); 689 $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao); 690 691 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 692 'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)), 693 equalTo(false)); 694 assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 695 'setMap', array($reportId, $licenseId, LicenseMap::REPORT)), 696 equalTo(false)); 697 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); 702 703 assertThat($row, equalTo(false)); 704 $row = $dbManager->getSingleRow($sql, array($licenseId, 705 LicenseMap::REPORT), $statement); 706 assertThat($row, equalTo(false)); 707 } 708 709 /** 710 * Create candidate license table 711 * @param DbManager $dbManager 712 */ 713 private function createCandidateTable($dbManager) 714 { 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); 719 } 720 721 /** 722 * Add a new mockery handler for new license insertion in DB 723 * @param DbManager $dbManager The mock object of DbManager 724 * @param array $row The associated array 725 * @param mixed $return The value which should be returned 726 * @param string $table The table where new data should go 727 */ 728 private function addLicenseInsertToDbManager(&$dbManager, $row, $return, 729 $table = "license_ref") 730 { 731 $dbManager->shouldReceive('insertTableRow') 732 ->with($table, $row, anything(), 'rf_pk') 733 ->once() 734 ->andReturn($return); 735 } 736} 737