FOSSology  4.7.1
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 {
24  public $assertCountBefore;
25 
30  protected function setUp() : void
31  {
32  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
33  }
34 
39  protected function tearDown() : void
40  {
41  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
42  M::close();
43  }
44 
54  public function testGetKeyFromShortname()
55  {
56  $testDb = new TestLiteDb();
57  $testDb->createPlainTables(array('license_ref'));
58  $shortname = 'licA';
59  $knownId = 101;
60  $knownGroup = 4;
61  /*** @var DbManager ***/
62  $dbManager = &$testDb->getDbManager();
63  $dbManager->insertTableRow('license_ref', array('rf_pk'=>$knownId,'rf_shortname'=>$shortname));
64  $this->createCandidateTable($dbManager);
65  $dbManager->insertTableRow('license_candidate', array(
66  'rf_pk' => $knownId + 2,
67  'rf_shortname' => "candidate-$shortname",
68  'group_fk' => $knownGroup
69  ));
70  $userDao = M::mock(UserDao::class);
71  $userDao->shouldReceive('getGroupIdByName')
72  ->with("fossy")
73  ->once()
74  ->andReturn(4);
75  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
76 
77  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'getKeyFromShortname', array($shortname)), equalTo($knownId));
78  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'getKeyFromShortname', array("no $shortname")), equalTo(false));
79  // Candidates
80  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
81  'getKeyFromShortname',
82  array("candidate-$shortname", "fossy")),
83  equalTo($knownId + 2));
84  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
85  'getKeyFromShortname',
86  array("candidate-$shortname")),
87  equalTo(false));
88  }
89 
99  public function testGetKeyFromMd5()
100  {
101  $licenseText = 'I am a strong license';
102  $knownId = 101;
103  $falseLicenseText = "I am a weak license";
104 
105  $dbManager = M::mock(DbManager::class);
106  $dbManager->shouldReceive('getSingleRow')
107  ->with('SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1) AND rf_external_id IS NULL',
108  array($licenseText))
109  ->once()
110  ->andReturn(array('rf_pk' => $knownId));
111  $dbManager->shouldReceive('getSingleRow')
112  ->with('SELECT rf_pk FROM ONLY license_ref WHERE rf_md5=md5($1) AND rf_external_id IS NULL',
113  array($falseLicenseText))
114  ->andReturnNull();
115  $userDao = M::mock(UserDao::class);
116  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
117 
118  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
119  'getKeyFromMd5', array($licenseText)), equalTo($knownId));
120  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
121  'getKeyFromMd5', array($falseLicenseText)), equalTo(false));
122  }
123 
131  public function testHandleCsvLicense()
132  {
133  $dbManager = M::mock(DbManager::class);
134  $nkMap = array(
135  'licA' => 101,
136  'licB' => false,
137  'licC' => false,
138  'licE' => false,
139  'licF' => false,
140  'licG' => false,
141  'licH' => false,
142  'licZ' => 100,
143  'canLicAfossy' => 200,
144  'canLicBfossy' => false
145  );
146  $mdkMap = array(
147  md5('txA') => 101,
148  md5('txB') => false,
149  md5('txC') => false,
150  md5('txD') => 102,
151  md5('txE') => false,
152  md5('txF') => false,
153  md5('txG') => false,
154  md5('txH') => false,
155  md5('txZ') => 100,
156  md5('txCan') => 200,
157  md5('Text of candidate license') => false
158  );
159  $userDao = M::mock(UserDao::class);
160  $userDao->shouldReceive('getGroupIdByName')
161  ->with("fossy")
162  ->times(3)
163  ->andReturn(4);
164 
165  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
166  Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', $nkMap);
167  Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', $mdkMap);
168 
169  $singleRowA = array(
170  'rf_shortname' => 'licA',
171  'rf_spdx_id' => 'lrf-licA',
172  'rf_licensetype' => 'lictypeA',
173  'rf_fullname' => 'licennnseA',
174  'rf_text' => 'someRandom',
175  'rf_md5' => md5('someRandom'),
176  'rf_detector_type' => 1,
177  'rf_url' => '',
178  'rf_notes' => '',
179  'rf_source' => '',
180  'rf_risk' => 4,
181  'rf_external_id' => null,
182  );
183  $dbManager->shouldReceive('getSingleRow')
184  ->with(
185  'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype, rf_active ' .
186  'FROM license_ref WHERE rf_pk = $1', array(101), anything())
187  ->once()
188  ->andReturn($singleRowA);
189 
190  // Test for licB insert
191  $dbManager->shouldReceive('insertTableRow')
192  ->withArgs(array(
193  'license_map', array(
194  'rf_fk' => 103, 'rf_parent' => 101, 'usage' => LicenseMap::CONCLUSION
195  )))
196  ->once();
197  $singleRowB = $singleRowA;
198  $singleRowB["rf_shortname"] = "licB";
199  $singleRowB["rf_licensetype"] = "lictypeB";
200  $singleRowB["rf_fullname"] = "liceB";
201  $singleRowB["rf_spdx_id"] = "lrf-B";
202  $singleRowB["rf_text"] = "txB";
203  $singleRowB["rf_md5"] = md5("txB");
204  $singleRowB["rf_risk"] = 0;
205  $this->addLicenseInsertToDbManager($dbManager, $singleRowB, 103);
206  $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
207  'handleCsvLicense', array(array(
208  'shortname' => 'licB',
209  'spdx_id' => 'lrf-B',
210  'licensetype' => 'lictypeB',
211  'fullname' => 'liceB',
212  'text' => 'txB',
213  'url' => '',
214  'notes' => '',
215  'source' => '',
216  'risk' => 0,
217  'parent_shortname' => 'licA',
218  'report_shortname' => null,
219  'group' => null
220  )));
221  assertThat($returnB, is("Inserted 'licB' in DB with conclusion 'licA'"));
222 
223  // Test for licF insert
224  $singleRowF = $singleRowA;
225  $singleRowF["rf_shortname"] = "licF";
226  $singleRowF["rf_licensetype"] = "lictypeF";
227  $singleRowF["rf_fullname"] = "liceF";
228  $singleRowF["rf_spdx_id"] = null;
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  'licensetype' => 'lictypeF',
245  'fullname' => 'liceF',
246  'spdx_id' => null,
247  'text' => 'txF',
248  'url' => '',
249  'notes' => '',
250  'source' => '',
251  'risk' => 1,
252  'parent_shortname' => null,
253  'report_shortname' => 'licZ',
254  'group' => null
255  )));
256  assertThat($returnF, is("Inserted 'licF' in DB reporting 'licZ'"));
257 
258  // Test licC insert
259  $singleRowC = $singleRowA;
260  $singleRowC["rf_shortname"] = "licC";
261  $singleRowC["rf_licensetype"] = "lictypeC";
262  $singleRowC["rf_fullname"] = "liceC";
263  $singleRowC["rf_spdx_id"] = "lrf-licC";
264  $singleRowC["rf_text"] = "txC";
265  $singleRowC["rf_md5"] = md5("txC");
266  $singleRowC["rf_risk"] = 2;
267  $this->addLicenseInsertToDbManager($dbManager, $singleRowC, 105);
268  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
269  'handleCsvLicense', array(array(
270  'shortname' => 'licC',
271  'licensetype' => 'lictypeC',
272  'fullname' => 'liceC',
273  'spdx_id' => 'lrf-licC',
274  'text' => 'txC',
275  'url' => '',
276  'notes' => '',
277  'source' => '',
278  'risk' => 2,
279  'parent_shortname' => null,
280  'report_shortname' => null,
281  'group' => null
282  )));
283  assertThat($returnC, is("Inserted 'licC' in DB"));
284 
285  // Test canlicC update
286  $canLicA = $singleRowA;
287  $canLicA["rf_shortname"] = "canLicA";
288  $canLicA["rf_licensetype"] = "canLicTypeA";
289  $canLicA["rf_fullname"] = "canLiceA";
290  $canLicA["rf_spdx_id"] = null;
291  $canLicA["rf_text"] = "txcan";
292  $canLicA["rf_risk"] = 0;
293  $canLicA["rf_group"] = 4;
294  $dbManager->shouldReceive('getSingleRow')
295  ->with(
296  'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype, rf_active ' .
297  'FROM license_ref WHERE rf_pk = $1', array(200), anything())
298  ->once()
299  ->andReturn($canLicA);
300  $dbManager->shouldReceive('getSingleRow')
301  ->with(
302  "UPDATE license_candidate SET " .
303  "rf_fullname=$2,rf_spdx_id=$3,rf_text=$4,rf_md5=md5($4) WHERE rf_pk=$1;",
304  array(200, 'canDidateLicenseA', 'lrf-canLicA', 'Text of candidate license'),
305  anything())
306  ->once();
307  $dbManager->shouldReceive('getSingleRow')
308  ->with(
309  'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
310  anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)),
311  anything())
312  ->twice()
313  ->andReturn(array('rf_parent' => null));
314  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
315  'handleCsvLicense', array(array(
316  'shortname' => 'canLicA',
317  'licensetype' => 'canLicTypeA',
318  'fullname' => 'canDidateLicenseA',
319  'spdx_id' => 'lrf-canLicA',
320  'text' => 'Text of candidate license',
321  'url' => '', 'notes' => '', 'source' => '', 'risk' => 0,
322  'parent_shortname' => null, 'report_shortname' => null,
323  'group' => 'fossy', 'external_id' => null
324  )));
325  assertThat($returnC, is(
326  "License 'canLicA' already exists in DB (id = 200)" .
327  ", updated fullname, updated SPDX ID, updated text"
328  ));
329 
330  // Test licA update
331  $dbManager->shouldReceive('getSingleRow')
332  ->with(
333  "UPDATE license_ref SET " .
334  "rf_fullname=$2,rf_text=$3,rf_md5=md5($3),rf_risk=$4 WHERE rf_pk=$1;",
335  array(101, 'liceB', 'txA', 2), anything())
336  ->once();
337  $dbManager->shouldReceive('getSingleRow')
338  ->with(
339  'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
340  anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)),
341  anything())
342  ->twice()
343  ->andReturn(array('rf_parent' => null));
344  $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
345  'handleCsvLicense', array(array(
346  'shortname' => 'licA',
347  'licensetype' => 'lictypeA',
348  'fullname' => 'liceB',
349  'text' => 'txA',
350  'url' => '',
351  'notes' => '',
352  'source' => '',
353  'risk' => 2,
354  'parent_shortname' => null,
355  'report_shortname' => null,
356  'group' => null,
357  'spdx_id' => null
358  )));
359  assertThat($returnA, is(
360  "License 'licA' already exists in DB (id = 101)" .
361  ", updated fullname, updated text, updated the risk level"));
362 
363  // Test licE md5 collision
364  $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
365  'handleCsvLicense', array(array(
366  'shortname' => 'licE',
367  'licensetype' => 'lictypeE',
368  'fullname' => 'liceE',
369  'text' => 'txD',
370  'url' => '',
371  'notes' => '',
372  'source' => '',
373  'risk' => false,
374  'parent_shortname' => null,
375  'report_shortname' => null,
376  'group' => null
377  )));
378  assertThat($returnE, is(
379  "Error: MD5 checksum of 'licE' collides with license id=102"));
380 
381  // Test licG md5 collision
382  $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
383  'handleCsvLicense', array(array(
384  'shortname' => 'licG',
385  'licensetype' => 'lictypeG',
386  'fullname' => 'liceG',
387  'text' => 'txD',
388  'url' => '',
389  'notes' => '',
390  'source' => '_G_go_G_',
391  'parent_shortname' => null,
392  'report_shortname' => null,
393  'risk' => false,
394  'group' => null
395  )));
396  assertThat($returnG, is(
397  "Error: MD5 checksum of 'licG' collides with license id=102"));
398 
399  // Test canlicB insert
400  $canlicB = $singleRowA;
401  $canlicB["rf_shortname"] = "canLicB";
402  $canlicB["rf_licensetype"] = "canLicTypeB";
403  $canlicB["rf_fullname"] = "canLiceB";
404  $canlicB["rf_spdx_id"] = null;
405  $canlicB["rf_text"] = "txCan";
406  $canlicB["rf_md5"] = md5("txCan");
407  $canlicB["rf_risk"] = 2;
408  $canlicB["group_fk"] = 4;
409  $canlicB["marydone"] = 't';
410  $this->addLicenseInsertToDbManager($dbManager, $canlicB, 201,
411  "license_candidate");
412  $dbManager->shouldReceive('booleanToDb')
413  ->with(true)
414  ->once()
415  ->andReturn('t');
416  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
417  'handleCsvLicense', array(array(
418  'shortname' => 'canLicB',
419  'licensetype' => 'canLicTypeB',
420  'fullname' => 'canLiceB',
421  'spdx_id' => null,
422  'text' => 'txCan',
423  'url' => '',
424  'notes' => '',
425  'source' => '',
426  'risk' => 2,
427  'parent_shortname' => null,
428  'report_shortname' => null,
429  'group' => 'fossy'
430  )));
431  assertThat($returnC, is("Inserted 'canLicB' in DB" .
432  " as candidate license under group fossy"));
433  }
434 
445  public function testHandleHeadCsv()
446  {
447  $dbManager = M::mock(DbManager::class);
448  $userDao = M::mock(UserDao::class);
449  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
450 
451  assertThat(
452  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv',
453  array(array(
454  'shortname', 'foo', 'text', 'fullname', 'notes', 'bar', 'spdx_id', 'licensetype'
455  ))),
456  is(array(
457  'shortname' => 0, 'fullname' => 3, 'text' => 2, 'spdx_id' => 6,
458  'parent_shortname' => false, 'report_shortname' => false,
459  'url' => false, 'notes' => 4, 'source' => false, 'risk' => 0,
460  'group' => false,'licensetype' => 7
461  )));
462 
463  assertThat(
464  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv',
465  array(array(
466  'Short Name', 'URL', 'text', 'fullname', 'notes', 'Foreign ID', 'SPDX ID',
467  'License group', 'License Type'
468  ))),
469  is(array(
470  'shortname' => 0, 'fullname' => 3, 'spdx_id' => 6, 'text' => 2,
471  'parent_shortname' => false, 'report_shortname' => false, 'url' => 1,
472  'notes' => 4, 'source' => 5, 'risk' => false, 'group' => 7, 'licensetype' => 8
473  )));
474  }
475 
484  {
485  $this->expectException(Exception::class);
486  $dbManager = M::mock(DbManager::class);
487  $userDao = M::mock(UserDao::class);
488  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
489  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'handleHeadCsv',array(array('shortname','foo','text')));
490  }
491 
501  public function testSetDelimiter()
502  {
503  $dbManager = M::mock(DbManager::class);
504  $userDao = M::mock(UserDao::class);
505  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
506 
507  $licenseCsvImport->setDelimiter('|');
508  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('|'));
509 
510  $licenseCsvImport->setDelimiter('<>');
511  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('<'));
512  }
513 
523  public function testSetEnclosure()
524  {
525  $dbManager = M::mock(DbManager::class);
526  $userDao = M::mock(UserDao::class);
527  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
528 
529  $licenseCsvImport->setEnclosure('|');
530  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure') ,is('|'));
531 
532  $licenseCsvImport->setEnclosure('<>');
533  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure'),is('<'));
534  }
535 
543  public function testHandleCsv()
544  {
545  $dbManager = M::mock(DbManager::class);
546  $userDao = M::mock(UserDao::class);
547  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
548 
549  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleCsv',
550  array(array('shortname','licensetype', 'foo', 'text', 'fullname', 'notes', 'spdx_id')));
551  assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'headrow'),
552  is(notNullValue()));
553 
554  $dbManager->shouldReceive('getSingleRow')
555  ->with(
556  'SELECT rf_shortname,rf_source,rf_pk,rf_risk FROM license_ref WHERE rf_md5=md5($1)',
557  anything())
558  ->andReturn(false);
559  $licenseRow = array(
560  "rf_shortname" => "licA",
561  "rf_licensetype" => "lictypeA",
562  "rf_fullname" => "liceA",
563  "rf_spdx_id" => null,
564  "rf_text" => "txA",
565  "rf_md5" => md5("txA"),
566  "rf_detector_type" => 1,
567  "rf_url" => '',
568  "rf_notes" => 'noteA',
569  "rf_source" => '',
570  "rf_risk" => 0,
571  "rf_external_id" => null,
572  );
573  $this->addLicenseInsertToDbManager($dbManager, $licenseRow, 101);
574  Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', array(
575  'licA' => false
576  ));
577  Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', array(
578  md5('txA') => false
579  ));
580  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleCsv',
581  array(array('licA', 'lictypeA', 'bar', 'txA', 'liceA', 'noteA')));
582  assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'nkMap'),
583  is(array('licA' => 101)));
584  assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'mdkMap'),
585  is(array(md5('txA') => 101)));
586  }
587 
596  {
597  $dbManager = M::mock(DbManager::class);
598  $userDao = M::mock(UserDao::class);
599  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
600  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleCsv',
601  array(array('shortname', 'fullname', 'text')));
602  Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', array(
603  'licA' => 101, 'licB' => false
604  ));
605  Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', array(
606  md5('txA') => 101, md5('txB') => false
607  ));
608 
609  $dbManager->shouldReceive('getSingleRow')
610  ->with(
611  'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype, rf_active ' .
612  'FROM license_ref WHERE rf_pk = $1', array(101), anything())
613  ->once()
614  ->andReturn(array(
615  'rf_shortname' => 'licA',
616  'rf_fullname' => 'liceA',
617  'rf_spdx_id' => null,
618  'rf_text' => 'txA',
619  'rf_url' => '',
620  'rf_notes' => '',
621  'rf_source' => '',
622  'rf_risk' => 0,
623  'rf_licensetype' => 'Strong Copyleft'
624  ));
625  $dbManager->shouldReceive('getSingleRow')
626  ->with('SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
627  anything(), anything())
628  ->andReturn(false);
629  $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
630  'handleCsv', array(array('licA', 'liceA', 'txA')));
631  assertThat($returnA, is("License 'licA' already exists in DB (id = 101)"));
632 
633  $this->addLicenseInsertToDbManager($dbManager, array(
634  "rf_shortname" => "licB",
635  "rf_licensetype" => "Unknown",
636  "rf_fullname" => "liceB",
637  "rf_spdx_id" => null,
638  "rf_text" => "txB",
639  "rf_md5" => md5("txB"),
640  "rf_detector_type" => 1,
641  "rf_url" => '',
642  "rf_notes" => '',
643  "rf_source" => '',
644  "rf_risk" => 0,
645  "rf_external_id" => null,
646  ), 102);
647  $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
648  'handleCsv', array(array('licB', 'liceB', 'txB')));
649  assertThat($returnB, is("Inserted 'licB' in DB"));
650  }
651 
659  {
660  $dbManager = M::mock(DbManager::class);
661  $userDao = M::mock(UserDao::class);
662  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
663  $msg = $licenseCsvImport->handleFile('/tmp/thisFileNameShouldNotExists', 'csv');
664  assertThat($msg, is(equalTo(_('Internal error'))));
665  }
666 
674  {
675  $dbManager = M::mock(DbManager::class);
676  $userDao = M::mock(UserDao::class);
677  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
678  $msg = $licenseCsvImport->handleFile(__FILE__, 'csv');
679  assertThat($msg, startsWith( _('Error while parsing file')));
680  }
681 
689  public function testHandleFile()
690  {
691  $dbManager = M::mock(DbManager::class);
692  $userDao = M::mock(UserDao::class);
693  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
694  $filename = tempnam("/tmp", "FOO");
695  $handle = fopen($filename, 'w');
696  fwrite($handle, "shortname,fullname,text,spdx_id");
697  fclose($handle);
698  $msg = $licenseCsvImport->handleFile($filename, 'csv');
699  assertThat($msg, startsWith( _('head okay')));
700  unlink($filename);
701  }
702 
710  public function testSetMapTrue()
711  {
712  $testDb = new TestLiteDb();
713  $testDb->createPlainTables(array('license_ref', 'license_map'));
714  $licenseId = 101;
715  $parentId = 102;
716  $reportId = 103;
718  $dbManager = &$testDb->getDbManager();
719  $dbManager->insertTableRow('license_ref', array(
720  'rf_pk' => $licenseId,
721  'rf_shortname' => "Main License"
722  ));
723  $dbManager->insertTableRow('license_ref', array(
724  'rf_pk' => $parentId,
725  'rf_shortname' => "Parent License"
726  ));
727  $dbManager->insertTableRow('license_ref', array(
728  'rf_pk' => $reportId,
729  'rf_shortname' => "Reported License"
730  ));
731  $userDao = M::mock(UserDao::class);
732  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
733 
734  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
735  'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
736  equalTo(true));
737  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
738  'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
739  equalTo(true));
740 
741  $sql = "SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
742  $statement = __METHOD__ . ".getMap";
743  $row = $dbManager->getSingleRow($sql, array($licenseId,
744  LicenseMap::CONCLUSION), $statement);
745 
746  assertThat($row['rf_parent'], equalTo($parentId));
747  $row = $dbManager->getSingleRow($sql, array($licenseId,
748  LicenseMap::REPORT), $statement);
749  assertThat($row['rf_parent'], equalTo($reportId));
750  }
751 
760  public function testSetMapFalse()
761  {
762  $testDb = new TestLiteDb();
763  $testDb->createPlainTables(array('license_ref', 'license_map'));
764  $licenseId = 101;
765  $parentId = false;
766  $reportId = false;
768  $dbManager = &$testDb->getDbManager();
769  $dbManager->insertTableRow('license_ref', array(
770  'rf_pk' => $licenseId,
771  'rf_shortname' => "Main License"
772  ));
773  $userDao = M::mock(UserDao::class);
774  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
775 
776  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
777  'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
778  equalTo(false));
779  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
780  'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
781  equalTo(false));
782 
783  $sql = "SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
784  $statement = __METHOD__ . ".getMap";
785  $row = $dbManager->getSingleRow($sql, array($licenseId,
786  LicenseMap::CONCLUSION), $statement);
787 
788  assertThat($row, equalTo(false));
789  $row = $dbManager->getSingleRow($sql, array($licenseId,
790  LicenseMap::REPORT), $statement);
791  assertThat($row, equalTo(false));
792  }
793 
801  {
802  $baseRow = array(
803  'shortname' => 'licI', 'licensetype' => 'Permissive',
804  'fullname' => 'License I', 'spdx_id' => 'LicenseRef-licI',
805  'text' => 'txI', 'url' => '', 'notes' => '', 'source' => '',
806  'risk' => 0, 'external_id' => 'ext-licI'
807  );
808  $cases = array(
809  array('active' => false, 'expected' => false),
810  array('active' => true, 'expected' => true),
811  array('expected' => true)
812  );
813  foreach ($cases as $case) {
814  $expected = $case['expected'];
815  unset($case['expected']);
816  $dbManager = M::mock(DbManager::class);
817  $dbManager->shouldReceive('booleanToDb')
818  ->with($expected)->once()->andReturn($expected ? 't' : 'f');
819  $dbManager->shouldReceive('insertTableRow')
820  ->with('license_ref', M::on(function ($columns) use ($expected) {
821  return $columns['rf_active'] === ($expected ? 't' : 'f');
822  }), anything(), 'rf_pk')
823  ->once()->andReturn(110);
824  $licenseCsvImport = new LicenseCsvImport($dbManager,
825  M::mock(UserDao::class));
826 
827  $result = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
828  'insertNewLicense', array(array_merge($baseRow, $case)));
829  assertThat($result['pkey'], equalTo(110));
830  }
831  }
832 
837  private function createCandidateTable($dbManager)
838  {
839  $sql = "CREATE TABLE license_candidate (" .
840  "rf_pk, rf_shortname, rf_fullname, rf_text, rf_md5, rf_url, rf_notes, " .
841  "marydone, rf_source, rf_risk, rf_detector_type, group_fk)";
842  $dbManager->queryOnce($sql);
843  }
844 
852  private function addLicenseInsertToDbManager(&$dbManager, $row, $return,
853  $table = "license_ref")
854  {
855  $dbManager->shouldReceive('booleanToDb')
856  ->with(true)
857  ->once()
858  ->andReturn('t');
859  $row['rf_active'] = 't';
860  $dbManager->shouldReceive('insertTableRow')
861  ->with($table, $row, anything(), 'rf_pk')
862  ->once()
863  ->andReturn($return);
864  }
865 }
testHandleFile()
Test for LicenseCsvImport::handleFile() (csv file with header)
testHandleFileIfFileIsNotParsable()
Test for LicenseCsvImport::handleFile() (non-csv file)
testInsertNewLicenseKeepsActiveFlag()
Test for LicenseCsvImport::insertNewLicense() active flag.
testGetKeyFromMd5()
Test for LicenseCsvImport::getKeyFromMd5()
testGetKeyFromShortname()
Test for LicenseCsvImport::getKeyFromShortname()
testSetDelimiter()
Test for LicenseCsvImport::setDelimiter()
testHandleHeadCsv_missingMandidatoryKey()
Test for LicenseCsvImport::handleHeadCsv()
testHandleCsvWithoutLicenseType()
Test for LicenseCsvImport::handleCsv() without licensetype column.
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.