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