FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseCsvImportTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
10 use Exception;
16 use Mockery as M;
17 
22 class LicenseCsvImportTest extends \PHPUnit\Framework\TestCase
23 {
28  protected function setUp() : void
29  {
30  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
31  }
32 
37  protected function tearDown() : void
38  {
39  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
40  M::close();
41  }
42 
52  public function testGetKeyFromShortname()
53  {
54  $testDb = new TestLiteDb();
55  $testDb->createPlainTables(array('license_ref'));
56  $shortname = 'licA';
57  $knownId = 101;
58  $knownGroup = 4;
59  /*** @var DbManager ***/
60  $dbManager = &$testDb->getDbManager();
61  $dbManager->insertTableRow('license_ref', array('rf_pk'=>$knownId,'rf_shortname'=>$shortname));
62  $this->createCandidateTable($dbManager);
63  $dbManager->insertTableRow('license_candidate', array(
64  'rf_pk' => $knownId + 2,
65  'rf_shortname' => "candidate-$shortname",
66  'group_fk' => $knownGroup
67  ));
68  $userDao = M::mock(UserDao::class);
69  $userDao->shouldReceive('getGroupIdByName')
70  ->with("fossy")
71  ->once()
72  ->andReturn(4);
73  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
74 
75  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'getKeyFromShortname', array($shortname)), equalTo($knownId));
76  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'getKeyFromShortname', array("no $shortname")), equalTo(false));
77  // Candidates
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")),
85  equalTo(false));
86  }
87 
97  public function testGetKeyFromMd5()
98  {
99  $licenseText = 'I am a strong license';
100  $knownId = 101;
101  $falseLicenseText = "I am a weak license";
102 
103  $dbManager = M::mock(DbManager::class);
104  $dbManager->shouldReceive('getSingleRow')
105  ->with('SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1)',
106  array($licenseText))
107  ->once()
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))
112  ->andReturnNull();
113  $userDao = M::mock(UserDao::class);
114  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
115 
116  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
117  'getKeyFromMd5', array($licenseText)), equalTo($knownId));
118  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
119  'getKeyFromMd5', array($falseLicenseText)), equalTo(false));
120  }
121 
129  public function testHandleCsvLicense()
130  {
131  $dbManager = M::mock(DbManager::class);
132  $nkMap = array(
133  'licA' => 101,
134  'licB' => false,
135  'licC' => false,
136  'licE' => false,
137  'licF' => false,
138  'licG' => false,
139  'licH' => false,
140  'licZ' => 100,
141  'canLicAfossy' => 200,
142  'canLicBfossy' => false
143  );
144  $mdkMap = array(
145  md5('txA') => 101,
146  md5('txB') => false,
147  md5('txC') => false,
148  md5('txD') => 102,
149  md5('txE') => false,
150  md5('txF') => false,
151  md5('txG') => false,
152  md5('txH') => false,
153  md5('txZ') => 100,
154  md5('txCan') => 200,
155  md5('Text of candidate license') => false
156  );
157  $userDao = M::mock(UserDao::class);
158  $userDao->shouldReceive('getGroupIdByName')
159  ->with("fossy")
160  ->times(3)
161  ->andReturn(4);
162 
163  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
164  Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', $nkMap);
165  Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', $mdkMap);
166 
167  $singleRowA = array(
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,
175  'rf_url' => '',
176  'rf_notes' => '',
177  'rf_source' => '',
178  'rf_risk' => 4
179  );
180  $dbManager->shouldReceive('getSingleRow')
181  ->with(
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())
184  ->once()
185  ->andReturn($singleRowA);
186 
187  // Test for licB insert
188  $dbManager->shouldReceive('insertTableRow')
189  ->withArgs(array(
190  'license_map', array(
191  'rf_fk' => 103, 'rf_parent' => 101, 'usage' => LicenseMap::CONCLUSION
192  )))
193  ->once();
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;
202  $this->addLicenseInsertToDbManager($dbManager, $singleRowB, 103);
203  $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
204  'handleCsvLicense', array(array(
205  'shortname' => 'licB',
206  'spdx_id' => 'lrf-B',
207  'licensetype' => 'lictypeB',
208  'fullname' => 'liceB',
209  'text' => 'txB',
210  'url' => '',
211  'notes' => '',
212  'source' => '',
213  'risk' => 0,
214  'parent_shortname' => 'licA',
215  'report_shortname' => null,
216  'group' => null
217  )));
218  assertThat($returnB, is("Inserted 'licB' in DB with conclusion 'licA'"));
219 
220  // Test for licF insert
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;
229  $this->addLicenseInsertToDbManager($dbManager, $singleRowF, 104);
230  $dbManager->shouldReceive('insertTableRow')
231  ->withArgs(array(
232  'license_map', array(
233  'rf_fk' => 104,
234  'rf_parent' => 100,
235  'usage' => LicenseMap::REPORT
236  )))
237  ->once();
238  $returnF = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
239  'handleCsvLicense', array(array(
240  'shortname' => 'licF',
241  'licensetype' => 'lictypeF',
242  'fullname' => 'liceF',
243  'spdx_id' => null,
244  'text' => 'txF',
245  'url' => '',
246  'notes' => '',
247  'source' => '',
248  'risk' => 1,
249  'parent_shortname' => null,
250  'report_shortname' => 'licZ',
251  'group' => null
252  )));
253  assertThat($returnF, is("Inserted 'licF' in DB reporting 'licZ'"));
254 
255  // Test licC insert
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;
264  $this->addLicenseInsertToDbManager($dbManager, $singleRowC, 105);
265  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
266  'handleCsvLicense', array(array(
267  'shortname' => 'licC',
268  'licensetype' => 'lictypeC',
269  'fullname' => 'liceC',
270  'spdx_id' => 'lrf-licC',
271  'text' => 'txC',
272  'url' => '',
273  'notes' => '',
274  'source' => '',
275  'risk' => 2,
276  'parent_shortname' => null,
277  'report_shortname' => null,
278  'group' => null
279  )));
280  assertThat($returnC, is("Inserted 'licC' in DB"));
281 
282  // Test canlicC update
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')
292  ->with(
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())
295  ->once()
296  ->andReturn($canLicA);
297  $dbManager->shouldReceive('getSingleRow')
298  ->with(
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'),
302  anything())
303  ->once();
304  $dbManager->shouldReceive('getSingleRow')
305  ->with(
306  'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
307  anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)),
308  anything())
309  ->twice()
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,
320  'group' => 'fossy'
321  )));
322  assertThat($returnC, is(
323  "License 'canLicA' already exists in DB (id = 200)" .
324  ", updated fullname, updated SPDX ID, updated text"
325  ));
326 
327  // Test licA update
328  $dbManager->shouldReceive('getSingleRow')
329  ->with(
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())
333  ->once();
334  $dbManager->shouldReceive('getSingleRow')
335  ->with(
336  'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
337  anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)),
338  anything())
339  ->twice()
340  ->andReturn(array('rf_parent' => null));
341  $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
342  'handleCsvLicense', array(array(
343  'shortname' => 'licA',
344  'licensetype' => 'lictypeA',
345  'fullname' => 'liceB',
346  'text' => 'txA',
347  'url' => '',
348  'notes' => '',
349  'source' => '',
350  'risk' => 2,
351  'parent_shortname' => null,
352  'report_shortname' => null,
353  'group' => null
354  )));
355  assertThat($returnA, is(
356  "License 'licA' already exists in DB (id = 101)" .
357  ", updated fullname, updated text, updated the risk level"));
358 
359  // Test licE md5 collision
360  $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
361  'handleCsvLicense', array(array(
362  'shortname' => 'licE',
363  'licensetype' => 'lictypeE',
364  'fullname' => 'liceE',
365  'text' => 'txD',
366  'url' => '',
367  'notes' => '',
368  'source' => '',
369  'risk' => false,
370  'parent_shortname' => null,
371  'report_shortname' => null,
372  'group' => null
373  )));
374  assertThat($returnE, is(
375  "Error: MD5 checksum of 'licE' collides with license id=102"));
376 
377  // Test licG md5 collision
378  $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
379  'handleCsvLicense', array(array(
380  'shortname' => 'licG',
381  'licensetype' => 'lictypeG',
382  'fullname' => 'liceG',
383  'text' => 'txD',
384  'url' => '',
385  'notes' => '',
386  'source' => '_G_go_G_',
387  'parent_shortname' => null,
388  'report_shortname' => null,
389  'risk' => false,
390  'group' => null
391  )));
392  assertThat($returnG, is(
393  "Error: MD5 checksum of 'licG' collides with license id=102"));
394 
395  // Test canlicB insert
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';
406  $this->addLicenseInsertToDbManager($dbManager, $canlicB, 201,
407  "license_candidate");
408  $dbManager->shouldReceive('booleanToDb')
409  ->with(true)
410  ->once()
411  ->andReturn('t');
412  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
413  'handleCsvLicense', array(array(
414  'shortname' => 'canLicB',
415  'licensetype' => 'canLicTypeB',
416  'fullname' => 'canLiceB',
417  'spdx_id' => null,
418  'text' => 'txCan',
419  'url' => '',
420  'notes' => '',
421  'source' => '',
422  'risk' => 2,
423  'parent_shortname' => null,
424  'report_shortname' => null,
425  'group' => 'fossy'
426  )));
427  assertThat($returnC, is("Inserted 'canLicB' in DB" .
428  " as candidate license under group fossy"));
429  }
430 
441  public function testHandleHeadCsv()
442  {
443  $dbManager = M::mock(DbManager::class);
444  $userDao = M::mock(UserDao::class);
445  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
446 
447  assertThat(
448  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv',
449  array(array(
450  'shortname', 'foo', 'text', 'fullname', 'notes', 'bar', 'spdx_id', 'licensetype'
451  ))),
452  is(array(
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
457  )));
458 
459  assertThat(
460  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv',
461  array(array(
462  'Short Name', 'URL', 'text', 'fullname', 'notes', 'Foreign ID', 'SPDX ID',
463  'License group', 'License Type'
464  ))),
465  is(array(
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
469  )));
470  }
471 
480  {
481  $this->expectException(Exception::class);
482  $dbManager = M::mock(DbManager::class);
483  $userDao = M::mock(UserDao::class);
484  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
485  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'handleHeadCsv',array(array('shortname','foo','text')));
486  }
487 
497  public function testSetDelimiter()
498  {
499  $dbManager = M::mock(DbManager::class);
500  $userDao = M::mock(UserDao::class);
501  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
502 
503  $licenseCsvImport->setDelimiter('|');
504  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('|'));
505 
506  $licenseCsvImport->setDelimiter('<>');
507  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('<'));
508  }
509 
519  public function testSetEnclosure()
520  {
521  $dbManager = M::mock(DbManager::class);
522  $userDao = M::mock(UserDao::class);
523  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
524 
525  $licenseCsvImport->setEnclosure('|');
526  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure') ,is('|'));
527 
528  $licenseCsvImport->setEnclosure('<>');
529  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure'),is('<'));
530  }
531 
539  public function testHandleCsv()
540  {
541  $dbManager = M::mock(DbManager::class);
542  $userDao = M::mock(UserDao::class);
543  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
544 
545  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleCsv',
546  array(array('shortname','licensetype', 'foo', 'text', 'fullname', 'notes', 'spdx_id')));
547  assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'headrow'),
548  is(notNullValue()));
549 
550  $dbManager->shouldReceive('getSingleRow')
551  ->with(
552  'SELECT rf_shortname,rf_source,rf_pk,rf_risk FROM license_ref WHERE rf_md5=md5($1)',
553  anything())
554  ->andReturn(false);
555  $licenseRow = array(
556  "rf_shortname" => "licA",
557  "rf_licensetype" => "lictypeA",
558  "rf_fullname" => "liceA",
559  "rf_spdx_id" => null,
560  "rf_text" => "txA",
561  "rf_md5" => md5("txA"),
562  "rf_detector_type" => 1,
563  "rf_url" => '',
564  "rf_notes" => 'noteA',
565  "rf_source" => '',
566  "rf_risk" => 0
567  );
568  $this->addLicenseInsertToDbManager($dbManager, $licenseRow, 101);
569  Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', array(
570  'licA' => false
571  ));
572  Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', array(
573  md5('txA') => false
574  ));
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)));
581  }
582 
590  {
591  $dbManager = M::mock(DbManager::class);
592  $userDao = M::mock(UserDao::class);
593  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
594  $msg = $licenseCsvImport->handleFile('/tmp/thisFileNameShouldNotExists', 'csv');
595  assertThat($msg, is(equalTo(_('Internal error'))));
596  }
597 
605  {
606  $dbManager = M::mock(DbManager::class);
607  $userDao = M::mock(UserDao::class);
608  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
609  $msg = $licenseCsvImport->handleFile(__FILE__, 'csv');
610  assertThat($msg, startsWith( _('Error while parsing file')));
611  }
612 
620  public function testHandleFile()
621  {
622  $dbManager = M::mock(DbManager::class);
623  $userDao = M::mock(UserDao::class);
624  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
625  $filename = tempnam("/tmp", "FOO");
626  $handle = fopen($filename, 'w');
627  fwrite($handle, "shortname,fullname,text,spdx_id");
628  fclose($handle);
629  $msg = $licenseCsvImport->handleFile($filename, 'csv');
630  assertThat($msg, startsWith( _('head okay')));
631  unlink($filename);
632  }
633 
641  public function testSetMapTrue()
642  {
643  $testDb = new TestLiteDb();
644  $testDb->createPlainTables(array('license_ref', 'license_map'));
645  $licenseId = 101;
646  $parentId = 102;
647  $reportId = 103;
649  $dbManager = &$testDb->getDbManager();
650  $dbManager->insertTableRow('license_ref', array(
651  'rf_pk' => $licenseId,
652  'rf_shortname' => "Main License"
653  ));
654  $dbManager->insertTableRow('license_ref', array(
655  'rf_pk' => $parentId,
656  'rf_shortname' => "Parent License"
657  ));
658  $dbManager->insertTableRow('license_ref', array(
659  'rf_pk' => $reportId,
660  'rf_shortname' => "Reported License"
661  ));
662  $userDao = M::mock(UserDao::class);
663  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
664 
665  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
666  'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
667  equalTo(true));
668  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
669  'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
670  equalTo(true));
671 
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);
676 
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));
681  }
682 
691  public function testSetMapFalse()
692  {
693  $testDb = new TestLiteDb();
694  $testDb->createPlainTables(array('license_ref', 'license_map'));
695  $licenseId = 101;
696  $parentId = false;
697  $reportId = false;
699  $dbManager = &$testDb->getDbManager();
700  $dbManager->insertTableRow('license_ref', array(
701  'rf_pk' => $licenseId,
702  'rf_shortname' => "Main License"
703  ));
704  $userDao = M::mock(UserDao::class);
705  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
706 
707  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
708  'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
709  equalTo(false));
710  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
711  'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
712  equalTo(false));
713 
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);
718 
719  assertThat($row, equalTo(false));
720  $row = $dbManager->getSingleRow($sql, array($licenseId,
721  LicenseMap::REPORT), $statement);
722  assertThat($row, equalTo(false));
723  }
724 
729  private function createCandidateTable($dbManager)
730  {
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);
735  }
736 
744  private function addLicenseInsertToDbManager(&$dbManager, $row, $return,
745  $table = "license_ref")
746  {
747  $dbManager->shouldReceive('insertTableRow')
748  ->with($table, $row, anything(), 'rf_pk')
749  ->once()
750  ->andReturn($return);
751  }
752 }
testHandleFile()
Test for LicenseCsvImport::handleFile() (csv file with header)
testHandleFileIfFileIsNotParsable()
Test for LicenseCsvImport::handleFile() (non-csv file)
testGetKeyFromMd5()
Test for LicenseCsvImport::getKeyFromMd5()
testGetKeyFromShortname()
Test for LicenseCsvImport::getKeyFromShortname()
testSetDelimiter()
Test for LicenseCsvImport::setDelimiter()
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()
Wrapper class for license map.
Definition: LicenseMap.php:19
Utility functions for specific applications.