FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseMapTest.php
1 <?php
2 /*
3 Copyright (C) 2014-2015, Siemens AG
4 
5 This program is free software; you can redistribute it and/or
6 modify it under the terms of the GNU General Public License
7 version 2 as published by the Free Software Foundation.
8 
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
13 
14 You should have received a copy of the GNU General Public License along
15 with this program; if not, write to the Free Software Foundation, Inc.,
16 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 */
18 
20 
24 
25 class LicenseMapTest extends \PHPUnit\Framework\TestCase
26 {
29  private $dbManager;
32  private $groupId = 101;
33 
34 
35  protected function setUp()
36  {
37  $this->testDb = new TestPgDb();
38  $this->testDb->createPlainTables(
39  array(
40  'license_ref',
41  'license_map',
42  'obligation_ref',
43  'obligation_map',
44  'obligation_candidate_map'
45  ));
46  $this->dbManager = $this->testDb->getDbManager();
47  $this->dbManager->queryOnce("CREATE TABLE license_candidate (group_fk integer) INHERITS (license_ref)");
48  $this->dbManager->insertTableRow('license_map',array('license_map_pk'=>0,'rf_fk'=>2,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
49  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1'));
50  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>2,'rf_shortname'=>'Two','rf_fullname'=>'Two-2'));
51  $this->dbManager->insertTableRow('license_candidate',
52  array('rf_pk'=>3,'rf_shortname'=>'Three','rf_fullname'=>'Three-3','group_fk'=>$this->groupId));
53  $this->dbManager->insertTableRow('obligation_ref',
54  array(
55  'ob_pk' => 2,
56  'ob_type' => 'Obligation',
57  'ob_topic' => 'Obligation-1',
58  'ob_text' => 'Obligation text',
59  'ob_classification' => 'white',
60  'ob_modifications' => 'Yes',
61  'ob_comment' => 'Obligation comment',
62  'ob_active' => true,
63  'ob_text_updatable' => false,
64  'ob_md5' => '0ffdddc657a16b95894437b4af736102'
65  ));
66  $this->dbManager->insertTableRow('obligation_map',
67  array(
68  'om_pk' => 2,
69  'ob_fk' => 2,
70  'rf_fk' => 2
71  ));
72  $this->dbManager->insertTableRow('obligation_candidate_map',
73  array(
74  'om_pk' => 2,
75  'ob_fk' => 2,
76  'rf_fk' => 3
77  ));
78  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
79  }
80 
81  protected function tearDown()
82  {
83  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount()-$this->assertCountBefore);
84  }
85 
86  function testProjectedIdOfUnmappedIdIsIdItself()
87  {
88  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
89  $licenseId = 1;
90  assertThat($licenseMap->getProjectedId($licenseId),is($licenseId));
91  }
92 
93  function testProjectedIdOfCandidatesAreRecognized()
94  {
95  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
96  $licenseId = 3;
97  assertThat($licenseMap->getProjectedId($licenseId),is($licenseId));
98  }
99 
100  function testProjectedIdOfUnmappedIdIsParentId()
101  {
102  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
103  $licenseMap->getGroupId();
104  assertThat($licenseMap->getProjectedId(2),is(1));
105  }
106 
107  function testProjectedShortNameOfMappedId()
108  {
109  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
110  assertThat($licenseMap->getProjectedShortName(2),is('One'));
111  }
112 
113  function testObligationForLicense()
114  {
115  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
116  assertThat($licenseMap->getObligationsForLicenseRef(2), contains(2));
117  }
118 
119  function testObligationForUnassignedLicense()
120  {
121  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
122  assertThat($licenseMap->getObligationsForLicenseRef(1), is(emptyArray()));
123  }
124 
125  function testObligationForCandidateLicense()
126  {
127  $licenseMap = new LicenseMap($this->dbManager, $this->groupId);
128  assertThat($licenseMap->getObligationsForLicenseRef(3, true), contains(2));
129  }
130 
131  function testProjectedIdOfMappedIdIsIdItselfIfTrivialMap()
132  {
133  $licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::TRIVIAL);
134  assertThat($licenseMap->getProjectedId(2),is(2));
135  }
136 
137  function testGetTopLevelLicenseRefs()
138  {
139  $licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::CONCLUSION);
140  $topLevelLicenses = $licenseMap->getTopLevelLicenseRefs();
141  assertThat($topLevelLicenses,hasItemInArray(new LicenseRef(1,'One','One-1')));
142  assertThat($topLevelLicenses, not(hasKeyInArray(2)));
143  }
144 
145 
146  public function testGetMappedLicenseRefView()
147  {
148  $this->testDb = new TestPgDb();
149  $this->testDb->createPlainTables(array('license_ref','license_map'));
150  $this->dbManager = $this->testDb->getDbManager();
151  $this->dbManager->queryOnce("CREATE TABLE license_candidate (group_fk integer) INHERITS (license_ref)");
152  $this->dbManager->insertTableRow('license_map',array('license_map_pk'=>0,'rf_fk'=>2,'rf_parent'=>1,'usage'=>LicenseMap::CONCLUSION));
153  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1'));
154  $this->dbManager->insertTableRow('license_ref',array('rf_pk'=>2,'rf_shortname'=>'Two','rf_fullname'=>'Two-2'));
155  $this->dbManager->insertTableRow('license_candidate',
156  array('rf_pk'=>3,'rf_shortname'=>'Three','rf_fullname'=>'Three-3','group_fk'=>$this->groupId));
157  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
158 
159  $view = LicenseMap::getMappedLicenseRefView(LicenseMap::CONCLUSION);
160  $stmt = __METHOD__;
161  $this->dbManager->prepare($stmt,$view);
162  $res = $this->dbManager->execute($stmt);
163  $map = $this->dbManager->fetchAll($res);
164  $this->dbManager->freeResult($res);
165  assertThat($map,is(arrayWithSize(2)));
166  $expected = array(
167  array('rf_origin'=>1, 'rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1'),
168  array('rf_origin'=>2, 'rf_pk'=>1,'rf_shortname'=>'One','rf_fullname'=>'One-1')
169  );
170  assertThat($map,containsInAnyOrder($expected));
171  }
172 
173  public function testFullMap()
174  {
175  $licenseMap = new LicenseMap($this->dbManager, $this->groupId+1, LicenseMap::CONCLUSION, true);
176  $map = \Fossology\Lib\Test\Reflectory::getObjectsProperty($licenseMap, 'map');
177  assertThat($map,hasItemInArray(array('rf_fk'=>1,'parent_shortname'=>'One','rf_parent'=>1)));
178  assertThat($map,hasItemInArray(array('rf_fk'=>2,'parent_shortname'=>'One','rf_parent'=>1)));
179  }
180 
181  public function testFullMapWithCandidates()
182  {
183  $licenseMap = new LicenseMap($this->dbManager, $this->groupId, LicenseMap::CONCLUSION, true);
184  $map = \Fossology\Lib\Test\Reflectory::getObjectsProperty($licenseMap, 'map');
185  assertThat($map,hasItemInArray(array('rf_fk'=>1,'parent_shortname'=>'One','rf_parent'=>1)));
186  assertThat($map,hasItemInArray(array('rf_fk'=>2,'parent_shortname'=>'One','rf_parent'=>1)));
187  assertThat($map,hasItemInArray(array('rf_fk'=>3,'parent_shortname'=>'Three','rf_parent'=>3)));
188  }
189 }
Wrapper class for license map.
Definition: LicenseMap.php:29
Contains business rules for FOSSology.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
static getMappedLicenseRefView($usageExpr='$1')
Query to get license map view along with license ref.
Definition: LicenseMap.php:160