FOSSology  4.7.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) AND rf_external_id IS NULL',
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) AND rf_external_id IS NULL',
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  'rf_external_id' => null,
180  );
181  $dbManager->shouldReceive('getSingleRow')
182  ->with(
183  'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype ' .
184  'FROM license_ref WHERE rf_pk = $1', array(101), anything())
185  ->once()
186  ->andReturn($singleRowA);
187 
188  // Test for licB insert
189  $dbManager->shouldReceive('insertTableRow')
190  ->withArgs(array(
191  'license_map', array(
192  'rf_fk' => 103, 'rf_parent' => 101, 'usage' => LicenseMap::CONCLUSION
193  )))
194  ->once();
195  $singleRowB = $singleRowA;
196  $singleRowB["rf_shortname"] = "licB";
197  $singleRowB["rf_licensetype"] = "lictypeB";
198  $singleRowB["rf_fullname"] = "liceB";
199  $singleRowB["rf_spdx_id"] = "lrf-B";
200  $singleRowB["rf_text"] = "txB";
201  $singleRowB["rf_md5"] = md5("txB");
202  $singleRowB["rf_risk"] = 0;
203  $this->addLicenseInsertToDbManager($dbManager, $singleRowB, 103);
204  $returnB = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
205  'handleCsvLicense', array(array(
206  'shortname' => 'licB',
207  'spdx_id' => 'lrf-B',
208  'licensetype' => 'lictypeB',
209  'fullname' => 'liceB',
210  'text' => 'txB',
211  'url' => '',
212  'notes' => '',
213  'source' => '',
214  'risk' => 0,
215  'parent_shortname' => 'licA',
216  'report_shortname' => null,
217  'group' => null
218  )));
219  assertThat($returnB, is("Inserted 'licB' in DB with conclusion 'licA'"));
220 
221  // Test for licF insert
222  $singleRowF = $singleRowA;
223  $singleRowF["rf_shortname"] = "licF";
224  $singleRowF["rf_licensetype"] = "lictypeF";
225  $singleRowF["rf_fullname"] = "liceF";
226  $singleRowF["rf_spdx_id"] = null;
227  $singleRowF["rf_text"] = "txF";
228  $singleRowF["rf_md5"] = md5("txF");
229  $singleRowF["rf_risk"] = 1;
230  $this->addLicenseInsertToDbManager($dbManager, $singleRowF, 104);
231  $dbManager->shouldReceive('insertTableRow')
232  ->withArgs(array(
233  'license_map', array(
234  'rf_fk' => 104,
235  'rf_parent' => 100,
236  'usage' => LicenseMap::REPORT
237  )))
238  ->once();
239  $returnF = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
240  'handleCsvLicense', array(array(
241  'shortname' => 'licF',
242  'licensetype' => 'lictypeF',
243  'fullname' => 'liceF',
244  'spdx_id' => null,
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_licensetype"] = "lictypeC";
260  $singleRowC["rf_fullname"] = "liceC";
261  $singleRowC["rf_spdx_id"] = "lrf-licC";
262  $singleRowC["rf_text"] = "txC";
263  $singleRowC["rf_md5"] = md5("txC");
264  $singleRowC["rf_risk"] = 2;
265  $this->addLicenseInsertToDbManager($dbManager, $singleRowC, 105);
266  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
267  'handleCsvLicense', array(array(
268  'shortname' => 'licC',
269  'licensetype' => 'lictypeC',
270  'fullname' => 'liceC',
271  'spdx_id' => 'lrf-licC',
272  'text' => 'txC',
273  'url' => '',
274  'notes' => '',
275  'source' => '',
276  'risk' => 2,
277  'parent_shortname' => null,
278  'report_shortname' => null,
279  'group' => null
280  )));
281  assertThat($returnC, is("Inserted 'licC' in DB"));
282 
283  // Test canlicC update
284  $canLicA = $singleRowA;
285  $canLicA["rf_shortname"] = "canLicA";
286  $canLicA["rf_licensetype"] = "canLicTypeA";
287  $canLicA["rf_fullname"] = "canLiceA";
288  $canLicA["rf_spdx_id"] = null;
289  $canLicA["rf_text"] = "txcan";
290  $canLicA["rf_risk"] = 0;
291  $canLicA["rf_group"] = 4;
292  $dbManager->shouldReceive('getSingleRow')
293  ->with(
294  'SELECT rf_shortname, rf_fullname, rf_spdx_id, rf_text, rf_url, rf_notes, rf_source, rf_risk, rf_licensetype ' .
295  'FROM license_ref WHERE rf_pk = $1', array(200), anything())
296  ->once()
297  ->andReturn($canLicA);
298  $dbManager->shouldReceive('getSingleRow')
299  ->with(
300  "UPDATE license_candidate SET " .
301  "rf_fullname=$2,rf_spdx_id=$3,rf_text=$4,rf_md5=md5($4) WHERE rf_pk=$1;",
302  array(200, 'canDidateLicenseA', 'lrf-canLicA', 'Text of candidate license'),
303  anything())
304  ->once();
305  $dbManager->shouldReceive('getSingleRow')
306  ->with(
307  'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
308  anyof(array(200, LicenseMap::CONCLUSION), array(200, LicenseMap::REPORT)),
309  anything())
310  ->twice()
311  ->andReturn(array('rf_parent' => null));
312  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
313  'handleCsvLicense', array(array(
314  'shortname' => 'canLicA',
315  'licensetype' => 'canLicTypeA',
316  'fullname' => 'canDidateLicenseA',
317  'spdx_id' => 'lrf-canLicA',
318  'text' => 'Text of candidate license',
319  'url' => '', 'notes' => '', 'source' => '', 'risk' => 0,
320  'parent_shortname' => null, 'report_shortname' => null,
321  'group' => 'fossy', 'external_id' => null
322  )));
323  assertThat($returnC, is(
324  "License 'canLicA' already exists in DB (id = 200)" .
325  ", updated fullname, updated SPDX ID, updated text"
326  ));
327 
328  // Test licA update
329  $dbManager->shouldReceive('getSingleRow')
330  ->with(
331  "UPDATE license_ref SET " .
332  "rf_fullname=$2,rf_text=$3,rf_md5=md5($3),rf_risk=$4 WHERE rf_pk=$1;",
333  array(101, 'liceB', 'txA', 2), anything())
334  ->once();
335  $dbManager->shouldReceive('getSingleRow')
336  ->with(
337  'SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;',
338  anyof(array(101, LicenseMap::CONCLUSION), array(101, LicenseMap::REPORT)),
339  anything())
340  ->twice()
341  ->andReturn(array('rf_parent' => null));
342  $returnA = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
343  'handleCsvLicense', array(array(
344  'shortname' => 'licA',
345  'licensetype' => 'lictypeA',
346  'fullname' => 'liceB',
347  'text' => 'txA',
348  'url' => '',
349  'notes' => '',
350  'source' => '',
351  'risk' => 2,
352  'parent_shortname' => null,
353  'report_shortname' => null,
354  'group' => null,
355  'spdx_id' => null
356  )));
357  assertThat($returnA, is(
358  "License 'licA' already exists in DB (id = 101)" .
359  ", updated fullname, updated text, updated the risk level"));
360 
361  // Test licE md5 collision
362  $returnE = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
363  'handleCsvLicense', array(array(
364  'shortname' => 'licE',
365  'licensetype' => 'lictypeE',
366  'fullname' => 'liceE',
367  'text' => 'txD',
368  'url' => '',
369  'notes' => '',
370  'source' => '',
371  'risk' => false,
372  'parent_shortname' => null,
373  'report_shortname' => null,
374  'group' => null
375  )));
376  assertThat($returnE, is(
377  "Error: MD5 checksum of 'licE' collides with license id=102"));
378 
379  // Test licG md5 collision
380  $returnG = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
381  'handleCsvLicense', array(array(
382  'shortname' => 'licG',
383  'licensetype' => 'lictypeG',
384  'fullname' => 'liceG',
385  'text' => 'txD',
386  'url' => '',
387  'notes' => '',
388  'source' => '_G_go_G_',
389  'parent_shortname' => null,
390  'report_shortname' => null,
391  'risk' => false,
392  'group' => null
393  )));
394  assertThat($returnG, is(
395  "Error: MD5 checksum of 'licG' collides with license id=102"));
396 
397  // Test canlicB insert
398  $canlicB = $singleRowA;
399  $canlicB["rf_shortname"] = "canLicB";
400  $canlicB["rf_licensetype"] = "canLicTypeB";
401  $canlicB["rf_fullname"] = "canLiceB";
402  $canlicB["rf_spdx_id"] = null;
403  $canlicB["rf_text"] = "txCan";
404  $canlicB["rf_md5"] = md5("txCan");
405  $canlicB["rf_risk"] = 2;
406  $canlicB["group_fk"] = 4;
407  $canlicB["marydone"] = 't';
408  $this->addLicenseInsertToDbManager($dbManager, $canlicB, 201,
409  "license_candidate");
410  $dbManager->shouldReceive('booleanToDb')
411  ->with(true)
412  ->once()
413  ->andReturn('t');
414  $returnC = Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
415  'handleCsvLicense', array(array(
416  'shortname' => 'canLicB',
417  'licensetype' => 'canLicTypeB',
418  'fullname' => 'canLiceB',
419  'spdx_id' => null,
420  'text' => 'txCan',
421  'url' => '',
422  'notes' => '',
423  'source' => '',
424  'risk' => 2,
425  'parent_shortname' => null,
426  'report_shortname' => null,
427  'group' => 'fossy'
428  )));
429  assertThat($returnC, is("Inserted 'canLicB' in DB" .
430  " as candidate license under group fossy"));
431  }
432 
443  public function testHandleHeadCsv()
444  {
445  $dbManager = M::mock(DbManager::class);
446  $userDao = M::mock(UserDao::class);
447  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
448 
449  assertThat(
450  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv',
451  array(array(
452  'shortname', 'foo', 'text', 'fullname', 'notes', 'bar', 'spdx_id', 'licensetype'
453  ))),
454  is(array(
455  'shortname' => 0, 'fullname' => 3, 'text' => 2, 'spdx_id' => 6,
456  'parent_shortname' => false, 'report_shortname' => false,
457  'url' => false, 'notes' => 4, 'source' => false, 'risk' => 0,
458  'group' => false,'licensetype' => 7
459  )));
460 
461  assertThat(
462  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleHeadCsv',
463  array(array(
464  'Short Name', 'URL', 'text', 'fullname', 'notes', 'Foreign ID', 'SPDX ID',
465  'License group', 'License Type'
466  ))),
467  is(array(
468  'shortname' => 0, 'fullname' => 3, 'spdx_id' => 6, 'text' => 2,
469  'parent_shortname' => false, 'report_shortname' => false, 'url' => 1,
470  'notes' => 4, 'source' => 5, 'risk' => false, 'group' => 7, 'licensetype' => 8
471  )));
472  }
473 
482  {
483  $this->expectException(Exception::class);
484  $dbManager = M::mock(DbManager::class);
485  $userDao = M::mock(UserDao::class);
486  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
487  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,'handleHeadCsv',array(array('shortname','foo','text')));
488  }
489 
499  public function testSetDelimiter()
500  {
501  $dbManager = M::mock(DbManager::class);
502  $userDao = M::mock(UserDao::class);
503  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
504 
505  $licenseCsvImport->setDelimiter('|');
506  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('|'));
507 
508  $licenseCsvImport->setDelimiter('<>');
509  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'delimiter'),is('<'));
510  }
511 
521  public function testSetEnclosure()
522  {
523  $dbManager = M::mock(DbManager::class);
524  $userDao = M::mock(UserDao::class);
525  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
526 
527  $licenseCsvImport->setEnclosure('|');
528  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure') ,is('|'));
529 
530  $licenseCsvImport->setEnclosure('<>');
531  assertThat(Reflectory::getObjectsProperty($licenseCsvImport,'enclosure'),is('<'));
532  }
533 
541  public function testHandleCsv()
542  {
543  $dbManager = M::mock(DbManager::class);
544  $userDao = M::mock(UserDao::class);
545  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
546 
547  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleCsv',
548  array(array('shortname','licensetype', 'foo', 'text', 'fullname', 'notes', 'spdx_id')));
549  assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'headrow'),
550  is(notNullValue()));
551 
552  $dbManager->shouldReceive('getSingleRow')
553  ->with(
554  'SELECT rf_shortname,rf_source,rf_pk,rf_risk FROM license_ref WHERE rf_md5=md5($1)',
555  anything())
556  ->andReturn(false);
557  $licenseRow = array(
558  "rf_shortname" => "licA",
559  "rf_licensetype" => "lictypeA",
560  "rf_fullname" => "liceA",
561  "rf_spdx_id" => null,
562  "rf_text" => "txA",
563  "rf_md5" => md5("txA"),
564  "rf_detector_type" => 1,
565  "rf_url" => '',
566  "rf_notes" => 'noteA',
567  "rf_source" => '',
568  "rf_risk" => 0,
569  "rf_external_id" => null,
570  );
571  $this->addLicenseInsertToDbManager($dbManager, $licenseRow, 101);
572  Reflectory::setObjectsProperty($licenseCsvImport, 'nkMap', array(
573  'licA' => false
574  ));
575  Reflectory::setObjectsProperty($licenseCsvImport, 'mdkMap', array(
576  md5('txA') => false
577  ));
578  Reflectory::invokeObjectsMethodnameWith($licenseCsvImport, 'handleCsv',
579  array(array('licA', 'lictypeA', 'bar', 'txA', 'liceA', 'noteA')));
580  assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'nkMap'),
581  is(array('licA' => 101)));
582  assertThat(Reflectory::getObjectsProperty($licenseCsvImport, 'mdkMap'),
583  is(array(md5('txA') => 101)));
584  }
585 
593  {
594  $dbManager = M::mock(DbManager::class);
595  $userDao = M::mock(UserDao::class);
596  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
597  $msg = $licenseCsvImport->handleFile('/tmp/thisFileNameShouldNotExists', 'csv');
598  assertThat($msg, is(equalTo(_('Internal error'))));
599  }
600 
608  {
609  $dbManager = M::mock(DbManager::class);
610  $userDao = M::mock(UserDao::class);
611  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
612  $msg = $licenseCsvImport->handleFile(__FILE__, 'csv');
613  assertThat($msg, startsWith( _('Error while parsing file')));
614  }
615 
623  public function testHandleFile()
624  {
625  $dbManager = M::mock(DbManager::class);
626  $userDao = M::mock(UserDao::class);
627  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
628  $filename = tempnam("/tmp", "FOO");
629  $handle = fopen($filename, 'w');
630  fwrite($handle, "shortname,fullname,text,spdx_id");
631  fclose($handle);
632  $msg = $licenseCsvImport->handleFile($filename, 'csv');
633  assertThat($msg, startsWith( _('head okay')));
634  unlink($filename);
635  }
636 
644  public function testSetMapTrue()
645  {
646  $testDb = new TestLiteDb();
647  $testDb->createPlainTables(array('license_ref', 'license_map'));
648  $licenseId = 101;
649  $parentId = 102;
650  $reportId = 103;
652  $dbManager = &$testDb->getDbManager();
653  $dbManager->insertTableRow('license_ref', array(
654  'rf_pk' => $licenseId,
655  'rf_shortname' => "Main License"
656  ));
657  $dbManager->insertTableRow('license_ref', array(
658  'rf_pk' => $parentId,
659  'rf_shortname' => "Parent License"
660  ));
661  $dbManager->insertTableRow('license_ref', array(
662  'rf_pk' => $reportId,
663  'rf_shortname' => "Reported License"
664  ));
665  $userDao = M::mock(UserDao::class);
666  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
667 
668  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
669  'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
670  equalTo(true));
671  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
672  'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
673  equalTo(true));
674 
675  $sql = "SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
676  $statement = __METHOD__ . ".getMap";
677  $row = $dbManager->getSingleRow($sql, array($licenseId,
678  LicenseMap::CONCLUSION), $statement);
679 
680  assertThat($row['rf_parent'], equalTo($parentId));
681  $row = $dbManager->getSingleRow($sql, array($licenseId,
682  LicenseMap::REPORT), $statement);
683  assertThat($row['rf_parent'], equalTo($reportId));
684  }
685 
694  public function testSetMapFalse()
695  {
696  $testDb = new TestLiteDb();
697  $testDb->createPlainTables(array('license_ref', 'license_map'));
698  $licenseId = 101;
699  $parentId = false;
700  $reportId = false;
702  $dbManager = &$testDb->getDbManager();
703  $dbManager->insertTableRow('license_ref', array(
704  'rf_pk' => $licenseId,
705  'rf_shortname' => "Main License"
706  ));
707  $userDao = M::mock(UserDao::class);
708  $licenseCsvImport = new LicenseCsvImport($dbManager, $userDao);
709 
710  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
711  'setMap', array($parentId, $licenseId, LicenseMap::CONCLUSION)),
712  equalTo(false));
713  assertThat(Reflectory::invokeObjectsMethodnameWith($licenseCsvImport,
714  'setMap', array($reportId, $licenseId, LicenseMap::REPORT)),
715  equalTo(false));
716 
717  $sql = "SELECT rf_parent FROM license_map WHERE rf_fk = $1 AND usage = $2;";
718  $statement = __METHOD__ . ".getMap";
719  $row = $dbManager->getSingleRow($sql, array($licenseId,
720  LicenseMap::CONCLUSION), $statement);
721 
722  assertThat($row, equalTo(false));
723  $row = $dbManager->getSingleRow($sql, array($licenseId,
724  LicenseMap::REPORT), $statement);
725  assertThat($row, equalTo(false));
726  }
727 
732  private function createCandidateTable($dbManager)
733  {
734  $sql = "CREATE TABLE license_candidate (" .
735  "rf_pk, rf_shortname, rf_fullname, rf_text, rf_md5, rf_url, rf_notes, " .
736  "marydone, rf_source, rf_risk, rf_detector_type, group_fk)";
737  $dbManager->queryOnce($sql);
738  }
739 
747  private function addLicenseInsertToDbManager(&$dbManager, $row, $return,
748  $table = "license_ref")
749  {
750  $dbManager->shouldReceive('insertTableRow')
751  ->with($table, $row, anything(), 'rf_pk')
752  ->once()
753  ->andReturn($return);
754  }
755 }
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.