FOSSology  3.2.0rc1
Open Source License Compliance by Open Source Software
LicenseStdCommentDaoTest.php
Go to the documentation of this file.
1 <?php
21 namespace Fossology\Lib\Dao;
22 
26 
33 class LicenseStdCommentDaoTest extends TestCase
34 {
35 
39  const COMMENTS_IN_DB = 7;
40 
43  private $testDb;
44 
47  private $dbManager;
48 
52 
54  private $assertCountBefore;
55 
56  private $authClass;
57 
58  protected function setUp()
59  {
60  $this->testDb = new TestPgDb("licensestdcommentdao");
61  $this->dbManager = $this->testDb->getDbManager();
62  $this->licenseStdCommentDao = new LicenseStdCommentDao($this->dbManager);
63  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
64  $this->testDb->createPlainTables(["users", "license_std_comment"]);
65  $this->testDb->createSequences(["license_std_comment_lsc_pk_seq"]);
66  $this->testDb->alterTables(["license_std_comment"]);
67  $this->testDb->insertData(["users", "license_std_comment"]);
68  $this->authClass = \Mockery::mock('alias:Fossology\Lib\Auth\Auth');
69  $this->authClass->expects('getUserId')->andReturn(2);
70  }
71 
72  protected function tearDown()
73  {
74  $this->addToAssertionCount(
75  \Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
76  $this->testDb = null;
77  $this->dbManager = null;
78  }
79 
87  public function testGetAllCommentsEverything()
88  {
89  $allComments = $this->licenseStdCommentDao->getAllComments();
90  $this->assertCount(self::COMMENTS_IN_DB, $allComments);
91  $this->assertContains([
92  "lsc_pk" => 2,
93  "name" => "Test comment #1",
94  "comment" => "This will be your first comment!",
95  "is_enabled" => "t"
96  ], $allComments, false);
97  $this->assertContains([
98  "lsc_pk" => 8,
99  "name" => "not-set",
100  "comment" => "This comment is not set!",
101  "is_enabled" => "f"
102  ], $allComments, false);
103  }
104 
112  public function testGetAllCommentsWithSkips()
113  {
114  $filteredComments = $this->licenseStdCommentDao->getAllComments(true);
115  $this->assertCount(self::COMMENTS_IN_DB - 1, $filteredComments);
116  $this->assertContains([
117  "lsc_pk" => 2,
118  "name" => "Test comment #1",
119  "comment" => "This will be your first comment!",
120  "is_enabled" => true
121  ], $filteredComments, false);
122  $this->assertNotContains([
123  "lsc_pk" => 8,
124  "name" => "not-set",
125  "comment" => "This comment is not set!",
126  "is_enabled" => false
127  ], $filteredComments);
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  $this->assertRegExp("/^" . date('Y-m-d') . ".*/", $row['updated']);
149  $this->assertEquals(2, $row['user_fk']);
150  }
151 
159  public function testUpdateCommentAsNonAdmin()
160  {
161  $this->authClass->expects('isAdmin')->andReturn(false);
162 
163  $returnVal = $this->licenseStdCommentDao->updateComment(3,
164  "Updated comment #2", "This comment is updated!");
165  $this->assertFalse($returnVal);
166  $sql = "SELECT name, comment FROM license_std_comment WHERE lsc_pk = 3;";
167  $row = $this->dbManager->getSingleRow($sql);
168  $this->assertNotEquals("Updated comment #2", $row["name"]);
169  $this->assertNotEquals("This comment is updated!", $row["comment"]);
170  }
171 
179  {
180  $this->authClass->expects('isAdmin')->andReturn(true);
181 
182  $this->expectException(\UnexpectedValueException::class);
183  $this->licenseStdCommentDao->updateComment(- 1, "Invalid comment #1",
184  "This comment is invalid!");
185  }
186 
196  {
197  $this->authClass->expects('isAdmin')->andReturn(true);
198 
199  $newValues = [];
200  $newValues[3]['name'] = "Updated comment #2";
201  $newValues[3]['comment'] = "This comment is updated!";
202  $newValues[4]['name'] = "Updated comment #3";
203  $newValues[4]['comment'] = "This comment is updated!";
204 
205  $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
206 
207  $this->assertEquals(2, $returnVal);
208  $sql = "SELECT * FROM license_std_comment WHERE lsc_pk IN (3, 4);";
209  $rows = $this->dbManager->getRows($sql);
210  $id = 3;
211  foreach ($rows as $row) {
212  $this->assertEquals("Updated comment #" . ($id - 1), $row["name"]);
213  $this->assertEquals("This comment is updated!", $row["comment"]);
214  $this->assertRegExp("/^" . date('Y-m-d') . ".*/", $row['updated']);
215  $this->assertEquals(2, $row['user_fk']);
216  $id++;
217  }
218  }
219 
228  {
229  $this->authClass->expects('isAdmin')->andReturn(false);
230 
231  $newValues = [];
232  $newValues[5]['name'] = "Updated comment #4";
233  $newValues[5]['comment'] = "This comment is updated!";
234 
235  $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
236 
237  $this->assertFalse($returnVal);
238  }
239 
249  {
250  $this->authClass->expects('isAdmin')->andReturn(true);
251 
252  $newValues = [];
253  $newValues[-1]['name'] = "Updated comment #4";
254  $newValues[-1]['comment'] = "This comment is updated!";
255 
256  $this->expectException(\UnexpectedValueException::class);
257 
258  $this->licenseStdCommentDao->updateCommentFromArray($newValues);
259  }
260 
270  {
271  $this->authClass->expects('isAdmin')->andReturn(true);
272 
273  $newValues = [];
274  $newValues[5]['naaaaame'] = "Updated comment #4";
275  $newValues[5]['commmmmment'] = "This comment is updated!";
276 
277  $this->expectException(\UnexpectedValueException::class);
278 
279  $this->licenseStdCommentDao->updateCommentFromArray($newValues);
280  }
281 
291  {
292  $this->authClass->expects('isAdmin')->andReturn(true);
293 
294  $newValues = [];
295 
296  $returnVal = $this->licenseStdCommentDao->updateCommentFromArray($newValues);
297  $this->assertEquals(0, $returnVal);
298  }
299 
309  {
310  $this->authClass->expects('isAdmin')->andReturn(true);
311 
312  $newValues = [3 => []];
313 
314  $this->expectException(\UnexpectedValueException::class);
315 
316  $this->licenseStdCommentDao->updateCommentFromArray($newValues);
317  }
318 
326  public function testGetCommentValid()
327  {
328  $commentId = 7;
329  $returnVal = $this->licenseStdCommentDao->getComment($commentId);
330 
331  $this->assertTrue(is_string($returnVal) || $returnVal === null);
332  if ($returnVal !== null) {
333  $this->assertEquals("This will be the sixth comment!", $returnVal);
334  }
335  }
336 
343  public function testGetCommentInvalid()
344  {
345  $this->expectException(\UnexpectedValueException::class);
346 
347  $commentId = -1;
348  $returnVal = $this->licenseStdCommentDao->getComment($commentId);
349  }
350 
358  public function testInsertCommentAsAdmin()
359  {
360  $this->authClass->expects('isAdmin')->andReturn(true);
361 
362  $returnVal = $this->licenseStdCommentDao->insertComment("Inserted comment #1",
363  "This first inserted comment!");
364  $this->assertTrue(is_numeric($returnVal));
365  $this->assertEquals(1, $returnVal);
366  $sql = "SELECT * FROM license_std_comment WHERE lsc_pk = $1;";
367  $row = $this->dbManager->getSingleRow($sql, [$returnVal]);
368  $this->assertEquals("Inserted comment #1", $row["name"]);
369  $this->assertEquals("This first inserted comment!", $row["comment"]);
370  $this->assertRegExp("/^" . date('Y-m-d') . ".*/", $row['updated']);
371  $this->assertEquals(2, $row['user_fk']);
372  $this->assertEquals("t", $row["is_enabled"]);
373  }
374 
381  public function testInsertCommentAsNonAdmin()
382  {
383  $this->authClass->expects('isAdmin')->andReturn(false);
384 
385  $returnVal = $this->licenseStdCommentDao->insertComment("Inserted comment #1",
386  "This first inserted comment!");
387  $this->assertEquals(-1, $returnVal);
388  }
389 
397  {
398  $this->authClass->expects('isAdmin')->andReturn(true);
399 
400  $returnVal = $this->licenseStdCommentDao->insertComment("",
401  " ");
402  $this->assertEquals(-1, $returnVal);
403  }
404 
412  public function testToggleCommentAsAdmin()
413  {
414  $this->authClass->expects('isAdmin')->andReturn(true);
415 
416  $returnVal = $this->licenseStdCommentDao->toggleComment(5);
417  $this->assertTrue($returnVal);
418  $sql = "SELECT is_enabled FROM license_std_comment WHERE lsc_pk = 5;";
419  $row = $this->dbManager->getSingleRow($sql);
420  $this->assertEquals("f", $row["is_enabled"]);
421  }
422 
429  public function testToggleCommentAsNonAdmin()
430  {
431  $this->authClass->expects('isAdmin')->andReturn(false);
432 
433  $returnVal = $this->licenseStdCommentDao->toggleComment(5);
434  $this->assertFalse($returnVal);
435  }
436 
443  public function testToggleCommentInvalidId()
444  {
445  $this->authClass->expects('isAdmin')->andReturn(true);
446 
447  $this->expectException(\UnexpectedValueException::class);
448 
449  $commentId = -1;
450  $returnVal = $this->licenseStdCommentDao->toggleComment($commentId);
451  }
452 }
testToggleCommentInvalidId()
Test for LicenseStdCommentDao::toggleComment() bad id.
testToggleCommentAsNonAdmin()
Test for LicenseStdCommentDao::toggleComment() as non admin.
testUpdateCommentFromArrayEmptyArray()
Test for LicenseStdCommentDao::updateCommentFromArray() with empty array.
testGetCommentValid()
Test for LicenseStdCommentDao::getComment() with valid id.
testGetAllCommentsEverything()
Test for LicenseStdCommentDao::getAllComments() with no skips.
testInsertCommentAsAdmin()
Test for LicenseStdCommentDao::insertComment() as an admin.
testUpdateCommentFromArrayInvalidId()
Test for LicenseStdCommentDao::updateCommentFromArray() with invalid id.
testUpdateCommentAsNonAdmin()
Test for LicenseStdCommentDao::updateComment() as non admin.
testUpdateCommentAsAdmin()
Test for LicenseStdCommentDao::updateComment() as an admin.
testInsertCommentEmptyValues()
Test for LicenseStdCommentDao::insertComment() with empty values.
testGetAllCommentsWithSkips()
Test for LicenseStdCommentDao::getAllComments() with skips.
testUpdateCommentAsAdminInvalidId()
Test for LicenseStdCommentDao::updateComment() with invalid id.
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:28
testUpdateCommentFromArrayAsAdmin()
Test for LicenseStdCommentDao::updateCommentFromArray() as admin.
testUpdateCommentFromArrayEmptyValues()
Test for LicenseStdCommentDao::updateCommentFromArray() with empty values.
testInsertCommentAsNonAdmin()
Test for LicenseStdCommentDao::insertComment() as non admin.
testGetCommentInvalid()
Test for LicenseStdCommentDao::getComment() with invalid id.
testToggleCommentAsAdmin()
Test for LicenseStdCommentDao::toggleComment() as admin.
testUpdateCommentFromArrayAsNonAdmin()
Test for LicenseStdCommentDao::updateCommentFromArray() as non admin.
testUpdateCommentFromArrayInvalidFields()
Test for LicenseStdCommentDao::updateCommentFromArray() with missing fields.