FOSSology  4.7.1
Open Source License Compliance by Open Source Software
LicenseMapTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2014-2015 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
9 
13 
14 class LicenseMapTest extends \PHPUnit\Framework\TestCase
15 {
16  public $assertCountBefore;
17  public $testDb;
18 
21  private $dbManager;
24  private $groupId = 101;
25 
26 
27  protected function setUp() : void
28  {
29  $this->testDb = new TestPgDb();
30  $this->testDb->createPlainTables(
31  array(
32  'license_ref',
33  'license_map',
34  'obligation_ref',
35  'obligation_map',
36  'obligation_candidate_map'
37  ));
38  $this->dbManager = $this->testDb->getDbManager();
39  $this->dbManager->queryOnce("CREATE TABLE license_candidate (group_fk integer) INHERITS (license_ref)");
40  $this->dbManager->insertTableRow('license_map',array('license_map_pk'=>0,'rf_fk'=>2,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
41  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1','rf_spdx_id'=>'One'));
42  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>2,'rf_shortname'=>'Two','rf_fullname'=>'Two-2','rf_spdx_id'=>'Two'));
43  $this->dbManager->insertTableRow('license_candidate',
44  array('rf_pk'=>3,'rf_shortname'=>'Three','rf_fullname'=>'Three-3','group_fk'=>$this->groupId,'rf_spdx_id'=>'Three'));
45  $this->dbManager->insertTableRow('obligation_ref',
46  array(
47  'ob_pk' => 2,
48  'ob_type' => 'Obligation',
49  'ob_topic' => 'Obligation-1',
50  'ob_text' => 'Obligation text',
51  'ob_classification' => 'white',
52  'ob_modifications' => 'Yes',
53  'ob_comment' => 'Obligation comment',
54  'ob_active' => true,
55  'ob_text_updatable' => false,
56  'ob_md5' => '0ffdddc657a16b95894437b4af736102'
57  ));
58  $this->dbManager->insertTableRow('obligation_map',
59  array(
60  'om_pk' => 2,
61  'ob_fk' => 2,
62  'rf_fk' => 2
63  ));
64  $this->dbManager->insertTableRow('obligation_candidate_map',
65  array(
66  'om_pk' => 2,
67  'ob_fk' => 2,
68  'rf_fk' => 3
69  ));
70  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
71  }
72 
73  protected function tearDown() : void
74  {
75  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
76  }
77 
78  function testProjectedIdOfUnmappedIdIsIdItself()
79  {
80  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
81  $licenseId = 1;
82  assertThat($licenseMap->getProjectedId($licenseId),is($licenseId));
83  }
84 
85  function testProjectedIdOfCandidatesAreRecognized()
86  {
87  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
88  $licenseId = 3;
89  assertThat($licenseMap->getProjectedId($licenseId),is($licenseId));
90  }
91 
92  function testProjectedIdOfUnmappedIdIsParentId()
93  {
94  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
95  $licenseMap->getGroupId();
96  assertThat($licenseMap->getProjectedId(2),is(1));
97  }
98 
99  function testProjectedShortNameOfMappedId()
100  {
101  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
102  assertThat($licenseMap->getProjectedShortName(2),is('One'));
103  }
104 
105  function testObligationForLicense()
106  {
107  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
108  assertThat($licenseMap->getObligationsForLicenseRef(2), contains(2));
109  }
110 
111  function testObligationForUnassignedLicense()
112  {
113  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
114  assertThat($licenseMap->getObligationsForLicenseRef(1), is(emptyArray()));
115  }
116 
117  function testObligationForCandidateLicense()
118  {
119  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
120  assertThat($licenseMap->getObligationsForLicenseRef(3, true), contains(2));
121  }
122 
123  function testProjectedIdOfMappedIdIsIdItselfIfTrivialMap()
124  {
125  $licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::TRIVIAL);
126  assertThat($licenseMap->getProjectedId(2),is(2));
127  }
128 
129  function testGetTopLevelLicenseRefs()
130  {
131  $licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::CONCLUSION);
132  $topLevelLicenses = $licenseMap->getTopLevelLicenseRefs();
133  assertThat($topLevelLicenses,hasItemInArray(new LicenseRef(1,'One','One-1', 'One')));
134  assertThat($topLevelLicenses, not(hasKeyInArray(2)));
135  }
136 
137 
138  public function testGetMappedLicenseRefView()
139  {
140  $this->testDb = new TestPgDb();
141  $this->testDb->createPlainTables(array('license_ref','license_map'));
142  $this->dbManager = $this->testDb->getDbManager();
143  $this->dbManager->queryOnce("CREATE TABLE license_candidate (group_fk integer) INHERITS (license_ref)");
144  $this->dbManager->insertTableRow('license_map',array('license_map_pk'=>0,'rf_fk'=>2,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
145  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1','rf_spdx_id'=>'One'));
146  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>2,'rf_shortname'=>'Two','rf_fullname'=>'Two-2','rf_spdx_id'=>'Two'));
147  $this->dbManager->insertTableRow('license_candidate',
148  array('rf_pk'=>3,'rf_shortname'=>'Three','rf_fullname'=>'Three-3','group_fk'=>$this->groupId,'rf_spdx_id'=>'Three'));
149  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
150 
151  $view = LicenseMap::getMappedLicenseRefView(LicenseMap::CONCLUSION);
152  $stmt = __METHOD__;
153  $this->dbManager->prepare($stmt,$view);
154  $res = $this->dbManager->execute($stmt);
155  $map = $this->dbManager->fetchAll($res);
156  $this->dbManager->freeResult($res);
157  assertThat($map,is(arrayWithSize(3)));
158  $expected = array(
159  array('rf_origin'=>1, 'rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1','rf_spdx_id'=>'One'),
160  array('rf_origin'=>2, 'rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1','rf_spdx_id'=>'One'),
161  array('rf_origin'=>3, 'rf_pk'=>3,'rf_shortname'=>'Three','rf_fullname'=>'Three-3','rf_spdx_id'=>'Three')
162  );
163  assertThat($map,containsInAnyOrder($expected));
164  }
165 
166  public function testFullMap()
167  {
168  $licenseMap = new LicenseMap($this->dbManager, $this->groupId+1, LicenseMap::CONCLUSION, true);
169  $map = \Fossology\Lib\Test\Reflectory::getObjectsProperty($licenseMap, 'map');
170  assertThat($map,hasItemInArray(array('rf_fk'=>1,'parent_shortname'=>'One','parent_spdx_id'=>'One','rf_parent'=>1,'parent_fullname'=>'One-1')));
171  assertThat($map,hasItemInArray(array('rf_fk'=>2,'parent_shortname'=>'One','parent_spdx_id'=>'One','rf_parent'=>1,'parent_fullname'=>'One-1')));
172  }
173 
174  public function testFullMapWithCandidates()
175  {
176  $licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::CONCLUSION, true);
177  $map = \Fossology\Lib\Test\Reflectory::getObjectsProperty($licenseMap, 'map');
178  assertThat($map,hasItemInArray(array('rf_fk'=>1,'parent_shortname'=>'One','parent_spdx_id'=>'One','rf_parent'=>1,'parent_fullname'=>'One-1')));
179  assertThat($map,hasItemInArray(array('rf_fk'=>2,'parent_shortname'=>'One','parent_spdx_id'=>'One','rf_parent'=>1,'parent_fullname'=>'One-1')));
180  assertThat($map,hasItemInArray(array('rf_fk'=>3,'parent_shortname'=>'Three','parent_spdx_id'=>'Three','rf_parent'=>3,'parent_fullname'=>'Three-3')));
181  }
182 }
static getMappedLicenseRefView($usageExpr=' $1')
Query to get license map view along with license ref.
Definition: LicenseMap.php:191
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16
Contains business rules for FOSSology.