FOSSology  4.4.0
Open Source License Compliance by Open Source Software
LicenseStdCommentDaoTest.php
Go to the documentation of this file.
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2019 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 LicenseStdCommentDaoTest extends TestCase
26 {
27 
31  const COMMENTS_IN_DB = 7;
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("licensestdcommentdao");
53  $this->dbManager = $this->testDb->getDbManager();
54  $this->licenseStdCommentDao = new LicenseStdCommentDao($this->dbManager);
55  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
56  $this->testDb->createPlainTables(["users", "license_std_comment"]);
57  $this->testDb->createSequences(["license_std_comment_lsc_pk_seq"]);
58  $this->testDb->alterTables(["license_std_comment"]);
59  $this->testDb->insertData(["users", "license_std_comment"]);
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 
79  public function testGetAllCommentsEverything()
80  {
81  $allComments = $this->licenseStdCommentDao->getAllComments();
82  $this->assertCount(self::COMMENTS_IN_DB, $allComments);
83  $testData = [
84  "lsc_pk" => 2,
85  "name" => "Test comment #1",
86  "comment" => "This will be your first comment!",
87  "is_enabled" => "t"
88  ];
89  $this->assertTrue(in_array($testData, $allComments),
90  'Missing test data 1 in DB.');
91  $testData = [
92  "lsc_pk" => 8,
93  "name" => "not-set",
94  "comment" => "This comment is not set!",
95  "is_enabled" => "f"
96  ];
97  $this->assertTrue(in_array($testData, $allComments),
98  'Missing test data 2 in DB.');
99  }
100 
108  public function testGetAllCommentsWithSkips()
109  {
110  $filteredComments = $this->licenseStdCommentDao->getAllComments(true);
111  $this->assertCount(self::COMMENTS_IN_DB - 1, $filteredComments);
112  $testData = [
113  "lsc_pk" => 2,
114  "name" => "Test comment #1",
115  "comment" => "This will be your first comment!",
116  "is_enabled" => "t"
117  ];
118  $this->assertTrue(in_array($testData, $filteredComments),
119  'Missing expected data in DB.');
120  $testData = [
121  "lsc_pk" => 8,
122  "name" => "not-set",
123  "comment" => "This comment is not set!",
124  "is_enabled" => "f"
125  ];
126  $this->assertFalse(in_array($testData, $filteredComments),
127  'Unexpected data returned for comments.');
128  }
129 
137  public function testUpdateCommentAsAdmin()
138  {
139  $this->authClass->expects('isAdmin')->andReturn(true);
140 
141  $returnVal = $this->licenseStdCommentDao->updateComment(2,
142  "Updated comment #1", "This comment is updated!");
143  $this->assertTrue($returnVal);
144  $sql = "SELECT * FROM license_std_comment WHERE lsc_pk = 2;";
145  $row = $this->dbManager->getSingleRow($sql);
146  $this->assertEquals("Updated comment #1", $row["name"]);
147  $this->assertEquals("This comment is updated!", $row["comment"]);
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 
165  public function testUpdateCommentAsNonAdmin()
166  {
167  $this->authClass->expects('isAdmin')->andReturn(false);
168 
169  $returnVal = $this->licenseStdCommentDao->updateComment(3,
170  "Updated comment #2", "This comment is updated!");
171  $this->assertFalse($returnVal);
172  $sql = "SELECT name, comment FROM license_std_comment WHERE lsc_pk = 3;";
173  $row = $this->dbManager->getSingleRow($sql);
174  $this->assertNotEquals("Updated comment #2", $row["name"]);
175  $this->assertNotEquals("This comment is updated!", $row["comment"]);
176  }
177 
185  {
186  $this->authClass->expects('isAdmin')->andReturn(true);
187 
188  $this->expectException(\UnexpectedValueException::class);
189  $this->licenseStdCommentDao->updateComment(- 1, "Invalid comment #1",
190  "This comment is invalid!");
191  }
192 
202  {
203  $this->authClass->expects('isAdmin')->andReturn(true);
204 
205  $newValues = [];
206  $newValues[3]['name'] = "Updated comment #2";
207  $newValues[3]['comment'] = "This comment is updated!";
208  $newValues[4]['name'] = "Updated comment #3";
209  $newValues[4]['comment'] = "This comment is updated!";
210 
211  $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
212 
213  $this->assertEquals(2, $returnVal);
214  $sql = "SELECT * FROM license_std_comment WHERE lsc_pk IN (3, 4);";
215  $rows = $this->dbManager->getRows($sql);
216  $id = 3;
217  foreach ($rows as $row) {
218  $this->assertEquals("Updated comment #" . ($id - 1), $row["name"]);
219  $this->assertEquals("This comment is updated!", $row["comment"]);
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 comment #4";
244  $newValues[5]['comment'] = "This comment is updated!";
245 
246  $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
247 
248  $this->assertFalse($returnVal);
249  }
250 
260  {
261  $this->authClass->expects('isAdmin')->andReturn(true);
262 
263  $newValues = [];
264  $newValues[-1]['name'] = "Updated comment #4";
265  $newValues[-1]['comment'] = "This comment is updated!";
266 
267  $this->expectException(\UnexpectedValueException::class);
268 
269  $this->licenseStdCommentDao->updateCommentFromArray($newValues);
270  }
271 
281  {
282  $this->authClass->expects('isAdmin')->andReturn(true);
283 
284  $newValues = [];
285  $newValues[5]['naaaaame'] = "Updated comment #4";
286  $newValues[5]['commmmmment'] = "This comment is updated!";
287 
288  $this->expectException(\UnexpectedValueException::class);
289 
290  $this->licenseStdCommentDao->updateCommentFromArray($newValues);
291  }
292 
302  {
303  $this->authClass->expects('isAdmin')->andReturn(true);
304 
305  $newValues = [];
306 
307  $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($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->licenseStdCommentDao->updateCommentFromArray($newValues);
328  }
329 
337  public function testGetCommentValid()
338  {
339  $commentId = 7;
340  $returnVal = $this->licenseStdCommentDao->getComment($commentId);
341 
342  $this->assertTrue(is_string($returnVal) || $returnVal === null);
343  if ($returnVal !== null) {
344  $this->assertEquals("This will be the sixth comment!", $returnVal);
345  }
346  }
347 
354  public function testGetCommentInvalid()
355  {
356  $this->expectException(\UnexpectedValueException::class);
357 
358  $commentId = -1;
359  $returnVal = $this->licenseStdCommentDao->getComment($commentId);
360  }
361 
369  public function testInsertCommentAsAdmin()
370  {
371  $this->authClass->expects('isAdmin')->andReturn(true);
372 
373  $returnVal = $this->licenseStdCommentDao->insertComment("Inserted comment #1",
374  "This first inserted comment!");
375  $this->assertTrue(is_numeric($returnVal));
376  $this->assertEquals(1, $returnVal);
377  $sql = "SELECT * FROM license_std_comment WHERE lsc_pk = $1;";
378  $row = $this->dbManager->getSingleRow($sql, [$returnVal]);
379  $this->assertEquals("Inserted comment #1", $row["name"]);
380  $this->assertEquals("This first inserted comment!", $row["comment"]);
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 
397  public function testInsertCommentAsNonAdmin()
398  {
399  $this->authClass->expects('isAdmin')->andReturn(false);
400 
401  $returnVal = $this->licenseStdCommentDao->insertComment("Inserted comment #1",
402  "This first inserted comment!");
403  $this->assertEquals(-1, $returnVal);
404  }
405 
413  {
414  $this->authClass->expects('isAdmin')->andReturn(true);
415 
416  $returnVal = $this->licenseStdCommentDao->insertComment("",
417  " ");
418  $this->assertEquals(-1, $returnVal);
419  }
420 
428  public function testToggleCommentAsAdmin()
429  {
430  $this->authClass->expects('isAdmin')->andReturn(true);
431 
432  $returnVal = $this->licenseStdCommentDao->toggleComment(5);
433  $this->assertTrue($returnVal);
434  $sql = "SELECT is_enabled FROM license_std_comment WHERE lsc_pk = 5;";
435  $row = $this->dbManager->getSingleRow($sql);
436  $this->assertEquals("f", $row["is_enabled"]);
437  }
438 
445  public function testToggleCommentAsNonAdmin()
446  {
447  $this->authClass->expects('isAdmin')->andReturn(false);
448 
449  $returnVal = $this->licenseStdCommentDao->toggleComment(5);
450  $this->assertFalse($returnVal);
451  }
452 
459  public function testToggleCommentInvalidId()
460  {
461  $this->authClass->expects('isAdmin')->andReturn(true);
462 
463  $this->expectException(\UnexpectedValueException::class);
464 
465  $commentId = -1;
466  $returnVal = $this->licenseStdCommentDao->toggleComment($commentId);
467  }
468 }
testToggleCommentAsAdmin()
Test for LicenseStdCommentDao::toggleComment() as admin.
testUpdateCommentFromArrayAsAdmin()
Test for LicenseStdCommentDao::updateCommentFromArray() as admin.
testUpdateCommentFromArrayInvalidFields()
Test for LicenseStdCommentDao::updateCommentFromArray() with missing fields.
testInsertCommentEmptyValues()
Test for LicenseStdCommentDao::insertComment() with empty values.
testUpdateCommentFromArrayEmptyValues()
Test for LicenseStdCommentDao::updateCommentFromArray() with empty values.
testInsertCommentAsNonAdmin()
Test for LicenseStdCommentDao::insertComment() as non admin.
testGetAllCommentsWithSkips()
Test for LicenseStdCommentDao::getAllComments() with skips.
testUpdateCommentFromArrayAsNonAdmin()
Test for LicenseStdCommentDao::updateCommentFromArray() as non admin.
testUpdateCommentFromArrayInvalidId()
Test for LicenseStdCommentDao::updateCommentFromArray() with invalid id.
testUpdateCommentFromArrayEmptyArray()
Test for LicenseStdCommentDao::updateCommentFromArray() with empty array.
testUpdateCommentAsAdmin()
Test for LicenseStdCommentDao::updateComment() as an admin.
testGetAllCommentsEverything()
Test for LicenseStdCommentDao::getAllComments() with no skips.
testToggleCommentInvalidId()
Test for LicenseStdCommentDao::toggleComment() bad id.
testGetCommentValid()
Test for LicenseStdCommentDao::getComment() with valid id.
testGetCommentInvalid()
Test for LicenseStdCommentDao::getComment() with invalid id.
testUpdateCommentAsNonAdmin()
Test for LicenseStdCommentDao::updateComment() as non admin.
testInsertCommentAsAdmin()
Test for LicenseStdCommentDao::insertComment() as an admin.
testUpdateCommentAsAdminInvalidId()
Test for LicenseStdCommentDao::updateComment() with invalid id.
testToggleCommentAsNonAdmin()
Test for LicenseStdCommentDao::toggleComment() as non admin.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16