FOSSology  4.6.0
Open Source License Compliance by Open Source Software
LicenseAcknowledgementDaoTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2022 Siemens AG
4  Author: Gaurav Mishra <mishra.gaurav@siemens.com>
5 
6  SPDX-License-Identifier: GPL-2.0-only
7 */
12 namespace Fossology\Lib\Dao;
13 
15 use PHPUnit\Framework\TestCase;
17 use PHPUnit\Runner\Version as PHPUnitVersion;
18 
25 class LicenseAcknowledgementDaoTest extends TestCase
26 {
27 
32 
35  private $testDb;
36 
39  private $dbManager;
40 
44 
46  private $assertCountBefore;
47 
48  private $authClass;
49 
50  protected function setUp() : void
51  {
52  $this->testDb = new TestPgDb("licenseacknowledgementdao");
53  $this->dbManager = $this->testDb->getDbManager();
54  $this->licenseAcknowledgementDao = new LicenseAcknowledgementDao($this->dbManager);
55  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
56  $this->testDb->createPlainTables(["users", "license_std_acknowledgement"]);
57  $this->testDb->createSequences(["license_std_acknowledgement_la_pk_seq"]);
58  $this->testDb->alterTables(["license_std_acknowledgement"]);
59  $this->testDb->insertData(["users", "license_std_acknowledgement"]);
60  $this->authClass = \Mockery::mock('alias:Fossology\Lib\Auth\Auth');
61  $this->authClass->expects('getUserId')->andReturn(2);
62  }
63 
64  protected function tearDown() : void
65  {
66  $this->addToAssertionCount(
67  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
68  $this->testDb = null;
69  $this->dbManager = null;
70  }
71 
80  {
81  $allAcknowledgements = $this->licenseAcknowledgementDao->getAllAcknowledgements();
82  $this->assertCount(self::ACKNOWLEDGEMENTS_IN_DB, $allAcknowledgements);
83  $testData = [
84  "la_pk" => 2,
85  "name" => "Acknowledgement #1",
86  "acknowledgement" => "This is the first acknowledgement!",
87  "is_enabled" => "t"
88  ];
89  $this->assertTrue(in_array($testData, $allAcknowledgements),
90  'Missing test data 1 in DB.');
91  $testData = [
92  "la_pk" => 8,
93  "name" => "not-set",
94  "acknowledgement" => "This acknowledgement is not set!",
95  "is_enabled" => "f"
96  ];
97  $this->assertTrue(in_array($testData, $allAcknowledgements),
98  'Missing test data 2 in DB.');
99  }
100 
109  {
110  $filteredAcknowledgements = $this->licenseAcknowledgementDao->getAllAcknowledgements(true);
111  $this->assertCount(self::ACKNOWLEDGEMENTS_IN_DB - 1, $filteredAcknowledgements);
112  $testData = [
113  "la_pk" => 2,
114  "name" => "Acknowledgement #1",
115  "acknowledgement" => "This is the first acknowledgement!",
116  "is_enabled" => "t"
117  ];
118  $this->assertTrue(in_array($testData, $filteredAcknowledgements),
119  'Missing expected data in DB.');
120  $testData = [
121  "la_pk" => 8,
122  "name" => "not-set",
123  "acknowledgement" => "This acknowledgement is not set!",
124  "is_enabled" => "f"
125  ];
126  $this->assertFalse(in_array($testData, $filteredAcknowledgements),
127  'Unexpected data returned for acknowledgements.');
128  }
129 
138  {
139  $this->authClass->expects('isAdmin')->andReturn(true);
140 
141  $returnVal = $this->licenseAcknowledgementDao->updateAcknowledgement(2,
142  "Updated acknowledgement #1", "This acknowledgement is updated!");
143  $this->assertTrue($returnVal);
144  $sql = "SELECT * FROM license_std_acknowledgement WHERE la_pk = 2;";
145  $row = $this->dbManager->getSingleRow($sql);
146  $this->assertEquals("Updated acknowledgement #1", $row["name"]);
147  $this->assertEquals("This acknowledgement is updated!", $row["acknowledgement"]);
148 
149  $pattern = "/^" . date('Y-m-d') . ".*/";
150  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
151  $this->assertMatchesRegularExpression($pattern, $row['updated']);
152  } else {
153  $this->assertRegExp($pattern, $row['updated']);
154  }
155  $this->assertEquals(2, $row['user_fk']);
156  }
157 
166  {
167  $this->authClass->expects('isAdmin')->andReturn(false);
168 
169  $returnVal = $this->licenseAcknowledgementDao->updateAcknowledgement(3,
170  "Updated acknowledgement #2", "This acknowledgement is updated!");
171  $this->assertFalse($returnVal);
172  $sql = "SELECT name, acknowledgement FROM license_std_acknowledgement WHERE la_pk = 3;";
173  $row = $this->dbManager->getSingleRow($sql);
174  $this->assertNotEquals("Updated acknowledgement #2", $row["name"]);
175  $this->assertNotEquals("This acknowledgement is updated!", $row["acknowledgement"]);
176  }
177 
185  {
186  $this->authClass->expects('isAdmin')->andReturn(true);
187 
188  $this->expectException(\UnexpectedValueException::class);
189  $this->licenseAcknowledgementDao->updateAcknowledgement(-1,
190  "Invalid acknowledgement #1", "This acknowledgement is invalid!");
191  }
192 
202  {
203  $this->authClass->expects('isAdmin')->andReturn(true);
204 
205  $newValues = [];
206  $newValues[3]['name'] = "Updated acknowledgement #2";
207  $newValues[3]['acknowledgement'] = "This acknowledgement is updated!";
208  $newValues[4]['name'] = "Updated acknowledgement #3";
209  $newValues[4]['acknowledgement'] = "This acknowledgement is updated!";
210 
211  $returnVal = $this->licenseAcknowledgementDao->updateAcknowledgementFromArray($newValues);
212 
213  $this->assertEquals(2, $returnVal);
214  $sql = "SELECT * FROM license_std_acknowledgement WHERE la_pk IN (3, 4);";
215  $rows = $this->dbManager->getRows($sql);
216  $id = 3;
217  foreach ($rows as $row) {
218  $this->assertEquals("Updated acknowledgement #" . ($id - 1), $row["name"]);
219  $this->assertEquals("This acknowledgement is updated!", $row["acknowledgement"]);
220  $pattern = "/^" . date('Y-m-d') . ".*/";
221  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
222  $this->assertMatchesRegularExpression($pattern, $row['updated']);
223  } else {
224  $this->assertRegExp($pattern, $row['updated']);
225  }
226  $this->assertEquals(2, $row['user_fk']);
227  $id++;
228  }
229  }
230 
239  {
240  $this->authClass->expects('isAdmin')->andReturn(false);
241 
242  $newValues = [];
243  $newValues[5]['name'] = "Updated acknowledgement #4";
244  $newValues[5]['acknowledgement'] = "This acknowledgement is updated!";
245 
246  $returnVal = $this->licenseAcknowledgementDao->updateAcknowledgementFromArray($newValues);
247 
248  $this->assertFalse($returnVal);
249  }
250 
260  {
261  $this->authClass->expects('isAdmin')->andReturn(true);
262 
263  $newValues = [];
264  $newValues[-1]['name'] = "Updated acknowledgement #4";
265  $newValues[-1]['acknowledgement'] = "This acknowledgement is updated!";
266 
267  $this->expectException(\UnexpectedValueException::class);
268 
269  $this->licenseAcknowledgementDao->updateAcknowledgementFromArray($newValues);
270  }
271 
281  {
282  $this->authClass->expects('isAdmin')->andReturn(true);
283 
284  $newValues = [];
285  $newValues[5]['naaaaame'] = "Updated acknowledgement #4";
286  $newValues[5]['acccccknowledgement'] = "This acknowledgement is updated!";
287 
288  $this->expectException(\UnexpectedValueException::class);
289 
290  $this->licenseAcknowledgementDao->updateAcknowledgementFromArray($newValues);
291  }
292 
302  {
303  $this->authClass->expects('isAdmin')->andReturn(true);
304 
305  $newValues = [];
306 
307  $returnVal = $this->licenseAcknowledgementDao->updateAcknowledgementFromArray($newValues);
308  $this->assertEquals(0, $returnVal);
309  }
310 
320  {
321  $this->authClass->expects('isAdmin')->andReturn(true);
322 
323  $newValues = [3 => []];
324 
325  $this->expectException(\UnexpectedValueException::class);
326 
327  $this->licenseAcknowledgementDao->updateAcknowledgementFromArray($newValues);
328  }
329 
337  public function testGetAcknowledgementValid()
338  {
339  $acknowledgementId = 7;
340  $returnVal = $this->licenseAcknowledgementDao->getAcknowledgement($acknowledgementId);
341 
342  $this->assertTrue(is_string($returnVal) || $returnVal === null);
343  if ($returnVal !== null) {
344  $this->assertEquals("This is the sixth acknowledgement!", $returnVal);
345  }
346  }
347 
355  {
356  $this->expectException(\UnexpectedValueException::class);
357 
358  $acknowledgementId = -1;
359  $this->licenseAcknowledgementDao->getAcknowledgement($acknowledgementId);
360  }
361 
370  {
371  $this->authClass->expects('isAdmin')->andReturn(true);
372 
373  $returnVal = $this->licenseAcknowledgementDao->insertAcknowledgement(
374  "Inserted acknowledgement #1", "This first inserted acknowledgement!");
375  $this->assertTrue(is_numeric($returnVal));
376  $this->assertEquals(1, $returnVal);
377  $sql = "SELECT * FROM license_std_acknowledgement WHERE la_pk = $1;";
378  $row = $this->dbManager->getSingleRow($sql, [$returnVal]);
379  $this->assertEquals("Inserted acknowledgement #1", $row["name"]);
380  $this->assertEquals("This first inserted acknowledgement!", $row["acknowledgement"]);
381  $pattern = "/^" . date('Y-m-d') . ".*/";
382  if (intval(explode('.', PHPUnitVersion::id())[0]) >= 9) {
383  $this->assertMatchesRegularExpression($pattern, $row['updated']);
384  } else {
385  $this->assertRegExp($pattern, $row['updated']);
386  }
387  $this->assertEquals(2, $row['user_fk']);
388  $this->assertEquals("t", $row["is_enabled"]);
389  }
390 
398  {
399  $this->authClass->expects('isAdmin')->andReturn(false);
400 
401  $returnVal = $this->licenseAcknowledgementDao->insertAcknowledgement(
402  "Inserted acknowledgement #1", "This first inserted acknowledgement!");
403  $this->assertEquals(-1, $returnVal);
404  }
405 
413  {
414  $this->authClass->expects('isAdmin')->andReturn(true);
415 
416  $returnVal = $this->licenseAcknowledgementDao->insertAcknowledgement("",
417  " ");
418  $this->assertEquals(-1, $returnVal);
419  }
420 
429  {
430  $this->authClass->expects('isAdmin')->andReturn(true);
431 
432  $returnVal = $this->licenseAcknowledgementDao->toggleAcknowledgement(5);
433  $this->assertTrue($returnVal);
434  $sql = "SELECT is_enabled FROM license_std_acknowledgement WHERE la_pk = 5;";
435  $row = $this->dbManager->getSingleRow($sql);
436  $this->assertEquals("f", $row["is_enabled"]);
437  }
438 
446  {
447  $this->authClass->expects('isAdmin')->andReturn(false);
448 
449  $returnVal = $this->licenseAcknowledgementDao->toggleAcknowledgement(5);
450  $this->assertFalse($returnVal);
451  }
452 
460  {
461  $this->authClass->expects('isAdmin')->andReturn(true);
462 
463  $this->expectException(\UnexpectedValueException::class);
464 
465  $acknowledgementId = -1;
466  $this->licenseAcknowledgementDao->toggleAcknowledgement($acknowledgementId);
467  }
468 }
testUpdateAcknowledgementFromArrayInvalidId()
Test for LicenseAcknowledgementDao::updateAcknowledgementFromArray() with invalid id.
testUpdateAcknowledgementAsAdminInvalidId()
Test for LicenseAcknowledgementDao::updateAcknowledgement() with invalid id.
testUpdateAcknowledgementFromArrayInvalidFields()
Test for LicenseAcknowledgementDao::updateAcknowledgementFromArray() with missing fields.
testUpdateAcknowledgementAsNonAdmin()
Test for LicenseAcknowledgementDao::updateAcknowledgement() as non admin.
testInsertAcknowledgementAsAdmin()
Test for LicenseAcknowledgementDao::insertAcknowledgement() as an admin.
testUpdateAcknowledgementFromArrayEmptyValues()
Test for LicenseAcknowledgementDao::updateAcknowledgementFromArray() with empty values.
testInsertAcknowledgementAsNonAdmin()
Test for LicenseAcknowledgementDao::insertAcknowledgement() as non admin.
testGetAcknowledgementValid()
Test for LicenseAcknowledgementDao::getAcknowledgement() with valid id.
testUpdateAcknowledgementFromArrayAsNonAdmin()
Test for LicenseAcknowledgementDao::updateAcknowledgementFromArray() as non admin.
testToggleAcknowledgementAsAdmin()
Test for LicenseAcknowledgementDao::toggleAcknowledgement() as admin.
testUpdateAcknowledgementFromArrayEmptyArray()
Test for LicenseAcknowledgementDao::updateAcknowledgementFromArray() with empty array.
testUpdateAcknowledgementAsAdmin()
Test for LicenseAcknowledgementDao::updateAcknowledgement() as an admin.
testGetAllAcknowledgementsEverything()
Test for LicenseAcknowledgementDao::getAllAcknowledgements() with no skips.
testInsertAcknowledgementEmptyValues()
Test for LicenseAcknowledgementDao::insertAcknowledgement() with empty values.
testToggleAcknowledgementInvalidId()
Test for LicenseAcknowledgementDao::toggleAcknowledgement() bad id.
testGetAllAcknowledgementsWithSkips()
Test for LicenseAcknowledgementDao::getAllAcknowledgements() with skips.
testGetAcknowledgementInvalid()
Test for LicenseAcknowledgementDao::getAcknowledgement() with invalid id.
testUpdateAcknowledgementFromArrayAsAdmin()
Test for LicenseAcknowledgementDao::updateAcknowledgementFromArray() as admin.
testToggleAcknowledgementAsNonAdmin()
Test for LicenseAcknowledgementDao::toggleAcknowledgement() as non admin.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16