FOSSology  4.4.0
Open Source License Compliance by Open Source Software
test_common_license_file.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2011-2013 Hewlett-Packard Development Company, L.P.
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
15 
16 require_once(dirname(__FILE__) . '/../common-license-file.php');
17 require_once(dirname(__FILE__) . '/../common-db.php');
18 require_once(dirname(__FILE__) . '/../common-dir.php');
19 require_once(dirname(__FILE__) . '/../common-ui.php');
20 
24 class test_common_license_file extends \PHPUnit\Framework\TestCase
25 {
26  public $upload_pk = 0;
27  public $uploadtree_pk_parent = 0;
28  public $uploadtree_pk_child = 0;
29  public $agent_pk = 0;
30  public $uploadtree_tablename = 'uploadtree';
31 
33  private $testDb;
34 
36  private $dbManager;
37 
38  private $logFileName;
39 
43  protected function setUp() : void
44  {
45  $this->testDb = new TestPgDb("fosslibtest");
46  $tables = array('license_ref','license_file','pfile','agent','upload','uploadtree');
47  $this->testDb->createPlainTables($tables);
48  $sequences = array('license_ref_rf_pk_seq', 'license_file_fl_pk_seq',
49  'pfile_pfile_pk_seq', 'agent_agent_pk_seq', 'upload_upload_pk_seq',
50  'uploadtree_uploadtree_pk_seq');
51  $this->testDb->createSequences($sequences);
52  $this->testDb->createConstraints([
53  'rf_pkpk', 'license_file_pkey', 'pfile_pkey', 'agent_pkey', 'upload_pkey_idx', 'ufile_rel_pkey'
54  ]);
55  $this->testDb->alterTables($tables);
56  $this->testDb->createViews(['license_file_ref']);
57  // $this->testDb->insertData($tables);
58 
59  global $upload_pk;
60  global $pfile_pk_parent;
61  global $pfile_pk_child;
62  global $agent_pk;
63 
64  $logger = new Monolog\Logger('default');
65  $this->logFileName = dirname(dirname(dirname(dirname(dirname(__FILE__))))) . '/db.sqlite.log';
66  $logger->pushHandler(new Monolog\Handler\StreamHandler($this->logFileName, Monolog\Logger::ERROR));
67  $this->dbManager = $this->testDb->getDbManager();
68 
70  $upload_filename = "license_file_test"; /* upload file name */
71 
72  $this->dbManager->prepare($stmt='pfile.insert',
73  $sql = "INSERT INTO pfile (pfile_sha1,pfile_md5,pfile_size) VALUES ($1,$2,$3)");
74  $this->dbManager->freeResult($this->dbManager->execute($stmt,array(
75  'AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC', '2239AA7DAC291B6F8D0A56396B1B8530', '4560')));
76  $this->dbManager->freeResult($this->dbManager->execute($stmt,array(
77  'B1938B14B9A573D59ABCBD3BF0F9200CE6E79FB6', '55EFE7F9B9D106047718F1CE9173B869', '1892')));
78 
80  $this->dbManager->queryOnce($sql="INSERT INTO agent (agent_name) VALUES('nomos')");
81 
83  $this->dbManager->prepare($stmt='license_ref.insert',
84  $sql="INSERT INTO license_ref"
85  . " (rf_pk, rf_shortname, rf_text, marydone, rf_active, rf_text_updatable, rf_detector_type)"
86  . " VALUES ($1,$2,$3,$4,$5,$6,$7)");
87  $this->dbManager->freeResult($this->dbManager->execute($stmt,
88  array(1, 'test_ref', 'test_ref', 'false', 'true', 'false', 1)));
89 
91  $this->dbManager->prepare($stmt='license_ref.select',
92  $sql = "SELECT pfile_pk from pfile where pfile_sha1"
93  . " IN ('AF1DF2C4B32E4115DB5F272D9EFD0E674CF2A0BC', 'B1938B14B9A573D59ABCBD3BF0F9200CE6E79FB6')");
94  $result = $this->dbManager->execute($stmt);
95  $row = $this->dbManager->fetchArray($result);
96  $pfile_pk_parent = $row['pfile_pk'];
97  $row = $this->dbManager->fetchArray($result);
98  $pfile_pk_child= $row['pfile_pk'];
99  $this->dbManager->freeResult($result);
100 
102  $agent_nomos = $this->dbManager->getSingleRow("SELECT agent_pk from agent where agent_name = 'nomos'",array(),__METHOD__.'.agent.select');
103  $agent_pk = $agent_nomos['agent_pk'];
104  $this->dbManager->prepare($stmt='license_file.insert',
105  $sql = "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES ($1,$2,$3)");
106  $this->dbManager->freeResult($this->dbManager->execute($stmt, array(1, $agent_pk, $pfile_pk_parent)));
107  $this->dbManager->freeResult($this->dbManager->execute($stmt, array(2, $agent_pk, $pfile_pk_child)));
108 
109  $this->dbManager->queryOnce("INSERT INTO upload (upload_filename,upload_mode,upload_ts, pfile_fk, uploadtree_tablename)"
110  . " VALUES ('$upload_filename',40,now(), '$pfile_pk_parent', '$this->uploadtree_tablename')");
111  $row = $this->dbManager->getSingleRow("SELECT upload_pk from upload where upload_filename = '$upload_filename'",array(),__METHOD__.'.upload.select');
112  $upload_pk= $row['upload_pk'];
113 
114  $this->dbManager->prepare($stmtIn=__METHOD__.'.uploadtree.insert',
115  "INSERT INTO uploadtree (parent, upload_fk, pfile_fk, ufile_mode, lft, rgt, ufile_name) VALUES ($1,$2,$3,$4,$5,$6,$7)");
116  $this->dbManager->freeResult(
117  $this->dbManager->execute($stmtIn,array(NULL, $upload_pk, $pfile_pk_parent, 33188, 1, 2, 'license_test.file.parent')));
118 
119  $this->dbManager->prepare($stmtOut=__METHOD__.'uploadtree.select',
120  "SELECT uploadtree_pk from uploadtree where pfile_fk=$1");
121  $res = $this->dbManager->execute($stmtOut,array($pfile_pk_parent));
122  $row = $this->dbManager->fetchArray($res);
123  $this->dbManager->freeResult($res);
124  $this->uploadtree_pk_parent = $row['uploadtree_pk'];
125 
127  $this->dbManager->freeResult(
128  $this->dbManager->execute($stmtIn,array($this->uploadtree_pk_parent, $upload_pk, $pfile_pk_child, 33188, 1, 2, 'license_test.file.child')));
129 
130  $res = $this->dbManager->execute($stmtOut,array($pfile_pk_child));
131  $row = $this->dbManager->fetchArray($res);
132  $this->dbManager->freeResult($res);
133  $this->uploadtree_pk_child = $row['uploadtree_pk'];
134 
135  $this->uploadtree_tablename = GetUploadtreeTableName($upload_pk);
136  print('.');
137  }
138 
144  {
145  global $agent_pk;
146 
147  $license_array = GetFileLicenses($agent_pk, '' , $this->uploadtree_pk_parent, $this->uploadtree_tablename);
149  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
150  $row = $this->dbManager->getSingleRow($sql);
151  $license_value_expected = $row['rf_shortname'];
152  $count = count($license_array);
153 
154  $this->assertEquals($license_value_expected, $license_array[1]);
155  $this->assertEquals(1, $count);
156  }
157 
163  {
164  global $pfile_pk_parent;
165  global $agent_pk;
166  $sql = "INSERT INTO license_file(rf_fk, agent_fk, pfile_fk) VALUES(1, $agent_pk, $pfile_pk_parent);";
167  $this->dbManager->getSingleRow($sql, [], __METHOD__ . ".insert");
168 
169  $license_array = GetFileLicenses($agent_pk, '' , $this->uploadtree_pk_parent, $this->uploadtree_tablename, "yes");
171  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
172  $row = $this->dbManager->getSingleRow($sql, [], __METHOD__ . ".get");
173  $license_value_expected = $row['rf_shortname'];
174 
175  $count = count($license_array);
176  $this->assertEquals(2, $count);
177  $this->assertEquals($license_value_expected, $license_array[1]);
178  $this->assertEquals($license_value_expected, $license_array[3]);
179  }
180 
186  {
187  global $agent_pk;
188 
189  $license_string = GetFileLicenses_string($agent_pk, '', $this->uploadtree_pk_parent, $this->uploadtree_tablename);
191  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
192  $row = $this->dbManager->getSingleRow($sql);
193  $license_value_expected = $row['rf_shortname'];
194 
195  $this->assertEquals($license_value_expected, $license_string);
196  }
197 
202  {
203  global $pfile_pk_parent;
204  global $agent_pk;
205 
207  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
208  $row = $this->dbManager->getSingleRow($sql);
209  $rf_shortname = $row['rf_shortname'];
210 
211  $files_result = GetFilesWithLicense($agent_pk, $rf_shortname, $this->uploadtree_pk_parent, false, 0, "ALL", "", null, $this->uploadtree_tablename);
212  $row = pg_fetch_assoc($files_result);
213  $pfile_id_actual = $row['pfile_fk'];
214  pg_free_result($files_result);
215  $this->assertEquals($pfile_pk_parent, $pfile_id_actual);
216  }
217 
222  {
223  global $agent_pk;
224 
226  $sql = "SELECT rf_shortname from license_ref where rf_pk = 1;";
227  $row = $this->dbManager->getSingleRow($sql);
228  $rf_shortname = $row['rf_shortname'];
229 
230  $file_name = Level1WithLicense($agent_pk, $rf_shortname, $this->uploadtree_pk_parent, false, $this->uploadtree_tablename);
231  $this->assertEquals("license_test.file.child", $file_name[$this->uploadtree_pk_child]);
232  }
233 
234 
238  protected function tearDown() : void
239  {
240  if (!is_callable('pg_connect')) {
241  return;
242  }
243  $this->testDb->fullDestruct();
244  $this->testDb = null;
245  $this->dbManager = null;
246  }
247 }
char * uploadtree_tablename
upload.uploadtree_tablename
Definition: adj2nest.c:100
testLevel1WithLicense()
testing for Level1WithLicense
testGetFilesWithLicense()
testing for GetFilesWithLicense
testGetFileLicenses()
testing from GetFileLicenses in this test case, this pfile have only one license
testGetFileLicenses_string()
testing from GetFileLicenses_tring in this test case, this pfile have only one license
testGetFileLicensesDul()
testing from GetFileLicenses in this test case, this pfile have 2 same license
GetFilesWithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $offset=0, $limit="ALL", $order="", $tag_pk=null, $uploadtree_tablename="uploadtree")
Get files with a given license (shortname).
GetFileLicenses_string($agent_pk, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree')
Same as GetFileLicenses() but returns license list as a single string.
Level1WithLicense($agent_pk, $rf_shortname, $uploadtree_pk, $PkgsOnly=false, $uploadtree_tablename="uploadtree")
Given an uploadtree_pk, find all the non-artifact, immediate children (uploadtree_pk's) that have lic...
GetFileLicenses($agent, $pfile_pk, $uploadtree_pk, $uploadtree_tablename='uploadtree', $duplicate="")
get all the licenses for a single file or uploadtree
FUNCTION char * GetUploadtreeTableName(PGconn *pgConn, int upload_pk)
Get the uploadtree table name for this upload_pk If upload_pk does not exist, return "uploadtree".
Definition: libfossagent.c:414
#define ERROR(...)
Definition: logging.h:79
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16