24 private $assertCountBefore;
35 private $tmpFiles = [];
37 protected function setUp():
void
39 $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
42 $GLOBALS[
'SysConf'][
'auth'][
'UserId'] = 1;
43 $GLOBALS[
'SysConf'][
'auth'][
'GroupId'] = 1;
45 $this->
dbManager = M::mock(DbManager::class);
46 $this->
dbManager->shouldReceive(
'begin')->andReturnNull();
47 $this->
dbManager->shouldReceive(
'commit')->andReturnNull();
48 $this->
dbManager->shouldReceive(
'rollback')->andReturnNull();
49 $this->userDao = M::mock(UserDao::class);
50 $this->licenseDao = M::mock(LicenseDao::class);
59 protected function tearDown():
void
61 $this->addToAssertionCount(
62 \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore
64 foreach ($this->tmpFiles as $f) {
65 if (file_exists($f)) {
72 private function makeLicense(
int $id):
object
74 $lic = M::mock(License::class);
75 $lic->shouldReceive(
'getId')->andReturn($id);
79 private function writeTempFile(
string $content,
string $ext):
string
81 $path = tempnam(sys_get_temp_dir(),
'cti_test_') .
'.' . $ext;
82 file_put_contents($path, $content);
83 $this->tmpFiles[] = $path;
90 $prop = new \ReflectionProperty(CustomTextImport::class,
'unescapeNewlines');
91 $prop->setAccessible(
true);
92 $prop->setValue($this->importer,
true);
104 'Text' =>
'some scanning text',
105 'Is Active' =>
'true',
106 'License Shortname' =>
'MIT',
107 'Removing' =>
'false',
108 'Comment' =>
'a comment',
109 'License Text' =>
'license text here',
110 'Acknowledgement' =>
'ack text',
113 $mapped = Reflectory::invokeObjectsMethodnameWith(
114 $this->importer,
'mapHeaders', [$row]
117 $this->assertSame(
'some scanning text', $mapped[
'text']);
118 $this->assertIsArray($mapped[
'licenses']);
119 $this->assertCount(1, $mapped[
'licenses']);
120 $this->assertSame(
'MIT', $mapped[
'licenses'][0][
'shortname']);
121 $this->assertFalse($mapped[
'licenses'][0][
'removing']);
122 $this->assertSame(
'license text here', $mapped[
'licenses'][0][
'reportinfo']);
123 $this->assertSame(
'ack text', $mapped[
'licenses'][0][
'acknowledgement']);
136 'Text' =>
'phrase',
'License Shortname' =>
'MIT',
'Removing' =>
'1',
139 $mapped = Reflectory::invokeObjectsMethodnameWith(
140 $this->importer,
'mapHeaders', [$row]
143 $this->assertTrue($mapped[
'licenses'][0][
'removing']);
154 [
'shortname' =>
'Apache-2.0',
'removing' =>
false,
155 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
''],
156 [
'shortname' =>
'MIT',
'removing' =>
true,
157 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
''],
159 $row = [
'text' =>
'json phrase',
'licenses' => $licenses];
161 $mapped = Reflectory::invokeObjectsMethodnameWith(
162 $this->importer,
'mapHeaders', [$row]
165 $this->assertCount(2, $mapped[
'licenses']);
166 $this->assertSame(
'Apache-2.0', $mapped[
'licenses'][0][
'shortname']);
167 $this->assertSame(
'MIT', $mapped[
'licenses'][1][
'shortname']);
179 $row = [
'text' =>
"line one\\nline two\\nline three"];
181 $mapped = Reflectory::invokeObjectsMethodnameWith(
182 $this->importer,
'mapHeaders', [$row]
185 $this->assertSame(
"line one\nline two\nline three", $mapped[
'text']);
199 $row = [
'text' =>
"printf(\"hi\\\\n\");"];
201 $mapped = Reflectory::invokeObjectsMethodnameWith(
202 $this->importer,
'mapHeaders', [$row]
205 $this->assertSame(
"printf(\"hi\\\\n\");", $mapped[
'text']);
220 'shortname' =>
'MIT',
222 'comment' =>
"line1\\nline2",
223 'reportinfo' =>
"a\\nb",
224 'acknowledgement' =>
"x\\ny",
228 $mapped = Reflectory::invokeObjectsMethodnameWith(
229 $this->importer,
'mapHeaders', [$row]
232 $this->assertSame(
"line1\nline2", $mapped[
'licenses'][0][
'comment']);
233 $this->assertSame(
"a\nb", $mapped[
'licenses'][0][
'reportinfo']);
234 $this->assertSame(
"x\ny", $mapped[
'licenses'][0][
'acknowledgement']);
246 $result = Reflectory::invokeObjectsMethodnameWith(
247 $this->importer,
'parseBoolean', [$input]
249 $this->assertSame($expected, $result);
252 public static function parseBooleanProvider(): array
280 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
281 ->with(
'CAND-1.0', 7)
283 ->andReturn($this->makeLicense(50));
285 $this->
dbManager->shouldReceive(
'getSingleRow')->andReturn(
null);
286 $this->
dbManager->shouldReceive(
'insertTableRow')->once();
288 $result = Reflectory::invokeObjectsMethodnameWith(
290 'associateLicensesWithMetadata',
291 [1, [[
'shortname' =>
'CAND-1.0',
'removing' =>
false,
292 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']], 7]
295 $this->assertSame(1, $result[
'inserted']);
309 $this->
dbManager->shouldReceive(
'getSingleRow')
310 ->withArgs(
function ($sql, $params, $name) use (&$seenNames) {
311 $seenNames[] = $name;
315 $this->
dbManager->shouldReceive(
'insertTableRow')->twice();
317 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
318 ->with(
'MIT',
null)->andReturn($this->makeLicense(1));
319 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
320 ->with(
'Apache-2.0',
null)->andReturn($this->makeLicense(2));
322 Reflectory::invokeObjectsMethodnameWith(
324 'associateLicensesWithMetadata',
326 [
'shortname' =>
'MIT',
'removing' =>
false,
'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
''],
327 [
'shortname' =>
'Apache-2.0',
'removing' =>
false,
'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
''],
331 $this->assertCount(1, array_unique($seenNames));
343 $this->
dbManager->shouldReceive(
'getSingleRow')->andReturn([
'exists' => 1]);
344 $this->
dbManager->shouldNotReceive(
'insertTableRow');
346 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
347 ->with(
'MIT',
null)->andReturn($this->makeLicense(1));
349 $result = Reflectory::invokeObjectsMethodnameWith(
351 'associateLicensesWithMetadata',
352 [1, [[
'shortname' =>
'MIT',
'removing' =>
false,
'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]]
355 $this->assertSame(0, $result[
'inserted']);
356 $this->assertSame(1, $result[
'skipped']);
366 $this->
dbManager->shouldReceive(
'getSingleRow')->andReturn(
null);
367 $this->
dbManager->shouldReceive(
'insertTableRow')
369 ->with(
'custom_phrase_license_map', M::on(
function ($row) {
370 return $row[
'comment'] ===
null && $row[
'reportinfo'] ===
null && $row[
'acknowledgement'] ===
null;
373 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
374 ->with(
'MIT',
null)->andReturn($this->makeLicense(1));
376 $result = Reflectory::invokeObjectsMethodnameWith(
378 'associateLicensesWithMetadata',
379 [1, [[
'shortname' =>
'MIT']]]
382 $this->assertSame(1, $result[
'inserted']);
391 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
392 ->with(
'GPL-2.0-only',
null)->once()->andReturn($this->makeLicense(7));
393 $this->licenseDao->shouldNotReceive(
'insertLicense');
395 $this->
dbManager->shouldReceive(
'getSingleRow')->andReturn(
null);
396 $this->
dbManager->shouldReceive(
'insertTableRow')->once();
398 $result = Reflectory::invokeObjectsMethodnameWith(
399 $this->importer,
'associateLicenseNames', [42,
'GPL-2.0-only',
false]
402 $this->assertSame(1, $result[
'inserted']);
403 $this->assertEmpty($result[
'failed']);
412 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
413 ->with(
'MIT',
null)->once()->andReturn($this->makeLicense(1));
414 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
415 ->with(
'Apache-2.0',
null)->once()->andReturn($this->makeLicense(2));
417 $this->
dbManager->shouldReceive(
'getSingleRow')->andReturn(
null);
418 $this->
dbManager->shouldReceive(
'insertTableRow')->twice();
420 $result = Reflectory::invokeObjectsMethodnameWith(
421 $this->importer,
'associateLicenseNames', [46,
'MIT, Apache-2.0',
false]
424 $this->assertSame(2, $result[
'inserted']);
433 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
434 ->with(
'NoSuchLicense',
null)->once()->andReturn(
null);
436 $result = Reflectory::invokeObjectsMethodnameWith(
437 $this->importer,
'associateLicenseNames', [1,
'NoSuchLicense',
false]
440 $this->assertSame(0, $result[
'inserted']);
441 $this->assertStringContainsString(
'unknown', $result[
'failed'][0]);
451 $this->
dbManager->shouldNotReceive(
'getSingleRow');
452 $this->
dbManager->shouldNotReceive(
'insertPreparedAndReturn');
454 $result = Reflectory::invokeObjectsMethodnameWith(
455 $this->importer,
'importSinglePhrase', [[
'text' =>
'']]
458 $this->assertFalse($result[
'success']);
459 $this->assertStringContainsStringIgnoringCase(
'required', $result[
'message']);
471 $this->
dbManager->shouldNotReceive(
'getSingleRow');
472 $this->
dbManager->shouldNotReceive(
'begin');
474 $result = Reflectory::invokeObjectsMethodnameWith(
475 $this->importer,
'importSinglePhrase', [[
'text' => [
'not',
'a',
'string']]]
478 $this->assertFalse($result[
'success']);
479 $this->assertStringContainsStringIgnoringCase(
'required', $result[
'message']);
490 $this->
dbManager->shouldReceive(
'getSingleRow')
491 ->with(M::pattern(
'/INSERT INTO custom_phrase/'), M::any(), M::any())
492 ->once()->andReturn([
'cp_pk' => 11]);
493 $this->
dbManager->shouldNotReceive(
'insertTableRow');
494 $this->licenseDao->shouldNotReceive(
'getLicenseByShortName');
496 $result = Reflectory::invokeObjectsMethodnameWith(
498 'importSinglePhrase',
500 'text' =>
'phrase with a malformed license entry',
501 'licenses' => [[
'shortname' => [
'nested',
'array'],
'removing' =>
false]]
505 $this->assertTrue($result[
'success']);
515 $this->
dbManager->shouldReceive(
'getSingleRow')
516 ->with(M::pattern(
'/INSERT INTO custom_phrase/'), M::any(), M::any())
518 ->withArgs(
function ($sql, $params) {
519 return $params[4] ===
'false';
521 ->andReturn([
'cp_pk' => 12]);
523 $result = Reflectory::invokeObjectsMethodnameWith(
524 $this->importer,
'importSinglePhrase', [[
'text' =>
'malformed is_active',
'is_active' => [
'x']]]
527 $this->assertTrue($result[
'success']);
539 $this->
dbManager->shouldReceive(
'getSingleRow')
540 ->andReturnUsing(
function ($sql) use (&$insertAttempt) {
541 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
543 return [
'cp_pk' => $insertAttempt];
549 [
'text' =>
'row one valid'],
550 [
'text' => [
'row',
'two',
'malformed']],
551 [
'text' =>
'row three valid'],
555 $result = $this->importer->importJsonData($data, $msg);
557 $this->assertStringContainsString(
'2 phrase(s) created', $result);
558 $this->assertStringContainsString(
'Row 2', $result);
568 $this->
dbManager->shouldReceive(
'getSingleRow')
569 ->with(M::pattern(
'/INSERT INTO custom_phrase/'), M::any(), M::any())
570 ->once()->andReturn([
'cp_pk' => 10]);
572 $result = Reflectory::invokeObjectsMethodnameWith(
573 $this->importer,
'importSinglePhrase', [[
'text' =>
'brand new unique phrase']]
576 $this->assertTrue($result[
'success']);
577 $this->assertEmpty($result[
'existing'] ??
'');
578 $this->assertEmpty($result[
'unchanged'] ??
'');
589 $this->
dbManager->shouldReceive(
'getSingleRow')
590 ->andReturnUsing(
function ($sql) {
591 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
594 if (strpos($sql,
'text_md5') !==
false) {
595 return [
'cp_pk' => 7];
599 $this->
dbManager->shouldReceive(
'insertTableRow')->once();
600 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
601 ->with(
'Apache-2.0', 1)->once()->andReturn($this->makeLicense(20));
603 $result = Reflectory::invokeObjectsMethodnameWith(
605 'importSinglePhrase',
607 'text' =>
'existing phrase',
608 'licenses' => [[
'shortname' =>
'Apache-2.0',
'removing' =>
false,
609 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]
613 $this->assertTrue($result[
'success']);
614 $this->assertTrue($result[
'existing']);
626 $this->
dbManager->shouldReceive(
'getSingleRow')
627 ->andReturnUsing(
function ($sql) {
628 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
631 if (strpos($sql,
'text_md5') !==
false) {
632 return [
'cp_pk' => 7];
634 return [
'exists' => 1];
636 $this->
dbManager->shouldNotReceive(
'insertTableRow');
637 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
638 ->with(
'MIT', 1)->once()->andReturn($this->makeLicense(5));
640 $result = Reflectory::invokeObjectsMethodnameWith(
642 'importSinglePhrase',
644 'text' =>
'existing phrase',
645 'licenses' => [[
'shortname' =>
'MIT',
'removing' =>
false,
646 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]
650 $this->assertTrue($result[
'success']);
651 $this->assertTrue($result[
'unchanged']);
652 $this->assertArrayNotHasKey(
'existing', $result);
662 $this->
dbManager->shouldReceive(
'getSingleRow')
663 ->andReturnUsing(
function ($sql) {
664 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
667 return [
'cp_pk' => 3];
670 $result = Reflectory::invokeObjectsMethodnameWith(
671 $this->importer,
'importSinglePhrase', [[
'text' =>
'existing phrase, no new license']]
674 $this->assertTrue($result[
'success']);
675 $this->assertTrue($result[
'unchanged']);
685 $this->
dbManager->shouldReceive(
'getSingleRow')
686 ->andReturnUsing(
function ($sql) {
687 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
690 return [
'cp_pk' => 3];
692 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
693 ->with(
'NoSuchLicense', 1)->once()->andReturn(
null);
695 $result = Reflectory::invokeObjectsMethodnameWith(
697 'importSinglePhrase',
699 'text' =>
'existing phrase',
700 'licenses' => [[
'shortname' =>
'NoSuchLicense',
'removing' =>
false,
701 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]
705 $this->assertFalse($result[
'success']);
706 $this->assertStringContainsString(
'NoSuchLicense', $result[
'message']);
719 $this->
dbManager->shouldReceive(
'getSingleRow')
720 ->andReturnUsing(
function ($sql) use (&$insertAttempt) {
721 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
723 return $insertAttempt === 1 ? [
'cp_pk' => 1] :
false;
725 if (strpos($sql,
'text_md5') !==
false) {
726 return [
'cp_pk' => 1];
730 $this->
dbManager->shouldReceive(
'insertTableRow')->twice();
732 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
733 ->with(
'MIT', 1)->andReturn($this->makeLicense(5));
734 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
735 ->with(
'Apache-2.0', 1)->andReturn($this->makeLicense(6));
738 [
'text' =>
'shared phrase',
'licenses' =>
739 [[
'shortname' =>
'MIT',
'removing' =>
false,
740 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]],
741 [
'text' =>
'shared phrase',
'licenses' =>
742 [[
'shortname' =>
'Apache-2.0',
'removing' =>
false,
743 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]],
747 $result = $this->importer->importJsonData($data, $msg);
749 $this->assertStringContainsString(
'1 phrase(s) created', $result);
750 $this->assertStringContainsString(
'1 license(s) added to existing phrase(s)', $result);
762 $this->
dbManager->shouldReceive(
'getSingleRow')
763 ->andReturnUsing(
function ($sql) {
764 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
767 return [
'cp_pk' => 5];
770 $data = [[
'text' =>
'already exists, no license']];
772 $result = $this->importer->importJsonData($data, $msg);
774 $this->assertStringContainsString(
'1 row(s) already up to date', $result);
775 $this->assertStringNotContainsString(
'Errors:', $result);
786 $result = $this->importer->importJsonData([], $msg);
788 $this->assertStringContainsString(
'nothing new to import', $result);
798 $this->
dbManager->shouldReceive(
'getSingleRow')
799 ->with(M::pattern(
'/INSERT INTO custom_phrase/'), M::any(), M::any())
800 ->once()->andReturn([
'cp_pk' => 2]);
803 [
'text' =>
'valid phrase'],
808 $result = $this->importer->importJsonData($data, $msg);
810 $this->assertStringContainsString(
'1 phrase(s) created', $result);
811 $this->assertStringContainsString(
'Row 2', $result);
823 $this->
dbManager->shouldReceive(
'getSingleRow')
824 ->with(M::pattern(
'/INSERT INTO custom_phrase/'), M::any(), M::any())
826 ->andReturn([
'cp_pk' => 1]);
827 $this->
dbManager->shouldReceive(
'getSingleRow')
828 ->with(M::pattern(
'/custom_phrase_license_map/'), M::any(), M::any())
831 $this->
dbManager->shouldReceive(
'insertTableRow')->once();
832 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
833 ->with(
'MIT', 1)->twice()->andReturn($this->makeLicense(1));
835 $data = [[
'text' =>
'idempotent phrase',
'licenses' =>
836 [[
'shortname' =>
'MIT',
'removing' =>
false,
'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]]];
839 $result1 = $this->importer->importJsonData($data, $msg1);
840 $this->assertStringContainsString(
'1 phrase(s) created', $result1);
843 $this->
dbManager->shouldReceive(
'getSingleRow')
844 ->with(M::pattern(
'/INSERT INTO custom_phrase/'), M::any(), M::any())
847 $this->
dbManager->shouldReceive(
'getSingleRow')
848 ->with(M::pattern(
'/^SELECT cp_pk FROM custom_phrase WHERE text_md5/'), M::any(), M::any())
850 ->andReturn([
'cp_pk' => 1]);
851 $this->
dbManager->shouldReceive(
'getSingleRow')
852 ->with(M::pattern(
'/custom_phrase_license_map/'), M::any(), M::any())
854 ->andReturn([
'exists' => 1]);
857 $result2 = $this->importer->importJsonData($data, $msg2);
859 $this->assertStringContainsString(
'1 row(s) already up to date', $result2);
860 $this->assertStringNotContainsString(
'phrase(s) created', $result2);
861 $this->assertStringNotContainsString(
'license(s) added', $result2);
871 $bom = chr(0xEF) . chr(0xBB) . chr(0xBF);
872 $csv = $bom .
"Text,Is Active,License Shortname,Removing,Comment,License Text,Acknowledgement\n";
873 $csv .=
'"simple scanning phrase",true,MIT,false,,,';
875 $path = $this->writeTempFile($csv,
'csv');
877 $this->
dbManager->shouldReceive(
'getSingleRow')
878 ->with(M::pattern(
'/INSERT INTO custom_phrase/'), M::any(), M::any())
879 ->once()->andReturn([
'cp_pk' => 1]);
880 $this->
dbManager->shouldReceive(
'getSingleRow')
881 ->with(M::pattern(
'/custom_phrase_license_map/'), M::any(), M::any())
882 ->once()->andReturn(
null);
883 $this->
dbManager->shouldReceive(
'insertTableRow')->once();
885 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
886 ->with(
'MIT', 1)->once()->andReturn($this->makeLicense(3));
888 $result = $this->importer->handleFile($path,
'csv');
890 $this->assertStringContainsString(
'1 phrase(s) created', $result);
903 $text =
"Licensed under any of the following licenses.";
904 $csv =
"Text,Is Active,License Shortname,Removing,Comment,License Text,Acknowledgement\n";
905 $csv .=
"\"$text\",true,GPL-2.0-or-later,false,,,\n";
906 $csv .=
"\"$text\",true,LGPL-2.1-or-later,true,,,\n";
907 $csv .=
"\"$text\",true,MPL-1.1+,true,,,\n";
909 $path = $this->writeTempFile($csv,
'csv');
912 $this->
dbManager->shouldReceive(
'getSingleRow')
913 ->andReturnUsing(
function ($sql) use (&$insertAttempt) {
914 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
916 return $insertAttempt === 1 ? [
'cp_pk' => 99] :
false;
918 if (strpos($sql,
'text_md5') !==
false) {
919 return [
'cp_pk' => 99];
923 $this->
dbManager->shouldReceive(
'insertTableRow')->times(3);
925 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
926 ->with(
'GPL-2.0-or-later', 1)->andReturn($this->makeLicense(10));
927 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
928 ->with(
'LGPL-2.1-or-later', 1)->andReturn($this->makeLicense(11));
929 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
930 ->with(
'MPL-1.1+', 1)->andReturn($this->makeLicense(12));
932 $result = $this->importer->handleFile($path,
'csv');
934 $this->assertStringContainsString(
'1 phrase(s) created', $result);
935 $this->assertStringContainsString(
'2 license(s) added to existing phrase(s)', $result);
945 $csv =
"Text,Is Active,License Shortname\n";
946 $csv .=
"only two columns,true\n";
948 $path = $this->writeTempFile($csv,
'csv');
950 $this->
dbManager->shouldNotReceive(
'insertPreparedAndReturn');
952 $result = $this->importer->handleFile($path,
'csv');
954 $this->assertStringContainsStringIgnoringCase(
'column', $result);
966 $csv =
"Text,Is Active,License Shortname,Removing,Comment,License Text,Acknowledgement\n";
967 $csv .=
"\"line one\\nline two\",true,MIT,false,,,\n";
969 $path = $this->writeTempFile($csv,
'csv');
971 $capturedText =
null;
972 $this->
dbManager->shouldReceive(
'getSingleRow')
973 ->andReturnUsing(
function ($sql, $params) use (&$capturedText) {
974 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
975 $capturedText = $params[0];
976 return [
'cp_pk' => 1];
980 $this->
dbManager->shouldReceive(
'insertTableRow')->once();
981 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
982 ->with(
'MIT', 1)->once()->andReturn($this->makeLicense(1));
984 $this->importer->handleFile($path,
'csv');
986 $this->assertSame(
"line one\nline two", $capturedText);
997 [
'text' =>
'first phrase',
'is_active' =>
true,
'licenses' =>
998 [[
'shortname' =>
'MIT',
'removing' =>
false,
999 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]],
1000 [
'text' =>
'second phrase',
'is_active' =>
true,
'licenses' => []],
1003 $path = $this->writeTempFile(json_encode($data),
'json');
1006 $this->
dbManager->shouldReceive(
'getSingleRow')
1007 ->andReturnUsing(
function ($sql) use (&$insertAttempt) {
1008 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
1010 return [
'cp_pk' => $insertAttempt];
1014 $this->
dbManager->shouldReceive(
'insertTableRow')->once();
1016 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
1017 ->with(
'MIT', 1)->once()->andReturn($this->makeLicense(7));
1019 $result = $this->importer->handleFile($path,
'json');
1021 $this->assertStringContainsString(
'2 phrase(s) created', $result);
1031 $path = $this->writeTempFile(
'{not valid json[',
'json');
1033 $result = $this->importer->handleFile($path,
'json');
1035 $this->assertStringContainsStringIgnoringCase(
'invalid json', $result);
1045 $path = $this->writeTempFile(
'"just a string"',
'json');
1047 $result = $this->importer->handleFile($path,
'json');
1049 $this->assertStringContainsStringIgnoringCase(
'array', $result);
1061 [
'text' =>
'repeat phrase',
'licenses' =>
1062 [[
'shortname' =>
'MIT',
'removing' =>
false,
1063 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]],
1064 [
'text' =>
'repeat phrase',
'licenses' =>
1065 [[
'shortname' =>
'Apache-2.0',
'removing' =>
false,
1066 'comment' =>
'',
'reportinfo' =>
'',
'acknowledgement' =>
'']]],
1069 $path = $this->writeTempFile(json_encode($data),
'json');
1072 $this->
dbManager->shouldReceive(
'getSingleRow')
1073 ->andReturnUsing(
function ($sql) use (&$insertAttempt) {
1074 if (strpos($sql,
'INSERT INTO custom_phrase') !==
false) {
1076 return $insertAttempt === 1 ? [
'cp_pk' => 20] :
false;
1078 if (strpos($sql,
'text_md5') !==
false) {
1079 return [
'cp_pk' => 20];
1083 $this->
dbManager->shouldReceive(
'insertTableRow')->twice();
1085 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
1086 ->with(
'MIT', 1)->andReturn($this->makeLicense(1));
1087 $this->licenseDao->shouldReceive(
'getLicenseByShortName')
1088 ->with(
'Apache-2.0', 1)->andReturn($this->makeLicense(2));
1090 $result = $this->importer->handleFile($path,
'json');
1092 $this->assertStringContainsString(
'1 phrase(s) created', $result);
1093 $this->assertStringContainsString(
'1 license(s) added to existing phrase(s)', $result);
Test for class CustomTextImport.
testReimportingSameDataIsIdempotentAndReportedAsUnchanged()
testHandleCsvFileCreatesSinglePhrase()
testParseBoolean($input, bool $expected)
testAssociateLicenseNamesLooksUpSpdxNameDirectly()
testAssociateLicenseNamesReportsUnknownLicense()
testHandleCsvFileMultiLicenseSameTextUpdatesExisting()
testHandleCsvFileRejectsColumnCountMismatch()
testMapHeadersParsesRemovingLeniently()
testHandleJsonFileSameTextTwiceAddsLicenseToExisting()
testImportPhrasesReportsUnchangedRowsSeparately()
testImportSinglePhraseOnDuplicateTextWithNoLicenseIsUnchanged()
testHandleJsonFileRejectsNonArrayRoot()
testImportSinglePhraseOnDuplicateTextWithNewLicenseAddsToExisting()
testMapHeadersBuildsLicensesArrayFromFlatCsvRow()
testAssociateLicensesHandlesMissingMetadataKeysWithoutWarning()
testImportPhrasesEmptyDataReportsNothingNewToImport()
testHandleJsonFileRejectsInvalidJson()
testMapHeadersUnescapesLicenseMetadataForCsvSource()
testImportPhrasesProducesCorrectCountMessage()
testImportSinglePhraseCreatesNewPhrase()
testMapHeadersRestoresEscapedNewlinesForCsvSource()
testImportSinglePhraseRejectsEmptyText()
testImportSinglePhraseOnDuplicateTextWithAlreadyMappedLicenseIsUnchanged()
testMapHeadersDoesNotUnescapeForJsonSource()
testAssociateLicensesUsesConstantStatementNameForMappingCheck()
testImportPhrasesReportsErrorRowsAlongSideSuccesses()
testImportSinglePhraseRejectsArrayText()
testImportSinglePhraseOnDuplicateTextWithUnknownLicenseFails()
testImportSinglePhraseSkipsLicenseEntryWithArrayShortname()
testAssociateLicensesCountsExistingMappingAsSkippedNotInserted()
testHandleCsvFileUnescapesNewlinesInText()
testHandleJsonFileCreatesTwoPhrases()
testMapHeadersPassesThroughNestedLicensesArray()
testImportPhrasesOneMalformedRowDoesNotAbortRemainingRows()
testAssociateLicenseNamesHandlesCommaSeparatedList()
testAssociateLicensesPassesGroupIdForCandidateResolution()
testImportSinglePhraseTreatsArrayIsActiveAsFalse()
Import custom text phrases from CSV/JSON.
fo_dbManager * dbManager
fo_dbManager object
Utility functions for specific applications.